/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christopher Mears * * Copyright: * Christopher Mears, 2012 * * 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 LDSB { /// Convert a \a DynamicStack into an \a ArgArray template ArgArray dynamicStackToArgArray(const Support::DynamicStack& s) { ArgArray a(s.entries()); for (int i = 0 ; i < s.entries() ; ++i) { a[i] = s[i]; } return a; } template SymmetryImp::~SymmetryImp(void) {} template void* SymmetryImp::operator new(size_t s, Space& home) { return home.ralloc(s); } template void SymmetryImp::operator delete(void*,Space&) {} template void SymmetryImp::operator delete(void*) {} template VariableSymmetryImp ::VariableSymmetryImp(Space& home, int* _indices, unsigned int n) : indices(home, 0, 0) { // Find minimum and maximum value in _indices: the minimum is the // offset, and the maximum dictates how large the bitset needs to // be. int maximum = _indices[0]; int minimum = _indices[0]; for (unsigned int i = 1 ; i < n ; i++) { if (_indices[i] > maximum) maximum = _indices[i]; if (_indices[i] < minimum) minimum = _indices[i]; } indices.resize(home, maximum-minimum+1, minimum); // Set the bits for the included indices. for (unsigned int i = 0 ; i < n ; i++) { indices.set(_indices[i]); } } template inline VariableSymmetryImp ::VariableSymmetryImp(Space& home, const VariableSymmetryImp& other) : indices(home, other.indices) {} template size_t VariableSymmetryImp ::dispose(Space& home) { indices.dispose(home); return sizeof(*this); } template void VariableSymmetryImp ::update(Literal l) { if (indices.valid(l._variable)) { indices.clear(l._variable); } } template SymmetryImp* VariableSymmetryImp::copy(Space& home) const { return new (home) VariableSymmetryImp(home, *this); } // The minimum value in vs is the bitset's offset, and the maximum // dictates how large the bitset needs to be. template ValueSymmetryImp ::ValueSymmetryImp(Space& home, int* vs, unsigned int n) : values(home, 0, 0) { // Find minimum and maximum value in vs: the minimum is the // offset, and the maximum dictates how large the bitset needs to // be. assert(n > 0); int maximum = vs[0]; int minimum = vs[0]; for (unsigned int i = 1 ; i < n ; i++) { if (vs[i] > maximum) maximum = vs[i]; if (vs[i] < minimum) minimum = vs[i]; } values.resize(home, maximum-minimum+1, minimum); // Set the bits for the included values. for (unsigned int i = 0 ; i < n ; i++) { values.set(vs[i]); } } template ValueSymmetryImp ::ValueSymmetryImp(Space& home, const ValueSymmetryImp& other) : values(home, other.values) { } template size_t ValueSymmetryImp ::dispose(Space& home) { values.dispose(home); return sizeof(*this); } template void ValueSymmetryImp ::update(Literal l) { if (values.valid(l._value)) values.clear(l._value); } template SymmetryImp* ValueSymmetryImp::copy(Space& home) const { return new (home) ValueSymmetryImp(home, *this); } template int VariableSequenceSymmetryImp ::getVal(unsigned int sequence, unsigned int position) const { return indices[sequence*seq_size + position]; } template VariableSequenceSymmetryImp ::VariableSequenceSymmetryImp(Space& home, int* _indices, unsigned int n, unsigned int seqsize) : n_indices(n), seq_size(seqsize), n_seqs(n/seqsize) { indices = home.alloc(n_indices); unsigned int max_index = _indices[0]; for (unsigned int i = 0 ; i < n_indices ; i++) { indices[i] = _indices[i]; if (indices[i] > max_index) max_index = indices[i]; } lookup_size = max_index+1; lookup = home.alloc(lookup_size); for (unsigned int i = 0 ; i < lookup_size ; i++) lookup[i] = -1; for (unsigned int i = 0 ; i < n_indices ; i++) { if (lookup[indices[i]] == -1) lookup[indices[i]] = i; } } template VariableSequenceSymmetryImp ::VariableSequenceSymmetryImp(Space& home, const VariableSequenceSymmetryImp& s) : n_indices(s.n_indices), seq_size(s.seq_size), n_seqs(s.n_seqs), lookup_size(s.lookup_size) { indices = home.alloc(n_indices); memcpy(indices, s.indices, n_indices * sizeof(int)); lookup = home.alloc(lookup_size); memcpy(lookup, s.lookup, lookup_size * sizeof(int)); } template size_t VariableSequenceSymmetryImp ::dispose(Space& home) { home.free(indices, n_indices); home.free(lookup, lookup_size); return sizeof(*this); } /// Compute symmetric literals template ArgArray VariableSequenceSymmetryImp ::symmetric(Literal l, const ViewArray& x) const { Region region; Support::DynamicStack s(region); if (l._variable < (int)lookup_size) { int posIt = lookup[l._variable]; if (posIt == -1) { return dynamicStackToArgArray(s); } unsigned int seqNum = posIt / seq_size; unsigned int seqPos = posIt % seq_size; for (unsigned int seq = 0 ; seq < n_seqs ; seq++) { if (seq == seqNum) { continue; } if (x[getVal(seq, seqPos)].assigned()) { continue; } bool active = true; const unsigned int *firstSeq = &indices[seqNum*seq_size]; const unsigned int *secondSeq = &indices[seq*seq_size]; for (unsigned int i = 0 ; i < seq_size ; i++) { const View& xv = x[firstSeq[i]]; const View& yv = x[secondSeq[i]]; if ((!xv.assigned() && !yv.assigned()) || (xv.assigned() && yv.assigned() && xv.val() == yv.val())) { continue; } else { active = false; break; } } if (active) { s.push(Literal(secondSeq[seqPos], l._value)); } } } return dynamicStackToArgArray(s); } template void VariableSequenceSymmetryImp ::update(Literal l) { // Do nothing. (void) l; } template SymmetryImp* VariableSequenceSymmetryImp ::copy(Space& home) const { return new (home) VariableSequenceSymmetryImp(home, *this); } template int ValueSequenceSymmetryImp ::getVal(unsigned int sequence, unsigned int position) const { return values[sequence*seq_size + position]; } template ValueSequenceSymmetryImp ::ValueSequenceSymmetryImp(Space& home, int* _values, unsigned int n, unsigned int seqsize) : n_values(n), seq_size(seqsize), n_seqs(n/seqsize), dead_sequences(home, n_seqs) { values = home.alloc(n_values); for (unsigned int i = 0 ; i < n_values ; i++) values[i] = _values[i]; } template ValueSequenceSymmetryImp ::ValueSequenceSymmetryImp(Space& home, const ValueSequenceSymmetryImp& vss) : n_values(vss.n_values), seq_size(vss.seq_size), n_seqs(vss.n_seqs), dead_sequences(home, vss.dead_sequences) { values = home.alloc(n_values); for (unsigned int i = 0 ; i < n_values ; i++) values[i] = vss.values[i]; } template size_t ValueSequenceSymmetryImp ::dispose(Space& home) { home.free(values, n_values); return sizeof(*this); } template void ValueSequenceSymmetryImp ::update(Literal l) { unsigned int seq = 0; unsigned int pos = 0; for (unsigned int i = 0 ; i < n_values ; i++) { if (values[i] == l._value) { dead_sequences.set(seq); // TODO: This can be slightly optimised. while (pos < seq_size) { i++; pos++; } } pos++; if (pos == seq_size) { pos = 0; seq++; } } } template SymmetryImp* ValueSequenceSymmetryImp ::copy(Space& home) const { return new (home) ValueSequenceSymmetryImp(home, *this); } }}} // STATISTICS: int-branch