You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

basic_types.h 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Frozen
  3. * Copyright 2016 QuarksLab
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. */
  22. #ifndef FROZEN_LETITGO_BASIC_TYPES_H
  23. #define FROZEN_LETITGO_BASIC_TYPES_H
  24. #include "frozen/bits/exceptions.h"
  25. #include <utility>
  26. #include <iterator>
  27. #include <string>
  28. namespace frozen {
  29. namespace bits {
  30. // used as a fake argument for frozen::make_set and frozen::make_map in the case of N=0
  31. struct ignored_arg {};
  32. template <class T, std::size_t N>
  33. class cvector {
  34. T data [N] = {}; // zero-initialization for scalar type T, default-initialized otherwise
  35. std::size_t dsize = 0;
  36. public:
  37. // Container typdefs
  38. using value_type = T;
  39. using reference = value_type &;
  40. using const_reference = const value_type &;
  41. using pointer = value_type *;
  42. using const_pointer = const value_type *;
  43. using iterator = pointer;
  44. using const_iterator = const_pointer;
  45. using size_type = std::size_t;
  46. using difference_type = std::ptrdiff_t;
  47. // Constructors
  48. constexpr cvector(void) = default;
  49. constexpr cvector(size_type count, const T& value) : dsize(count) {
  50. for (std::size_t i = 0; i < N; ++i)
  51. data[i] = value;
  52. }
  53. // Iterators
  54. constexpr iterator begin() noexcept { return data; }
  55. constexpr iterator end() noexcept { return data + dsize; }
  56. // Capacity
  57. constexpr size_type size() const { return dsize; }
  58. // Element access
  59. constexpr reference operator[](std::size_t index) { return data[index]; }
  60. constexpr const_reference operator[](std::size_t index) const { return data[index]; }
  61. constexpr reference back() { return data[dsize - 1]; }
  62. constexpr const_reference back() const { return data[dsize - 1]; }
  63. // Modifiers
  64. constexpr void push_back(const T & a) { data[dsize++] = a; }
  65. constexpr void push_back(T && a) { data[dsize++] = std::move(a); }
  66. constexpr void pop_back() { --dsize; }
  67. constexpr void clear() { dsize = 0; }
  68. };
  69. template <class T, std::size_t N>
  70. class carray {
  71. T data_ [N] = {}; // zero-initialization for scalar type T, default-initialized otherwise
  72. template <std::size_t M, std::size_t... I>
  73. constexpr carray(T const (&init)[M], std::index_sequence<I...>)
  74. : data_{init[I]...} {}
  75. template <class Iter, std::size_t... I>
  76. constexpr carray(Iter iter, std::index_sequence<I...>)
  77. : data_{((void)I, *iter++)...} {}
  78. public:
  79. // Container typdefs
  80. using value_type = T;
  81. using reference = value_type &;
  82. using const_reference = const value_type &;
  83. using pointer = value_type *;
  84. using const_pointer = const value_type *;
  85. using iterator = pointer;
  86. using const_iterator = const_pointer;
  87. using reverse_iterator = std::reverse_iterator<iterator>;
  88. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  89. using size_type = std::size_t;
  90. using difference_type = std::ptrdiff_t;
  91. // Constructors
  92. constexpr carray(void) = default;
  93. template <std::size_t M>
  94. constexpr carray(T const (&init)[M])
  95. : carray(init, std::make_index_sequence<N>())
  96. {
  97. static_assert(M >= N, "Cannot initialize a carray with an smaller array");
  98. }
  99. constexpr carray(std::initializer_list<T> init)
  100. : carray(init.begin(), std::make_index_sequence<N>())
  101. {
  102. // clang & gcc doesn't recognize init.size() as a constexpr
  103. // static_assert(init.size() >= N, "Cannot initialize a carray with an smaller initializer list");
  104. }
  105. // Iterators
  106. constexpr iterator begin() noexcept { return data_; }
  107. constexpr const_iterator begin() const noexcept { return data_; }
  108. constexpr const_iterator cbegin() const noexcept { return data_; }
  109. constexpr iterator end() noexcept { return data_ + N; }
  110. constexpr const_iterator end() const noexcept { return data_ + N; }
  111. constexpr const_iterator cend() const noexcept { return data_ + N; }
  112. constexpr reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
  113. constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
  114. constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
  115. constexpr reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
  116. constexpr const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
  117. constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
  118. // Capacity
  119. constexpr size_type size() const { return N; }
  120. constexpr size_type max_size() const { return N; }
  121. // Element access
  122. constexpr reference operator[](std::size_t index) { return data_[index]; }
  123. constexpr const_reference operator[](std::size_t index) const { return data_[index]; }
  124. constexpr reference at(std::size_t index) {
  125. if (index > N)
  126. FROZEN_THROW_OR_ABORT(std::out_of_range("Index (" + std::to_string(index) + ") out of bound (" + std::to_string(N) + ')'));
  127. return data_[index];
  128. }
  129. constexpr const_reference at(std::size_t index) const {
  130. if (index > N)
  131. FROZEN_THROW_OR_ABORT(std::out_of_range("Index (" + std::to_string(index) + ") out of bound (" + std::to_string(N) + ')'));
  132. return data_[index];
  133. }
  134. constexpr reference front() { return data_[0]; }
  135. constexpr const_reference front() const { return data_[0]; }
  136. constexpr reference back() { return data_[N - 1]; }
  137. constexpr const_reference back() const { return data_[N - 1]; }
  138. constexpr value_type* data() noexcept { return data_; }
  139. constexpr const value_type* data() const noexcept { return data_; }
  140. // Modifiers
  141. constexpr void fill(const value_type& val) {
  142. for (std::size_t i = 0; i < N; ++i)
  143. data_[i] = val;
  144. }
  145. };
  146. template <class T>
  147. class carray<T, 0> {
  148. public:
  149. // Container typdefs
  150. using value_type = T;
  151. using reference = value_type &;
  152. using const_reference = const value_type &;
  153. using pointer = value_type *;
  154. using const_pointer = const value_type *;
  155. using iterator = pointer;
  156. using const_iterator = const_pointer;
  157. using reverse_iterator = std::reverse_iterator<iterator>;
  158. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  159. using size_type = std::size_t;
  160. using difference_type = std::ptrdiff_t;
  161. // Constructors
  162. constexpr carray(void) = default;
  163. };
  164. } // namespace bits
  165. } // namespace frozen
  166. #endif