Alps  2.0.2
AlpsEncoded.h
Go to the documentation of this file.
1 /*===========================================================================*
2  * This file is part of the Abstract Library for Parallel Search (ALPS). *
3  * *
4  * ALPS is distributed under the Eclipse Public License as part of the *
5  * COIN-OR repository (http://www.coin-or.org). *
6  * *
7  * Authors: *
8  * *
9  * Yan Xu, Lehigh University *
10  * Aykut Bulut, Lehigh University *
11  * Ted Ralphs, Lehigh University *
12  * *
13  * Conceptual Design: *
14  * *
15  * Yan Xu, Lehigh University *
16  * Ted Ralphs, Lehigh University *
17  * Laszlo Ladanyi, IBM T.J. Watson Research Center *
18  * Matthew Saltzman, Clemson University *
19  * *
20  * *
21  * Copyright (C) 2001-2023, Lehigh University, Yan Xu, Aykut Bulut, and *
22  * Ted Ralphs. *
23  * All Rights Reserved. *
24  *===========================================================================*/
25 
26 
27 #ifndef AlpsEncoded_h
28 #define AlpsEncoded_h
29 
30 #include <cstring>
31 #include <string>
32 #include <memory>
33 #include <vector>
34 
35 #include "CoinError.hpp"
36 
37 #include "Alps.h"
38 
39 // AlpsEncoded is modified from BCP_buffer and CoinEncoded
40 
64 class AlpsEncoded {
65 
66  private:
68  AlpsEncoded(const AlpsEncoded&);
72 
73  private:
74 
76  size_t pos_;
77 
79  size_t maxSize_;
80 
85  //std::string type_;
86 
87  int type_;
88 
90  int size_;
91 
93  // const char* representation_; //why const ??? XY
95 
96  public:
97 
100 
104  :
105  pos_(0),
106  maxSize_(0x4000/*16K*/),
107  type_(0),
108  size_(0),
109  representation_(new char[maxSize_])
110  {}
111 
113  AlpsEncoded(int t)
114  :
115  pos_(0),
116  maxSize_(0),
117  type_(t),
118  size_(0),
119  representation_(0)
120  {}
121 
123  AlpsEncoded(int t, int s, char*& r)
124  :
125  pos_(0),
126  maxSize_(s + 4),
127  type_(t),
128  size_(s),
129  representation_(r)
130  { r = NULL; }
131 
134  if (representation_) {
135  delete [] representation_;
136  representation_ = 0;
137  }
138  }
142  int type() const { return type_; }
144  int size() const { return size_; }
145  const char* representation() const { return representation_; }
147 
148  inline void setPosition(const int pos) {
149  if (pos < 0 || pos >= size()) {
150  // const char msg [100] = "Incorrest position setting.";
151  //throw AlpsException(__FILE__, __LINE__, msg);
152  throw CoinError("Incorrest position setting.", "setPosition",
153  "AlpsEncoded");
154  }
155  pos_ = pos;
156  }
157 
158  inline void setRepresentation(char*& buf) {
159  //> 0x4000/*16K*/ ? (strlen(buf)+1) : 0x4000;
160  maxSize_ = strlen(buf) + 1;
161  representation_ = buf;
162  buf = 0;
163  }
164 
168  inline void make_fit(const int addSize){
169  assert(addSize > 0);
170  size_t addSize1 = static_cast<size_t>(addSize);
171 
172  if (maxSize_ < size_ + addSize1){
173  maxSize_ = 4 * (size_ + addSize1 + 0x1000/*4K*/);
174  char* newRep = new char[maxSize_];
175  if (size_)
176  memcpy(newRep, representation_, size_);
177  delete[] representation_;
178  representation_ = newRep;
179  }
180  }
181 
184  inline void clear(){
185  size_ = 0;
186  pos_ = 0;
187  type_ = 0;
188  if (representation_ != 0) {
189  delete representation_;
190  representation_ = 0;
191  }
192  }
193 
194  //------------------------------------------------------
195  // Following functiosn are used in parallel code only.
196  //------------------------------------------------------
197 
201  template <class T> AlpsEncoded& writeRep(const T& value) {
202  make_fit( sizeof(T) );
203  memcpy(representation_ + size_, &value, sizeof(T));
204  size_ += static_cast<int>(sizeof(T));
205  return *this;
206  }
207 
211  template <class T> AlpsEncoded& readRep(T& value){
212 #ifdef PARANOID
213  if (pos_ + sizeof(T) > size_) {
214  throw CoinError("Reading over the end of buffer.",
215  "readRep(const T& value)", "AlpsEncoded");
216  }
217 #endif
218  memcpy(&value, representation_ + pos_, sizeof(T));
219  pos_ += sizeof(T);
220  return *this;
221  }
222 
223 
227  template <class T> AlpsEncoded& writeRep(const T* const values,
228  const int length){
229  make_fit( static_cast<int>(sizeof(int)) +
230  static_cast<int>(sizeof(T)) * length );
231  memcpy(representation_ + size_, &length, sizeof(int));
232  size_ += static_cast<int>(sizeof(int));
233  if (length > 0){
234  memcpy(representation_ + size_, values,
235  static_cast<int>(sizeof(T)) * length);
236  size_ += static_cast<int>(sizeof(T)) * length;
237  }
238  return *this;
239  }
240 
254  template <class T> AlpsEncoded& readRep(T*& values,
255  int& length,
256  bool needAllocateMemory = true)
257  {
258 
259  if (needAllocateMemory) {
260  // Need allocate memeory for arrary "values".
261 
262 #ifdef PARANOID
263  if (pos_ + sizeof(int) > size_) {
264  throw CoinError("Reading over the end of buffer.",
265  "readRep(T*& values, int& length,...",
266  "AlpsEncoded");
267  }
268 #endif
269  memcpy(&length, representation_ + pos_, sizeof(int));
270  pos_ += sizeof(int);
271  if (length > 0){
272 #ifdef PARANOID
273  if (pos_ + sizeof(T)*length > size_) {
274  throw CoinError("Reading over the end of buffer.",
275  "readRep(T*& values, int& length,...",
276  "AlpsEncoded");
277  }
278 #endif
279  values = new T[length];
280  memcpy(values, representation_ + pos_, sizeof(T)*length);
281  pos_ += sizeof(T) * length;
282  }
283 
284  }
285  else { /* values has been allocated memory. */
286 
287  int l;
288 #ifdef PARANOID
289  if (pos_ + sizeof(int) > size_) {
290  throw CoinError("Reading over the end of buffer.",
291  "readRep(T*& values, int& length,...",
292  "AlpsEncoded");
293  }
294 #endif
295  memcpy(&l, representation_ + pos_, sizeof(int));
296  pos_ += sizeof(int);
297  if (l != length) {
298  throw CoinError("Reading over the end of buffer.",
299  "readRep(T*& values, int& length,...",
300  "AlpsEncoded");
301  }
302  if (length > 0){
303 #ifdef PARANOID
304  if (pos_ + sizeof(T)*length > size_) {
305  throw CoinError("Reading over the end of buffer.",
306  "readRep(T*& values, int& length,...",
307  "AlpsEncoded");
308  }
309 #endif
310  memcpy(values, representation_ + pos_, sizeof(T)*length);
311  pos_ += sizeof(T) * length;
312  }
313  }
314 
315  return *this;
316  }
317 
319  AlpsEncoded& writeRep(std::string& value){
320  // must define here, 'cos in *_message.C we have only templated members
321  const int len = static_cast<const int> (value.length());
322  make_fit( static_cast<int>(sizeof(int)) + len );
323  memcpy(representation_ + size_, &len, static_cast<int>(sizeof(int)));
324  size_ += static_cast<int>(sizeof(int));
325  if (len > 0){
326  memcpy(representation_ + size_, value.c_str(), len);
327  size_ += len;
328  }
329  return *this;
330  }
331 
333  AlpsEncoded& readRep(std::string& value){
334  int len;
335  readRep(len);
336  value.assign(representation_ + pos_, len);
337  pos_ += len;
338  return *this;
339  }
340 
342  template <class T> AlpsEncoded& writeRep(const std::vector<T>& vec) {
343  int objnum = vec.size();
344  int new_bytes = objnum * sizeof(T);
345  make_fit( sizeof(int) + new_bytes );
346  memcpy(representation_ + size_, &objnum, sizeof(int));
347  size_ += sizeof(int);
348  if (objnum > 0){
349  memcpy(representation_ + size_, &vec[0], new_bytes);
350  size_ += new_bytes;
351  }
352  return *this;
353  }
354 
356  template <class T> AlpsEncoded& readRep(std::vector<T>& vec) {
357  int objnum;
358 #ifdef PARANOID
359  if (pos_ + sizeof(int) > size_)
360  throw CoinError("Reading over the end of buffer.",
361  "AlpsEncoded", "readRep(std::vector<T>& vec");
362 #endif
363  memcpy(&objnum, representation_ + pos_, sizeof(int));
364  pos_ += sizeof(int);
365  vec.clear();
366  if (objnum > 0){
367 #ifdef PARANOID
368  if (pos_ + sizeof(T)*objnum > size_)
369  throw CoinError("Reading over the end of buffer.",
370  "AlpsEncoded", "readRep(std::vector<T>& vec");
371 #endif
372  vec.insert(vec.end(), objnum, T());
373  memcpy(&vec[0], representation_ + pos_, objnum * sizeof(T));
374  pos_ += objnum * sizeof(T);
375  }
376  return *this;
377  }
380 };
381 
382 #endif
AlpsEncoded::~AlpsEncoded
~AlpsEncoded()
Destructor.
Definition: AlpsEncoded.h:133
AlpsEncoded::make_fit
void make_fit(const int addSize)
Reallocate the size of encoded if necessary so that at least addsize_ number of additional bytes will...
Definition: AlpsEncoded.h:168
AlpsEncoded::size
int size() const
Definition: AlpsEncoded.h:144
AlpsEncoded::writeRep
AlpsEncoded & writeRep(const std::vector< T > &vec)
Write a std::vector into repsentation_ .
Definition: AlpsEncoded.h:342
AlpsEncoded::representation_
char * representation_
The encoded/compressed representation of the object.
Definition: AlpsEncoded.h:94
AlpsEncoded::readRep
AlpsEncoded & readRep(T *&values, int &length, bool needAllocateMemory=true)
Read an array of objects of type T from repsentation_, where T must be a built-in type (ar at least ...
Definition: AlpsEncoded.h:254
AlpsEncoded
Definition: AlpsEncoded.h:64
AlpsEncoded::type_
int type_
Represent the type of the object.
Definition: AlpsEncoded.h:87
AlpsEncoded::readRep
AlpsEncoded & readRep(std::string &value)
Read a std::string from repsentation_ .
Definition: AlpsEncoded.h:333
AlpsEncoded::writeRep
AlpsEncoded & writeRep(const T &value)
Write a single object of type T in repsentation_ .
Definition: AlpsEncoded.h:201
AlpsEncoded::readRep
AlpsEncoded & readRep(T &value)
Read a single object of type T from repsentation_ .
Definition: AlpsEncoded.h:211
Alps.h
AlpsEncoded::setPosition
void setPosition(const int pos)
Definition: AlpsEncoded.h:148
AlpsEncoded::size_
int size_
The size of the packed representation.
Definition: AlpsEncoded.h:90
AlpsEncoded::writeRep
AlpsEncoded & writeRep(const T *const values, const int length)
Write a C style array of objects of type T in repsentation_.
Definition: AlpsEncoded.h:227
AlpsEncoded::maxSize_
size_t maxSize_
The amount of memory allocated for the representation.
Definition: AlpsEncoded.h:79
AlpsEncoded::AlpsEncoded
AlpsEncoded(int t, int s, char *&r)
Useful constructor.
Definition: AlpsEncoded.h:123
AlpsEncoded::pos_
size_t pos_
The next read/write position in the representation.
Definition: AlpsEncoded.h:76
AlpsEncoded::operator=
AlpsEncoded & operator=(const AlpsEncoded &)
AlpsEncoded::type
int type() const
Definition: AlpsEncoded.h:143
AlpsEncoded::readRep
AlpsEncoded & readRep(std::vector< T > &vec)
Read a std::vector from repsentation_ .
Definition: AlpsEncoded.h:356
AlpsEncoded::AlpsEncoded
AlpsEncoded(int t)
Useful constructor.
Definition: AlpsEncoded.h:113
AlpsEncoded::setRepresentation
void setRepresentation(char *&buf)
Definition: AlpsEncoded.h:158
AlpsEncoded::writeRep
AlpsEncoded & writeRep(std::string &value)
Read a std::string in repsentation_ .
Definition: AlpsEncoded.h:319
AlpsEncoded::representation
const char * representation() const
Definition: AlpsEncoded.h:145
AlpsEncoded::clear
void clear()
Completely clear the encoded.
Definition: AlpsEncoded.h:184
AlpsEncoded::AlpsEncoded
AlpsEncoded()
The default constructor creates a buffer of size 16 Kbytes with no message in it.
Definition: AlpsEncoded.h:103