Generated on Sat Feb 7 2015 02:01:27 for Gecode by doxygen 1.8.9.1
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: 2011-10-11 19:28:17 +0200 (Tue, 11 Oct 2011) $ by $Author: tack $
11  * $Revision: 12440 $
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 {
45  class Archive {
46  private:
48  int _size;
50  int _n;
52  unsigned int* _a;
54  int _pos;
56  GECODE_KERNEL_EXPORT void resize(int n);
57  public:
59  Archive(void);
65  GECODE_KERNEL_EXPORT Archive& operator =(const Archive& e);
67  void put(unsigned int i);
69  int size(void) const;
71  unsigned int operator [](int i) const;
73  unsigned int get(void);
74  };
75 
79  Archive&
80  operator <<(Archive& e, unsigned int i);
84  Archive&
85  operator <<(Archive& e, int i);
89  Archive&
90  operator <<(Archive& e, unsigned short i);
94  Archive&
95  operator <<(Archive& e, short i);
99  Archive&
100  operator <<(Archive& e, unsigned char i);
104  Archive&
105  operator <<(Archive& e, char i);
109  Archive&
110  operator <<(Archive& e, bool i);
114  Archive&
115  operator <<(Archive& e, float d);
119  Archive&
120  operator <<(Archive& e, double d);
121 
125  Archive&
126  operator >>(Archive& e, unsigned int& i);
130  Archive&
131  operator >>(Archive& e, int& i);
135  Archive&
136  operator >>(Archive& e, unsigned short& i);
140  Archive&
141  operator >>(Archive& e, short& i);
145  Archive&
146  operator >>(Archive& e, unsigned char& i);
150  Archive&
151  operator >>(Archive& e, char& i);
155  Archive&
156  operator >>(Archive& e, bool& i);
160  Archive&
161  operator >>(Archive& e, float& d);
165  Archive&
166  operator >>(Archive& e, double& d);
167 
168  /*
169  * Implementation
170  *
171  */
172 
174  Archive::Archive(void) : _size(0), _n(0), _a(NULL), _pos(0) {}
175 
176  forceinline void
177  Archive::put(unsigned int i) {
178  if (_n==_size)
179  resize(_n+1);
180  _a[_n++] = i;
181  }
182 
183  forceinline int
184  Archive::size(void) const { return _n; }
185 
186  forceinline unsigned int
187  Archive::operator [](int i) const {
188  assert(i < _n);
189  return _a[i];
190  }
191 
192  forceinline unsigned int
193  Archive::get(void) {
194  assert(_pos < _n);
195  return _a[_pos++];
196  }
197 
199  operator <<(Archive& e, unsigned int i) {
200  e.put(i);
201  return e;
202  }
203  forceinline Archive&
204  operator <<(Archive& e, int i) {
205  e.put(static_cast<unsigned int>(i));
206  return e;
207  }
208  forceinline Archive&
209  operator <<(Archive& e, unsigned short i) {
210  e.put(i);
211  return e;
212  }
213  forceinline Archive&
214  operator <<(Archive& e, short i) {
215  e.put(static_cast<unsigned int>(i));
216  return e;
217  }
218  forceinline Archive&
219  operator <<(Archive& e, unsigned char i) {
220  e.put(i);
221  return e;
222  }
223  forceinline Archive&
224  operator <<(Archive& e, char i) {
225  e.put(static_cast<unsigned int>(i));
226  return e;
227  }
228  forceinline Archive&
229  operator <<(Archive& e, bool i) {
230  e.put(static_cast<unsigned int>(i));
231  return e;
232  }
233  forceinline Archive&
234  operator <<(Archive& e, float d) {
235  for (size_t i=0; i<sizeof(float); i++)
236  e.put(static_cast<unsigned int>(reinterpret_cast<char*>(&d)[i]));
237  return e;
238  }
239  forceinline Archive&
240  operator <<(Archive& e, double d) {
241  for (size_t i=0; i<sizeof(double); i++)
242  e.put(static_cast<unsigned int>(reinterpret_cast<char*>(&d)[i]));
243  return e;
244  }
245 
246  forceinline Archive&
247  operator >>(Archive& e, unsigned int& i) {
248  i = e.get();
249  return e;
250  }
251  forceinline Archive&
252  operator >>(Archive& e, int& i) {
253  i = static_cast<int>(e.get());
254  return e;
255  }
256  forceinline Archive&
257  operator >>(Archive& e, unsigned short& i) {
258  i = static_cast<unsigned short>(e.get());
259  return e;
260  }
261  forceinline Archive&
262  operator >>(Archive& e, short& i) {
263  i = static_cast<short>(e.get());
264  return e;
265  }
266  forceinline Archive&
267  operator >>(Archive& e, unsigned char& i) {
268  i = static_cast<unsigned char>(e.get());
269  return e;
270  }
271  forceinline Archive&
272  operator >>(Archive& e, char& i) {
273  i = static_cast<char>(e.get());
274  return e;
275  }
276  forceinline Archive&
277  operator >>(Archive& e, bool& i) {
278  i = static_cast<bool>(e.get());
279  return e;
280  }
281  forceinline Archive&
282  operator >>(Archive& e, float& d) {
283  char* cd = reinterpret_cast<char*>(&d);
284  for (size_t i=0; i<sizeof(float); i++)
285  cd[i] = static_cast<char>(e.get());
286  return e;
287  }
288  forceinline Archive&
289  operator >>(Archive& e, double& d) {
290  char* cd = reinterpret_cast<char*>(&d);
291  for (size_t i=0; i<sizeof(double); i++)
292  cd[i] = static_cast<char>(e.get());
293  return e;
294  }
295 
296 }
297 
298 // STATISTICS: kernel-branch
Archive & operator=(const Archive &e)
Assignment operator.
Definition: archive.cpp:55
void put(unsigned int i)
Add i to the contents.
Definition: archive.hpp:177
~Archive(void)
Destructor.
Definition: archive.cpp:64
unsigned int get(void)
Return next element to read.
Definition: archive.hpp:193
Gecode::IntSet d(v, 7)
Single _a(2, 3)
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
unsigned int operator[](int i) const
Return array element i.
Definition: archive.hpp:187
#define GECODE_KERNEL_EXPORT
Definition: kernel.hh:70
int size(void) const
Return size.
Definition: archive.hpp:184
Archive & operator>>(Archive &e, FloatNumBranch &nl)
Definition: val-sel.hpp:48
Archive(void)
Construct empty representation.
Definition: archive.hpp:174
Archive representation
Definition: archive.hpp:45
#define forceinline
Definition: config.hpp:132
Archive & operator<<(Archive &e, FloatNumBranch nl)
Definition: val-sel.hpp:43
Gecode toplevel namespace