uTL
micro Template library
id.h
Go to the documentation of this file.
1 
22 #ifndef __utl_container_id_h__
23 #define __utl_container_id_h__
24 
25 #include <utl/core/impl.h>
26 #include <utl/container/array.h>
27 #include <algorithm>
28 
29 namespace utl {
30 
36  template <typename _Tp, size_t _Nm>
38  struct id_traits {
39  typedef _Tp type[_Nm];
40 
41  static constexpr _Tp& Ref (const type& t, size_t n) noexcept {
42  return const_cast<_Tp&> (t[n]);
43  }
44 
45  static constexpr _Tp* Ptr(const type& t) noexcept {
46  return const_cast<_Tp*> (t);
47  }
48  };
49 
50  template <typename _Tp>
51  struct id_traits<_Tp, 0> {
52  struct type { };
53 
54  static constexpr _Tp& Ref(const type& t, size_t n) noexcept {
55  return *static_cast<_Tp*>(nullptr);
56  }
57 
58  static constexpr _Tp* Ptr(const type& t) noexcept {
59  return nullptr;
60  }
61  };
63 
77  template <typename _Tp, size_t _Nm>
78  struct id_t {
79  using value_type = _Tp;
80  using pointer = value_type*;
81  using const_pointer = const value_type*;
83  using const_reference = const value_type&;
84  using iterator = value_type*;
85  using const_iterator = const value_type*;
86  using size_type = size_t;
88  using reverse_iterator = std::reverse_iterator <iterator>;
90  = std::reverse_iterator <const_iterator>;
91 
92  // type and data
95 
96  // No explicit construct/copy/destroy for aggregate type.
97 
98  // DR 776 (std::array)
99  void fill (const value_type& v) { std::fill_n (begin(), size(), v); }
100 
101  void swap (id_t& other) noexcept {
102  std::swap_ranges (begin(), end(), other.begin());
103  }
104 
107  iterator begin() noexcept { return iterator (data()); }
108  const_iterator begin() const noexcept { return const_iterator (data()); }
109  iterator end() noexcept { return iterator (data() + _Nm); }
110  const_iterator end() const noexcept { return const_iterator (data() + _Nm); }
111  const_iterator cbegin() const noexcept { return const_iterator (data()); }
112  const_iterator cend() const noexcept { return const_iterator (data() + _Nm); }
113 
114  reverse_iterator rbegin() noexcept { return reverse_iterator (end()); }
115  reverse_iterator rend() noexcept { return reverse_iterator (begin()); }
116  const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator (end()); }
117  const_reverse_iterator rend() const noexcept { return const_reverse_iterator (begin()); }
118  const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator (end()); }
119  const_reverse_iterator crend() const noexcept { return const_reverse_iterator (begin()); }
123  constexpr size_type size() const noexcept { return _Nm; }
124  constexpr size_type max_size() const noexcept { return _Nm; }
125  constexpr bool empty() const noexcept { return size() == 0; }
127 
130 
133  return traits_t::Ref (_data, n);
134  }
136  constexpr const_reference operator[] (size_type n) const noexcept {
137  return traits_t::Ref (_data, n);
138  }
139 
144  reference at (size_type n) noexcept {
145  if (n < _Nm)
146  return traits_t::Ref (_data, n);
147  else
148  abort ();
149  }
151  constexpr const_reference at (size_type n) const noexcept {
152  static_assert ((n < _Nm), "id_t::at: out of range");
153  return traits_t::Ref (_data, n);
154  }
159  constexpr bool bit (uint8_t bit) const noexcept {
160  return traits_t::Ref (_data, bit/(8*sizeof(_Tp))) &
161  (static_cast<value_type>(0x01) << ((bit % (8*sizeof(_Tp)))-1));
162  }
163 
169  void bit (uint8_t bit, bool v) noexcept {
170  value_type one = 1;
171  uint8_t den = 8*sizeof(_Tp)/sizeof(uint8_t);
172  if (v) traits_t::Ref (_data, bit/den) |= one << ((bit % den)-1);
173  else traits_t::Ref (_data, bit/den) &= ~one << ((bit % den)-1);
174  }
175  // first item
176  reference front () noexcept {
177  return *begin ();
178  }
179  constexpr const_reference front () const noexcept {
180  return traits_t::Ref (_data, 0);
181  }
182 
183  // Last item
184  reference back () noexcept {
185  return _Nm ? *(end() - 1) : *end();
186  }
187  constexpr const_reference back () const noexcept {
188  return _Nm ? traits_t::Ref (_data, _Nm - 1)
189  : traits_t::Ref (_data, 0);
190  }
191 
192  // Pointer to data
193  pointer data () noexcept { return traits_t::Ptr (_data); }
194  const_pointer data () const noexcept { return traits_t::Ptr (_data); }
196  };
197 
200  template <typename _Tp, size_t _Nm>
201  inline bool operator== (const id_t<_Tp, _Nm>& lhs, const id_t<_Tp, _Nm>& rhs) {
202  return std::equal (lhs.begin(), lhs.end(), rhs.begin());
203  }
204 
205  template <typename _Tp, size_t _Nm>
206  inline bool operator!= (const id_t<_Tp, _Nm>& lhs, const id_t<_Tp, _Nm>& rhs) {
207  return !(lhs == rhs);
208  }
209 
210  template <typename _Tp, size_t _Nm>
211  inline bool operator< (const id_t<_Tp, _Nm>& lhs, const id_t<_Tp, _Nm>& rhs) {
212  // MSB plays bigger role in comparison
213  return std::lexicographical_compare(lhs.rbegin(), lhs.rend(), rhs.rbegin(), rhs.rend());
214  }
215 
216  template <typename _Tp, size_t _Nm>
217  inline bool operator> (const id_t<_Tp, _Nm>& lhs, const id_t<_Tp, _Nm>& rhs) {
218  return rhs < lhs;
219  }
220 
221  template <typename _Tp, size_t _Nm>
222  inline bool operator<= (const id_t<_Tp, _Nm>& lhs, const id_t<_Tp, _Nm>& rhs) {
223  return !(lhs > rhs);
224  }
225 
226  template <typename _Tp, size_t _Nm>
227  inline bool operator>= (const id_t<_Tp, _Nm>& lhs, const id_t<_Tp, _Nm>& rhs) {
228  return !(lhs < rhs);
229  }
231 
232 } // namespace utl
233 
234 
235 #endif /* __utl_container_id_h__ */
bool operator==(const array< _Tp, _Nm > &lhs, const array< _Tp, _Nm > &rhs)
Definition: array.h:181
reference back() noexcept
Definition: id.h:184
const value_type & const_reference
Definition: id.h:83
const_reverse_iterator rbegin() const noexcept
Definition: id.h:116
static constexpr _Tp & Ref(const type &t, size_t n) noexcept
Definition: id.h:41
iterator end() noexcept
Definition: id.h:109
static constexpr _Tp * Ptr(const type &t) noexcept
Definition: id.h:58
const_iterator begin() const noexcept
Definition: id.h:108
size_t size_type
Definition: id.h:86
reference front() noexcept
Definition: id.h:176
constexpr const_reference back() const noexcept
Definition: id.h:187
traits_t::type _data
Definition: id.h:94
void fill(const value_type &v)
Definition: id.h:99
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: id.h:90
const_iterator cbegin() const noexcept
Definition: id.h:111
const value_type * const_pointer
Definition: id.h:81
void bit(uint8_t bit, bool v) noexcept
Definition: id.h:169
const_iterator cend() const noexcept
Definition: id.h:112
id container traits helper
Definition: id.h:38
const_reverse_iterator crend() const noexcept
Definition: id.h:119
std::ptrdiff_t difference_type
Definition: id.h:87
std::ptrdiff_t ptrdiff_t
Definition: types.h:40
const_reverse_iterator rend() const noexcept
Definition: id.h:117
bool operator>=(const array< _Tp, _Nm > &lhs, const array< _Tp, _Nm > &rhs)
Definition: array.h:207
value_type * pointer
Definition: id.h:80
std::size_t size_t
Definition: types.h:37
STL&#39;s core language concepts.
Definition: _1wire.h:30
constexpr const_reference front() const noexcept
Definition: id.h:179
pointer data() noexcept
Definition: id.h:193
reverse_iterator rend() noexcept
Definition: id.h:115
value_type & reference
Definition: id.h:82
constexpr size_type max_size() const noexcept
Definition: id.h:124
bool operator>(const array< _Tp, _Nm > &lhs, const array< _Tp, _Nm > &rhs)
Definition: array.h:196
const_iterator end() const noexcept
Definition: id.h:110
const value_type * const_iterator
Definition: id.h:85
void swap(id_t &other) noexcept
Definition: id.h:101
A standard container for storing IDs as a fixed size sequence of bytes. This type is based on etl::ar...
Definition: id.h:78
std::reverse_iterator< iterator > reverse_iterator
Definition: id.h:88
reverse_iterator rbegin() noexcept
Definition: id.h:114
const_reverse_iterator crbegin() const noexcept
Definition: id.h:118
constexpr bool empty() const noexcept
Definition: id.h:125
reference at(size_type n) noexcept
Definition: id.h:144
bool operator!=(const array< _Tp, _Nm > &lhs, const array< _Tp, _Nm > &rhs)
Definition: array.h:186
uint8_t value_type
Definition: id.h:79
constexpr const_reference at(size_type n) const noexcept
Compile time boundary check dereference operator.
Definition: id.h:151
const_pointer data() const noexcept
Definition: id.h:194
reference operator[](size_type n) noexcept
Operator [].
Definition: id.h:132
iterator begin() noexcept
Definition: id.h:107
static constexpr _Tp * Ptr(const type &t) noexcept
Definition: id.h:45
_Tp type[_Nm]
Definition: id.h:39
value_type * iterator
Definition: id.h:84
Implementation detail main forward header.
static constexpr _Tp & Ref(const type &t, size_t n) noexcept
Definition: id.h:54
constexpr size_type size() const noexcept
Definition: id.h:123
constexpr bool bit(uint8_t bit) const noexcept
Definition: id.h:159