ESA JPIP server  0.1
vint_vector.h
Go to the documentation of this file.
1 #ifndef _DATA_VINT_VECTOR_H_
2 #define _DATA_VINT_VECTOR_H_
3 
4 
5 #include <vector>
6 #include <stdint.h>
7 #include <assert.h>
8 #include <algorithm>
9 
10 
11 namespace data
12 {
13  using namespace std;
14 
15 
25  {
26  private:
27  uint64_t mask;
28  int8_t num_bytes_;
29  vector<uint8_t> data; //< Internal vector used for the data
30 
31  public:
36  {
37  set_num_bytes(sizeof(uint64_t));
38  }
39 
45  vint_vector(int num_bytes)
46  {
47  set_num_bytes(num_bytes);
48  }
49 
54  {
55  *this = v;
56  }
57 
62  {
63  mask = v.mask;
64  num_bytes_ = v.num_bytes_;
65 
66  data.clear();
67  for(vector<uint8_t>::const_iterator i = v.data.begin(); i != v.data.end(); i++)
68  data.push_back(*i);
69 
70  return *this;
71  }
72 
78  void set_num_bytes(int num_bytes)
79  {
80  assert((num_bytes >= 1) && (num_bytes <= (int)sizeof(uint64_t)));
81 
82  data.clear();
83 
84  num_bytes_ = (int8_t)num_bytes;
85 
86  mask = 0xFF;
87  while(--num_bytes > 0) mask = ((mask << 8) | 0xFF);
88  }
89 
93  int num_bytes() const
94  {
95  return (int)num_bytes_;
96  }
97 
101  int data_bytes() const
102  {
103  return (int)data.size();
104  }
105 
111  uint64_t operator[](int index) const
112  {
113  assert(((index * num_bytes_) + sizeof(uint64_t)) <= data.size());
114  return (*((uint64_t *)&(data[index * (int)num_bytes_])) & mask);
115  }
116 
121  void push_back(uint64_t value)
122  {
123  data.insert(data.end(), data.size() <= 0 ? sizeof(uint64_t) : (int)num_bytes_, 0);
124  *((uint64_t *)&(data[data.size() - sizeof(uint64_t)])) = value;
125  }
126 
130  void clear()
131  {
132  data.clear();
133  }
134 
138  int size() const
139  {
140  return max(0, (((int)data.size() - (int)sizeof(uint64_t)) / (int)num_bytes_) + 1);
141  }
142 
146  uint64_t& back()
147  {
148  assert(data.size() >= sizeof(uint64_t));
149  return *((uint64_t *)&(data[data.size() - sizeof(uint64_t)]));
150  }
151 
152  virtual ~vint_vector()
153  {
154  }
155  };
156 
157 }
158 
159 
160 #endif /* _DATA_VINT_VECTOR_H_ */
void clear()
Clears the content.
Definition: vint_vector.h:130
const vint_vector & operator=(const vint_vector &v)
Copy assignment.
Definition: vint_vector.h:61
This class has been implemented with the same philosophy that the class STL vector, but specifically designed to store integers with a length in bytes that can be not multiple from 2 (e.g.
Definition: vint_vector.h:24
Contains a set of classes to easy the handling of data and files, as well as the serialization.
Definition: data.h:9
vint_vector(const vint_vector &v)
Copy constructor.
Definition: vint_vector.h:53
STL namespace.
int8_t num_bytes_
Number of bytes used for the integers.
Definition: vint_vector.h:28
vector< uint8_t > data
Definition: vint_vector.h:29
int size() const
Returns the size of the vector, in number of items.
Definition: vint_vector.h:138
vint_vector()
Initializes the vector to store 64-bit integers.
Definition: vint_vector.h:35
uint64_t mask
Mask used for accessing the data.
Definition: vint_vector.h:27
void set_num_bytes(int num_bytes)
Changes the number of bytes of the integer values.
Definition: vint_vector.h:78
virtual ~vint_vector()
Definition: vint_vector.h:152
void push_back(uint64_t value)
Adds a new item to the end of the vector.
Definition: vint_vector.h:121
vint_vector(int num_bytes)
Initializes the vector to store integers with the number of bytes given as parameter.
Definition: vint_vector.h:45
uint64_t operator[](int index) const
Operator overloading for indexing the integer values.
Definition: vint_vector.h:111
int data_bytes() const
Returns the current number of bytes stored.
Definition: vint_vector.h:101
uint64_t & back()
Return the reference of the last item of the vector.
Definition: vint_vector.h:146
int num_bytes() const
Returns the number of bytes used.
Definition: vint_vector.h:93