permlib  0.2.9
Library for permutation computations
lex_smaller_image_predicate.h
1 // ---------------------------------------------------------------------------
2 //
3 // This file is part of PermLib.
4 //
5 // Copyright (c) 2009-2011 Thomas Rehn <thomas@carmen76.de>
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 // 1. Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // 2. Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // 3. The name of the author may not be used to endorse or promote products
17 // derived from this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // ---------------------------------------------------------------------------
31 
32 
33 #ifndef LEXSMALLERIMAGE_PREDICATE_H_
34 #define LEXSMALLERIMAGE_PREDICATE_H_
35 
36 #include <permlib/predicate/subgroup_predicate.h>
37 
38 #include <set>
39 #include <boost/foreach.hpp>
40 #include <boost/dynamic_bitset.hpp>
41 
42 namespace permlib {
43 
45 
48 template <class PERM>
50 public:
52 
59  template<class ForwardIterator>
60  LexSmallerImagePredicate(unsigned int n, ForwardIterator zerosBegin, ForwardIterator zerosEnd, ForwardIterator onesBegin, ForwardIterator onesEnd);
61 
62  virtual bool operator()(const PERM &p) const;
63  virtual bool childRestriction(const PERM &h, unsigned int i, unsigned long beta_i) const;
64  virtual unsigned int limit() const;
65 private:
66  boost::dynamic_bitset<> m_zeros;
67  boost::dynamic_bitset<> m_ones;
68  unsigned long m_fixed;
69 };
70 
71 //
72 // ---- IMPLEMENTATION
73 //
74 
75 template <class PERM>
76 template<class ForwardIterator>
77 LexSmallerImagePredicate<PERM>::LexSmallerImagePredicate(unsigned int n, ForwardIterator zerosBegin, ForwardIterator zerosEnd, ForwardIterator onesBegin, ForwardIterator onesEnd)
78  : m_zeros(n), m_ones(n), m_fixed(0)
79 {
80  while (zerosBegin != zerosEnd) {
81  m_zeros.set(*zerosBegin++, 1);
82  ++m_fixed;
83  }
84  while (onesBegin != onesEnd) {
85  m_ones.set(*onesBegin++, 1);
86  ++m_fixed;
87  }
88 }
89 
90 
91 template <class PERM>
92 bool LexSmallerImagePredicate<PERM>::operator()(const PERM &p) const {
93  for (unsigned int i = 0; i < p.size(); ++i) {
94  const dom_int pi = p / i;
95  if (pi == i)
96  continue;
97  if (m_ones[i] && m_zeros[pi]) {
98  PERMLIB_DEBUG( std::cout << i << " , " << pi << " !0 -> 0 TRUE" << std::endl; )
99  return true;
100  }
101  if (m_zeros[i] && !m_zeros[pi]) {
102  PERMLIB_DEBUG( std::cout << i << " , " << pi << " 0 -> !0 FALSE" << std::endl; )
103  return false;
104  }
105  }
106  return false;
107 }
108 
109 template <class PERM>
110 bool LexSmallerImagePredicate<PERM>::childRestriction(const PERM &h, unsigned int i, unsigned long beta_i) const {
111  // Because limit() does not depend on h, we have to check at every node
112  // whether we have already found a element, which maps the given sets to a
113  // lexicographically smaller set
114  if ((*this)(h)) {
115  PERMLIB_DEBUG( std::cout << h << " is already the desired element; TRUE" << std::endl; )
116  return true;
117  }
118  if (m_zeros[beta_i] && !m_zeros[h / beta_i]) {
119  PERMLIB_DEBUG( std::cout << (h / beta_i) << " mapping zero " << beta_i << " to non-zero; FALSE" << std::endl; )
120  return false;
121  }
122  return true;
123 }
124 
125 template <class PERM>
127  return m_fixed;
128 }
129 
130 }
131 
132 #endif // -- LEXSMALLERIMAGE_PREDICATE_H_
LexSmallerImagePredicate(unsigned int n, ForwardIterator zerosBegin, ForwardIterator zerosEnd, ForwardIterator onesBegin, ForwardIterator onesEnd)
constructor
Definition: lex_smaller_image_predicate.h:77
coset-type predicate for group elements that map one set of zeros and ones to a lex-smaller set (w...
Definition: lex_smaller_image_predicate.h:49
virtual unsigned int limit() const
limit of recursion depth in backtrack search
Definition: lex_smaller_image_predicate.h:126
virtual bool operator()(const PERM &p) const
true iff group element fulfills predicate
Definition: lex_smaller_image_predicate.h:92
virtual bool childRestriction(const PERM &h, unsigned int i, unsigned long beta_i) const
checks if a given group element should not be followed in backtrack search
Definition: lex_smaller_image_predicate.h:110
abstract base class for subgroup (and coset) predicates
Definition: subgroup_predicate.h:45
Definition: abstract_bsgs.h:49