PeriDyno 1.0.0
Loading...
Searching...
No Matches
RandomAccessContainer.inl
Go to the documentation of this file.
1#include "type_traits.h"
2namespace dyno {
3
4 template <typename T>
5 template <typename InputIterator>
6 DYN_FUNC void RandomAccessContainer<T>::assign(InputIterator first, InputIterator last) {
7
8 assert((size_type)(last - first) <= m_maxSize);
9 iterator position(m_startLoc);
10
11 while ((position != m_End) && (first != last))
12 {
13 *position = *first;
14 ++first;
15 ++position;
16 }
17 if (first == last)
18 erase(position, m_End);
19 else
20 insert(m_End, first, last);
21 }
22
23 template<typename T>
24 DYN_FUNC void RandomAccessContainer<T>::assign(size_type n, const value_type& value) {
25 assert(n <= m_maxSize);
26 iterator position(m_startLoc);
27 size_type index = 0;
28 while ((position != m_End) && (index != n))
29 {
30 *position = value_type(value);
31 ++index;
32 ++position;
33 }
34 if (index == n)
35 erase(position, m_End);
36 else
37 insert(m_End, n - index, value);
38 }
39
40
41 template<typename T>
42 DYN_FUNC inline void RandomAccessContainer<T>::reserve(iterator beg, size_type buffer_size) {
43 m_startLoc = beg;
44 m_maxSize = buffer_size;
45 m_bufferEnd = this->bufferEnd();
46 m_End = beg;
47 m_Begin = beg;
48 m_size = 0;
49 }
50
51 template<typename T>
52 DYN_FUNC inline void RandomAccessContainer<T>::reserve(iterator beg, iterator end, size_type buffer_size) {
53 m_startLoc = beg;
54 m_maxSize = buffer_size ? buffer_size: (size_type)(end - beg);
55 m_bufferEnd = this->bufferEnd();
56 m_End = end;
57 m_Begin = beg;
58 m_size = (size_type)(end - beg);
59 }
60
61 template<typename T>
63 assert(n <= m_maxSize);
64 if (m_size == n)
65 return;
66
67 if (n > (size_type)(m_End - m_Begin)) {
68 //push back default value_type
69 while(n > (size_type)(m_End - m_Begin))
70 push_back();
71 }
72 else {
73 //destory
74 for (iterator beg = m_Begin + n; beg != m_End; ++beg)
75 beg->~value_type();
76 }
77 m_End = m_Begin + n;
78 m_size = n;
79 }
80
81 template<typename T>
87
88 template<typename T>
91 assert(n < m_size);
92 return *(m_Begin + n);
93 }
94
95 template<typename T>
98 assert(n < m_size); //although this should be changed as a throw.
99 return *(m_Begin + n);
100 }
101
102 template<typename T>
105 assert(n < m_size); //although this should be changed as a throw.
106 return *(m_Begin + n);
107 }
108
109 template<typename T>
112 assert(m_startLoc != nullptr && m_size > 0);
113 // The second assertion is because if one get a empty container and assign to its front (though the reference is vaild),
114 // the size will be change from 0->1, but front routine will not able to change m_size.
115 return *(m_Begin);
116 }
117
118 template<typename T>
121 assert(m_startLoc != nullptr && m_size > 0);
122 return *(m_Begin);
123 }
124
125 template<typename T>
128 assert(m_startLoc != nullptr && m_size > 0);
129 return *(m_End - 1);
130 }
131
132 template<typename T>
135 assert(m_startLoc != nullptr && m_size > 0);
136 return *(m_End - 1);
137 }
138
139 template<typename T>
142 *m_End = value_type(value);
143 ++m_End;
144 ++m_size;
145 }
146
147
148 template<typename T>
155
156 template<typename T>
160 value_type tmp = value_type();
161 push_back(tmp);
162 return *(m_End - 1);
163 }
164
165 template<typename T>
167 assert(m_size != 0);
168 --m_End;
169 m_End->~value_type(); //destruct
170 --m_size;
171 }
172
173 //inplace move
174 template <typename InputIterator, typename OutputIterator>
175 DYN_FUNC static OutputIterator move_or_copy(InputIterator first, InputIterator last, OutputIterator result)
176 {
177 for (; first != last; ++result, ++first)
178 *result = *first;
179 return result;
180 }
181
182 //inplace back_move
183 template <typename Iterator>
184 DYN_FUNC static Iterator move_or_copy_backward(Iterator first, Iterator last, Iterator resultEnd)
185 {
186 while (first != last)
187 *--resultEnd = *--last;
188 return resultEnd;
189 }
190
191 template<typename T>
195 assert(position >= m_Begin && position <= m_End);
196 push_back();
197 const difference_type n = position - m_Begin;
198 iterator destPosition = const_cast<value_type*>(position);
199 dyno::move_or_copy_backward(destPosition, m_End - 1, m_End);
200 *destPosition = value_type(value);
201 return m_Begin + n;
202 }
203
204
205 template<typename T>
209 assert(position >= m_Begin && position <= m_End);
210 push_back();
211 const difference_type n = position - m_Begin;
212 iterator destPosition = const_cast<value_type*>(position);
213 dyno::move_or_copy_backward(destPosition, m_End - 1, m_End);
214 *destPosition = value_type(dyno::forward<value_type>(value));
215 return m_Begin + n;
216 }
217
218
219 template<typename T>
222 assert((m_size + n) <= m_maxSize);
223 assert(position >= m_Begin && position <= m_End);
224 const difference_type p = position - m_Begin;
225 if (n > 0) {
226 for (int i = 0; i < n; ++i)
227 push_back();
228 iterator destPosition = const_cast<value_type*>(position);
229 dyno::move_or_copy_backward(destPosition, m_End - n, m_End);
230
231 for (; destPosition != (position + n); ++destPosition) {
232 *destPosition = value_type(value);
233 }
234 }
235 return m_Begin + p;
236 }
237
238 template<typename T>
239 template <typename InputIterator>
241 RandomAccessContainer<T>::insert(const_iterator position, InputIterator first, InputIterator last) {
242 assert((m_size + (size_type)(last - first)) <= m_maxSize);
243 assert(position >= m_Begin && position <= m_End);
244 const difference_type p = position - m_Begin;
245 size_type n = (size_type)(last - first);
246 if (n > 0) {
247 size_type i = 0;
248 for (size_type i = 0; i < n; ++i)
249 push_back();
250
251 iterator destPosition = const_cast<value_type*>(position);
252 dyno::move_or_copy_backward(destPosition, m_End - n, m_End);
253 for (; destPosition != (position + n); ++destPosition, ++first) {
254 iterator First = static_cast<value_type*>(first);
255 *destPosition = *(First);
256 }
257 }
258
259 return m_Begin + p;
260 }
261
262 template<typename T>
265 while ((first != last) && !(*first == value))
266 ++first;
267 return first;
268 }
269
270 template<typename T>
271 template<typename Predicate>
273 RandomAccessContainer<T>::find(iterator first, iterator last, const value_type& value, Predicate pre) {
274 while ((first != last) && !pre(*first, value))
275 ++first;
276 return first;
277 }
278
279 template<typename T>
282 iterator it = find(m_Begin, m_End, value);
283
284 if (it != m_End)
285 return erase(it);
286 else
287 return it;
288 }
289
290 template<typename T>
293 iterator it = find(m_Begin, m_End, value);
294
295 if (it != m_End)
296 return erase_unsorted(it);
297 else
298 return it;
299 }
300
301 template<typename T>
304 assert(position >= m_Begin && position < m_End);
305 iterator destPosition = const_cast<value_type*>(position);
306 //inplace movement
307 if ((position + 1) < m_End)
308 dyno::move_or_copy(destPosition + 1, m_End, destPosition);
309
310 (--m_End)->~value_type();
311 --m_size;
312 return destPosition;
313 }
314
315 template<typename T>
318 assert(first >= m_Begin && last <= m_End);
319 iterator destFirstPosition = const_cast<value_type*>(first);
320 iterator destLastPosition = const_cast<value_type*>(last);
321 if (first != last && (destLastPosition) < m_End) {
322 dyno::move_or_copy(destLastPosition, m_End, destFirstPosition);
323 }
325 for (; m_End != end - (destLastPosition - destFirstPosition); ) {
326
327 (--m_End)->~value_type();
328 --m_size;
329 }
330 return destFirstPosition;
331 }
332
333 template<typename T>
336 assert(position >= m_Begin && position < m_End);
337 iterator destPosition = const_cast<value_type*>(position);
338 *destPosition = *(m_End - 1);
339 (--m_End)->~value_type();
340 --m_size;
341 return destPosition;
342 }
343
344 template<typename T>
345 DYN_FUNC void RandomAccessContainer<T>::clear() noexcept {
346 if (!empty()) {
347 for (; m_End != m_Begin;)
348 (--m_End)->~value_type();
349
350 m_size = 0;
351 //assert(m_End == m_Begin);
352 }
353 }
354
355 //global operators
356 template <typename InputIterator1, typename InputIterator2>
357 DYN_FUNC inline bool
358 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
359 {
360 for (; (first1 != last1) && (first2 != last2); ++first1, ++first2)
361 {
362 if (*first1 < *first2)
363 return true;
364 if (*first2 < *first1)
365 return false;
366 }
367 return (first1 == last1) && (first2 != last2);
368 }
369
370 template <typename T>
372 {
373 return ((a.size() == b.size()) && (a.begin() == b.begin()));
374 }
375
376
377 template <typename T>
379 {
380 return ((a.size() != b.size()) || !(a.begin() == b.begin()));
381 }
382
383
384 template <typename T>
386 {
387 return lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
388 }
389
390
391 template <typename T>
393 {
394 return b < a;
395 }
396
397
398 template <typename T>
400 {
401 return !(b < a);
402 }
403
404
405 template <typename T>
407 {
408 return !(a < b);
409 }
410
411}
assert(queueCount >=1)
DYN_FUNC iterator erase(const_iterator position)
DYN_FUNC void push_back(const value_type &value)
DYN_FUNC iterator end() noexcept
DYN_FUNC iterator begin() noexcept
DYN_FUNC void reserve(iterator beg, size_type buffer_size)
DYN_FUNC iterator erase_first_unsorted(const T &value)
DYN_FUNC void resize(size_type n)
DYN_FUNC bool empty() const noexcept
DYN_FUNC iterator insert(const_iterator position, const value_type &value)
DYN_FUNC void assign(size_type n, const value_type &value)
DYN_FUNC iterator erase_unsorted(const_iterator position)
DYN_FUNC reference at(size_type n)
DYN_FUNC size_type size() const noexcept
DYN_FUNC reference operator[](size_type n)
DYN_FUNC iterator find(iterator first, iterator last, const value_type &value)
DYN_FUNC iterator erase_first(const T &value)
DYN_FUNC T * bufferEnd()
Definition STLBuffer.h:32
#define T(t)
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
DYN_FUNC bool operator<=(const priority_queue< T, Container, Compare > &a, const priority_queue< T, Container, Compare > &b)
DYN_FUNC bool operator>(const priority_queue< T, Container, Compare > &a, const priority_queue< T, Container, Compare > &b)
static DYN_FUNC Iterator move_or_copy_backward(Iterator first, Iterator last, Iterator resultEnd)
static DYN_FUNC OutputIterator move_or_copy(InputIterator first, InputIterator last, OutputIterator result)
DYN_FUNC bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
DYN_FUNC bool operator>=(const priority_queue< T, Container, Compare > &a, const priority_queue< T, Container, Compare > &b)
DYN_FUNC constexpr T && forward(typename dyno::remove_reference< T >::type &x) noexcept
Definition type_traits.h:17
DYN_FUNC bool operator<(const priority_queue< T, Container, Compare > &a, const priority_queue< T, Container, Compare > &b)
DYN_FUNC bool operator==(const priority_queue< T, Container, Compare > &a, const priority_queue< T, Container, Compare > &b)
DYN_FUNC bool operator!=(const priority_queue< T, Container, Compare > &a, const priority_queue< T, Container, Compare > &b)