this repo has no description
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 36 /** 37 * \brief %Archive representation 38 * 39 * An Archive is an array of unsigned integers, used as an external 40 * representation of internal data structures (such as Choice objects). 41 */ 42 class Archive { 43 private: 44 /// Size of array 45 int _size; 46 /// Used size of array 47 int _n; 48 /// Array elements 49 unsigned int* _a; 50 /// Current position of read iterator 51 int _pos; 52 /// Resize to at least \a n + 1 elements 53 GECODE_KERNEL_EXPORT void resize(int n); 54 public: 55 /// Construct empty representation 56 Archive(void); 57 /// Destructor 58 GECODE_KERNEL_EXPORT ~Archive(void); 59 /// Copy constructor 60 GECODE_KERNEL_EXPORT Archive(const Archive& e); 61 /// Assignment operator 62 GECODE_KERNEL_EXPORT Archive& operator =(const Archive& e); 63 /// Add \a i to the contents 64 void put(unsigned int i); 65 /// Return size 66 int size(void) const; 67 /// Return array element \a i 68 unsigned int operator [](int i) const; 69 /// Return next element to read 70 unsigned int get(void); 71 }; 72 73 /** Add \a i to the end of \a e 74 * \relates Archive 75 */ 76 Archive& 77 operator <<(Archive& e, unsigned int i); 78 /** Add \a i to the end of \a e 79 * \relates Archive 80 */ 81 Archive& 82 operator <<(Archive& e, int i); 83 /** Add \a i to the end of \a e 84 * \relates Archive 85 */ 86 Archive& 87 operator <<(Archive& e, unsigned short i); 88 /** Add \a i to the end of \a e 89 * \relates Archive 90 */ 91 Archive& 92 operator <<(Archive& e, short i); 93 /** Add \a i to the end of \a e 94 * \relates Archive 95 */ 96 Archive& 97 operator <<(Archive& e, unsigned char i); 98 /** Add \a i to the end of \a e 99 * \relates Archive 100 */ 101 Archive& 102 operator <<(Archive& e, char i); 103 /** Add \a i to the end of \a e 104 * \relates Archive 105 */ 106 Archive& 107 operator <<(Archive& e, bool i); 108 /** Add \a d to the end of \a e 109 * \relates Archive 110 */ 111 Archive& 112 operator <<(Archive& e, float d); 113 /** Add \a d to the end of \a e 114 * \relates Archive 115 */ 116 Archive& 117 operator <<(Archive& e, double d); 118 119 /** Read next element from \a e into \a i 120 * \relates Archive 121 */ 122 Archive& 123 operator >>(Archive& e, unsigned int& i); 124 /** Read next element from \a e into \a i 125 * \relates Archive 126 */ 127 Archive& 128 operator >>(Archive& e, int& i); 129 /** Read next element from \a e into \a i 130 * \relates Archive 131 */ 132 Archive& 133 operator >>(Archive& e, unsigned short& i); 134 /** Read next element from \a e into \a i 135 * \relates Archive 136 */ 137 Archive& 138 operator >>(Archive& e, short& i); 139 /** Read next element from \a e into \a i 140 * \relates Archive 141 */ 142 Archive& 143 operator >>(Archive& e, unsigned char& i); 144 /** Read next element from \a e into \a i 145 * \relates Archive 146 */ 147 Archive& 148 operator >>(Archive& e, char& i); 149 /** Read next element from \a e into \a i 150 * \relates Archive 151 */ 152 Archive& 153 operator >>(Archive& e, bool& i); 154 /** Read next element from \a e into \a d 155 * \relates Archive 156 */ 157 Archive& 158 operator >>(Archive& e, float& d); 159 /** Read next element from \a e into \a d 160 * \relates Archive 161 */ 162 Archive& 163 operator >>(Archive& e, double& d); 164 165 /* 166 * Implementation 167 * 168 */ 169 170 forceinline 171 Archive::Archive(void) : _size(0), _n(0), _a(nullptr), _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 190 Archive::get(void) { 191 assert(_pos < _n); 192 return _a[_pos++]; 193 } 194 195 forceinline Archive& 196 operator <<(Archive& e, unsigned int i) { 197 e.put(i); 198 return e; 199 } 200 forceinline Archive& 201 operator <<(Archive& e, int i) { 202 e.put(static_cast<unsigned int>(i)); 203 return e; 204 } 205 forceinline Archive& 206 operator <<(Archive& e, unsigned short i) { 207 e.put(i); 208 return e; 209 } 210 forceinline Archive& 211 operator <<(Archive& e, short i) { 212 e.put(static_cast<unsigned int>(i)); 213 return e; 214 } 215 forceinline Archive& 216 operator <<(Archive& e, unsigned char i) { 217 e.put(i); 218 return e; 219 } 220 forceinline Archive& 221 operator <<(Archive& e, char i) { 222 e.put(static_cast<unsigned int>(i)); 223 return e; 224 } 225 forceinline Archive& 226 operator <<(Archive& e, bool i) { 227 e.put(static_cast<unsigned int>(i)); 228 return e; 229 } 230 forceinline Archive& 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 } 236 forceinline Archive& 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 243 forceinline Archive& 244 operator >>(Archive& e, unsigned int& i) { 245 i = e.get(); 246 return e; 247 } 248 forceinline Archive& 249 operator >>(Archive& e, int& i) { 250 i = static_cast<int>(e.get()); 251 return e; 252 } 253 forceinline Archive& 254 operator >>(Archive& e, unsigned short& i) { 255 i = static_cast<unsigned short>(e.get()); 256 return e; 257 } 258 forceinline Archive& 259 operator >>(Archive& e, short& i) { 260 i = static_cast<short>(e.get()); 261 return e; 262 } 263 forceinline Archive& 264 operator >>(Archive& e, unsigned char& i) { 265 i = static_cast<unsigned char>(e.get()); 266 return e; 267 } 268 forceinline Archive& 269 operator >>(Archive& e, char& i) { 270 i = static_cast<char>(e.get()); 271 return e; 272 } 273 forceinline Archive& 274 operator >>(Archive& e, bool& i) { 275 i = (e.get() != 0); 276 return e; 277 } 278 forceinline Archive& 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 } 285 forceinline Archive& 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