21 #include "SheafSystem/zn_to_bool.h" 22 #include "SheafSystem/assert_contract.h" 23 #include "SheafSystem/block.h" 24 #include "SheafSystem/error_message.h" 25 #include "SheafSystem/std_iomanip.h" 42 invariance(ub() >= 0);
46 invariance(ct() == ub());
85 _word_ub = xother._word_ub;
86 _values_ub = xother._values_ub;
90 _values =
new word_t[_values_ub];
97 const_cast<zn_to_bool&
>(xother).equal_pa(
this);
102 ensure(is_equal_to(&xother));
120 _word_ub = (_ub+BITS_PER_WORD-1)/BITS_PER_WORD;
121 _values_ub = _word_ub;
123 _values = _values_ub > 0 ?
new word_t[_values_ub] : 0;
125 if(xub > 0 && xinitialize)
135 ensure((xub > 0 && xinitialize) ? is_false() :
true);
147 require(values != 0);
153 _word_ub = (_ub+BITS_PER_WORD-1)/BITS_PER_WORD;
154 _values_ub = _word_ub;
156 _values =
new word_t[_values_ub];
161 for (
int i = 0; i < _values_ub; ++i)
162 _values[i] = values[i];
200 result = _values != 0;
241 result = (0 <= i) && (i < _ub);
245 ensure( result == ((0 <= i) && (i < ub())) );
266 while( (i<indices->ct()) && result)
268 result = index_in_bounds((*indices)[i]);
310 word_t* word_ptr = _values;
316 for(
int i=0; i<_ub; i++)
318 if( *word_ptr & mask )
321 if(j < BITS_PER_WORD - 1)
350 int result = ct() - true_ct();
354 ensure(result == ct() - true_ct());
375 _word_ub = (xub + BITS_PER_WORD -1)/BITS_PER_WORD;
377 if(_word_ub > _values_ub)
383 int new_values_ub = (_word_ub > 2*_values_ub) ? _word_ub : 2*_values_ub;
393 for(
int i=0; i<_values_ub; i++)
395 *(new_values+i) = *(_values+i);
400 for(
int i=_values_ub; i<new_values_ub; i++)
402 *(new_values+i) = (
word_t)0;
411 _values = new_values;
412 _values_ub = new_values_ub;
419 post_fatal_error_message(
"Unable to allocate memory.");
423 assertion(_word_ub <= _values_ub);
438 ensure( ub() >= xub );
459 result = _values_ub*BITS_PER_WORD;
483 require(index_in_bounds(indices));
487 int i = (*indices)[0];
489 while( (i<indices->ct()) && result )
491 result = (*this)[(*indices)[i]] && result;
513 require(index_in_bounds(indices));
517 int i = (*indices)[0];
519 while( (i<indices->ct()) && result )
521 result = !((*this)[(*indices)[i]]) && result;
542 require(index_in_bounds(xindex));
549 get_word(xindex) |= get_mask(xindex);
555 get_word(xindex) &= ~get_mask(xindex);
561 ensure((*
this)[xindex] == val);
575 require(indices != 0);
576 require( index_in_bounds(indices) );
581 for(
int i=0; i<indices->
ct(); i++)
583 int j = (*indices)[i];
586 get_word(j) |= get_mask(j);
590 get_word(j) &= ~get_mask(j);
597 ensure(val ? is_true_for(indices) : is_false_for(indices));
612 require(xindex >= 0);
618 int lnew_ub = (xindex < 2*_ub) ? 2*_ub : xindex+1;
622 assertion(_ub > xindex);
629 ensure((*
this)[xindex] == val);
644 require(index_in_bounds(xindex));
650 word_t& w = get_word(xindex);
651 word_t m = get_mask(xindex);
653 w = get_word(xindex);
654 m = get_mask(xindex);
655 w = (w | m) & ~(w & m);
675 require(indices != 0);
676 require( index_in_bounds(indices) );
681 for(
int i=0; i<indices->
ct(); i++)
683 int j = (*indices)[i];
686 w = (w | m) & ~(w & m);
692 ensure(unexecutable(
"for all i in indices: (*this)[i] = ! old (*this)[i]"));
717 while( (i<_word_ub) && result )
719 result = ( *(_values + i) == ~(static_cast<word_t>(0)) );
745 while( (i<_word_ub) && result )
747 result = ( *(_values + i) == static_cast<word_t>(0) );
771 require((other != 0) && (other->
ub() == ub()));
775 while( (i<_word_ub) && result )
777 result = ( *(_values + i) == *(other->_values +i) );
799 require(other.
ub() == ub());
803 while( (i<_word_ub) && result )
805 result = ( *(_values + i) == *(other._values +i) );
829 require((other != 0) && (other->
ub() == ub()));
833 while( (i<_word_ub) && result )
835 word_t other_value = *(other->_values + i);
836 result = ( ( (*(_values + i)) & other_value ) == other_value );
860 require((other != 0) && (other->
ub() == ub()));
864 while( (i<_word_ub) && result )
866 result = ( *(_values + i) == ~(*(other->_values +i)) );
896 ensure((result != 0) && result->
invariant());
913 for(
int i=0; i<_word_ub; i++)
915 *(_values+i) = ~((
word_t)0);
944 ensure((result != 0) && result->
invariant());
961 for(
int i=0; i<_word_ub; i++)
994 ensure((result != 0) && result->
invariant());
995 ensure(is_equal_to(result));
1011 for(
int i=0; i<_word_ub; i++)
1013 *(result->_values+ i) = *(_values+i);
1018 ensure(is_equal_to(result));
1040 _word_ub = xother._word_ub;
1041 _values =
new word_t[_word_ub];
1043 for(
int i=0; i<_word_ub; i++)
1045 _values[i] = xother._values[i];
1050 ensure(*
this == xother);
1076 ensure((result != 0) && result->
invariant());
1077 ensure(is_not(result));
1093 for(
int i=0; i<_word_ub; i++)
1095 *(result->_values+ i) = ~(*(_values+i));
1134 require((other != 0) && (other->
ub() == ub()));
1141 b_and_pa(other, result);
1145 ensure((result != 0) && result->
invariant());
1160 require((other != 0));
1161 require(result != 0);
1162 require(other->
ub() == result->
ub());
1166 for(
int i=0; i<_word_ub; i++)
1168 *(result->_values+i) = *(_values+i) & *(other->_values+i);
1186 require((other != 0) && (other->
ub() == ub()));
1190 b_and_pa(other,
this);
1208 require((other != 0) && (other->
ub() == ub()));
1215 b_or_pa(other, result);
1219 ensure((result != 0) && result->
invariant());
1234 require((other != 0));
1235 require(result != 0);
1236 require(other->
ub() == result->
ub());
1240 for(
int i=0; i<_word_ub; i++)
1242 *(result->_values+i) = *(_values+i) | *(other->_values+i);
1260 require((other != 0) && (other->
ub() == ub()));
1264 b_or_pa(other,
this);
1282 require((other != 0) && (other->
ub() == ub()));
1289 b_and_not_pa(other, result);
1293 ensure((result != 0) && result->
invariant());
1308 require((other != 0));
1309 require(result != 0);
1310 require(other->
ub() == result->
ub());
1314 for(
int i=0; i<_word_ub; i++)
1316 *(result->_values+i) = *(_values+i) & ~(*(other->_values+i));
1334 require((other != 0) && (other->
ub() == ub()));
1338 b_and_not_pa(other,
this);
1362 xos << endl <<
"word ub: " << xzn._word_ub <<
"; words:" << endl;
1364 char lfill_char = xos.fill(
'0');
1368 for (
int i = 0; i < xzn._word_ub; ++i)
1370 xos << setw(lwidth) << *(xzn._values+i) << endl;
1374 xos.fill(lfill_char);
1376 xos << endl <<
"bit ub: " << xzn.
ub() <<
" bits: " << endl;
1378 for(
int i=0; i < xzn.
ub(); ++i)
1380 if( *word_ptr & mask )
1389 if(j < zn_to_bool::BITS_PER_WORD - 1)
1420 result = xinclude_shallow ?
sizeof(xp) : 0;
1426 if(lxp.values() != 0)
1433 ensure(result >= 0);
zn_to_bool * b_and_not(const zn_to_bool *other)
This AND NOT other, auto-allocated.
void b_and_pa(const zn_to_bool *other, zn_to_bool *result)
This AND other, pre-allocated.
size_type ct() const
The number of items currently in use.
bool operator==(const zn_to_bool &other) const
True if function is equal to other evrywhere on domain.
void b_and_not_pa(const zn_to_bool *other, zn_to_bool *result)
This AND NOT other, pre-allocated.
void make_false_sa()
Constant function false, self-allocated. Note on terminology: "false" is a keyword, so this function can not be just "false"
void b_or_sa(const zn_to_bool *other)
This OR other, self-allocated.
void put(int i, bool value)
Sets i-th member to value.
unsigned int word_t
The type of the private, internal representation used for bits; Unsigned int is the type used in the ...
bool is_true_for(const block< int > *indices) const
True if for all i in indices: member with index i is true.
int ub() const
The upper bound for the domain index.
zn_to_bool * make_true()
Constant function true, auto-allocated.
bool is_false_for(const block< int > *indices) const
True if for all i in indices: member with index i is false.
void b_not_pa(const zn_to_bool *other)
Boolean complement, pre-allocated.
bool is_not(const zn_to_bool *other) const
True if function is complement of other evrywhere on domain.
void put_not(int i)
Sets i-th member to its complement.
A map from Zn (the integers mod n) to bools. A characteristic function used to represent subsets of Z...
zn_to_bool()
Default constructor.
void b_not_sa()
Boolean complement, self-allocated.
int ct() const
The number of members in the domain.
bool index_in_bounds(int i) const
True if index i is in bounds for this vector.
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.
zn_to_bool * equal()
OBSOLETE; use copy contructor.
bool is_equal_to(const zn_to_bool *other) const
OBSOLETE: use operator==(const zn_to_bool&). True if function is equal to other evrywhere on domain...
void equal_pa(const zn_to_bool *result)
OBSOLETE; use assignment operator.
zn_to_bool * make_false()
Constant function false, auto-allocated. Note on terminology: "false" is a keyword, so this function can not be just "false"
bool domain_is_valid() const
True if domain properly allocated.
zn_to_bool * b_and(const zn_to_bool *other)
This AND other, auto-allocated.
zn_to_bool * b_not()
Boolean complement, auto-allocated.
int false_ct() const
The number of members with value = false.
void b_or_pa(const zn_to_bool *other, zn_to_bool *result)
This OR other, pre-allocated.
size_t capacity() const
The number of members this can extend_to() without reallocating.
zn_to_bool * b_or(const zn_to_bool *other)
This OR other, auto-allocated.
int true_ct() const
The number of members with value = true.
bool is_false() const
True if function is false evrywhere on domain.
SHEAF_DLL_SPEC std::ostream & operator<<(std::ostream &os, const dof_descriptor_array &p)
Insert dof_descriptor_array& p into ostream& os.
bool invariant() const
Class Invariant.
bool is_true() const
True function is true everywhere on domain.
void b_and_sa(const zn_to_bool *other)
This AND other, self-allocated.
void extend_to(int xub)
Make the upper bound at least xub.
bool includes(const zn_to_bool *other) const
True if this function is true everywhere other is true.
zn_to_bool & operator=(const zn_to_bool &xother)
Assignment operator.
void b_and_not_sa(const zn_to_bool *other)
This AND NOT other, self-allocated.
void make_true_sa()
Constant function true, self-allocated. Note on terminology: "true" is a keyword, so this function can not be just "true"
void force(int i, bool value)
Sets the i-th member to value, extends the upper bound if necessary.