permlib  0.2.6
Library for permutation computations
 All Classes Functions Variables Typedefs Enumerations Friends
include/permlib/predicate/lex_smaller_image_predicate.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 LEXSMALLERIMAGE_PREDICATE_H_
00034 #define LEXSMALLERIMAGE_PREDICATE_H_
00035 
00036 #include <permlib/predicate/subgroup_predicate.h>
00037 
00038 #include <set>
00039 #include <boost/foreach.hpp>
00040 #include <boost/dynamic_bitset.hpp>
00041 
00042 namespace permlib {
00043 
00045 
00048 template <class PERM>
00049 class LexSmallerImagePredicate : public SubgroupPredicate<PERM> {
00050 public:
00052 
00059         template<class ForwardIterator>
00060         LexSmallerImagePredicate(unsigned int n, ForwardIterator zerosBegin, ForwardIterator zerosEnd, ForwardIterator onesBegin, ForwardIterator onesEnd);
00061 
00062         virtual bool operator()(const PERM &p) const;
00063         virtual bool childRestriction(const PERM &h, unsigned int i, unsigned long beta_i) const;
00064         virtual unsigned int limit() const;
00065 private:
00066         boost::dynamic_bitset<> m_zeros;
00067         boost::dynamic_bitset<> m_ones;
00068         unsigned long m_fixed;
00069 };
00070 
00071 //
00072 //     ----       IMPLEMENTATION
00073 //
00074 
00075 template <class PERM>
00076 template<class ForwardIterator>
00077 LexSmallerImagePredicate<PERM>::LexSmallerImagePredicate(unsigned int n, ForwardIterator zerosBegin, ForwardIterator zerosEnd, ForwardIterator onesBegin, ForwardIterator onesEnd)
00078         : m_zeros(n), m_ones(n), m_fixed(0)
00079 { 
00080         while (zerosBegin != zerosEnd) {
00081                 m_zeros.set(*zerosBegin++, 1);
00082                 ++m_fixed;
00083         }
00084         while (onesBegin != onesEnd) {
00085                 m_ones.set(*onesBegin++, 1);
00086                 ++m_fixed;
00087         }
00088 }
00089 
00090 
00091 template <class PERM>
00092 bool LexSmallerImagePredicate<PERM>::operator()(const PERM &p) const {
00093         for (unsigned int i = 0; i < p.size(); ++i) {
00094                 const dom_int pi = p / i;
00095                 if (pi == i)
00096                         continue;
00097                 if (m_ones[i] && m_zeros[pi]) {
00098                         PERMLIB_DEBUG( std::cout << i << " , " << pi << "  !0 ->  0  TRUE" << std::endl; )
00099                         return true;
00100                 }
00101                 if (m_zeros[i] && !m_zeros[pi]) {
00102                         PERMLIB_DEBUG( std::cout << i << " , " << pi << "  0  -> !0  FALSE" << std::endl; )
00103                         return false;
00104                 }
00105         }
00106         return false;
00107 }
00108 
00109 template <class PERM>
00110 bool LexSmallerImagePredicate<PERM>::childRestriction(const PERM &h, unsigned int i, unsigned long beta_i) const {
00111         // Because limit() does not depend on h, we have to check at every node
00112         // whether we have already found a element, which maps the given sets to a
00113         // lexicographically smaller set
00114         if ((*this)(h)) {
00115                 PERMLIB_DEBUG( std::cout << h << " is already the desired element; TRUE" << std::endl; )
00116                 return true;
00117         }
00118         if (m_zeros[beta_i] && !m_zeros[h / beta_i]) {
00119                 PERMLIB_DEBUG( std::cout << (h / beta_i) << " mapping zero " << beta_i << " to non-zero; FALSE" << std::endl; )
00120                 return false;
00121         }
00122         return true;
00123 }
00124 
00125 template <class PERM>
00126 unsigned int LexSmallerImagePredicate<PERM>::limit() const {
00127         return m_fixed;
00128 }
00129 
00130 }
00131 
00132 #endif // -- LEXSMALLERIMAGE_PREDICATE_H_