Generated on for Gecode by doxygen 1.17.0
bitset-offset.hpp
Go to the documentation of this file.
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 Zayenz Lagerkvist <lagerkvist@gecode.dev>
8 * Christian Schulte <schulte@gecode.dev>
9 *
10 * Copyright:
11 * Mikael Zayenz 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.dev
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
50 template<class A>
51 class BitSetOffset : public BitSetBase {
52 protected:
54 A& a;
57 public:
59 BitSetOffset(A& a, unsigned int s, int o);
61 BitSetOffset(A& a, const BitSetOffset& bs);
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
70 bool get(int i) const;
72 void set(int i);
74 void clear(int i);
76 int next(int i) const;
78 void resize(A& a, unsigned int n, int offset, bool set=false);
79
81 int offset(void) const;
83 int max_bit(void) const;
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
96 : BitSetBase(a0,bs), a(a0), _offset(bs._offset) {}
97
98 template<class A>
99 forceinline
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
113 BitSetBase::set(static_cast<unsigned int>(i-_offset));
114 }
115
116 template<class A>
117 forceinline void
119 BitSetBase::clear(static_cast<unsigned int>(i-_offset));
120 }
121
122 template<class A>
123 forceinline int
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) {
133 _offset = offset;
134 }
135
136 template<class A>
137 forceinline int
139 return _offset;
140 }
141
142 template<class A>
143 forceinline int
145 return _offset + static_cast<int>(size()) - 1;
146 }
147
148 template<class A>
149 forceinline bool
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
void dispose(A &a)
Dispose memory for bit set.
bool get(unsigned int i) const
Access value at bit i.
unsigned int size(void) const
Return size of bitset (number of bits).
void clear(unsigned int i)
Clear bit i.
void set(unsigned int i)
Set bit i.
unsigned int next(unsigned int i) const
Return position greater or equal i of next set bit (i is allowed to be equal to size).
void resize(A &a, unsigned int n, bool setbits=false)
Resize bitset to n elememts.
Bitsets with index offset.
bool get(int i) const
Access value at bit i.
int next(int i) const
Return position greater or equal i of next set bit (i is allowed to be equal to size).
bool valid(int i) const
Is the bit index valid for this bitset?
int max_bit(void) const
Retrieve the maximum valid index.
void resize(A &a, unsigned int n, int offset, bool set=false)
Resize bitset to n elements with specified offset.
void clear(int i)
Clear bit i.
BitSetOffset(A &a, unsigned int s, int o)
Bit set with space for s bits with offset of o.
Support algorithms and datastructures
std::basic_ostream< Char, Traits > & operator<<(std::basic_ostream< Char, Traits > &os, const BitSetOffset< A > &bs)
Gecode toplevel namespace