PermLib

orbit.h

00001 // ---------------------------------------------------------------------------
00002 //
00003 //  This file is part of PermLib.
00004 //
00005 // Copyright (c) 2009-2011 Thomas Rehn <thomas@carmen76.de>
00006 // All rights reserved.
00007 // 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions
00010 // are met:
00011 // 1. Redistributions of source code must retain the above copyright
00012 //    notice, this list of conditions and the following disclaimer.
00013 // 2. Redistributions in binary form must reproduce the above copyright
00014 //    notice, this list of conditions and the following disclaimer in the
00015 //    documentation and/or other materials provided with the distribution.
00016 // 3. The name of the author may not be used to endorse or promote products
00017 //    derived from this software without specific prior written permission.
00018 // 
00019 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00020 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00021 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00022 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00023 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00024 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00028 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // ---------------------------------------------------------------------------
00031 
00032 
00033 #ifndef ORBIT_H_
00034 #define ORBIT_H_
00035 
00036 #include <list>
00037 
00038 #include <boost/foreach.hpp>
00039 
00040 namespace permlib {
00041 
00043 template<class PERM,class PDOMAIN>
00044 class Orbit {
00045 public:
00046         virtual ~Orbit() {}
00047         
00049         virtual bool contains(const PDOMAIN& val) const = 0;
00050 
00052         virtual const PDOMAIN& element() const = 0;
00053 protected:
00055 
00061         template<class Action>
00062         void orbit(const PDOMAIN& beta, const std::list<typename PERM::ptr> &generators, Action a, std::list<PDOMAIN>& orbitList);
00063         
00065 
00074         template<class Action>
00075         void orbitUpdate(const PDOMAIN& beta, const std::list<typename PERM::ptr> &generators, const typename PERM::ptr &g, Action a, std::list<PDOMAIN>& orbitList);
00076         
00078 
00081         virtual bool foundOrbitElement(const PDOMAIN& alpha, const PDOMAIN& alpha_p, const typename PERM::ptr& p) = 0;
00082 };
00083 
00084 template <class PERM,class PDOMAIN>
00085 template<class Action>
00086 inline void Orbit<PERM,PDOMAIN>::orbit(const PDOMAIN& beta, const std::list<typename PERM::ptr> &generators, Action a, std::list<PDOMAIN>& orbitList) {
00087         if (orbitList.empty()) {
00088                 orbitList.push_back(beta);
00089                 foundOrbitElement(beta, beta, typename PERM::ptr());
00090         }
00091         BOOST_ASSERT( orbitList.size() >= 1 );
00092         
00093         PERMLIB_DEBUG(std::cout << "orbit of " << beta << std::endl;)
00094         typename std::list<PDOMAIN>::const_iterator it;
00095         for (it = orbitList.begin(); it != orbitList.end(); ++it) {
00096                 const PDOMAIN &alpha = *it;
00097                 BOOST_FOREACH(const typename PERM::ptr& p, generators) {
00098                         PDOMAIN alpha_p = a(*p, alpha);
00099                         if (foundOrbitElement(alpha, alpha_p, p))
00100                                 orbitList.push_back(alpha_p);
00101                 }
00102         }
00103 }
00104 
00105 template <class PERM,class PDOMAIN>
00106 template<class Action>
00107 inline void Orbit<PERM,PDOMAIN>::orbitUpdate(const PDOMAIN& beta, const std::list<typename PERM::ptr> &generators, const typename PERM::ptr &g, Action a, std::list<PDOMAIN>& orbitList) {
00108     if (orbitList.empty()) {
00109                 orbitList.push_back(beta);
00110                 foundOrbitElement(beta, beta, typename PERM::ptr());
00111         }
00112         BOOST_ASSERT( orbitList.size() >= 1 );
00113         
00114         PERMLIB_DEBUG(std::cout << "orbiUpdate of " << beta << " and " << *g << std::endl;)
00115         const unsigned int oldSize = orbitList.size();
00116         // first, compute only ORBIT^g
00117         typename std::list<PDOMAIN>::const_iterator it;
00118         for (it = orbitList.begin(); it != orbitList.end(); ++it) {
00119                 const PDOMAIN &alpha = *it;
00120                 PDOMAIN alpha_g = a(*g, alpha);
00121                 if (foundOrbitElement(alpha, alpha_g, g))
00122                         orbitList.push_back(alpha_g);
00123         }
00124         
00125         if (oldSize == orbitList.size())
00126                 return;
00127         
00128         orbit(beta, generators, a, orbitList);
00129 }
00130 
00131 }
00132 
00133 #endif // -- ORBIT_H_