neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
deque.hpp
Go to the documentation of this file.
1// deque.hpp
2/*
3 * Copyright (c) 2007 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <neolib/neolib.hpp>
39#include <deque>
43
44namespace neolib
45{
46 template <typename T>
47 class deque : public reference_counted<i_deque<abstract_t<T>>>
48 {
49 typedef deque<T> self_type;
51 // types
52 public:
54 typedef T value_type;
56 typedef std::deque<value_type> std_type;
57 using typename abstract_type::size_type;
58 using typename abstract_type::const_iterator;
59 using typename abstract_type::iterator;
61 protected:
64 protected:
67 // construction
68 public:
69 deque() {}
70 deque(const deque& aOther) :
71 iDeque(aOther.iDeque) {}
72 deque(deque&& aOther) :
73 iDeque(std::move(aOther.iDeque)) {}
75 {
76 assign(aOther);
77 }
78 deque(const std_type& aOtherContainer) :
79 iDeque(aOtherContainer) {}
80 deque(std::initializer_list<value_type> aValues) :
81 iDeque{ aValues } {}
82 template <typename InputIter>
83 deque(InputIter aFirst, InputIter aLast) :
84 iDeque(aFirst, aLast) {}
85 deque& operator=(const deque& aOther)
86 {
87 assign(aOther);
88 return *this;
89 }
90 deque& operator=(deque&& aOther)
91 {
92 iDeque = std::move(aOther.iDeque);
93 return *this;
94 }
96 {
97 assign(aOther);
98 return *this;
99 }
100 // operations
101 public:
102 const std_type& as_std_deque() const
103 {
104 return iDeque;
105 }
107 {
108 return iDeque;
109 }
111 {
112 return iDeque;
113 }
114 template <typename... Args>
115 iterator emplace(const_iterator aPos, Args&&... aArgs)
116 {
117 auto newPos = iDeque.emplace(iDeque.begin() + (aPos - abstract_type::cbegin()), std::forward<Args>(aArgs)...);
118 return abstract_type::begin() + (newPos - iDeque.begin());
119 }
120 // comparison
121 public:
122 constexpr bool operator==(const self_type& that) const noexcept
123 {
124 return as_std_deque() == that.as_std_deque();
125 }
126 constexpr std::partial_ordering operator<=>(const self_type& that) const noexcept
127 {
128 return as_std_deque() <=> that.as_std_deque();
129 }
130 // implementation
131 // from i_container
132 public:
133 size_type size() const noexcept final
134 {
135 return iDeque.size();
136 }
137 size_type max_size() const noexcept final
138 {
139 return iDeque.max_size();
140 }
141 void clear() final
142 {
143 iDeque.clear();
144 }
145 void assign(const generic_container_type& aOther) final
146 {
147 if (&aOther == this)
148 return; clear();
149 reserve(aOther.size());
150 std::copy(aOther.begin(), aOther.end(), std::back_insert_iterator{ iDeque });
151 }
152 // from i_container
153 private:
154 abstract_const_iterator* do_begin(void* memory) const final
155 {
156 return new (memory) container_const_iterator(iDeque.begin());
157 }
158 abstract_const_iterator* do_end(void* memory) const final
159 {
160 return new (memory) container_const_iterator(iDeque.end());
161 }
162 abstract_iterator* do_begin(void* memory) final
163 {
164 return new (memory) container_iterator(iDeque.begin());
165 }
166 abstract_iterator* do_end(void* memory) final
167 {
168 return new (memory) container_iterator(iDeque.end());
169 }
170 abstract_iterator* do_erase(void* memory, const abstract_const_iterator& aPosition) final
171 {
172 return new (memory) container_iterator(iDeque.erase(static_cast<const container_const_iterator&>(aPosition)));
173 }
174 abstract_iterator* do_erase(void* memory, const abstract_const_iterator& aFirst, const abstract_const_iterator& aLast) final
175 {
176 return new (memory) container_iterator(iDeque.erase(static_cast<const container_const_iterator&>(aFirst), static_cast<const container_const_iterator&>(aLast)));
177 }
178 // from i_sequence_container
179 public:
180 size_type capacity() const final
181 {
182 return iDeque.max_size();
183 }
184 void reserve(size_type aCapacity) final
185 {
186 /* do nothing */
187 }
188 void resize(size_type aSize) final
189 {
190 if constexpr (std::is_default_constructible_v<value_type>)
191 iDeque.resize(aSize);
192 else if (aSize <= size())
193 iDeque.erase(std::next(iDeque.begin(), aSize), iDeque.end());
194 else
195 throw std::logic_error{ "neolib::deque::value_type not default constructible" };
196 }
197 void resize(size_type aSize, const abstract_value_type& aValue) final
198 {
199 iDeque.resize(aSize, aValue);
200 }
201 void push_front(const abstract_value_type& aValue) final
202 {
203 iDeque.push_front(aValue);
204 }
205 template <typename... Args>
206 void emplace_front(Args&&... aArgs)
207 {
208 iDeque.emplace_front(std::forward<Args>(aArgs)...);
209 }
210 void pop_front() final
211 {
212 iDeque.pop_front();
213 }
214 void push_back(const abstract_value_type& aValue) final
215 {
216 iDeque.push_back(aValue);
217 }
218 template <typename... Args>
219 void emplace_back(Args&&... aArgs)
220 {
221 iDeque.emplace_back(std::forward<Args>(aArgs)...);
222 }
223 void pop_back() final
224 {
225 iDeque.pop_back();
226 }
227 const value_type& front() const final
228 {
229 return iDeque.front();
230 }
232 {
233 return iDeque.front();
234 }
235 const value_type& back() const final
236 {
237 return iDeque.back();
238 }
239 value_type& back() final
240 {
241 return iDeque.back();
242 }
243 // from i_random_access_container
244 public:
245 const value_type& at(size_type aIndex) const final
246 {
247 return iDeque.at(aIndex);
248 }
249 value_type& at(size_type aIndex) final
250 {
251 return iDeque.at(aIndex);
252 }
253 const value_type& operator[](size_type aIndex) const final
254 {
255 return iDeque[aIndex];
256 }
258 {
259 return iDeque[aIndex];
260 }
261 // from i_sequence_container
262 private:
263 abstract_iterator* do_insert(void* memory, const abstract_const_iterator& aPosition, const abstract_value_type& aValue) final
264 {
265 return new (memory) container_iterator(iDeque.insert(static_cast<const container_const_iterator&>(aPosition), aValue));
266 }
267 // attributes
268 private:
269 std::deque<value_type> iDeque;
270 };
271}
const value_type & at(size_type aIndex) const final
Definition deque.hpp:245
const std_type & as_std_deque() const
Definition deque.hpp:102
value_type & at(size_type aIndex) final
Definition deque.hpp:249
void pop_back() final
Definition deque.hpp:223
std::deque< value_type > std_type
Definition deque.hpp:56
void emplace_front(Args &&... aArgs)
Definition deque.hpp:206
size_type capacity() const final
Definition deque.hpp:180
void pop_front() final
Definition deque.hpp:210
deque(deque &&aOther)
Definition deque.hpp:72
void push_front(const abstract_value_type &aValue) final
Definition deque.hpp:201
deque(InputIter aFirst, InputIter aLast)
Definition deque.hpp:83
const value_type & back() const final
Definition deque.hpp:235
deque(const std_type &aOtherContainer)
Definition deque.hpp:78
deque(std::initializer_list< value_type > aValues)
Definition deque.hpp:80
iterator emplace(const_iterator aPos, Args &&... aArgs)
Definition deque.hpp:115
abstract_t< T > abstract_value_type
Definition deque.hpp:55
std_type & as_std_deque()
Definition deque.hpp:106
i_deque< abstract_t< T > > abstract_type
Definition deque.hpp:53
std_type to_std_deque() const
Definition deque.hpp:110
size_type max_size() const noexcept final
Definition deque.hpp:137
constexpr std::partial_ordering operator<=>(const self_type &that) const noexcept
Definition deque.hpp:126
void resize(size_type aSize, const abstract_value_type &aValue) final
Definition deque.hpp:197
deque & operator=(deque &&aOther)
Definition deque.hpp:90
void clear() final
Definition deque.hpp:141
void reserve(size_type aCapacity) final
Definition deque.hpp:184
void push_back(const abstract_value_type &aValue) final
Definition deque.hpp:214
const value_type & operator[](size_type aIndex) const final
Definition deque.hpp:253
container::random_access_iterator< T, typename std_type::iterator, typename std_type::const_iterator > container_iterator
Definition deque.hpp:66
deque(const deque &aOther)
Definition deque.hpp:70
deque & operator=(const i_deque< abstract_value_type > &aOther)
Definition deque.hpp:95
size_type size() const noexcept final
Definition deque.hpp:133
deque & operator=(const deque &aOther)
Definition deque.hpp:85
deque(const i_deque< abstract_value_type > &aOther)
Definition deque.hpp:74
container::random_access_const_iterator< T, typename std_type::const_iterator > container_const_iterator
Definition deque.hpp:65
value_type & front() final
Definition deque.hpp:231
constexpr bool operator==(const self_type &that) const noexcept
Definition deque.hpp:122
value_type & operator[](size_type aIndex) final
Definition deque.hpp:257
void assign(const generic_container_type &aOther) final
Definition deque.hpp:145
const value_type & front() const final
Definition deque.hpp:227
void emplace_back(Args &&... aArgs)
Definition deque.hpp:219
value_type & back() final
Definition deque.hpp:239
void resize(size_type aSize) final
Definition deque.hpp:188
IteratorType abstract_iterator
const_iterator begin() const
abstract_const_iterator::iterator_wrapper const_iterator
const_iterator cbegin() const
abstract_iterator::iterator_wrapper iterator
ConstIteratorType abstract_const_iterator
i_container< T, ConstIteratorType, IteratorType > generic_container_type
typename detail::abstract_type< T >::type abstract_t
Definition neolib.hpp:178
Definition plf_hive.h:79
it_type next(it_type it, const typename iterator_traits< it_type >::difference_type distance=1)
Definition plf_hive.h:89