Debian Codegraph @

common/include/countarray.h

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7 #pragma once
8
9 #include <algorithm>
10 #include <cstddef>
11 #include <memory>
12 #include <stdexcept>
13 #include <type_traits>
14 #include "dxxsconf.h"
15 #include <array>
16
17 namespace d_count_array {
18
19 namespace detail {
20
21 template <typename count_type>
22 class count_storage
23 {
24 protected:
25 using size_type = count_type;
26 size_type m_count{};
27 public:
28 constexpr size_type size() const { return m_count; }
29 constexpr bool empty() const { return !m_count; }
30 };
31
32 template <std::size_t size>
33 using base_count_value = count_storage<typename std::conditional_t<
34 (size <= UINT8_MAX), uint8_t,
35 typename std::conditional_t<(size <= UINT16_MAX), uint16_t, uint32_t>
36 >>;
37
38 }
39
40 }
41
42 template <typename T, std::size_t S>
43 class count_array_t : public d_count_array::detail::base_count_value<S>
44 {
45 using base_count_value = d_count_array::detail::base_count_value<S>;
46 using base_count_value::m_count;
47 public:
48 using array_type = std::array<T, S>;
49 using value_type = typename array_type::value_type;
50 using iterator = typename array_type::iterator;
51 using const_iterator = typename array_type::const_iterator;
52 using const_reference = typename array_type::const_reference;
53 static constexpr typename array_type::size_type max_size() { return {S}; }
54 auto data() const { return arrayref().data(); }
55 ~count_array_t() { clear(); }
56 count_array_t &operator=(const count_array_t &rhs)
57 {
58 if (this != &rhs)
59 {
60 iterator lb = begin();
61 const_iterator rb = rhs.begin();
62 const const_iterator re{rhs.end()};
63 for (const auto le{end()}; lb != le && rb != re; ++lb, ++rb)
64 *lb = *rb;
65 shrink(lb);
66 for (; rb != re; ++rb)
67 emplace_back(*rb);
68 }
69 return *this;
70 }
71 template <typename... Args>
72 void emplace_back(Args&&... args)
73 {
74 if (m_count >= S)
75 throw std::length_error("too many elements");
76 T *uninitialized = static_cast<T *>(&arrayref()[m_count]);
77 new(static_cast<void *>(uninitialized)) T(std::forward<Args>(args)...);
78 ++ m_count;
79 }
80 void clear()
81 {
82 shrink(begin());
83 }
84 // for std::back_insert_iterator
85 void push_back(const T& t)
86 {
87 emplace_back(t);
88 }
89 void pop_back()
90 {
91 shrink(end() - 1);
92 }
93 iterator begin() { return arrayref().begin(); }
94 iterator end() { return std::next(begin(), m_count); }
95 iterator find(const T &t) { return std::find(begin(), end(), t); }
96 const_iterator find(const T &t) const { return std::find(begin(), end(), t); }
97 const_reference operator[](const typename base_count_value::size_type i) const
98 {
99 if (i >= m_count)
100 throw std::out_of_range("not enough elements");
101 return arrayref()[i];
102 }
103 const_reference back() const { return (*this)[m_count - 1]; }
104 bool contains(const T &t) const { return find(t) != end(); }
105 void erase(iterator i)
106 {
107 shrink(i);
108 }
109 void erase(const T &t)
110 {
111 shrink(std::remove(begin(), end(), t));
112 }
113 template <typename F>
114 void erase_if(F &&f)
115 {
116 shrink(std::remove_if(begin(), end(), std::forward<F>(f)));
117 }
118 void replace(const T &o, const T &n)
119 {
120 std::replace(begin(), end(), o, n);
121 }
122 const_iterator begin() const { return arrayref().begin(); }
123 const_iterator end() const { return std::next(begin(), m_count); }
124 private:
125 static void destroy(const iterator b, iterator e)
126 {
127 if constexpr (std::is_trivially_destructible_v<T>)
128 {
129 (void)b;
130 (void)e;
131 }
132 else
133 {
134 for (; b != e;)
135 (-- e)->~T();
136 }
137 }
138 void shrink(const iterator b)
139 {
140 destroy(b, end());
141 m_count = std::distance(begin(), b);
142 }
143 union U {
144 array_type m_data;
145 U() {}
146 } u;
147 array_type &arrayref() { return u.m_data; }
148 const array_type &arrayref() const { return u.m_data; }
149 };