this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 *
6 * Copyright:
7 * Christian Schulte, 2008
8 *
9 * Converted into offset version:
10 * Christopher Mears <chris.mears@monash.edu>
11 *
12 * This file is part of Gecode, the generic constraint
13 * development environment:
14 * http://www.gecode.org
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining
17 * a copy of this software and associated documentation files (the
18 * "Software"), to deal in the Software without restriction, including
19 * without limitation the rights to use, copy, modify, merge, publish,
20 * distribute, sublicense, and/or sell copies of the Software, and to
21 * permit persons to whom the Software is furnished to do so, subject to
22 * the following conditions:
23 *
24 * The above copyright notice and this permission notice shall be
25 * included in all copies or substantial portions of the Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 *
35 */
36
37namespace Gecode { namespace Iter { namespace Values {
38
39 /**
40 * \brief Value iterator for values in an offset bitset
41 *
42 * \ingroup FuncIterValues
43 */
44 template<class BS>
45 class BitSetOffset {
46 protected:
47 /// Bitset
48 const BS& bs;
49 /// Current value
50 int cur;
51 /// Limit value
52 int limit;
53 /// Move to next set bit
54 void move(void);
55 public:
56 /// \name Constructors and initialization
57 //@{
58 /// Initialize with bitset \a bs
59 BitSetOffset(const BS& bs);
60 /// Initialize with bitset \a bs and start \a n and stop \a m
61 BitSetOffset(const BS& bs, int n, int m);
62 //@}
63
64 /// \name Iteration control
65 //@{
66 /// Test whether iterator is still at a value or done
67 bool operator ()(void) const;
68 /// Move iterator to next value (if possible)
69 void operator ++(void);
70 //@}
71
72 /// \name Value access
73 //@{
74 /// Return current value
75 int val(void) const;
76 //@}
77 };
78
79
80 template<class BS>
81 forceinline
82 BitSetOffset<BS>::BitSetOffset(const BS& bs0)
83 : bs(bs0), cur(bs.next(bs0.offset())), limit(bs.offset()+bs.size()) {
84 }
85
86 template<class BS>
87 forceinline
88 BitSetOffset<BS>::BitSetOffset(const BS& bs0, int n, int m)
89 : bs(bs0),
90 cur(bs.next(n)),
91 limit(std::min(bs.offset()+bs.size(),m+1)) {
92 }
93
94 template<class BS>
95 forceinline void
96 BitSetOffset<BS>::operator ++(void) {
97 cur=bs.next(cur+1);
98 }
99 template<class BS>
100 forceinline bool
101 BitSetOffset<BS>::operator ()(void) const {
102 return cur < limit;
103 }
104
105 template<class BS>
106 forceinline int
107 BitSetOffset<BS>::val(void) const {
108 return static_cast<int>(cur);
109 }
110
111}}}
112
113// STATISTICS: iter-any