cx_stringlist.h
Go to the documentation of this file.
1 #ifndef CX_STRINGLIST
2 #define CX_STRINGLIST
3 
4 #include <cstddef>
5 #include <array>
6 #include "cx_string.h"
7 
8 // Inspired by constexpr_all_the_things by Jason Turner & Ben Deane
9 // github : https://github.com/lefticus/constexpr_all_the_things
10 // video : https://www.youtube.com/watch?v=HMB9oXFobJc
11 
12 namespace dnai
13 {
14  namespace utils
15  {
16  template <typename Value, std::size_t Size = 5>
17  class vector
18  {
19  using storage_t = std::array<Value, Size>;
20  public:
21  using iterator = typename storage_t::iterator;
22  using const_iterator = typename storage_t::const_iterator;
23  using value_type = Value;
24  using reference = typename storage_t::reference;
25  using const_reference = typename storage_t::const_reference;
26 
27  template<typename Itr>
28  constexpr vector(Itr begin, const Itr &end)
29  {
30  while (begin != end) {
31  push_back(*begin);
32  ++begin;
33  }
34  }
35  constexpr vector(std::initializer_list<Value> init)
36  : vector(init.begin(), init.end())
37  {
38  }
39 
40  constexpr vector() = default;
41 
42  constexpr auto begin() const { return m_data.begin(); }
43  constexpr auto begin() { return m_data.begin(); }
44 
45  // We would have prefered to use `std::next`, however it does not seem to be
46  // enabled for constexpr use for std::array in this version of gcc. As of
47  // September 2017 this is fixed in GCC trunk but not in GCC 7.2.
48  constexpr auto end() const { return m_data.begin() + m_size; }
49  constexpr auto end() { return m_data.begin() + m_size; }
50 
51  constexpr auto cbegin() const { return m_data.begin(); }
52  constexpr auto cend() const { return m_data.begin() + m_size; }
53 
54  constexpr const Value &operator[](const std::size_t t_pos) const {
55  return m_data[t_pos];
56  }
57  constexpr Value &operator[](const std::size_t t_pos) {
58  return m_data[t_pos];
59  }
60 
61  constexpr Value &at(const std::size_t t_pos) {
62  if (t_pos >= m_size) {
63  // This is allowed in constexpr context, but if the constexpr evaluation
64  // hits this exception the compile would fail
65  throw std::range_error("Index past end of vector");
66  }
67  else {
68  return m_data[t_pos];
69  }
70  }
71  constexpr const Value &at(const std::size_t t_pos) const {
72  if (t_pos >= m_size) {
73  throw std::range_error("Index past end of vector");
74  }
75  else {
76  return m_data[t_pos];
77  }
78  }
79 
80  constexpr Value& push_back(Value t_v) {
81  if (m_size >= Size) {
82  throw std::range_error("Index past end of vector");
83  }
84  else {
85  Value& v = m_data[m_size++];
86  v = std::move(t_v);
87  return v;
88  }
89  }
90 
91  constexpr const Value &back() const {
92  if (empty()) {
93  throw std::range_error("Index past end of vector");
94  }
95  else {
96  return m_data[m_size - 1];
97  }
98  }
99  constexpr Value &back() {
100  if (empty()) {
101  throw std::range_error("Index past end of vector");
102  }
103  else {
104  return m_data[m_size - 1];
105  }
106  }
107 
108  constexpr auto capacity() const { return Size; }
109  constexpr auto size() const { return m_size; }
110  constexpr auto empty() const { return m_size == 0; }
111 
112  constexpr void clear() { m_size = 0; }
113 
114  constexpr const Value* data() const {
115  return m_data.data();
116  }
117 
118  private:
120  std::size_t m_size{ 0 };
121  };
122 
123  template<typename T, size_t Size1, size_t Size2>
124  constexpr bool operator==(const vector<T, Size1> &x, const vector<T, Size2> &y)
125  {
126  auto first1 = x.begin();
127  auto first2 = y.begin();
128  const auto last1 = x.begin();
129  const auto last2 = y.begin();
130  while (first1 != last1 && first2 != last2 && *first1 == *first2) {
131  ++first1, ++first2;
132  }
133  return first1 == last1 && first2 == last2;
134  }
135 
136 
137  // Is addition (concatenation) on strings useful? Not sure yet. But it does
138  // allow us to carry the type information properly.
139 
140  template <typename Value, std::size_t S1, std::size_t S2>
143  copy(a.cbegin(), a.cend(), back_insert_iterator(v));
144  copy(b.cbegin(), b.cend(), back_insert_iterator(v));
145  return v;
146  }
147 
148  template <std::size_t Count = 5>
150  }
151 }
152 
153 #endif //CX_STRINGLIST
constexpr auto end() const
Definition: cx_stringlist.h:48
constexpr Value & back()
Definition: cx_stringlist.h:99
constexpr bool operator==(const static_string &x, const static_string &y)
Definition: cx_string.h:49
typename storage_t::const_reference const_reference
Definition: cx_stringlist.h:25
constexpr const Value * data() const
Definition: cx_stringlist.h:114
typename storage_t::const_iterator const_iterator
Definition: cx_stringlist.h:22
constexpr vector(Itr begin, const Itr &end)
Definition: cx_stringlist.h:28
constexpr Value & operator[](const std::size_t t_pos)
Definition: cx_stringlist.h:57
constexpr const Value & operator[](const std::size_t t_pos) const
Definition: cx_stringlist.h:54
typename storage_t::reference reference
Definition: cx_stringlist.h:24
constexpr auto end()
Definition: cx_stringlist.h:49
constexpr vector(std::initializer_list< Value > init)
Definition: cx_stringlist.h:35
constexpr auto size() const
Definition: cx_stringlist.h:109
Value value_type
Definition: cx_stringlist.h:23
constexpr auto cend() const
Definition: cx_stringlist.h:52
constexpr auto begin()
Definition: cx_stringlist.h:43
constexpr Value & at(const std::size_t t_pos)
Definition: cx_stringlist.h:61
constexpr Value & push_back(Value t_v)
Definition: cx_stringlist.h:80
constexpr auto empty() const
Definition: cx_stringlist.h:110
Definition: cx_stringlist.h:17
constexpr const Value & back() const
Definition: cx_stringlist.h:91
constexpr const Value & at(const std::size_t t_pos) const
Definition: cx_stringlist.h:71
std::array< Value, Size > storage_t
Definition: cx_stringlist.h:19
constexpr auto operator+(vector< Value, S1 > a, vector< Value, S2 > b)
Definition: cx_stringlist.h:141
storage_t m_data
Definition: cx_stringlist.h:119
Definition: api.h:13
constexpr vector()=default
typename storage_t::iterator iterator
Definition: cx_stringlist.h:21
constexpr auto cbegin() const
Definition: cx_stringlist.h:51
constexpr auto capacity() const
Definition: cx_stringlist.h:108
std::size_t m_size
Definition: cx_stringlist.h:120
constexpr void clear()
Definition: cx_stringlist.h:112
constexpr auto begin() const
Definition: cx_stringlist.h:42