tudocomp
– The TU Dortmund Compression Framework
GenericView.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <cmath>
5 #include <cstddef>
6 #include <fstream>
7 #include <iostream>
8 #include <memory>
9 #include <sstream>
10 #include <string>
11 #include <type_traits>
12 #include <utility>
13 #include <iomanip>
14 #include <cstring>
15 #include <glog/logging.h>
16 #include <functional>
17 
20 
21 namespace tdc {
22 
30 template<class T>
31 class GenericView: GenericViewBase<T, T*> {
32  using Super = GenericViewBase<T, T*>;
33  inline GenericView(const Super& other): Super::GenericViewBase(other) {}
34 public:
35  // Type members
36 
37  using value_type = typename Super::value_type;
38  using reference = T&;
40  using pointer = T*;
42  using iterator = pointer;
44  using reverse_iterator = std::reverse_iterator<iterator>;
47  using size_type = typename Super::size_type;
48 
49  // static value members
50 
52  static const size_type npos = Super::npos;
53 
54  // Constructors
55 
57  inline GenericView():
59 
61  inline GenericView(T* data, size_t len):
62  Super::GenericViewBase(data, len) {}
63 
65  inline GenericView(const GenericView& other):
66  Super::GenericViewBase(other) {}
67 
69  inline GenericView(std::vector<T>& other):
70  Super::GenericViewBase(other.data(), other.size()) {}
71 
73  template<size_t N>
74  inline GenericView(std::array<uliteral_t, N>& other):
75  Super::GenericViewBase(other.data(), other.size()) {}
76 
78  inline operator std::vector<T>() const {
79  return Super::operator std::vector<T>();
80  }
81 
83  inline const_iterator begin() const {
84  return Super::begin();
85  }
86 
88  inline iterator begin() {
89  return this->m_data;
90  }
91 
93  inline const_iterator end() const {
94  return Super::end();
95  }
96 
98  inline iterator end() {
99  return this->m_data + this->m_size;
100  }
101 
104  return Super::rbegin();
105  }
106 
109  return std::reverse_iterator<iterator>(end());
110  }
111 
113  inline const_reverse_iterator rend() const {
114  return Super::rend();
115  }
116 
119  return std::reverse_iterator<iterator>(begin());
120  }
121 
123  inline const_iterator cbegin() const {
124  return Super::cbegin();
125  }
126 
128  inline const_iterator cend() const {
129  return Super::cend();
130  }
131 
134  return Super::crbegin();
135  }
136 
138  inline const_reverse_iterator crend() const {
139  return Super::crend();
140  }
141 
143  inline size_type size() const {
144  return Super::size();
145  }
146 
148  inline size_type max_size() const {
149  return Super::max_size();
150  }
151 
153  inline bool empty() const {
154  return Super::empty();
155  }
156 
161  return Super::operator[](pos);
162  }
163 
169  return this->m_data[n];
170  }
171 
175  inline const_reference at(size_type pos) const {
176  return Super::at(pos);
177  }
178 
182  inline reference at(size_type n) {
184  return this->m_data[n];
185  }
186 
188  inline const_reference front() const {
189  return Super::front();
190  }
191 
193  inline reference front() {
195  return this->m_data[0];
196  }
197 
199  inline const_reference back() const {
200  return Super::back();
201  }
202 
204  inline reference back() {
206  return this->m_data[size() - 1];
207  }
208 
210  inline const value_type* data() const noexcept {
211  return Super::data();
212  }
213 
215  inline value_type* data() noexcept {
216  return this->m_data;
217  }
218 
220  inline void pop_back() {
221  Super::pop_back();
222  }
223 
225  inline void pop_front() {
227  }
228 
230  inline void swap(GenericView& other) {
231  Super::swap(other);
232  }
233 
235  inline void clear() {
236  Super::clear();
237  }
238 
252  inline GenericView substr(size_type pos, size_type len = npos) const {
253  return Super::substr(pos, len);
254  }
255 
267  inline GenericView slice(size_type from, size_type to = npos) const {
268  return Super::slice(from, to);
269  }
270 
272  inline void remove_prefix(size_type n) {
273  return Super::remove_prefix(n);
274  }
275 
277  inline void remove_suffix(size_type n) {
278  return Super::remove_suffix(n);
279  }
280 
282  inline bool starts_with(const T& other) const {
283  return Super::starts_with(other);
284  }
285 
288  inline bool starts_with(const ConstGenericView<T>& other) const {
289  return Super::starts_with(other);
290  }
291 
293  inline bool ends_with(const T& other) const {
294  return Super::ends_with(other);
295  }
296 
299  inline bool ends_with(const ConstGenericView<T>& other) const {
300  return Super::ends_with(other);
301  }
302 
303  template<class U>
304  friend void swap(GenericView<U>& lhs, GenericView<U>& rhs);
305 };
306 
307 template<class T>
309  using Super = typename GenericView<T>::Super;
310  return swap((Super&) lhs, (Super&) rhs);
311 }
312 
313 template<class T>
314 bool operator==(const GenericView<T>& lhs, const ConstGenericView<T>& rhs) {
315  return ConstGenericView<T>(lhs) == ConstGenericView<T>(rhs);
316 }
317 
318 template<class T>
319 bool operator!=(const GenericView<T>& lhs, const ConstGenericView<T>& rhs) {
320  return ConstGenericView<T>(lhs) != ConstGenericView<T>(rhs);
321 }
322 
323 template<class T>
324 bool operator<(const GenericView<T>& lhs, const ConstGenericView<T>& rhs) {
325  return ConstGenericView<T>(lhs) < ConstGenericView<T>(rhs);
326 }
327 
328 template<class T>
329 bool operator<=(const GenericView<T>& lhs, const ConstGenericView<T>& rhs) {
330  return ConstGenericView<T>(lhs) <= ConstGenericView<T>(rhs);
331 }
332 
333 template<class T>
334 bool operator>(const GenericView<T>& lhs, const ConstGenericView<T>& rhs) {
335  return ConstGenericView<T>(lhs) > ConstGenericView<T>(rhs);
336 }
337 
338 template<class T>
339 bool operator>=(const GenericView<T>& lhs, const ConstGenericView<T>& rhs) {
340  return ConstGenericView<T>(lhs) >= ConstGenericView<T>(rhs);
341 }
342 
343 template<class T>
344 bool operator==(const ConstGenericView<T>& lhs, const GenericView<T>& rhs) {
345  return ConstGenericView<T>(lhs) == ConstGenericView<T>(rhs);
346 }
347 
348 template<class T>
349 bool operator!=(const ConstGenericView<T>& lhs, const GenericView<T>& rhs) {
350  return ConstGenericView<T>(lhs) != ConstGenericView<T>(rhs);
351 }
352 
353 template<class T>
354 bool operator<(const ConstGenericView<T>& lhs, const GenericView<T>& rhs) {
355  return ConstGenericView<T>(lhs) < ConstGenericView<T>(rhs);
356 }
357 
358 template<class T>
359 bool operator<=(const ConstGenericView<T>& lhs, const GenericView<T>& rhs) {
360  return ConstGenericView<T>(lhs) <= ConstGenericView<T>(rhs);
361 }
362 
363 template<class T>
364 bool operator>(const ConstGenericView<T>& lhs, const GenericView<T>& rhs) {
365  return ConstGenericView<T>(lhs) > ConstGenericView<T>(rhs);
366 }
367 
368 template<class T>
369 bool operator>=(const ConstGenericView<T>& lhs, const GenericView<T>& rhs) {
370  return ConstGenericView<T>(lhs) >= ConstGenericView<T>(rhs);
371 }
372 
373 template<class T>
374 bool operator==(const GenericView<T>& lhs, const GenericView<T>& rhs) {
375  return ConstGenericView<T>(lhs) == ConstGenericView<T>(rhs);
376 }
377 
378 template<class T>
379 bool operator!=(const GenericView<T>& lhs, const GenericView<T>& rhs) {
380  return ConstGenericView<T>(lhs) != ConstGenericView<T>(rhs);
381 }
382 
383 template<class T>
384 bool operator<(const GenericView<T>& lhs, const GenericView<T>& rhs) {
385  return ConstGenericView<T>(lhs) < ConstGenericView<T>(rhs);
386 }
387 
388 template<class T>
389 bool operator<=(const GenericView<T>& lhs, const GenericView<T>& rhs) {
390  return ConstGenericView<T>(lhs) <= ConstGenericView<T>(rhs);
391 }
392 
393 template<class T>
394 bool operator>(const GenericView<T>& lhs, const GenericView<T>& rhs) {
395  return ConstGenericView<T>(lhs) > ConstGenericView<T>(rhs);
396 }
397 
398 template<class T>
399 bool operator>=(const GenericView<T>& lhs, const GenericView<T>& rhs) {
400  return ConstGenericView<T>(lhs) >= ConstGenericView<T>(rhs);
401 }
402 
403 }
404 
405 namespace std {
406  template<class T>
407  struct hash<tdc::GenericView<T>> {
408  size_t operator()(const tdc::GenericView<T>& x) const {
409  return hash<tdc::ConstGenericView<T>>()(x);
410  }
411  };
412 }
const_reverse_iterator rbegin() const
Begin of reverse iterator.
GenericView substr(size_type pos, size_type len=npos) const
Construct a new View that is a sub view into the current one.
bool operator!=(const ConstGenericView< uliteral_t > &lhs, const ConstGenericView< uliteral_t > &rhs)
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
const_reverse_iterator crbegin() const
Begin of const reverse iterator.
void bound_check(size_t pos) const
static const size_type npos
Sentinel value indicating a index at the end of the view.
Definition: GenericView.hpp:52
GenericView(const GenericView &other)
Construct a View as a copy of other
Definition: GenericView.hpp:65
void swap(GenericView &other)
Swap two Views.
A view into a slice of memory.
A const view into a slice of memory.
const_reference back() const
const_iterator begin() const
Begin of iterator.
Definition: GenericView.hpp:83
GenericView(T *data, size_t len)
Construct a View pointing at len elements starting from data
Definition: GenericView.hpp:61
bool operator==(const ConstGenericView< uliteral_t > &lhs, const ConstGenericView< uliteral_t > &rhs)
reference at(size_type n)
Access the element at pos
size_t operator()(const tdc::GenericView< T > &x) const
reference back()
Access the last element.
bool operator>=(const ConstGenericView< uliteral_t > &lhs, const ConstGenericView< uliteral_t > &rhs)
const_iterator cend() const
const_iterator cend() const
End of const iterator.
reference operator[](size_type n)
Access the element at pos
bool ends_with(const ConstGenericView< T > &other) const
Returns true if the View ends with the sequence of literals contained in other.
reference front()
Access the first element.
len_compact_t len
Definition: LZSSFactors.hpp:38
const_reference at(size_type pos) const
Access the element at pos
value_type * data() noexcept
The backing memory location.
size_type size() const
Returns size of the View.
const_reverse_iterator rbegin() const
GenericView slice(size_type from, size_type to=npos) const
Construct a new View that is a sub view into the current one.
const_iterator begin() const
void swap(GenericViewBase &other)
iterator end()
End of iterator.
Definition: GenericView.hpp:98
const_reference front() const
typename Super::size_type size_type
Definition: GenericView.hpp:47
void clear()
Sets the size to 0.
void pop_back()
Remove the last element from the View.
GenericView()
Construct a empty View.
Definition: GenericView.hpp:57
const value_type * data() const noexcept
The backing memory location.
const_reference at(size_type n) const
const_reference back() const
Access the last element.
const_reference operator[](size_type n) const
bool starts_with(const ConstGenericView< T > &other) const
Returns true if the View starts with the sequence of literals contained in other. ...
void pop_front()
Remove the first element from the View.
std::reverse_iterator< const_iterator > const_reverse_iterator
const_reference front() const
Access the first element.
bool starts_with(const T &other) const
Returns true if the View starts with the literal other
iterator begin()
Begin of iterator.
Definition: GenericView.hpp:88
len_compact_t pos
Definition: LZSSFactors.hpp:38
const_iterator end() const
static const size_type npos
size_type max_size() const
Returns max size of the View. Always the same as size()
bool ends_with(const T &other) const
Returns true if the View ends with the literal other
std::reverse_iterator< iterator > reverse_iterator
Definition: GenericView.hpp:44
const_reference operator[](size_type pos) const
Access the element at pos
const_reverse_iterator crbegin() const
bool ends_with(const T &c) const
const_iterator cbegin() const
GenericViewBase slice(size_type from, size_type to=npos) const
const_reverse_iterator crend() const
reverse_iterator rbegin()
Begin of reverse iterator.
reverse_iterator rend()
End of reverse iterator.
void debug_bound_check(size_t IF_DEBUG(pos)) const
const_pointer const_iterator
void remove_suffix(size_type n)
Removes the last n elements from the View.
bool empty() const
Returns true if empty.
const_reverse_iterator rend() const
End of reverse iterator.
bool starts_with(const T &c) const
GenericView(std::array< uliteral_t, N > &other)
Construct a View pointing at the contents of a array.
Definition: GenericView.hpp:74
GenericView(std::vector< T > &other)
Construct a View pointing at the contents of a vector.
Definition: GenericView.hpp:69
const_iterator end() const
End of iterator.
Definition: GenericView.hpp:93
const value_type * data() const noexcept
void remove_prefix(size_type n)
Removes the first n elements from the View.
bool operator>(const ConstGenericView< uliteral_t > &lhs, const ConstGenericView< uliteral_t > &rhs)
friend class GenericViewBase< T, T *>
const_iterator cbegin() const
Begin of const iterator.
GenericViewBase substr(size_type pos, size_type len=npos) const
const_reverse_iterator crend() const
End of const reverse iterator.
const_reverse_iterator rend() const