openMSX
array_ref.hh
Go to the documentation of this file.
1 #ifndef ARRAY_REF_HH
2 #define ARRAY_REF_HH
3 
4 #include <algorithm>
5 #include <iterator>
6 #include <vector>
7 #include <cassert>
8 #include <cstddef>
9 
18 template<typename T>
19 class array_ref
20 {
21 public:
22  // types
23  using value_type = T;
24  using pointer = const T*;
25  using reference = const T&;
26  using const_reference = const T&;
27  using const_iterator = const T*;
29  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
31  using size_type = size_t;
32  using difference_type = ptrdiff_t;
33 
34  // construct/copy/assign
36  : dat(nullptr), siz(0) {}
37  array_ref(const array_ref& r)
38  : dat(r.data()), siz(r.size()) {}
39  array_ref(const T* array, size_t length)
40  : dat(array), siz(length) {}
41  template<typename ITER> array_ref(ITER first, ITER last)
42  : dat(&*first), siz(std::distance(first, last)) {}
43  /*implicit*/ array_ref(const std::vector<T>& v)
44  : dat(v.data()), siz(v.size()) {}
45  /*implicit*/ template<size_t N> array_ref(const T(&a)[N])
46  : dat(a), siz(N) {}
47 
48  array_ref& operator=(const array_ref& rhs) {
49  dat = rhs.data();
50  siz = rhs.size();
51  return *this;
52  }
53 
54  // iterators
55  const_iterator begin() const { return dat; }
56  const_iterator end() const { return dat + siz; }
59 
60  // capacity
61  size_type size() const { return siz; }
62  bool empty() const { return siz == 0; }
63 
64  // element access
65  const T& operator[](size_t i) const {
66  assert(i < siz);
67  return dat[i];
68  }
69  const T& front() const { return (*this)[0]; }
70  const T& back() const { return (*this)[siz - 1]; }
71  const T* data() const { return dat; }
72 
73  // mutators
74  void clear() { siz = 0; } // no need to change 'dat'
76  if (n <= siz) {
77  dat += n;
78  siz -= n;
79  } else {
80  clear();
81  }
82  }
84  if (n <= siz) {
85  siz -= n;
86  } else {
87  clear();
88  }
89  }
90  void pop_back() { remove_suffix(1); }
91  void pop_front() { remove_prefix(1); }
92 
94  if (pos >= siz) return array_ref();
95  return array_ref(dat + pos, std::min(n, siz - pos));
96  }
97 
98 private:
99  const T* dat;
100  size_type siz;
101 };
102 
103 // deducing constructor wrappers
104 template<typename T> inline array_ref<T> make_array_ref(const T* array, size_t length)
105 {
106  return array_ref<T>(array, length);
107 }
108 
109 template<typename T, size_t N> inline array_ref<T> make_array_ref(const T(&a)[N])
110 {
111  return array_ref<T>(a);
112 }
113 
114 template<typename T> inline array_ref<T> make_array_ref(const std::vector<T>& v)
115 {
116  return array_ref<T>(v);
117 }
118 
119 #endif
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:334
void pop_front()
Definition: array_ref.hh:91
void pop_back()
Definition: array_ref.hh:90
vecN< N, T > min(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:269
const T & reference
Definition: array_ref.hh:25
const_iterator begin() const
Definition: array_ref.hh:55
STL namespace.
array_ref(const std::vector< T > &v)
Definition: array_ref.hh:43
size_t size_type
Definition: array_ref.hh:31
T value_type
Definition: array_ref.hh:23
void remove_suffix(size_type n)
Definition: array_ref.hh:83
array_ref(const array_ref &r)
Definition: array_ref.hh:37
const T & const_reference
Definition: array_ref.hh:26
array_ref & operator=(const array_ref &rhs)
Definition: array_ref.hh:48
void clear()
Definition: array_ref.hh:74
This class implements a subset of the proposal for std::array_ref (proposed for the next c++ standard...
Definition: array_ref.hh:19
const T * data() const
Definition: array_ref.hh:71
ptrdiff_t difference_type
Definition: array_ref.hh:32
array_ref< T > make_array_ref(const T *array, size_t length)
Definition: array_ref.hh:104
array_ref(const T(&a)[N])
Definition: array_ref.hh:45
const T & back() const
Definition: array_ref.hh:70
const T * const_iterator
Definition: array_ref.hh:27
std::iterator_traits< octet_iterator >::difference_type distance(octet_iterator first, octet_iterator last)
bool empty() const
Definition: array_ref.hh:62
array_ref substr(size_type pos, size_type n=size_type(-1)) const
Definition: array_ref.hh:93
size_type size() const
Definition: array_ref.hh:61
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: array_ref.hh:29
const_iterator iterator
Definition: array_ref.hh:28
void remove_prefix(size_type n)
Definition: array_ref.hh:75
array_ref(ITER first, ITER last)
Definition: array_ref.hh:41
const_reverse_iterator reverse_iterator
Definition: array_ref.hh:30
const_iterator end() const
Definition: array_ref.hh:56
const T & operator[](size_t i) const
Definition: array_ref.hh:65
const_reverse_iterator rend() const
Definition: array_ref.hh:58
const T & front() const
Definition: array_ref.hh:69
array_ref(const T *array, size_t length)
Definition: array_ref.hh:39
const T * pointer
Definition: array_ref.hh:24
const_reverse_iterator rbegin() const
Definition: array_ref.hh:57