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, 2010
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34namespace Gecode { namespace Iter { namespace Values {
35
36 /**
37 * \brief Iterator over value lists
38 *
39 * \ingroup FuncIterVlues
40 */
41 class ValueListIter {
42 protected:
43 /// Value list class
44 class ValueList : public Support::BlockClient<ValueList,Region> {
45 public:
46 /// Value
47 int val;
48 /// Next element
49 ValueList* next;
50 };
51 /// Shared object for allocation
52 class VLIO : public Support::BlockAllocator<ValueList,Region> {
53 public:
54 /// Counter used for reference counting
55 unsigned int use_cnt;
56 /// Initialize
57 VLIO(Region& r);
58 };
59 /// Reference to shared object
60 VLIO* vlio;
61 /// Head of value list
62 ValueList* h;
63 /// Current list element
64 ValueList* c;
65 /// Set value lists
66 void set(ValueList* l);
67 public:
68 /// \name Constructors and initialization
69 //@{
70 /// Default constructor
71 ValueListIter(void);
72 /// Copy constructor
73 ValueListIter(const ValueListIter& i);
74 /// Initialize
75 ValueListIter(Region& r);
76 /// Initialize
77 void init(Region& r);
78 /// Assignment operator (both iterators must be allocated from the same region)
79 ValueListIter& operator =(const ValueListIter& i);
80 //@}
81
82 /// \name Iteration control
83 //@{
84 /// Test whether iterator is still at a value or done
85 bool operator ()(void) const;
86 /// Move iterator to next value (if possible)
87 void operator ++(void);
88 /// Reset iterator to start
89 void reset(void);
90 //@}
91
92 /// \name Value access
93 //@{
94 /// Return value
95 int val(void) const;
96 //@}
97
98 /// Destructor
99 ~ValueListIter(void);
100 };
101
102
103 forceinline
104 ValueListIter::VLIO::VLIO(Region& r)
105 : Support::BlockAllocator<ValueList,Region>(r), use_cnt(1) {}
106
107
108 forceinline
109 ValueListIter::ValueListIter(void)
110 : vlio(nullptr) {}
111
112 forceinline
113 ValueListIter::ValueListIter(Region& r)
114 : vlio(new (r.ralloc(sizeof(VLIO))) VLIO(r)),
115 h(nullptr), c(nullptr) {}
116
117 forceinline void
118 ValueListIter::init(Region& r) {
119 vlio = new (r.ralloc(sizeof(VLIO))) VLIO(r);
120 h = c = nullptr;
121 }
122
123 forceinline
124 ValueListIter::ValueListIter(const ValueListIter& i)
125 : vlio(i.vlio), h(i.h), c(i.c) {
126 vlio->use_cnt++;
127 }
128
129 forceinline ValueListIter&
130 ValueListIter::operator =(const ValueListIter& i) {
131 if (&i != this) {
132 if (--vlio->use_cnt == 0) {
133 Region& r = vlio->allocator();
134 vlio->~VLIO();
135 r.rfree(vlio,sizeof(VLIO));
136 }
137 vlio = i.vlio;
138 vlio->use_cnt++;
139 c=i.c; h=i.h;
140 }
141 return *this;
142 }
143
144 forceinline
145 ValueListIter::~ValueListIter(void) {
146 if (--vlio->use_cnt == 0) {
147 Region& r = vlio->allocator();
148 vlio->~VLIO();
149 r.rfree(vlio,sizeof(VLIO));
150 }
151 }
152
153
154 forceinline void
155 ValueListIter::set(ValueList* l) {
156 h = c = l;
157 }
158
159 forceinline bool
160 ValueListIter::operator ()(void) const {
161 return c != nullptr;
162 }
163
164 forceinline void
165 ValueListIter::operator ++(void) {
166 c = c->next;
167 }
168
169 forceinline void
170 ValueListIter::reset(void) {
171 c = h;
172 }
173
174 forceinline int
175 ValueListIter::val(void) const {
176 return c->val;
177 }
178
179}}}
180
181// STATISTICS: iter-any
182