include/deal.II/base/event.h

00001 //---------------------------------------------------------------------------
00002 //    @f$Id: event.h 25345 2012-03-31 08:37:04Z bangerth @f$
00003 //
00004 //    Copyright (C) 2010, 2012 by the deal.II authors
00005 //
00006 //    This file is subject to QPL and may not be  distributed
00007 //    without copyright and license information. Please refer
00008 //    to the file deal.II/doc/license.html for the  text  and
00009 //    further information on this license.
00010 //
00011 //---------------------------------------------------------------------------
00012 
00013 #ifndef __deal2__event_h
00014 #define __deal2__event_h
00015 
00016 #include <deal.II/base/config.h>
00017 
00018 #include <vector>
00019 #include <string>
00020 #include <iostream>
00021 
00022 DEAL_II_NAMESPACE_OPEN
00023 
00024 namespace Algorithms
00025 {
00044   class Event
00045   {
00046     public:
00055       static Event assign (const char* name);
00056 
00063 //      static Event find(const std::string& name);
00064 
00069       Event ();
00070 
00074       void clear();
00075 
00079       void all();
00080 
00084       Event& operator += (const Event& event);
00085 
00089       Event& operator -= (const Event& event);
00090 
00096       bool test (const Event& event) const;
00097 
00102       bool any () const;
00103 
00107       template <class OS>
00108       void print (OS& os) const;
00109 
00113       template <class OS>
00114       static void print_assigned (OS& os);
00115 
00116     private:
00124       bool all_true;
00125 
00129       std::vector<bool> flags;
00130 
00134       static std::vector<std::string> names;
00135   };
00136 
00140   namespace Events
00141   {
00147     extern const Event initial;
00148 
00154     extern const Event bad_derivative;
00155 
00160     extern const Event new_time;
00161 
00165     extern const Event new_timestep_size;
00166   }
00167 
00168 
00169 //----------------------------------------------------------------------//
00170 
00171 
00172   inline
00173   bool
00174   Event::any () const
00175   {
00176     if (all_true) return true;
00177     for (std::vector<bool>::const_iterator i=flags.begin();
00178          i != flags.end(); ++i)
00179       if (*i) return true;
00180     return false;
00181   }
00182 
00183 
00184   inline
00185   bool
00186   Event::test (const Event& event) const
00187   {
00188 
00189                                      // First, test all_true in this
00190     if (all_true) return true;
00191 
00192     const unsigned int n = flags.size();
00193     const unsigned int m = event.flags.size();
00194     const unsigned int n_min = std::min(n, m);
00195 
00196                                      // Now, if all_true set in the
00197                                      // other, then all must be true
00198                                      // in this
00199     if (event.all_true)
00200       {
00201                                          // Non existing flags are
00202                                          // always assumed false
00203         if (m > n)
00204           return false;
00205 
00206                                          // Test all flags separately
00207                                          // and return false if one is
00208                                          // not set
00209         for (std::vector<bool>::const_iterator i=flags.begin();
00210              i != flags.end(); ++i)
00211           if (!*i) return false;
00212                                          // All flags are set
00213         return true;
00214       }
00215 
00216                                      // Finally, compare each flag
00217                                      // separately
00218     for (unsigned int i=0;i<n_min;++i)
00219       if (event.flags[i] && !flags[i])
00220         return false;
00221     for (unsigned int i=n_min;i<m;++i)
00222       if (event.flags[i])
00223         return false;
00224     return true;
00225   }
00226 
00227 
00228 
00229   inline
00230   Event& Event::operator += (const Event& event)
00231   {
00232     all_true |= event.all_true;
00233     if (all_true) return *this;
00234 
00235     if (flags.size() < event.flags.size())
00236       flags.resize(event.flags.size());
00237     for (unsigned int i=0;i<event.flags.size();++i)
00238       flags[i] = flags[i] || event.flags[i];
00239 
00240     return *this;
00241   }
00242 
00243 
00244   inline
00245   Event& Event::operator -= (const Event& event)
00246   {
00247     if (!event.any()) return *this;
00248 
00249     all_true = false;
00250     if(event.all_true)
00251       {
00252         for (std::vector<bool>::iterator i=flags.begin();
00253              i != flags.end(); ++i)
00254           *i = false;
00255         return *this;
00256       }
00257 
00258     if (flags.size() < event.flags.size())
00259       flags.resize(event.flags.size());
00260     for (unsigned int i=0;i<event.flags.size();++i)
00261       if (event.flags[i]) flags[i] = false;
00262 
00263     return *this;
00264   }
00265 
00266 
00267   template <class OS>
00268   inline
00269   void
00270   Event::print (OS& os) const
00271   {
00272     if (all_true)
00273       os << " ALL";
00274 
00275     for (unsigned int i=0;i<flags.size();++i)
00276       if (flags[i])
00277         os << ' ' << names[i];
00278   }
00279 
00280 
00281   template <class OS>
00282   inline
00283   void
00284   Event::print_assigned (OS& os)
00285   {
00286     for (unsigned int i=0;i<names.size();++i)
00287       os << i << '\t' << names[i] << std::endl;
00288   }
00289 
00290 
00297   template <class OS>
00298   OS& operator << (OS& o, const Event& e)
00299   {
00300     e.print(o);
00301     return o;
00302   }
00303 }
00304 
00305 DEAL_II_NAMESPACE_CLOSE
00306 
00307 #endif
00308 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

deal.II documentation generated on Tue May 22 2012 12:06:05 by doxygen 1.7.3