/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Linnea Ingmar * * Contributing authors: * Christian Schulte * * Copyright: * Linnea Ingmar, 2017 * Christian Schulte, 2017 * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ namespace Gecode { namespace Int { namespace Extensional { template forceinline unsigned int BitSet::limit(void) const { return static_cast(_limit); } template forceinline bool BitSet::empty(void) const { return _limit == 0U; } template forceinline unsigned int BitSet::words(void) const { return static_cast(_limit); } template forceinline unsigned int BitSet::size(void) const { return words(); } template forceinline unsigned int BitSet::width(void) const { assert(!empty()); IndexType width = _index[0]; for (IndexType i=1; i<_limit; i++) width = std::max(width,_index[i]); assert(static_cast(width+1U) >= words()); return static_cast(width+1U); } template forceinline BitSet::BitSet(Space& home, unsigned int n) : _limit(static_cast(n)), _index(home.alloc(n)), _bits(home.alloc(n)) { // Set all bits in all words (including the last) for (IndexType i=0; i<_limit; i++) { _bits[i].init(true); _index[i] = i; } } template template forceinline BitSet::BitSet(Space& home, const BitSet& bs) : _limit(static_cast(bs._limit)), _index(home.alloc(_limit)), _bits(home.alloc(_limit)) { assert(_limit > 0U); for (IndexType i=0; i<_limit; i++) { _bits[i] = bs._bits[i]; _index[i] = static_cast(bs._index[i]); } } template forceinline void BitSet::flush(void) { _limit = 0U; assert(empty()); } template forceinline BitSet::BitSet(Space&, const TinyBitSet<1U>&) { GECODE_NEVER; } template forceinline BitSet::BitSet(Space&, const TinyBitSet<2U>&) { GECODE_NEVER; } template forceinline BitSet::BitSet(Space&, const TinyBitSet<3U>&) { GECODE_NEVER; } template forceinline BitSet::BitSet(Space&, const TinyBitSet<4U>&) { GECODE_NEVER; } template forceinline void BitSet::replace_and_decrease(IndexType i, BitSetData w) { assert(_limit > 0U); BitSetData w_i = _bits[i]; if (w != w_i) { _bits[i] = w; if (w.none()) { assert(_bits[i].none()); _limit--; _bits[i] = _bits[_limit]; _index[i] = _index[_limit]; } } } template forceinline void BitSet::clear_mask(BitSetData* mask) const { assert(_limit > 0U); for (IndexType i=0; i<_limit; i++) { mask[i].init(false); assert(mask[i].none()); } } template forceinline void BitSet::add_to_mask(const BitSetData* b, BitSetData* mask) const { assert(_limit > 0U); for (IndexType i=0; i<_limit; i++) mask[i] = BitSetData::o(mask[i],b[_index[i]]); } template template forceinline void BitSet::intersect_with_mask(const BitSetData* mask) { assert(_limit > 0U); if (sparse) { for (IndexType i = _limit; i--; ) { assert(!_bits[i].none()); BitSetData w_i = _bits[i]; BitSetData w_a = BitSetData::a(w_i, mask[_index[i]]); replace_and_decrease(i,w_a); assert(i == _limit || !_bits[i].none()); } } else { // The same except different _indexing in mask for (IndexType i = _limit; i--; ) { assert(!_bits[i].none()); BitSetData w_i = _bits[i]; BitSetData w_a = BitSetData::a(w_i, mask[i]); replace_and_decrease(i,w_a); assert(i == _limit || !_bits[i].none()); } } } template forceinline void BitSet::intersect_with_masks(const BitSetData* a, const BitSetData* b) { assert(_limit > 0U); for (IndexType i = _limit; i--; ) { assert(!_bits[i].none()); BitSetData w_i = _bits[i]; IndexType offset = _index[i]; BitSetData w_o = BitSetData::o(a[offset], b[offset]); BitSetData w_a = BitSetData::a(w_i,w_o); replace_and_decrease(i,w_a); assert(i == _limit || !_bits[i].none()); } } template forceinline void BitSet::nand_with_mask(const BitSetData* b) { assert(_limit > 0U); for (IndexType i = _limit; i--; ) { assert(!_bits[i].none()); BitSetData w = BitSetData::a(_bits[i],~(b[_index[i]])); replace_and_decrease(i,w); assert(i == _limit || !_bits[i].none()); } } template forceinline bool BitSet::intersects(const BitSetData* b) const { for (IndexType i=0; i<_limit; i++) if (!BitSetData::a(_bits[i],b[_index[i]]).none()) return true; return false; } template forceinline unsigned long long int BitSet::ones(const BitSetData* b) const { unsigned long long int o = 0U; for (IndexType i=0; i<_limit; i++) o += static_cast (BitSetData::a(_bits[i],b[_index[i]]).ones()); return o; } template forceinline unsigned long long int BitSet::ones(void) const { unsigned long long int o = 0U; for (IndexType i=0; i<_limit; i++) o += static_cast(_bits[i].ones()); return o; } template forceinline unsigned long long int BitSet::bits(void) const { return (static_cast(_limit) * static_cast(BitSetData::bpb)); } }}} // STATISTICS: int-prop