SheafSystem  0.0.0.0
zn_to_bool.h
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 // Interface for class ZN_TO_BOOL
19 
20 
21 #ifndef ZN_TO_BOOL_H
22 #define ZN_TO_BOOL_H
23 
24 #ifndef SHEAF_DLL_SPEC_H
25 #include "SheafSystem/sheaf_dll_spec.h"
26 #endif
27 
28 #include "SheafSystem/assert_contract.h"
29 
30 #ifndef STD_IOSTREAM_H
31 #include "SheafSystem/std_iostream.h"
32 #endif
33 
34 #ifndef STD_LIMITS_H
35 #include "SheafSystem/std_limits.h"
36 #endif
37 
38 namespace sheaf
39 {
40 
41 template <typename T>
42 class block;
43 
44 class zn_to_bool;
45 class index_iterator;
46 class subposet;
47 
52 class SHEAF_DLL_SPEC zn_to_bool
53 {
54 
55 
56  friend SHEAF_DLL_SPEC std::ostream & operator << (std::ostream &os, zn_to_bool& zn);
57  friend SHEAF_DLL_SPEC size_t deep_size(const zn_to_bool& zn, bool xinclude_shallow);
58  friend class index_iterator;
59  friend class subposet;
60 
61  // ===========================================================
62  // ZN_TO_BOOL FACET
63  // ===========================================================
64 
65 public:
66 
70  bool invariant() const;
71 
75  zn_to_bool();
76 
80  zn_to_bool(const zn_to_bool& xother);
81 
86  zn_to_bool(int xub, bool xinitialize = true);
87 
93  zn_to_bool(int xub, unsigned *values);
94 
98  ~zn_to_bool();
99 
100  // ===========================================================
101  // DOMAIN FACET
102  // ===========================================================
103 
107  bool domain_is_valid() const;
108 
112  int ub() const;
113 
117  bool index_in_bounds(int i) const;
118 
122  bool index_in_bounds(const block<int>* indices) const;
123 
127  int ct() const;
128 
132  int true_ct() const;
133 
137  int false_ct() const;
138 
142  void extend_to(int xub);
143 
147  size_t capacity() const;
148 
149  // $$ISSUE: If this typedef is private, then there is no access
150  // to it in other classes once they are inserted into the sheaf
151  // namespace. JEB 090408
152 
160  typedef unsigned int word_t;
161 
167  enum static_const_int {BITS_PER_WORD = std::numeric_limits<word_t>::digits};
168 
169 protected:
170 private:
171 
175  int _ub;
176 
180  int _word_ub;
181 
185  inline int word_ub()
186  {
187  return _word_ub;
188  }
189 
193  word_t* _values;
194 
198  inline word_t* values()
199  {
200  return _values;
201  };
202 
206  int _values_ub;
207 
208  // ===========================================================
209  // CHARACTERISTIC FUNCTION FACET
210  // ===========================================================
211 
212 public:
213 
217  inline bool operator [] (int i) const
218  {
219  // preconditions:
220 
221  require(index_in_bounds(i));
222 
223  // body:
224 
225  bool result = (get_word(i) & get_mask(i)) != 0;
226 
227  return result;
228  }
229 
233  bool is_true_for(const block<int>* indices) const;
234 
238  bool is_false_for(const block<int>* indices) const;
239 
243  void put(int i, bool value);
244 
248  void put(const block<int>* indices, bool value);
249 
253  void force(int i, bool value);
254 
258  void put_not(int i);
259 
263  void put_not(const block<int>* indices);
264 
265 protected:
266 private:
267 
271  inline word_t& get_word(int xindex)
272  {
273  return(*(_values+(xindex/BITS_PER_WORD)));
274  }
275 
279  inline const word_t& get_word(int xindex) const
280  {
281  return(*(_values+(xindex/BITS_PER_WORD)));
282  }
283 
287  inline word_t get_mask(int xindex) const
288  {
289  return( 1 << (xindex%BITS_PER_WORD) );
290  }
291 
292  // ===========================================================
293  // BOOLEAN ALGEBRA FACET
294  // ===========================================================
295 
296 public:
297 
301  bool is_true() const;
302 
306  bool is_false() const;
307 
312  bool is_equal_to(const zn_to_bool* other) const;
313 
317  bool operator==(const zn_to_bool& other) const;
318 
322  bool includes(const zn_to_bool* other) const;
323 
327  bool is_not(const zn_to_bool* other) const;
328 
332  zn_to_bool* make_true();
333 
339  void make_true_sa();
340 
341  // pre-allocated version not needed for unary operators
342 
348  zn_to_bool* make_false();
349 
355  void make_false_sa();
356 
357  // pre-allocated version not needed for unary operators
358 
362  zn_to_bool* equal();
363 
367  void equal_pa(const zn_to_bool* result);
368 
372  zn_to_bool& operator=(const zn_to_bool& xother);
373 
374 
375  // The operators below have 'b_' prefix. This indicates that
376  // these operators are boolean operators. The need for this
377  // prefix arises from 'not', 'and' & 'or' being reserved words
378  // in the ansi C++ standard.
379 
383  zn_to_bool* b_not();
384 
388  void b_not_pa(const zn_to_bool* other);
389 
393  void b_not_sa();
394 
398  zn_to_bool* b_and(const zn_to_bool* other);
399 
403  void b_and_pa(const zn_to_bool* other, zn_to_bool* result);
404 
408  void b_and_sa(const zn_to_bool* other);
409 
413  zn_to_bool* b_or(const zn_to_bool* other);
414 
418  void b_or_pa(const zn_to_bool* other, zn_to_bool* result);
419 
423  void b_or_sa(const zn_to_bool* other);
424 
428  zn_to_bool* b_and_not(const zn_to_bool* other);
429 
433  void b_and_not_pa(const zn_to_bool* other, zn_to_bool* result);
434 
438  void b_and_not_sa(const zn_to_bool* other);
439 
440 protected:
441 private:
442 };
443 
444 
445 // ===========================================================
446 // NON-MEMBER FUNCTIONS
447 // ===========================================================
448 
452 SHEAF_DLL_SPEC
453 std::ostream & operator << (std::ostream &os, zn_to_bool& zn);
454 
458 SHEAF_DLL_SPEC
459 size_t deep_size(const zn_to_bool& xp, bool xinclude_shallow = true);
460 
461 } // namespace sheaf
462 
463 #endif // ifndef ZN_TO_BOOL_H
A client handle for a subposet.
Definition: subposet.h:86
unsigned int word_t
The type of the private, internal representation used for bits; Unsigned int is the type used in the ...
Definition: zn_to_bool.h:160
A map from Zn (the integers mod n) to bools. A characteristic function used to represent subsets of Z...
Definition: zn_to_bool.h:52
static_const_int
Number of bits per word.
Definition: zn_to_bool.h:167
SHEAF_DLL_SPEC size_t deep_size(const dof_descriptor_array &xp, bool xinclude_shallow=true)
The deep size of the referenced object of type dof_descriptor_array.
Iterates over the subset of Zn defined by the characteristic function host().
bool operator==(const singly_linked_list< T, Alloc > &lhs, const singly_linked_list< T, Alloc > &rhs)
Checks if the contents of lhs and rhs are equal, that is, whether lhs.size() == rhs.size() and each element in lhs compares equal with the element in rhs at the same position.
SHEAF_DLL_SPEC std::ostream & operator<<(std::ostream &os, const dof_descriptor_array &p)
Insert dof_descriptor_array& p into ostream& os.
Namespace for the sheaves component of the sheaf system.