/
partial_array.h++
81 lines (67 loc) · 2.98 KB
/
partial_array.h++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Ogonek
//
// Written in 2012-2013 by Martinho Fernandes <martinho.fernandes@gmail.com>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
//
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
// Vector like container backed by a fixed size array (fixed capacity)
#ifndef OGONEK_DETAIL_CONTAINER_PARTIAL_ARRAY_HPP
#define OGONEK_DETAIL_CONTAINER_PARTIAL_ARRAY_HPP
#include <algorithm>
#include <array>
#include <initializer_list>
#include <cstddef>
namespace ogonek {
namespace detail {
template <typename T, std::size_t N>
struct partial_array {
static_assert(std::is_pod<T>::value, "T must be POD");
public:
partial_array() = default;
template <typename U>
partial_array(partial_array<U, N> const& that)
: count{ that.count }, array() {
std::copy(that.begin(), that.end(), array.begin());
}
partial_array(std::array<T, N> const& array, std::size_t count)
: count{ count }, array(array) {}
partial_array(std::initializer_list<T> list)
: count{ list.size() }, array() {
std::copy(list.begin(), list.end(), array.begin());
}
template <typename Iterator>
partial_array(Iterator first, Iterator last)
: count(last - first), array() {
std::copy(first, last, array.begin());
}
void push_back(T const& item) { array[count++] = item; }
void clear() { count = 0; }
using iterator = typename std::array<T, N>::iterator;
using const_iterator = typename std::array<T, N>::const_iterator;
iterator begin() { return array.begin(); }
const_iterator begin() const { return array.begin(); }
iterator end() { return array.begin() + count; }
const_iterator end() const { return array.begin() + count; }
T* data() { return array.data(); }
T const* data() const { return array.data(); }
std::size_t size() const { return count; }
T& operator[](std::ptrdiff_t index) { return array[index]; }
T const& operator[](std::ptrdiff_t index) const { return array[index]; }
iterator erase(iterator first) {
auto next = std::next(first);
std::move(next, end(), first);
--count;
return first;
}
private:
std::size_t count = 0;
std::array<T, N> array;
};
} // namespace detail
} // namespace ogonek
#endif // OGONEK_DETAIL_CONTAINER_PARTIAL_ARRAY_HPP