Line |
Branch |
Exec |
Source |
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 |
|
|
#include "frozen/bits/constexpr_assert.h" |
28 |
|
|
|
29 |
|
|
#include <array> |
30 |
|
|
#include <utility> |
31 |
|
|
#include <string> |
32 |
|
|
#include <type_traits> |
33 |
|
|
|
34 |
|
|
namespace frozen { |
35 |
|
|
|
36 |
|
|
namespace bits { |
37 |
|
|
|
38 |
|
|
// used as a fake argument for frozen::make_set and frozen::make_map in the case of N=0 |
39 |
|
|
struct ignored_arg {}; |
40 |
|
|
|
41 |
|
|
template <class T, std::size_t N> |
42 |
|
|
class cvector { |
43 |
|
|
T data [N] = {}; // zero-initialization for scalar type T, default-initialized otherwise |
44 |
|
|
std::size_t dsize = 0; |
45 |
|
|
|
46 |
|
|
public: |
47 |
|
|
// Container typdefs |
48 |
|
|
using value_type = T; |
49 |
|
|
using reference = value_type &; |
50 |
|
|
using const_reference = const value_type &; |
51 |
|
|
using pointer = value_type *; |
52 |
|
|
using const_pointer = const value_type *; |
53 |
|
|
using iterator = pointer; |
54 |
|
|
using const_iterator = const_pointer; |
55 |
|
|
using size_type = std::size_t; |
56 |
|
|
using difference_type = std::ptrdiff_t; |
57 |
|
|
|
58 |
|
|
// Constructors |
59 |
|
|
constexpr cvector(void) = default; |
60 |
|
|
constexpr cvector(size_type count, const T& value) : dsize(count) { |
61 |
|
|
for (std::size_t i = 0; i < N; ++i) |
62 |
|
|
data[i] = value; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
// Iterators |
66 |
|
|
constexpr iterator begin() noexcept { return data; } |
67 |
|
|
constexpr iterator end() noexcept { return data + dsize; } |
68 |
|
|
constexpr const_iterator begin() const noexcept { return data; } |
69 |
|
|
constexpr const_iterator end() const noexcept { return data + dsize; } |
70 |
|
|
|
71 |
|
|
// Capacity |
72 |
|
|
constexpr size_type size() const { return dsize; } |
73 |
|
|
|
74 |
|
|
// Element access |
75 |
|
|
constexpr reference operator[](std::size_t index) { return data[index]; } |
76 |
|
|
constexpr const_reference operator[](std::size_t index) const { return data[index]; } |
77 |
|
|
|
78 |
|
|
constexpr reference back() { return data[dsize - 1]; } |
79 |
|
|
constexpr const_reference back() const { return data[dsize - 1]; } |
80 |
|
|
|
81 |
|
|
// Modifiers |
82 |
|
|
constexpr void push_back(const T & a) { data[dsize++] = a; } |
83 |
|
|
constexpr void push_back(T && a) { data[dsize++] = std::move(a); } |
84 |
|
|
constexpr void pop_back() { --dsize; } |
85 |
|
|
|
86 |
|
|
constexpr void clear() { dsize = 0; } |
87 |
|
|
}; |
88 |
|
|
|
89 |
|
|
template <class T, std::size_t N> |
90 |
|
|
class carray { |
91 |
|
|
T data_ [N] = {}; // zero-initialization for scalar type T, default-initialized otherwise |
92 |
|
|
|
93 |
|
|
template <class Iter, std::size_t... I> |
94 |
|
|
constexpr carray(Iter iter, std::index_sequence<I...>) |
95 |
|
|
: data_{((void)I, *iter++)...} {} |
96 |
|
|
template <std::size_t... I> |
97 |
|
|
constexpr carray(const T& value, std::index_sequence<I...>) |
98 |
|
|
: data_{((void)I, value)...} {} |
99 |
|
|
|
100 |
|
|
static constexpr void check_initializer(std::initializer_list<T> init) { |
101 |
|
|
(void)init; |
102 |
|
|
constexpr_assert(init.size() == N, "Cannot initialize a carray with an initializer list of different size."); |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
public: |
106 |
|
|
// Container typdefs |
107 |
|
|
using value_type = T; |
108 |
|
|
using reference = value_type &; |
109 |
|
|
using const_reference = const value_type &; |
110 |
|
|
using pointer = value_type *; |
111 |
|
|
using const_pointer = const value_type *; |
112 |
|
|
using iterator = pointer; |
113 |
|
|
using const_iterator = const_pointer; |
114 |
|
|
using size_type = std::size_t; |
115 |
|
|
using difference_type = std::ptrdiff_t; |
116 |
|
|
|
117 |
|
|
// Constructors |
118 |
|
|
constexpr carray() = default; |
119 |
|
|
constexpr carray(const value_type& val) |
120 |
|
|
: carray(val, std::make_index_sequence<N>()) {} |
121 |
|
|
template <typename U, std::enable_if_t<std::is_convertible<U, T>::value, std::size_t> M> |
122 |
|
|
constexpr carray(U const (&init)[M]) |
123 |
|
|
: carray(init, std::make_index_sequence<N>()) |
124 |
|
|
{ |
125 |
|
|
static_assert(M >= N, "Cannot initialize a carray with an smaller array"); |
126 |
|
|
} |
127 |
|
|
template <typename U, std::enable_if_t<std::is_convertible<U, T>::value, std::size_t> M> |
128 |
|
|
constexpr carray(std::array<U, M> const &init) |
129 |
|
|
: carray(init.begin(), std::make_index_sequence<N>()) |
130 |
|
|
{ |
131 |
|
|
static_assert(M >= N, "Cannot initialize a carray with an smaller array"); |
132 |
|
|
} |
133 |
|
|
template <typename U, std::enable_if_t<std::is_convertible<U, T>::value>* = nullptr> |
134 |
|
|
constexpr carray(std::initializer_list<U> init) |
135 |
|
|
: carray((check_initializer(init), init.begin()), std::make_index_sequence<N>()) |
136 |
|
|
{ |
137 |
|
|
} |
138 |
|
|
template <typename U, std::enable_if_t<std::is_convertible<U, T>::value>* = nullptr> |
139 |
|
|
constexpr carray(const carray<U, N>& rhs) |
140 |
|
|
: carray(rhs.begin(), std::make_index_sequence<N>()) |
141 |
|
|
{ |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
// Iterators |
145 |
|
|
constexpr iterator begin() noexcept { return data_; } |
146 |
|
150205 |
constexpr const_iterator begin() const noexcept { return data_; } |
147 |
|
|
constexpr iterator end() noexcept { return data_ + N; } |
148 |
|
392714 |
constexpr const_iterator end() const noexcept { return data_ + N; } |
149 |
|
|
|
150 |
|
|
// Capacity |
151 |
|
|
constexpr size_type size() const { return N; } |
152 |
|
|
constexpr size_type max_size() const { return N; } |
153 |
|
|
|
154 |
|
|
// Element access |
155 |
|
|
constexpr reference operator[](std::size_t index) { return data_[index]; } |
156 |
|
195670 |
constexpr const_reference operator[](std::size_t index) const { return data_[index]; } |
157 |
|
|
|
158 |
|
|
constexpr reference at(std::size_t index) { |
159 |
|
|
if (index > N) |
160 |
|
|
FROZEN_THROW_OR_ABORT(std::out_of_range("Index (" + std::to_string(index) + ") out of bound (" + std::to_string(N) + ')')); |
161 |
|
|
return data_[index]; |
162 |
|
|
} |
163 |
|
|
constexpr const_reference at(std::size_t index) const { |
164 |
|
|
if (index > N) |
165 |
|
|
FROZEN_THROW_OR_ABORT(std::out_of_range("Index (" + std::to_string(index) + ") out of bound (" + std::to_string(N) + ')')); |
166 |
|
|
return data_[index]; |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
constexpr reference front() { return data_[0]; } |
170 |
|
|
constexpr const_reference front() const { return data_[0]; } |
171 |
|
|
|
172 |
|
|
constexpr reference back() { return data_[N - 1]; } |
173 |
|
|
constexpr const_reference back() const { return data_[N - 1]; } |
174 |
|
|
|
175 |
|
|
constexpr value_type* data() noexcept { return data_; } |
176 |
|
|
constexpr const value_type* data() const noexcept { return data_; } |
177 |
|
|
}; |
178 |
|
|
template <class T> |
179 |
|
|
class carray<T, 0> { |
180 |
|
|
|
181 |
|
|
public: |
182 |
|
|
// Container typdefs |
183 |
|
|
using value_type = T; |
184 |
|
|
using reference = value_type &; |
185 |
|
|
using const_reference = const value_type &; |
186 |
|
|
using pointer = value_type *; |
187 |
|
|
using const_pointer = const value_type *; |
188 |
|
|
using iterator = pointer; |
189 |
|
|
using const_iterator = const_pointer; |
190 |
|
|
using size_type = std::size_t; |
191 |
|
|
using difference_type = std::ptrdiff_t; |
192 |
|
|
|
193 |
|
|
// Constructors |
194 |
|
|
constexpr carray(void) = default; |
195 |
|
|
|
196 |
|
|
}; |
197 |
|
|
|
198 |
|
|
} // namespace bits |
199 |
|
|
|
200 |
|
|
} // namespace frozen |
201 |
|
|
|
202 |
|
|
#endif |
203 |
|
|
|