tudocomp
– The TU Dortmund Compression Framework
IntVector.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <cmath>
5 #include <cstddef>
6 #include <cstring>
7 #include <fstream>
8 #include <iomanip>
9 #include <iostream>
10 #include <iterator>
11 #include <memory>
12 #include <sstream>
13 #include <string>
14 #include <type_traits>
15 #include <utility>
16 #include <climits>
17 
18 #include <tudocomp/ds/IntRepr.hpp>
19 #include <tudocomp/ds/IntPtr.hpp>
21 #include <tudocomp/ds/uint_t.hpp>
24 
25 #include <glog/logging.h>
26 
27 namespace tdc {
28 namespace int_vector {
30 
31  template<typename T>
32  struct IntVectorStdVectorRepr {
33  typedef typename std::vector<T>::value_type value_type;
34 
35  typedef typename std::vector<T>::reference reference;
36  typedef typename std::vector<T>::const_reference const_reference;
37 
38  typedef typename std::vector<T>::pointer pointer;
39  typedef typename std::vector<T>::const_pointer const_pointer;
40 
41  typedef typename std::vector<T>::iterator iterator;
42  typedef typename std::vector<T>::const_iterator const_iterator;
43 
44  typedef typename std::vector<T>::reverse_iterator reverse_iterator;
45  typedef typename std::vector<T>::const_reverse_iterator const_reverse_iterator;
46 
47  typedef typename std::vector<T>::difference_type difference_type;
48  typedef typename std::vector<T>::size_type size_type;
49 
50  typedef std::vector<T> backing_data;
51  typedef T internal_data_type;
52 
53  inline static uint64_t bit_size(const backing_data& self) {
54  return sizeof(T) * CHAR_BIT * self.size();
55  }
56 
57  inline static uint64_t bit_capacity(const backing_data& self) {
58  return sizeof(T) * CHAR_BIT * self.capacity();
59  }
60 
61  inline static constexpr ElementStorageMode element_storage_mode() {
63  }
64 
65  inline static uint8_t width(const backing_data& self) {
66  return sizeof(T) * CHAR_BIT;
67  }
68 
69  inline static backing_data with_width(size_type n, const value_type& val, uint8_t width) {
70  return backing_data(n, val, width);
71  }
72 
73  inline static void width(backing_data& self, uint8_t w) {
74  }
75 
76  inline static void resize(backing_data& self, size_type n, const value_type& val, uint8_t w) {
77  self.resize(n, val);
78  }
79 
80  inline static void bit_reserve(backing_data& self, uint64_t n) {
81  // TODO: Should this round up to the size of element, and then reserve normally?
82  }
83  };
84 
85  template<typename T>
86  struct IntVectorBitPackingVectorRepr {
87  typedef typename BitPackingVector<T>::value_type value_type;
88 
89  typedef typename BitPackingVector<T>::reference reference;
90  typedef typename BitPackingVector<T>::const_reference const_reference;
91 
92  typedef typename BitPackingVector<T>::pointer pointer;
93  typedef typename BitPackingVector<T>::const_pointer const_pointer;
94 
95  typedef typename BitPackingVector<T>::iterator iterator;
96  typedef typename BitPackingVector<T>::const_iterator const_iterator;
97 
98  typedef typename BitPackingVector<T>::reverse_iterator reverse_iterator;
99  typedef typename BitPackingVector<T>::const_reverse_iterator const_reverse_iterator;
100 
101  typedef typename BitPackingVector<T>::difference_type difference_type;
102  typedef typename BitPackingVector<T>::size_type size_type;
103 
104  typedef BitPackingVector<T> backing_data;
105  typedef typename BitPackingVector<T>::internal_data_type internal_data_type;
106 
107  inline static uint64_t bit_size(const backing_data& self) {
108  return self.size() * self.width();
109  }
110 
111  inline static uint64_t bit_capacity(const backing_data& self) {
112  return self.capacity() * self.width();
113  }
114 
115  inline static constexpr ElementStorageMode element_storage_mode() {
117  }
118 
119  inline static uint8_t width(const backing_data& self) {
120  return self.width();
121  }
122 
123  inline static backing_data with_width(size_type n, const value_type& val, uint8_t width) {
124  return backing_data(n, val, width);
125  }
126 
127  inline static void width(backing_data& self, uint8_t w) {
128  self.width(w);
129  }
130 
131  inline static void resize(backing_data& self, size_type n, const value_type& val, uint8_t w) {
132  self.resize(n, val, w);
133  }
134 
135  inline static void bit_reserve(backing_data& self, uint64_t n) {
136  self.bit_reserve(n);
137  }
138  };
139 
140  template<class T, class X = void>
141  struct IntVectorTrait:
142  public IntVectorStdVectorRepr<T> {};
143 
144  template<size_t N>
145  struct IntVectorTrait<uint_impl_t<N>, typename std::enable_if_t<(N % 8 != 0) && (N > 1)>>:
146  public IntVectorBitPackingVectorRepr<uint_t<N>> {};
147 
148  template<>
149  struct IntVectorTrait<dynamic_t>:
150  public IntVectorBitPackingVectorRepr<dynamic_t> {};
151 
152  template<>
153  struct IntVectorTrait<bool>:
154  public IntVectorBitPackingVectorRepr<uint_t<1>> {};
155 
157 
173  // TODO ^ Could just not offer the methods for the non-dynamic case
174  template<class T>
175  class IntVector {
176  // TODO: Add custom allocator support
177  public:
178  typedef typename IntVectorTrait<T>::value_type value_type;
179  typedef typename IntVectorTrait<T>::reference reference;
180  typedef typename IntVectorTrait<T>::const_reference const_reference;
181  typedef typename IntVectorTrait<T>::pointer pointer;
182  typedef typename IntVectorTrait<T>::const_pointer const_pointer;
183  typedef typename IntVectorTrait<T>::iterator iterator;
184  typedef typename IntVectorTrait<T>::const_iterator const_iterator;
185  typedef typename IntVectorTrait<T>::reverse_iterator reverse_iterator;
186  typedef typename IntVectorTrait<T>::const_reverse_iterator const_reverse_iterator;
187  typedef typename IntVectorTrait<T>::difference_type difference_type;
188  typedef typename IntVectorTrait<T>::size_type size_type;
189 
191  typedef typename IntVectorTrait<T>::internal_data_type internal_data_type;
192 
194  return IntVectorTrait<T>::element_storage_mode();
195  }
196  private:
197  typename IntVectorTrait<T>::backing_data m_data;
198  public:
199  // default
200  inline explicit IntVector() {}
201 
202  // fill
203  explicit IntVector(size_type n): m_data(n) {}
204  inline IntVector(size_type n, const value_type& val): m_data(n, val) {}
205  inline IntVector(size_type n, const value_type& val, uint8_t width):
206  m_data(IntVectorTrait<T>::with_width(n, val, width)) {}
207 
208  // range
209  template <class InputIterator>
210  inline IntVector(InputIterator first, InputIterator last): m_data(first, last) {}
211 
212  // copy
213  inline IntVector(const IntVector& other): m_data(other.m_data) {}
214 
215  // move
216  inline IntVector(IntVector&& other): m_data(std::move(other.m_data)) {}
217 
218  // initializer list
219  inline IntVector(std::initializer_list<value_type> il): m_data(il) {}
220 
221  inline IntVector& operator=(const IntVector& other) {
222  m_data = other.m_data;
223  return *this;
224  }
225 
226  inline IntVector& operator=(IntVector&& other) {
227  m_data = std::move(other.m_data);
228  return *this;
229  }
230 
231  inline IntVector& operator=(std::initializer_list<value_type> il) {
232  m_data = il;
233  return *this;
234  }
235 
236  inline iterator begin() {
237  return m_data.begin();
238  }
239 
240  inline iterator end() {
241  return m_data.end();
242  }
243 
244  inline reverse_iterator rbegin() {
245  return m_data.rbegin();
246  }
247 
248  inline reverse_iterator rend() {
249  return m_data.rend();
250  }
251 
252  inline const_iterator begin() const {
253  return m_data.begin();
254  }
255 
256  inline const_iterator end() const {
257  return m_data.end();
258  }
259 
260  inline const_reverse_iterator rbegin() const {
261  return m_data.rbegin();
262  }
263 
264  inline const_reverse_iterator rend() const {
265  return m_data.rend();
266  }
267 
268  inline const_iterator cbegin() const {
269  return m_data.cbegin();
270  }
271 
272  inline const_iterator cend() const {
273  return m_data.cend();
274  }
275 
276  inline const_reverse_iterator crbegin() const {
277  return m_data.crbegin();
278  }
279 
280  inline const_reverse_iterator crend() const {
281  return m_data.crend();
282  }
283 
284  inline size_type size() const {
285  return m_data.size();
286  }
287 
288  inline uint64_t bit_size() const {
289  return IntVectorTrait<T>::bit_size(m_data);
290  }
291 
292  inline size_type max_size() const {
293  return m_data.max_size();
294  }
295 
296  inline uint8_t width() const {
297  return IntVectorTrait<T>::width(m_data);
298  }
299 
300  inline void width(uint8_t w) {
301  IntVectorTrait<T>::width(m_data, w);
302  }
303 
304  private:
305  static const bool do_resize_check = false;
306 
311  template<typename F>
312  void check_for_growth(F f) {
313  auto old_cap = this->capacity();
314  f();
315  auto new_cap = this->capacity();
316 
317  if (do_resize_check) {
318  DCHECK_EQ(old_cap, new_cap)
319  << "\nresize() call grew the capacity!"
320  << "\nThis does not cause a single reallocation, but dynamical growth"
321  " with overallocation!"
322  << "\nConsider calling reserve() beforehand.";
323  }
324  }
325  public:
326 
327  inline void resize(size_type n) {
328  check_for_growth([&]() {
329  m_data.resize(n);
330  });
331  }
332 
333  inline void resize(size_type n, const value_type& val) {
334  check_for_growth([&]() {
335  m_data.resize(n, val);
336  });
337  }
338 
339  inline void resize(size_type n, const value_type& val, uint8_t w) {
340  check_for_growth([&]() {
341  IntVectorTrait<T>::resize(m_data, n, val, w);
342  });
343  }
344 
345  inline size_type capacity() const {
346  return m_data.capacity();
347  }
348 
349  inline uint64_t bit_capacity() const {
350  return IntVectorTrait<T>::bit_capacity(m_data);
351  }
352 
353  inline bool empty() const {
354  return m_data.empty();
355  }
356 
357  inline void reserve(size_type n) {
358  m_data.reserve(n);
359  }
360 
361  inline void reserve(size_type n, uint8_t w) {
362  IntVectorTrait<T>::bit_reserve(m_data, n * w);
363  }
364 
365  inline void bit_reserve(uint64_t n) {
366  IntVectorTrait<T>::bit_reserve(m_data, n);
367  }
368 
369  inline void shrink_to_fit() {
370  m_data.shrink_to_fit();
371  }
372 
373  inline reference operator[](size_type n) {
374  return m_data[n];
375  }
376 
377  inline const_reference operator[](size_type n) const {
378  return m_data[n];
379  }
380 
381  inline reference at(size_type n) {
382  return m_data.at(n);
383  }
384 
385  inline const_reference at(size_type n) const {
386  return m_data.at(n);
387  }
388 
389  inline reference front() {
390  return m_data.front();
391  }
392 
393  inline const_reference front() const {
394  return m_data.front();
395  }
396 
397  inline reference back() {
398  return m_data.back();
399  }
400 
401  inline const_reference back() const {
402  return m_data.back();
403  }
404 
405  inline internal_data_type* data() noexcept {
406  return m_data.data();
407  }
408 
409  inline const internal_data_type* data() const noexcept {
410  return m_data.data();
411  }
412 
413  template <class InputIterator>
414  inline void assign(InputIterator first, InputIterator last) {
415  m_data.assign(first, last);
416  }
417 
418  inline void assign(size_type n, const value_type& val) {
419  m_data.assign(n, val);
420  }
421 
422  inline void assign(std::initializer_list<value_type> il) {
423  m_data.assign(il);
424  }
425 
426  inline void push_back(const value_type& val) {
427  m_data.push_back(val);
428  }
429 
430  inline void push_back(value_type&& val) {
431  m_data.push_back(std::move(val));
432  }
433 
434  inline void pop_back() {
435  m_data.pop_back();
436  }
437 
438  inline iterator insert(const_iterator position, const value_type& val) {
439  return m_data.insert(position, val);
440  }
441 
442  inline iterator insert(const_iterator position, size_type n, const value_type& val) {
443  return m_data.insert(position, n, val);
444  }
445 
446  template <class InputIterator>
447  inline iterator insert(const_iterator position, InputIterator first, InputIterator last) {
448  return m_data.insert(position, first, last);
449  }
450 
451  inline iterator insert(const_iterator position, value_type&& val) {
452  return m_data.insert(position, std::move(val));
453  }
454 
455  inline iterator insert(const_iterator position, std::initializer_list<value_type> il) {
456  return m_data.insert(position, il);
457  }
458 
459  inline iterator erase(const_iterator position) {
460  return m_data.erase(position);
461  }
462 
463  inline iterator erase(const_iterator first, const_iterator last) {
464  return m_data.erase(first, last);
465  }
466 
467  inline void swap(IntVector& other) {
468  m_data.swap(other.m_data);
469  }
470 
471  inline void clear() {
472  m_data.clear();
473  }
474 
475  template <class... Args>
476  inline iterator emplace(const_iterator position, Args&&... args) {
477  return m_data.emplace(position, std::forward<Args...>(args)...);
478  }
479 
480  template <class... Args>
481  inline void emplace_back(Args&&... args) {
482  m_data.emplace_back(std::forward<Args...>(args)...);
483  }
484 
485  template<class U>
486  friend bool operator==(const IntVector<U>& lhs, const IntVector<U>& rhs);
487  template<class U>
488  friend bool operator!=(const IntVector<U>& lhs, const IntVector<U>& rhs);
489  template<class U>
490  friend bool operator<(const IntVector<U>& lhs, const IntVector<U>& rhs);
491  template<class U>
492  friend bool operator<=(const IntVector<U>& lhs, const IntVector<U>& rhs);
493  template<class U>
494  friend bool operator>(const IntVector<U>& lhs, const IntVector<U>& rhs);
495  template<class U>
496  friend bool operator>=(const IntVector<U>& lhs, const IntVector<U>& rhs);
497  template<class U>
498  friend void swap(IntVector<U>& lhs, IntVector<U>& rhs);
499  };
500 
501  template<class T>
502  bool operator==(const IntVector<T>& lhs, const IntVector<T>& rhs) {
503  return lhs.m_data == rhs.m_data;
504  }
505 
506  template<class T>
507  bool operator!=(const IntVector<T>& lhs, const IntVector<T>& rhs) {
508  return lhs.m_data != rhs.m_data;
509  }
510 
511  template<class T>
512  bool operator<(const IntVector<T>& lhs, const IntVector<T>& rhs) {
513  return lhs.m_data < rhs.m_data;
514  }
515 
516  template<class T>
517  bool operator<=(const IntVector<T>& lhs, const IntVector<T>& rhs) {
518  return lhs.m_data <= rhs.m_data;
519  }
520 
521  template<class T>
522  bool operator>(const IntVector<T>& lhs, const IntVector<T>& rhs) {
523  return lhs.m_data > rhs.m_data;
524  }
525 
526  template<class T>
527  bool operator>=(const IntVector<T>& lhs, const IntVector<T>& rhs) {
528  return lhs.m_data >= rhs.m_data;
529  }
530 
531  template<class T>
532  void swap(IntVector<T>& lhs, IntVector<T>& rhs) {
533  using std::swap;
534  swap(lhs.m_data, rhs.m_data);
535  }
536 
537 }
538 
540 template<class T>
542 
546 
554 
555 }
556 
void assign(size_type n, const value_type &val)
Definition: IntVector.hpp:418
void assign(std::initializer_list< value_type > il)
Definition: IntVector.hpp:422
void reserve(size_type n, uint8_t w)
Definition: IntVector.hpp:361
reference operator[](size_type n)
Definition: IntVector.hpp:373
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
const_iterator cbegin() const
Definition: IntVector.hpp:268
const_iterator end() const
Definition: IntVector.hpp:256
bool operator>(const IntVector< T > &lhs, const IntVector< T > &rhs)
Definition: IntVector.hpp:522
iterator erase(const_iterator first, const_iterator last)
Definition: IntVector.hpp:463
IntVector(IntVector &&other)
Definition: IntVector.hpp:216
IntVectorTrait< T >::reverse_iterator reverse_iterator
Definition: IntVector.hpp:185
bool operator==(const IntVector< T > &lhs, const IntVector< T > &rhs)
Definition: IntVector.hpp:502
void resize(size_type n, const value_type &val, uint8_t w)
Definition: IntVector.hpp:339
IntVector(const IntVector &other)
Definition: IntVector.hpp:213
A vector over arbitrary unsigned integer types.
Definition: IntVector.hpp:175
const_iterator cend() const
Definition: IntVector.hpp:272
const_reference back() const
Definition: IntVector.hpp:401
uint64_t bit_capacity() const
Definition: IntVector.hpp:349
reverse_iterator rbegin()
Definition: IntVector.hpp:244
void push_back(const value_type &val)
Definition: IntVector.hpp:426
size_type max_size() const
Definition: IntVector.hpp:292
IntVector & operator=(std::initializer_list< value_type > il)
Definition: IntVector.hpp:231
IntVectorTrait< T >::size_type size_type
Definition: IntVector.hpp:188
IntVector(size_type n, const value_type &val)
Definition: IntVector.hpp:204
IntVectorTrait< T >::const_reverse_iterator const_reverse_iterator
Definition: IntVector.hpp:186
void emplace_back(Args &&... args)
Definition: IntVector.hpp:481
const_reference at(size_type n) const
Definition: IntVector.hpp:385
static constexpr ElementStorageMode element_storage_mode()
Definition: IntVector.hpp:193
IntVectorTrait< T >::difference_type difference_type
Definition: IntVector.hpp:187
const_reference front() const
Definition: IntVector.hpp:393
iterator insert(const_iterator position, std::initializer_list< value_type > il)
Definition: IntVector.hpp:455
void push_back(value_type &&val)
Definition: IntVector.hpp:430
IntVector(std::initializer_list< value_type > il)
Definition: IntVector.hpp:219
IntVector & operator=(IntVector &&other)
Definition: IntVector.hpp:226
iterator emplace(const_iterator position, Args &&... args)
Definition: IntVector.hpp:476
IntVectorTrait< T >::pointer pointer
Definition: IntVector.hpp:181
IntVector & operator=(const IntVector &other)
Definition: IntVector.hpp:221
void swap(IntVector &other)
Definition: IntVector.hpp:467
IntVectorTrait< T >::iterator iterator
Definition: IntVector.hpp:183
void swap(IntVector< T > &lhs, IntVector< T > &rhs)
Definition: IntVector.hpp:532
void resize(size_type n)
Definition: IntVector.hpp:327
IntVectorTrait< T >::reference reference
Definition: IntVector.hpp:179
IntVectorTrait< T >::value_type value_type
Definition: IntVector.hpp:178
const_reverse_iterator crbegin() const
Definition: IntVector.hpp:276
const_reverse_iterator rend() const
Definition: IntVector.hpp:264
IntVectorTrait< T >::internal_data_type internal_data_type
The element type of the internal data buffer accessed with data()
Definition: IntVector.hpp:191
internal_data_type * data() noexcept
Definition: IntVector.hpp:405
uint64_t m_data
Definition: uint_t.hpp:30
IntVectorTrait< T >::const_reference const_reference
Definition: IntVector.hpp:180
iterator insert(const_iterator position, const value_type &val)
Definition: IntVector.hpp:438
const_iterator begin() const
Definition: IntVector.hpp:252
reverse_iterator rend()
Definition: IntVector.hpp:248
bool operator>=(const IntVector< T > &lhs, const IntVector< T > &rhs)
Definition: IntVector.hpp:527
void bit_reserve(uint64_t n)
Definition: IntVector.hpp:365
const_reverse_iterator rbegin() const
Definition: IntVector.hpp:260
size_type capacity() const
Definition: IntVector.hpp:345
iterator insert(const_iterator position, InputIterator first, InputIterator last)
Definition: IntVector.hpp:447
uint64_t bit_size() const
Definition: IntVector.hpp:288
IntVector(InputIterator first, InputIterator last)
Definition: IntVector.hpp:210
void reserve(size_type n)
Definition: IntVector.hpp:357
constexpr uint_impl_t()
Definition: uint_t.hpp:38
bool operator!=(const IntVector< T > &lhs, const IntVector< T > &rhs)
Definition: IntVector.hpp:507
IntVectorTrait< T >::const_iterator const_iterator
Definition: IntVector.hpp:184
const_reference operator[](size_type n) const
Definition: IntVector.hpp:377
reference at(size_type n)
Definition: IntVector.hpp:381
const_reverse_iterator crend() const
Definition: IntVector.hpp:280
IntVectorTrait< T >::const_pointer const_pointer
Definition: IntVector.hpp:182
IntVector(size_type n, const value_type &val, uint8_t width)
Definition: IntVector.hpp:205
iterator insert(const_iterator position, size_type n, const value_type &val)
Definition: IntVector.hpp:442
size_type size() const
Definition: IntVector.hpp:284
const internal_data_type * data() const noexcept
Definition: IntVector.hpp:409
iterator insert(const_iterator position, value_type &&val)
Definition: IntVector.hpp:451
void resize(size_type n, const value_type &val)
Definition: IntVector.hpp:333
void assign(InputIterator first, InputIterator last)
Definition: IntVector.hpp:414
iterator erase(const_iterator position)
Definition: IntVector.hpp:459