$extrastylesheet
single_predicates.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 #ifndef LIBMESH_SINGLE_PREDICATES_H
00019 #define LIBMESH_SINGLE_PREDICATES_H
00020 
00021 // Local includes
00022 #include <cstddef>         // for NULL with gcc 4.6.2 - I'm serious!
00023 #include "libmesh/enum_elem_type.h"
00024 #include "libmesh/id_types.h"
00025 
00026 // C++ includes
00027 #include <cstddef>
00028 #include <vector>
00029 
00030 namespace libMesh
00031 {
00032 
00043 namespace Predicates
00044 {
00045 // Forward declaration
00046 template <typename T> struct abstract_multi_predicate;
00047 
00048 // abstract single predicate.  Derived classes must implement the clone()
00049 // function.  Be careful using it since it allocates memory!  The clone()
00050 // function is necessary since the predicate class has pure virtual
00051 // functions.
00052 template <typename T>
00053 struct predicate
00054 {
00055   virtual ~predicate() {}
00056   virtual bool operator()(const T& it) const = 0;
00057 
00058 
00059 protected:
00060   friend struct abstract_multi_predicate<T>;
00061   virtual predicate* clone() const = 0;
00062 
00063 };
00064 
00065 
00066 // The is_null predicate returns true if the underlying pointer is NULL.
00067 template <typename T>
00068 struct is_null : predicate<T>
00069 {
00070   virtual ~is_null() {}
00071   virtual bool operator()(const T& it) const { return *it == NULL; }
00072 
00073 protected:
00074   virtual predicate<T>* clone() const { return new is_null<T>(*this); }
00075 };
00076 
00077 // The not_null predicate simply returns true if the pointer is not NULL.
00078 template <typename T>
00079 struct not_null : is_null<T>
00080 {
00081   virtual bool operator()(const T& it) const { return !is_null<T>::operator()(it); }
00082 
00083 protected:
00084   virtual predicate<T>* clone() const { return new not_null<T>(*this); }
00085 };
00086 
00087 
00088 // The active predicate returns true if the pointer is active.
00089 template <typename T>
00090 struct active : predicate<T>
00091 {
00092   virtual ~active() {}
00093   virtual bool operator()(const T& it) const { return (*it)->active(); }
00094 
00095 protected:
00096   virtual predicate<T>* clone() const { return new active<T>(*this); }
00097 };
00098 
00099 // The not active predicate returns true when the pointer is inactive
00100 template <typename T>
00101 struct not_active : active<T>
00102 {
00103   virtual bool operator()(const T& it) const { return !active<T>::operator()(it); }
00104 
00105 protected:
00106   virtual predicate<T>* clone() const { return new not_active<T>(*this); }
00107 };
00108 
00109 
00110 // The ancestor predicate returns true if the pointer is ancestor.
00111 template <typename T>
00112 struct ancestor : predicate<T>
00113 {
00114   virtual ~ancestor() {}
00115   virtual bool operator()(const T& it) const { return (*it)->ancestor(); }
00116 
00117 protected:
00118   virtual predicate<T>* clone() const { return new ancestor<T>(*this); }
00119 };
00120 
00121 // The not_ancestor predicate returns true when the pointer is not ancestor
00122 template <typename T>
00123 struct not_ancestor : ancestor<T>
00124 {
00125   virtual bool operator()(const T& it) const { return !ancestor<T>::operator()(it); }
00126 
00127 protected:
00128   virtual predicate<T>* clone() const { return new not_ancestor<T>(*this); }
00129 };
00130 
00131 
00132 // The subactive predicate returns true if the pointer is subactive.
00133 template <typename T>
00134 struct subactive : predicate<T>
00135 {
00136   virtual ~subactive() {}
00137   virtual bool operator()(const T& it) const { return (*it)->subactive(); }
00138 
00139 protected:
00140   virtual predicate<T>* clone() const { return new subactive<T>(*this); }
00141 };
00142 
00143 // The not_subactive predicate returns true when the pointer is not subactive
00144 template <typename T>
00145 struct not_subactive : subactive<T>
00146 {
00147   virtual bool operator()(const T& it) const { return !subactive<T>::operator()(it); }
00148 
00149 protected:
00150   virtual predicate<T>* clone() const { return new not_subactive<T>(*this); }
00151 };
00152 
00153 
00154 
00155 // The pid predicate returns true if the pointers
00156 // processor id matches a given processor id.
00157 template <typename T>
00158 struct pid : predicate<T>
00159 {
00160   // Constructor
00161   pid(const processor_id_type p) : _pid(p) {}
00162   virtual ~pid() {}
00163 
00164   // op()
00165   virtual bool operator()(const T& it) const { return (*it)->processor_id() == _pid; }
00166 
00167 protected:
00168   virtual predicate<T>* clone() const { return new pid<T>(*this); }
00169   const processor_id_type _pid;
00170 };
00171 
00172 
00173 
00174 // The semilocal_pid predicate returns true if the element
00175 // pointed to is semilocal to (has nodes shared with an element of) a
00176 // given processor id.
00177 template <typename T>
00178 struct semilocal_pid : predicate<T>
00179 {
00180   // Constructor
00181   semilocal_pid(const processor_id_type p) : _pid(p) {}
00182   virtual ~semilocal_pid() {}
00183 
00184   // op()
00185   virtual bool operator()(const T& it) const { return (*it)->is_semilocal(_pid); }
00186 
00187 protected:
00188   virtual predicate<T>* clone() const { return new semilocal_pid<T>(*this); }
00189   const processor_id_type _pid;
00190 };
00191 
00192 
00193 
00194 // The facelocal_pid predicate returns true if the element
00195 // pointed to is face-local to (is on or has a neighbor on the
00196 // partition of) a given processor id.
00197 template <typename T>
00198 struct facelocal_pid : predicate<T>
00199 {
00200   // Constructor
00201   facelocal_pid(const processor_id_type p) : _pid(p) {}
00202   virtual ~facelocal_pid() {}
00203 
00204   // op()
00205   virtual bool operator()(const T& it) const {
00206     if ((*it)->processor_id() == _pid)
00207       return true;
00208     for (unsigned int n = 0; n != (*it)->n_neighbors(); ++n)
00209       if ((*it)->neighbor(n) &&
00210           (*it)->neighbor(n)->processor_id() == _pid)
00211         return true;
00212     return false;
00213   }
00214 
00215 protected:
00216   virtual predicate<T>* clone() const { return new facelocal_pid<T>(*this); }
00217   const processor_id_type _pid;
00218 };
00219 
00220 
00221 
00222 // The not_pid predicate returns ture if the pointers
00223 // processor id does _not_ match p.
00224 template <typename T>
00225 struct not_pid : pid<T>
00226 {
00227   not_pid(const processor_id_type p) : pid<T>(p) {}
00228 
00229   virtual bool operator()(const T& it) const { return !pid<T>::operator()(it); }
00230 
00231 protected:
00232   virtual predicate<T>* clone() const { return new not_pid<T>(*this); }
00233 };
00234 
00235 
00236 // The elem_type predicate returns true if the pointers
00237 // type matches the given type.  Of course, this one can only
00238 // be instantiated for objects which return Elem*s when dereferened.
00239 template <typename T>
00240 struct elem_type : predicate<T>
00241 {
00242   // Constructor
00243   elem_type (const ElemType t) : _elem_type(t) {}
00244   virtual ~elem_type() {}
00245 
00246   virtual bool operator()(const T& it) const { return (*it)->type() == _elem_type; }
00247 
00248 protected:
00249   virtual predicate<T>* clone() const { return new elem_type<T>(*this); }
00250   const ElemType _elem_type;
00251 };
00252 
00253 
00254 
00255 
00256 
00257 // The level predicate returns true if the pointers level
00258 // matches the given level.
00259 template <typename T>
00260 struct level : predicate<T>
00261 {
00262   // Constructor
00263   level (const unsigned int l) : _level(l) {}
00264   virtual ~level() {}
00265 
00266   virtual bool operator()(const T& it) const { return (*it)->level() == _level; }
00267 
00268 protected:
00269   virtual predicate<T>* clone() const { return new level<T>(*this); }
00270   const unsigned int _level;
00271 };
00272 
00273 
00274 
00275 // The not_level predicate returns true if the pointers level
00276 // _does not_ match the given level.
00277 template <typename T>
00278 struct not_level : level<T>
00279 {
00280   // Constructor
00281   not_level(const unsigned int l) : level<T>(l) {}
00282 
00283   virtual bool operator()(const T& it) const { return !level<T>::operator()(it); }
00284 
00285 protected:
00286   virtual predicate<T>* clone() const { return new not_level<T>(*this); }
00287 };
00288 
00289 
00290 
00291 
00292 // The null_neighbor predicate returns true if the pointer has any
00293 // NULL neigbors.
00294 template <typename T>
00295 struct null_neighbor : predicate<T>
00296 {
00297   virtual ~null_neighbor() {}
00298   virtual bool operator()(const T& it) const
00299   {
00300     return (*it)->on_boundary();
00301   }
00302 
00303 protected:
00304   virtual predicate<T>* clone() const { return new null_neighbor<T>(*this); }
00305 };
00306 
00307 
00308 
00309 // This predicate simply forwards the work of determining whether
00310 // a particular side is on the boundary to the iterator itself, which
00311 // has more information.
00312 template <typename T>
00313 struct boundary_side : predicate<T>
00314 {
00315   virtual ~boundary_side() {}
00316   virtual bool operator()(const T& it) const
00317   {
00318     return it.side_on_boundary();
00319   }
00320 
00321 protected:
00322   virtual predicate<T>* clone() const { return new boundary_side<T>(*this); }
00323 };
00324 
00325 // The subdomain predicate returns true if the pointers
00326 // subdimain id matches a given subdomain id.
00327 template <typename T>
00328 struct subdomain : predicate<T>
00329 {
00330   // Constructor
00331   subdomain(const subdomain_id_type sid) : _subdomain(sid) {}
00332   virtual ~subdomain() {}
00333 
00334   // op()
00335   virtual bool operator()(const T& it) const { return (*it)->subdomain_id() == _subdomain; }
00336 
00337 protected:
00338   virtual predicate<T>* clone() const { return new subdomain<T>(*this); }
00339   const subdomain_id_type _subdomain;
00340 };
00341 
00342 }
00343 
00344 
00345 } // namespace libMesh
00346 
00347 #endif // LIBMESH_SINGLE_PREDICATES_H