Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
archive.hpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Guido Tack <tack@gecode.org>
5 *
6 * Copyright:
7 * Guido Tack, 2011
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 {
35
42 class Archive {
43 private:
45 int _size;
47 int _n;
49 unsigned int* _a;
51 int _pos;
53 GECODE_KERNEL_EXPORT void resize(int n);
54 public:
56 Archive(void);
64 void put(unsigned int i);
66 int size(void) const;
68 unsigned int operator [](int i) const;
70 unsigned int get(void);
71 };
72
76 Archive&
77 operator <<(Archive& e, unsigned int i);
81 Archive&
82 operator <<(Archive& e, int i);
86 Archive&
87 operator <<(Archive& e, unsigned short i);
91 Archive&
92 operator <<(Archive& e, short i);
96 Archive&
97 operator <<(Archive& e, unsigned char i);
101 Archive&
102 operator <<(Archive& e, char i);
106 Archive&
107 operator <<(Archive& e, bool i);
111 Archive&
112 operator <<(Archive& e, float d);
116 Archive&
117 operator <<(Archive& e, double d);
118
122 Archive&
123 operator >>(Archive& e, unsigned int& i);
127 Archive&
128 operator >>(Archive& e, int& i);
132 Archive&
133 operator >>(Archive& e, unsigned short& i);
137 Archive&
138 operator >>(Archive& e, short& i);
142 Archive&
143 operator >>(Archive& e, unsigned char& i);
147 Archive&
148 operator >>(Archive& e, char& i);
152 Archive&
153 operator >>(Archive& e, bool& i);
157 Archive&
158 operator >>(Archive& e, float& d);
162 Archive&
163 operator >>(Archive& e, double& d);
164
165 /*
166 * Implementation
167 *
168 */
169
171 Archive::Archive(void) : _size(0), _n(0), _a(NULL), _pos(0) {}
172
173 forceinline void
174 Archive::put(unsigned int i) {
175 if (_n==_size)
176 resize(_n+1);
177 _a[_n++] = i;
178 }
179
180 forceinline int
181 Archive::size(void) const { return _n; }
182
183 forceinline unsigned int
184 Archive::operator [](int i) const {
185 assert(i < _n);
186 return _a[i];
187 }
188
189 forceinline unsigned int
191 assert(_pos < _n);
192 return _a[_pos++];
193 }
194
196 operator <<(Archive& e, unsigned int i) {
197 e.put(i);
198 return e;
199 }
201 operator <<(Archive& e, int i) {
202 e.put(static_cast<unsigned int>(i));
203 return e;
204 }
206 operator <<(Archive& e, unsigned short i) {
207 e.put(i);
208 return e;
209 }
211 operator <<(Archive& e, short i) {
212 e.put(static_cast<unsigned int>(i));
213 return e;
214 }
216 operator <<(Archive& e, unsigned char i) {
217 e.put(i);
218 return e;
219 }
221 operator <<(Archive& e, char i) {
222 e.put(static_cast<unsigned int>(i));
223 return e;
224 }
226 operator <<(Archive& e, bool i) {
227 e.put(static_cast<unsigned int>(i));
228 return e;
229 }
231 operator <<(Archive& e, float d) {
232 for (size_t i=0; i<sizeof(float); i++)
233 e.put(static_cast<unsigned int>(reinterpret_cast<char*>(&d)[i]));
234 return e;
235 }
237 operator <<(Archive& e, double d) {
238 for (size_t i=0; i<sizeof(double); i++)
239 e.put(static_cast<unsigned int>(reinterpret_cast<char*>(&d)[i]));
240 return e;
241 }
242
244 operator >>(Archive& e, unsigned int& i) {
245 i = e.get();
246 return e;
247 }
249 operator >>(Archive& e, int& i) {
250 i = static_cast<int>(e.get());
251 return e;
252 }
254 operator >>(Archive& e, unsigned short& i) {
255 i = static_cast<unsigned short>(e.get());
256 return e;
257 }
259 operator >>(Archive& e, short& i) {
260 i = static_cast<short>(e.get());
261 return e;
262 }
264 operator >>(Archive& e, unsigned char& i) {
265 i = static_cast<unsigned char>(e.get());
266 return e;
267 }
269 operator >>(Archive& e, char& i) {
270 i = static_cast<char>(e.get());
271 return e;
272 }
274 operator >>(Archive& e, bool& i) {
275 i = (e.get() != 0);
276 return e;
277 }
279 operator >>(Archive& e, float& d) {
280 char* cd = reinterpret_cast<char*>(&d);
281 for (size_t i=0; i<sizeof(float); i++)
282 cd[i] = static_cast<char>(e.get());
283 return e;
284 }
286 operator >>(Archive& e, double& d) {
287 char* cd = reinterpret_cast<char*>(&d);
288 for (size_t i=0; i<sizeof(double); i++)
289 cd[i] = static_cast<char>(e.get());
290 return e;
291 }
292
293}
294
295// STATISTICS: kernel-branch
int n
Number of negative literals for node type.
Archive representation
Definition archive.hpp:42
void put(unsigned int i)
Add i to the contents.
Definition archive.hpp:174
int size(void) const
Return size.
Definition archive.hpp:181
Archive(void)
Construct empty representation.
Definition archive.hpp:171
~Archive(void)
Destructor.
Definition archive.cpp:60
unsigned int operator[](int i) const
Return array element i.
Definition archive.hpp:184
Archive & operator=(const Archive &e)
Assignment operator.
Definition archive.cpp:51
unsigned int get(void)
Return next element to read.
Definition archive.hpp:190
#define GECODE_KERNEL_EXPORT
Definition kernel.hh:70
Gecode toplevel namespace
Archive & operator<<(Archive &e, FloatNumBranch nl)
Definition val-sel.hpp:39
Archive & operator>>(Archive &e, FloatNumBranch &nl)
Definition val-sel.hpp:44
#define forceinline
Definition config.hpp:187