| 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 |
|
| 23 |
#ifndef FROZEN_LETITGO_BASIC_TYPES_H |
| 24 |
#define FROZEN_LETITGO_BASIC_TYPES_H |
| 25 |
|
| 26 |
#include "frozen/bits/exceptions.h" |
| 27 |
|
| 28 |
#include <array> |
| 29 |
#include <utility> |
| 30 |
#include <iterator> |
| 31 |
#include <string> |
| 32 |
|
| 33 |
namespace frozen { |
| 34 |
|
| 35 |
namespace bits { |
| 36 |
|
| 37 |
// used as a fake argument for frozen::make_set and frozen::make_map in the case of N=0 |
| 38 |
struct ignored_arg {}; |
| 39 |
|
| 40 |
template <class T, std::size_t N> class cvector { |
| 41 |
T data[N] = {}; // zero-initialization for scalar type T, default-initialized otherwise |
| 42 |
std::size_t dsize = 0; |
| 43 |
|
| 44 |
public: |
| 45 |
// Container typdefs |
| 46 |
using value_type = T; |
| 47 |
using reference = value_type &; |
| 48 |
using const_reference = const value_type &; |
| 49 |
using pointer = value_type *; |
| 50 |
using const_pointer = const value_type *; |
| 51 |
using iterator = pointer; |
| 52 |
using const_iterator = const_pointer; |
| 53 |
using size_type = std::size_t; |
| 54 |
using difference_type = std::ptrdiff_t; |
| 55 |
|
| 56 |
// Constructors |
| 57 |
constexpr cvector(void) = default; |
| 58 |
constexpr cvector(size_type count, const T &value) : dsize(count) { |
| 59 |
for (std::size_t i = 0; i < N; ++i) data[i] = value; |
| 60 |
} |
| 61 |
|
| 62 |
// Iterators |
| 63 |
constexpr iterator |
| 64 |
begin() noexcept { |
| 65 |
return data; |
| 66 |
} |
| 67 |
constexpr iterator |
| 68 |
end() noexcept { |
| 69 |
return data + dsize; |
| 70 |
} |
| 71 |
|
| 72 |
// Capacity |
| 73 |
constexpr size_type |
| 74 |
size() const { |
| 75 |
return dsize; |
| 76 |
} |
| 77 |
|
| 78 |
// Element access |
| 79 |
constexpr reference |
| 80 |
operator[](std::size_t index) { |
| 81 |
return data[index]; |
| 82 |
} |
| 83 |
constexpr const_reference |
| 84 |
operator[](std::size_t index) const { |
| 85 |
return data[index]; |
| 86 |
} |
| 87 |
|
| 88 |
constexpr reference |
| 89 |
back() { |
| 90 |
return data[dsize - 1]; |
| 91 |
} |
| 92 |
constexpr const_reference |
| 93 |
back() const { |
| 94 |
return data[dsize - 1]; |
| 95 |
} |
| 96 |
|
| 97 |
// Modifiers |
| 98 |
constexpr void |
| 99 |
push_back(const T &a) { |
| 100 |
data[dsize++] = a; |
| 101 |
} |
| 102 |
constexpr void |
| 103 |
push_back(T &&a) { |
| 104 |
data[dsize++] = std::move(a); |
| 105 |
} |
| 106 |
constexpr void |
| 107 |
pop_back() { |
| 108 |
--dsize; |
| 109 |
} |
| 110 |
|
| 111 |
constexpr void |
| 112 |
clear() { |
| 113 |
dsize = 0; |
| 114 |
} |
| 115 |
}; |
| 116 |
|
| 117 |
template <class T, std::size_t N> class carray { |
| 118 |
T data_[N] = {}; // zero-initialization for scalar type T, default-initialized otherwise |
| 119 |
|
| 120 |
template <std::size_t M, std::size_t... I> constexpr carray(T const (&init)[M], std::index_sequence<I...>) : data_{init[I]...} {} |
| 121 |
template <class Iter, std::size_t... I> constexpr carray(Iter iter, std::index_sequence<I...>) : data_{((void)I, *iter++)...} {} |
| 122 |
|
| 123 |
public: |
| 124 |
// Container typdefs |
| 125 |
using value_type = T; |
| 126 |
using reference = value_type &; |
| 127 |
using const_reference = const value_type &; |
| 128 |
using pointer = value_type *; |
| 129 |
using const_pointer = const value_type *; |
| 130 |
using iterator = pointer; |
| 131 |
using const_iterator = const_pointer; |
| 132 |
using reverse_iterator = std::reverse_iterator<iterator>; |
| 133 |
using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
| 134 |
using size_type = std::size_t; |
| 135 |
using difference_type = std::ptrdiff_t; |
| 136 |
|
| 137 |
// Constructors |
| 138 |
constexpr carray(void) = default; |
| 139 |
template <std::size_t M> constexpr carray(T const (&init)[M]) : carray(init, std::make_index_sequence<N>()) { |
| 140 |
static_assert(M >= N, "Cannot initialize a carray with an smaller array"); |
| 141 |
} |
| 142 |
template <std::size_t M> constexpr carray(std::array<T, M> const &init) : carray(&init[0], std::make_index_sequence<N>()) { |
| 143 |
static_assert(M >= N, "Cannot initialize a carray with an smaller array"); |
| 144 |
} |
| 145 |
constexpr carray(std::initializer_list<T> init) : carray(init.begin(), std::make_index_sequence<N>()) { |
| 146 |
// clang & gcc doesn't recognize init.size() as a constexpr |
| 147 |
// static_assert(init.size() >= N, "Cannot initialize a carray with an smaller initializer list"); |
| 148 |
} |
| 149 |
|
| 150 |
// Iterators |
| 151 |
constexpr iterator |
| 152 |
begin() noexcept { |
| 153 |
return data_; |
| 154 |
} |
| 155 |
constexpr const_iterator |
| 156 |
begin() const noexcept { |
| 157 |
return data_; |
| 158 |
} |
| 159 |
constexpr const_iterator |
| 160 |
cbegin() const noexcept { |
| 161 |
return data_; |
| 162 |
} |
| 163 |
constexpr iterator |
| 164 |
end() noexcept { |
| 165 |
return data_ + N; |
| 166 |
} |
| 167 |
constexpr const_iterator |
| 168 |
end() const noexcept { |
| 169 |
return data_ + N; |
| 170 |
} |
| 171 |
constexpr const_iterator |
| 172 |
cend() const noexcept { |
| 173 |
return data_ + N; |
| 174 |
} |
| 175 |
|
| 176 |
constexpr reverse_iterator |
| 177 |
rbegin() noexcept { |
| 178 |
return reverse_iterator(end()); |
| 179 |
} |
| 180 |
constexpr const_reverse_iterator |
| 181 |
rbegin() const noexcept { |
| 182 |
return const_reverse_iterator(end()); |
| 183 |
} |
| 184 |
constexpr const_reverse_iterator |
| 185 |
crbegin() const noexcept { |
| 186 |
return const_reverse_iterator(end()); |
| 187 |
} |
| 188 |
constexpr reverse_iterator |
| 189 |
rend() noexcept { |
| 190 |
return reverse_iterator(begin()); |
| 191 |
} |
| 192 |
constexpr const_reverse_iterator |
| 193 |
rend() const noexcept { |
| 194 |
return const_reverse_iterator(begin()); |
| 195 |
} |
| 196 |
constexpr const_reverse_iterator |
| 197 |
crend() const noexcept { |
| 198 |
return const_reverse_iterator(begin()); |
| 199 |
} |
| 200 |
|
| 201 |
// Capacity |
| 202 |
constexpr size_type |
| 203 |
size() const { |
| 204 |
return N; |
| 205 |
} |
| 206 |
constexpr size_type |
| 207 |
max_size() const { |
| 208 |
return N; |
| 209 |
} |
| 210 |
|
| 211 |
// Element access |
| 212 |
constexpr reference |
| 213 |
operator[](std::size_t index) { |
| 214 |
return data_[index]; |
| 215 |
} |
| 216 |
constexpr const_reference |
| 217 |
operator[](std::size_t index) const { |
| 218 |
return data_[index]; |
| 219 |
} |
| 220 |
|
| 221 |
constexpr reference |
| 222 |
at(std::size_t index) { |
| 223 |
if (index > N) FROZEN_THROW_OR_ABORT(std::out_of_range("Index (" + std::to_string(index) + ") out of bound (" + std::to_string(N) + ')')); |
| 224 |
return data_[index]; |
| 225 |
} |
| 226 |
constexpr const_reference |
| 227 |
at(std::size_t index) const { |
| 228 |
if (index > N) FROZEN_THROW_OR_ABORT(std::out_of_range("Index (" + std::to_string(index) + ") out of bound (" + std::to_string(N) + ')')); |
| 229 |
return data_[index]; |
| 230 |
} |
| 231 |
|
| 232 |
constexpr reference |
| 233 |
front() { |
| 234 |
return data_[0]; |
| 235 |
} |
| 236 |
constexpr const_reference |
| 237 |
front() const { |
| 238 |
return data_[0]; |
| 239 |
} |
| 240 |
|
| 241 |
constexpr reference |
| 242 |
back() { |
| 243 |
return data_[N - 1]; |
| 244 |
} |
| 245 |
constexpr const_reference |
| 246 |
back() const { |
| 247 |
return data_[N - 1]; |
| 248 |
} |
| 249 |
|
| 250 |
constexpr value_type * |
| 251 |
data() noexcept { |
| 252 |
return data_; |
| 253 |
} |
| 254 |
constexpr const value_type * |
| 255 |
data() const noexcept { |
| 256 |
return data_; |
| 257 |
} |
| 258 |
|
| 259 |
// Modifiers |
| 260 |
constexpr void |
| 261 |
fill(const value_type &val) { |
| 262 |
for (std::size_t i = 0; i < N; ++i) data_[i] = val; |
| 263 |
} |
| 264 |
}; |
| 265 |
template <class T> class carray<T, 0> { |
| 266 |
public: |
| 267 |
// Container typdefs |
| 268 |
using value_type = T; |
| 269 |
using reference = value_type &; |
| 270 |
using const_reference = const value_type &; |
| 271 |
using pointer = value_type *; |
| 272 |
using const_pointer = const value_type *; |
| 273 |
using iterator = pointer; |
| 274 |
using const_iterator = const_pointer; |
| 275 |
using reverse_iterator = std::reverse_iterator<iterator>; |
| 276 |
using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
| 277 |
using size_type = std::size_t; |
| 278 |
using difference_type = std::ptrdiff_t; |
| 279 |
|
| 280 |
// Constructors |
| 281 |
constexpr carray(void) = default; |
| 282 |
}; |
| 283 |
|
| 284 |
} // namespace bits |
| 285 |
|
| 286 |
} // namespace frozen |
| 287 |
|
| 288 |
#endif |