Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
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  * Last modified:
10  * $Date$ by $Author$
11  * $Revision$
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode {
39 
46  class Archive {
47  private:
49  int _size;
51  int _n;
53  unsigned int* _a;
55  int _pos;
57  GECODE_KERNEL_EXPORT void resize(int n);
58  public:
60  Archive(void);
66  GECODE_KERNEL_EXPORT Archive& operator =(const Archive& e);
68  void put(unsigned int i);
70  int size(void) const;
72  unsigned int operator [](int i) const;
74  unsigned int get(void);
75  };
76 
80  Archive&
81  operator <<(Archive& e, unsigned int i);
85  Archive&
86  operator <<(Archive& e, int i);
90  Archive&
91  operator <<(Archive& e, unsigned short i);
95  Archive&
96  operator <<(Archive& e, short i);
100  Archive&
101  operator <<(Archive& e, unsigned char i);
105  Archive&
106  operator <<(Archive& e, char i);
110  Archive&
111  operator <<(Archive& e, bool i);
115  Archive&
116  operator <<(Archive& e, float d);
120  Archive&
121  operator <<(Archive& e, double d);
122 
126  Archive&
127  operator >>(Archive& e, unsigned int& i);
131  Archive&
132  operator >>(Archive& e, int& i);
136  Archive&
137  operator >>(Archive& e, unsigned short& i);
141  Archive&
142  operator >>(Archive& e, short& i);
146  Archive&
147  operator >>(Archive& e, unsigned char& i);
151  Archive&
152  operator >>(Archive& e, char& i);
156  Archive&
157  operator >>(Archive& e, bool& i);
161  Archive&
162  operator >>(Archive& e, float& d);
166  Archive&
167  operator >>(Archive& e, double& d);
168 
169  /*
170  * Implementation
171  *
172  */
173 
175  Archive::Archive(void) : _size(0), _n(0), _a(NULL), _pos(0) {}
176 
177  forceinline void
178  Archive::put(unsigned int i) {
179  if (_n==_size)
180  resize(_n+1);
181  _a[_n++] = i;
182  }
183 
184  forceinline int
185  Archive::size(void) const { return _n; }
186 
187  forceinline unsigned int
188  Archive::operator [](int i) const {
189  assert(i < _n);
190  return _a[i];
191  }
192 
193  forceinline unsigned int
194  Archive::get(void) {
195  assert(_pos < _n);
196  return _a[_pos++];
197  }
198 
200  operator <<(Archive& e, unsigned int i) {
201  e.put(i);
202  return e;
203  }
205  operator <<(Archive& e, int i) {
206  e.put(static_cast<unsigned int>(i));
207  return e;
208  }
210  operator <<(Archive& e, unsigned short i) {
211  e.put(i);
212  return e;
213  }
215  operator <<(Archive& e, short i) {
216  e.put(static_cast<unsigned int>(i));
217  return e;
218  }
220  operator <<(Archive& e, unsigned char i) {
221  e.put(i);
222  return e;
223  }
225  operator <<(Archive& e, char i) {
226  e.put(static_cast<unsigned int>(i));
227  return e;
228  }
230  operator <<(Archive& e, bool i) {
231  e.put(static_cast<unsigned int>(i));
232  return e;
233  }
235  operator <<(Archive& e, float d) {
236  for (size_t i=0; i<sizeof(float); i++)
237  e.put(static_cast<unsigned int>(reinterpret_cast<char*>(&d)[i]));
238  return e;
239  }
241  operator <<(Archive& e, double d) {
242  for (size_t i=0; i<sizeof(double); i++)
243  e.put(static_cast<unsigned int>(reinterpret_cast<char*>(&d)[i]));
244  return e;
245  }
246 
248  operator >>(Archive& e, unsigned int& i) {
249  i = e.get();
250  return e;
251  }
253  operator >>(Archive& e, int& i) {
254  i = static_cast<int>(e.get());
255  return e;
256  }
258  operator >>(Archive& e, unsigned short& i) {
259  i = static_cast<unsigned short>(e.get());
260  return e;
261  }
263  operator >>(Archive& e, short& i) {
264  i = static_cast<short>(e.get());
265  return e;
266  }
268  operator >>(Archive& e, unsigned char& i) {
269  i = static_cast<unsigned char>(e.get());
270  return e;
271  }
273  operator >>(Archive& e, char& i) {
274  i = static_cast<char>(e.get());
275  return e;
276  }
278  operator >>(Archive& e, bool& i) {
279  i = (e.get() != 0);
280  return e;
281  }
283  operator >>(Archive& e, float& d) {
284  char* cd = reinterpret_cast<char*>(&d);
285  for (size_t i=0; i<sizeof(float); i++)
286  cd[i] = static_cast<char>(e.get());
287  return e;
288  }
290  operator >>(Archive& e, double& d) {
291  char* cd = reinterpret_cast<char*>(&d);
292  for (size_t i=0; i<sizeof(double); i++)
293  cd[i] = static_cast<char>(e.get());
294  return e;
295  }
296 
297 }
298 
299 // STATISTICS: kernel-branch
Archive & operator=(const Archive &e)
Assignment operator.
Definition: archive.cpp:55
Archive & operator>>(Archive &e, unsigned int &i)
Definition: archive.hpp:248
void put(unsigned int i)
Add i to the contents.
Definition: archive.hpp:178
~Archive(void)
Destructor.
Definition: archive.cpp:64
unsigned int operator[](int i) const
Return array element i.
Definition: archive.hpp:188
unsigned int get(void)
Return next element to read.
Definition: archive.hpp:194
#define forceinline
Definition: config.hpp:182
Gecode::IntSet d(v, 7)
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
#define GECODE_KERNEL_EXPORT
Definition: kernel.hh:74
int size(void) const
Return size.
Definition: archive.hpp:185
Archive(void)
Construct empty representation.
Definition: archive.hpp:175
Archive representation
Definition: archive.hpp:46
Gecode toplevel namespace
Archive & operator<<(Archive &e, unsigned int i)
Definition: archive.hpp:200