this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christopher Mears <chris.mears@monash.edu>
5 *
6 * Contributing authors:
7 * Mikael Lagerkvist <lagerkvist@gecode.org>
8 * Christian Schulte <schulte@gecode.org>
9 *
10 * Copyright:
11 * Mikael Lagerkvist, 2007
12 * Christopher Mears, 2012
13 * Christian Schulte, 2007
14 *
15 * This file is part of Gecode, the generic constraint
16 * development environment:
17 * http://www.gecode.org
18 *
19 * Permission is hereby granted, free of charge, to any person obtaining
20 * a copy of this software and associated documentation files (the
21 * "Software"), to deal in the Software without restriction, including
22 * without limitation the rights to use, copy, modify, merge, publish,
23 * distribute, sublicense, and/or sell copies of the Software, and to
24 * permit persons to whom the Software is furnished to do so, subject to
25 * the following conditions:
26 *
27 * The above copyright notice and this permission notice shall be
28 * included in all copies or substantial portions of the Software.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 *
38 */
39
40#include <climits>
41#include <cmath>
42#include <iostream>
43
44namespace Gecode { namespace Support {
45
46 /// Bitsets with index offset.
47 ///
48 /// The valid range of indices for a \a BitSetOffset with \a s bits
49 /// and an offset of \a o is [o, o+1, ..., o+s-1].
50 template<class A>
51 class BitSetOffset : public BitSetBase {
52 protected:
53 /// Allocator
54 A& a;
55 /// Offset
56 int _offset;
57 public:
58 /// Bit set with space for \a s bits with offset of \a o
59 BitSetOffset(A& a, unsigned int s, int o);
60 /// Copy bit set \a bs
61 BitSetOffset(A& a, const BitSetOffset& bs);
62 /// Destructor
63 ~BitSetOffset(void);
64
65 // As for the ordinary bitset, most operations can be inherited
66 // directly from BitSetBase. We only modify the operations that
67 // involve indices or the offset itself.
68
69 /// Access value at bit \a i
70 bool get(int i) const;
71 /// Set bit \a i
72 void set(int i);
73 /// Clear bit \a i
74 void clear(int i);
75 /// Return position greater or equal \a i of next set bit (\a i is allowed to be equal to size)
76 int next(int i) const;
77 /// Resize bitset to \a n elements with specified offset.
78 void resize(A& a, unsigned int n, int offset, bool set=false);
79
80 /// Retrieve the minimum valid index (the offset).
81 int offset(void) const;
82 /// Retrieve the maximum valid index.
83 int max_bit(void) const;
84 /// Is the bit index valid for this bitset?
85 bool valid(int i) const;
86 };
87
88 template<class A>
89 forceinline
90 BitSetOffset<A>::BitSetOffset(A& a0, unsigned int s, int o)
91 : BitSetBase(a0,s), a(a0), _offset(o) {}
92
93 template<class A>
94 forceinline
95 BitSetOffset<A>::BitSetOffset(A& a0, const BitSetOffset<A>& bs)
96 : BitSetBase(a0,bs), a(a0), _offset(bs._offset) {}
97
98 template<class A>
99 forceinline
100 BitSetOffset<A>::~BitSetOffset(void) {
101 dispose(a);
102 }
103
104 template<class A>
105 forceinline bool
106 BitSetOffset<A>::get(int i) const {
107 return BitSetBase::get(static_cast<unsigned int>(i-_offset));
108 }
109
110 template<class A>
111 forceinline void
112 BitSetOffset<A>::set(int i) {
113 BitSetBase::set(static_cast<unsigned int>(i-_offset));
114 }
115
116 template<class A>
117 forceinline void
118 BitSetOffset<A>::clear(int i) {
119 BitSetBase::clear(static_cast<unsigned int>(i-_offset));
120 }
121
122 template<class A>
123 forceinline int
124 BitSetOffset<A>::next(int i) const {
125 return _offset + static_cast<int>
126 (BitSetBase::next(static_cast<unsigned int>(i-_offset)));
127 }
128
129 template<class A>
130 void
131 BitSetOffset<A>::resize(A& a, unsigned int n, int offset, bool set) {
132 BitSetBase::resize(a, n, set);
133 _offset = offset;
134 }
135
136 template<class A>
137 forceinline int
138 BitSetOffset<A>::offset(void) const {
139 return _offset;
140 }
141
142 template<class A>
143 forceinline int
144 BitSetOffset<A>::max_bit(void) const {
145 return _offset + static_cast<int>(size()) - 1;
146 }
147
148 template<class A>
149 forceinline bool
150 BitSetOffset<A>::valid(int i) const {
151 return ((_offset <= i) &&
152 (i <= _offset + static_cast<int>(size()) - 1));
153 }
154
155 template <class A, class Char, class Traits>
156 std::basic_ostream<Char,Traits>&
157 operator <<(std::basic_ostream<Char,Traits>& os, const BitSetOffset<A>& bs) {
158 for (int i = bs.offset() ; i < bs.offset()+static_cast<int>(bs.size()) ; i++)
159 if (bs.get(i))
160 os << i << " ";
161 return os;
162 }
163
164}}
165
166// STATISTICS: support-any
167