API Documentation
Loading...
Searching...
No Matches
Buffer.hpp
Go to the documentation of this file.
1/**--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: Buffer
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/BaseValues.h>
34#include <NDEVR/LibAssert.h>
35#include <NDEVR/ObjectAllocator.h>
36#include <NDEVR/BufferAllocator.h>
37#include <NDEVR/ObjectInfo.h>
38#include <initializer_list>
39#include <utility>
40#include <functional>
41/**--------------------------------------------------------------------------------------------------
42Namespace: Parke
43Namespace that wraps all Logic created by Tyler Parke
44 *-----------------------------------------------------------------------------------------------**/
45
46namespace NDEVR
47{
48 /**--------------------------------------------------------------------------------------------------
49 Class: Buffer
50
51 \brief The equivelent of std::vector but with a bit more control. The basic array unit of the library.
52
53 t_type = what the buffer holds
54 t_index_type = the index reference (default uint04 but can be made uint08 for large buffer support)
55 t_memory_allocator = The object in charge of object allocation when creating space
56 t_memory_manager = The thing that actually stores the memory
57 Author: Tyler Parke
58
59 Date: 2017-11-19
60 *-----------------------------------------------------------------------------------------------**/
61
62 template<class t_type, class t_index_type = uint04, class t_memory_allocator = ObjectAllocator<ObjectInfo<t_type>::Primitive, t_index_type>, class t_memory_manager = BufferAllocator<t_type, t_index_type, false>>
64 {
65 public:
66 constexpr Buffer()
68 {}
69 constexpr Buffer(const Buffer& buffer)
71 {
72 addAll(buffer);
73 }
74 constexpr Buffer(Buffer&& buffer) noexcept
75 : m_memory_interface(std::move(buffer.m_memory_interface))
76 {}
77
78 explicit Buffer(t_index_type size)
80 {
81 m_memory_interface.resizeSpace(size);
82 }
83 explicit Buffer(const t_type* buffer, t_index_type size)
85 {
86 if (size != 0)
87 addAll(buffer, size);
88 }
89
90 Buffer(t_index_type size, const t_type& fill_object)
92 {
93 m_memory_interface.template createSpace<false>(size);
94 t_memory_allocator::template allocateElement<t_type>(m_memory_interface, 0, size, fill_object);
95 }
96
97 Buffer(std::initializer_list<t_type> l)
99 {
100 addAll(l.begin(), cast<t_index_type>(l.size()));
101 }
102
103 template<class t_iterator>
104 Buffer(const t_iterator& begin, const t_iterator& end)
106 {
107 setSize(cast<t_index_type>(std::distance(begin, end)));
108 uint04 index = 0;
109 for (auto iter = begin; iter != end; iter++)
110 {
111 get(index++) = *iter;
112 }
113 }
114
115 /**--------------------------------------------------------------------------------------------------
116 Fn: Buffer::~Buffer()
117
118 Destructor.
119
120 Author: Tyler Parke
121
122 Date: 2017-11-19
123 *-----------------------------------------------------------------------------------------------**/
124
126 {
127 t_memory_allocator::template deallocate<t_type>(m_memory_interface, 0, size());
128 }
129
130 /**--------------------------------------------------------------------------------------------------
131 Fn: operator const Buffer::t_type* () const
132
133 Gets the first location of type* in the array.
134
135 Author: Tyler Parke
136
137 Date: 2017-11-19
138
139 Returns: A const pointer to the beginning of the array.
140 *-----------------------------------------------------------------------------------------------**/
141
142 operator const t_type* () const
143 {
144 return begin();
145 }
146
147 /**--------------------------------------------------------------------------------------------------
148 Fn: operator const Buffer::t_type* () const
149
150 Gets the first location of type* in the array.
151
152 Author: Tyler Parke
153
154 Date: 2017-11-19
155
156 Returns: A const pointer to the beginning of the array.
157 *-----------------------------------------------------------------------------------------------**/
158
159 operator t_type* ()
160 {
161 return begin();
162 }
163
164 /**--------------------------------------------------------------------------------------------------
165 Fn: Buffer<t_other_type, t_other_memory_allocator, t_other_memory_manager> Buffer::getAs() const
166
167 Gets a copy of this buffer, with filled objects t_other_type, where all objects are created using
168 the contents of this buffer.
169
170 Author: Tyler Parke
171
172 Date: 2017-11-19
173
174 Returns: a copy of the buffer.
175 *-----------------------------------------------------------------------------------------------**/
176 template<class t_other_type, class t_other_index_type = uint04, class t_other_memory_allocator = ObjectAllocator<ObjectInfo<t_other_type>::Primitive>, class t_other_memory_manager = BufferAllocator<t_other_type, t_index_type, false>>
178 {
180 for (t_index_type i = 0; i < size(); i++)
181 {
182 other.add(t_other_type(get(i)));
183 }
184 return other;
185 }
186
187 /**--------------------------------------------------------------------------------------------------
188 Fn: void Buffer::add(const t_type& object)
189
190 Adds object.
191
192 Author: Tyler Parke
193
194 Date: 2017-11-19
195
196 Parameters:
197 object - The object to add.
198 *-----------------------------------------------------------------------------------------------**/
199 void add(t_type&& object)
200 {
201 m_memory_interface.addIndex();
202 t_memory_allocator::template allocateElement(m_memory_interface, size() - 1, std::move(object));
203 }
204 void add(const t_type& object)
205 {
206 m_memory_interface.addIndex();
207 t_memory_allocator::template allocateElement(m_memory_interface, size() - 1, object);
208 }
209
210 /**--------------------------------------------------------------------------------------------------
211 Fn: void Buffer::add(t_index_type location, const t_type& object)
212
213 Adds location.
214
215 Author: Tyler Parke
216
217 Date: 2017-11-19
218
219 Parameters:
220 location - The location.
221 object - The object.
222 *-----------------------------------------------------------------------------------------------**/
223
224 void add(t_index_type location, const t_type& object)
225 {
226 m_memory_interface.addIndex(location);
227 t_memory_allocator::template allocateElement(m_memory_interface, location, object);
228 }
229 void add(t_index_type location, t_type&& object)
230 {
231 m_memory_interface.addIndex(location);
232 t_memory_allocator::template allocateElement(m_memory_interface, location, std::move(object));
233 }
234
235 /**--------------------------------------------------------------------------------------------------
236 Fn: void Buffer::addAll(const Buffer& buffer)
237
238 Adds all.
239
240 Author: Tyler Parke
241
242 Date: 2017-11-19
243
244 Parameters:
245 buffer - The buffer.
246 *-----------------------------------------------------------------------------------------------**/
247 template<class t_other_memory_allocator, class t_other_index_type, class t_other_memory_manager>
249 {
250 t_index_type offset = size();
251 m_memory_interface.template createSpace<false>(buffer.size());
252 if constexpr (t_memory_allocator::isPrimitive())
253 {
254 m_memory_interface.template setAll<true>(buffer.memoryInterface(), offset, 0, buffer.size());
255 }
256 else
257 {
258 for (t_index_type i = 0; i < buffer.size(); i++)
259 t_memory_allocator::template allocateElement(m_memory_interface, i + offset, buffer[i]);
260 }
261 }
262
263 /**--------------------------------------------------------------------------------------------------
264 Fn: void Buffer::addAll(const t_type* buffer, t_index_type buffer_size)
265
266 Adds all to 'buffer_size'.
267
268 Author: Tyler Parke
269
270 Date: 2017-11-19
271
272 Parameters:
273 buffer - The buffer.
274 buffer_size - Size of the buffer.
275 *-----------------------------------------------------------------------------------------------**/
276
277 void addAll(const t_type* buffer, t_index_type buffer_size)
278 {
279 t_index_type offset = size();
280 m_memory_interface.template createSpace<false>(buffer_size);
281 if constexpr (t_memory_allocator::isPrimitive())
282 {
283 m_memory_interface.template setAll<true>(buffer, offset, buffer_size);
284 }
285 else
286 {
287 for (t_index_type i = 0; i < buffer_size; i++)
288 t_memory_allocator::allocateElement(m_memory_interface, i + offset, buffer[i]);
289 }
290 }
291
292 /**--------------------------------------------------------------------------------------------------
293 Fn: void Buffer::addAll(const t_type* buffer)
294
295 Adds all.
296
297 Author: Tyler Parke
298
299 Date: 2017-11-19
300
301 Parameters:
302 buffer - The buffer.
303 *-----------------------------------------------------------------------------------------------**/
304
305 void addAll(const t_type* buffer)
306 {
307 t_index_type size = 0;
308 int loc = 0;
309 for (;;)
310 {
311 if (memcmp(&buffer[size], &loc, sizeof(t_type)) == 0)
312 break;
313 ++size;
314 }
315 addAll(buffer, size);
316 }
317
318 /**--------------------------------------------------------------------------------------------------
319 Fn: void Buffer::insert(t_index_type offset, const Buffer& buffer)
320
321 Inserts.
322
323 Author: Tyler Parke
324
325 Date: 2017-11-19
326
327 Parameters:
328 offset - The offset.
329 buffer - The buffer.
330 *-----------------------------------------------------------------------------------------------**/
331
332 void insert(t_index_type offset, const Buffer& buffer)
333 {
334 m_memory_interface.template createSpace<false>(offset, buffer.size());
335 if constexpr (t_memory_allocator::isPrimitive())
336 {
337 m_memory_interface.template setAll<true>(buffer.m_memory_interface, offset, 0, buffer.size());
338 }
339 else
340 {
341 for (t_index_type i = 0; i < buffer.size(); i++)
342 t_memory_allocator::allocateElement(m_memory_interface, i + offset, buffer[i]);
343 }
344 }
345
346 void replaceIndexRange(t_index_type offset, t_index_type replace_size, const Buffer& buffer)
347 {
348 lib_assert(offset + replace_size <= size(), "end > size in buffer replace");
349 if (replace_size < buffer.size())
350 {
351 t_index_type size_dff = buffer.size() - replace_size;
352 m_memory_interface.template createSpace<false>(offset, size_dff);
353 if constexpr (!t_memory_allocator::isPrimitive())
354 {
355 for (t_index_type i = 0; i < size_dff; i++)
356 t_memory_allocator::allocateElement(m_memory_interface, i + offset, buffer[i]);
357 }
358 }
359 else if (replace_size > buffer.size())
360 {
361 removeAllIndex(offset, offset + (replace_size - buffer.size()));
362 }
363 setAll(buffer, offset, buffer.size());
364 }
365
366 /**--------------------------------------------------------------------------------------------------
367 Fn: void Buffer::insert(t_index_type offset, const t_type* const buffer, t_index_type buffer_size)
368
369 Inserts.
370
371 Author: Tyler Parke
372
373 Date: 2017-11-19
374
375 Parameters:
376 offset - The offset.
377 buffer - The buffer.
378 buffer_size - Size of the buffer.
379 *-----------------------------------------------------------------------------------------------**/
380#ifdef __clang__
381 #pragma clang diagnostic push
382 #pragma clang diagnostic ignored "-Wunused-value"//Weird error in clang thinks expression result not used
383#endif
384 void insert(t_index_type offset, const t_type* const buffer, t_index_type buffer_size)
385 {
386 m_memory_interface.template createSpace<false>(offset, buffer_size);
387 if constexpr (t_memory_allocator::isPrimitive())
388 {
389 m_memory_interface.template setAll<true>(buffer, offset, buffer_size);
390 }
391 else
392 {
393 for (t_index_type i = 0; i < buffer_size; i++)
394 t_memory_allocator::allocateElement(m_memory_interface, i + offset, buffer[i]);
395 }
396 }
397#ifdef __clang__
398 #pragma clang diagnostic pop
399#endif
400 /**--------------------------------------------------------------------------------------------------
401 Fn: void Buffer::insert(t_index_type location, const t_type* buffer)
402
403 Inserts.
404
405 Author: Tyler Parke
406
407 Date: 2017-11-19
408
409 Parameters:
410 location - The location.
411 buffer - The buffer.
412 *-----------------------------------------------------------------------------------------------**/
413
414 void insert(const t_index_type location, const t_type* buffer)
415 {
416 t_index_type size = 0;
417 int loc = 0;
418 for (;;)
419 {
420 if (memcmp(&buffer[size], &loc, sizeof(t_type)) == 0)
421 break;
422 ++size;
423 }
424 insert(location, buffer, size);
425 }
426
427 /**--------------------------------------------------------------------------------------------------
428 Fn: void Buffer::addSpace(t_index_type space_to_add)
429
430 Adds a space.
431
432 Author: Tyler Parke
433
434 Date: 2017-11-19
435
436 Parameters:
437 space_to_add - The space to add.
438 *-----------------------------------------------------------------------------------------------**/
439 template<bool t_managed>
440 void addSpace(t_index_type space_to_add)
441 {
442 const t_index_type offset = size();
443 m_memory_interface.template createSpace<t_managed>(space_to_add);
444 t_memory_allocator::template allocate<t_type>(m_memory_interface, offset, size() - offset);
445 }
446 template<bool t_managed = false>
447 void addAndFillSpace(t_index_type space_to_add, const t_type& fill_object)
448 {
449 const t_index_type offset = size();
450 m_memory_interface.template createSpace<t_managed>(space_to_add);
451 t_memory_allocator::template allocateElement<t_type>(m_memory_interface, offset, size() - offset, fill_object);
452 }
453
454 /**--------------------------------------------------------------------------------------------------
455 Fn: void Buffer::addSpace(t_index_type location, t_index_type size)
456
457 Adds a space to 'size'.
458
459 Author: Tyler Parke
460
461 Date: 2017-11-19
462
463 Parameters:
464 location - The location.
465 size - The size.
466 *-----------------------------------------------------------------------------------------------**/
467 template<bool t_managed>
468 void addSpace(t_index_type location, t_index_type size)
469 {
470 lib_assert(location <= this->size(), "Tried to insert into a bad location");
471 m_memory_interface.template createSpace<t_managed>(location, size);
472
473 t_memory_allocator::template allocate<t_type>(m_memory_interface, location, size);
474 }
475
476 /**--------------------------------------------------------------------------------------------------
477 Fn: Buffer::decltype(auto) begin()
478
479 Constructor.
480
481 Author: Tyler Parke
482
483 Date: 2017-11-19
484
485 Parameters:
486 parameter1 - The first parameter.
487 *-----------------------------------------------------------------------------------------------**/
488
489 [[nodiscard]] decltype(auto) ptr()
490 {
491 return m_memory_interface.ptr();
492 }
493
494 [[nodiscard]] decltype(auto) ptr() const
495 {
496 return m_memory_interface.ptr();
497 }
498
499 [[nodiscard]] t_index_type memSize() const
500 {
501 return m_memory_interface.memSize();
502 }
503
504 [[nodiscard]] decltype(auto) begin()
505 {
506 return m_memory_interface.begin();
507 }
508
509 /**--------------------------------------------------------------------------------------------------
510 Fn: Buffer::decltype(auto) begin() const
511
512 Constructor.
513
514 Author: Tyler Parke
515
516 Date: 2017-11-19
517
518 Parameters:
519 parameter1 - The first parameter.
520 *-----------------------------------------------------------------------------------------------**/
521
522 [[nodiscard]] decltype(auto) begin() const
523 {
524 return m_memory_interface.begin();
525 }
526
527 /**--------------------------------------------------------------------------------------------------
528 Fn: Buffer::decltype(auto) begin(t_index_type index) const
529
530 Constructor.
531
532 Author: Tyler Parke
533
534 Date: 2017-11-19
535
536 Parameters:
537 parameter1 - The first parameter.
538 *-----------------------------------------------------------------------------------------------**/
539
540 [[nodiscard]] decltype(auto) begin(t_index_type index) const
541 {
542 return m_memory_interface.begin(index);
543 }
544
545 /**--------------------------------------------------------------------------------------------------
546 Fn: t_index_type Buffer::capacity() const
547
548 Gets the capacity.
549
550 Author: Tyler Parke
551
552 Date: 2017-11-19
553
554 Returns: An t_index_type.
555 *-----------------------------------------------------------------------------------------------**/
556
557 [[nodiscard]] t_index_type capacity() const
558 {
559 return m_memory_interface.capacity();
560 }
561
562 /**--------------------------------------------------------------------------------------------------
563 Fn: void Buffer::clear()
564
565 Clears this object to its blank/initial state.
566
567 Author: Tyler Parke
568
569 Date: 2017-11-19
570 *-----------------------------------------------------------------------------------------------**/
571
572 void clear()
573 {
574 t_memory_allocator::template deallocate<t_type>(m_memory_interface, 0U, size());
575 m_memory_interface.clear();
576 }
577
578 /**--------------------------------------------------------------------------------------------------
579 Fn: void Buffer::clear(t_index_type new_capacity)
580
581 Clears this object to its blank/initial state.
582
583 Author: Tyler Parke
584
585 Date: 2017-11-19
586
587 Parameters:
588 new_capacity - The new capacity.
589 *-----------------------------------------------------------------------------------------------**/
590
591 void clear(t_index_type new_capacity)
592 {
593 t_memory_allocator::template deallocate<t_type>(m_memory_interface, 0, size());
594 m_memory_interface.clear();
595 ensureCapacity(new_capacity, true, true);
596 }
597
598 /**--------------------------------------------------------------------------------------------------
599 Fn: inline sint04 Buffer::compare(const Buffer& value) const
600
601 Compares this const Buffer&amp; object to another to determine their relative ordering.
602
603 Author: Tyler Parke
604
605 Date: 2017-11-19
606
607 Parameters:
608 value - The constant buffer&amp; to compare to this object.
609
610 Returns: Negative if 'value' is less than '', 0 if they are equal, or positive if it is greater.
611 *-----------------------------------------------------------------------------------------------**/
612
613 [[nodiscard]] sint04 compare(const Buffer& value) const
614 {
615 sint04 cmp_value = m_memory_interface.compare(value.m_memory_interface);
616 if (cmp_value != 0)
617 return cmp_value;
618 else if (value.size() > size())
619 return -1;
620 else if (value.size() < size())
621 return 1;
622 else
623 return 0;
624 }
625
626 /**--------------------------------------------------------------------------------------------------
627 Fn: inline sint04 Buffer::compare(const Buffer& value, t_index_type start, t_index_type end) const
628
629 Compares objects.
630
631 Author: Tyler Parke
632
633 Date: 2017-11-19
634
635 Parameters:
636 value - Constant buffer&amp; to be compared.
637 start - Uint 04 to be compared.
638 end - Uint 04 to be compared.
639
640 Returns: Negative if 'value' is less than 'start', 0 if they are equal, or positive if it is
641 greater.
642 *-----------------------------------------------------------------------------------------------**/
643
644 [[nodiscard]] sint04 compare(const Buffer& value, t_index_type start, t_index_type end) const
645 {
646 sint04 cmp_value = m_memory_interface.compare(value.m_memory_interface, start, end);
647 if (cmp_value != 0)
648 return cmp_value;
649 else if (value.size() == size())
650 return 0;
651 else if (end > size())
652 return -1;
653 else if (end > value.size())
654 return 1;
655 else
656 return 0;
657 }
658
659 /**--------------------------------------------------------------------------------------------------
660 Fn: bool Buffer::contains(const t_type& element) const
661
662 Query if this object contains the given element.
663
664 Author: Tyler Parke
665
666 Date: 2017-11-19
667
668 Parameters:
669 element - The const t_type&amp; to test for containment.
670
671 Returns: True if the object is in this collection, false if not.
672 *-----------------------------------------------------------------------------------------------**/
673
674 [[nodiscard]] bool contains(const t_type& element) const
675 {
676 for (t_index_type i = 0; i < size(); ++i)
677 {
678 if (get(i) == element)
679 return true;
680 }
681 return false;
682 }
683 [[nodiscard]] bool contains(const t_type& element, const std::function<bool(const t_type&, const t_type&)>& equal_function) const
684 {
685 for (uint04 i = 0; i < size(); ++i)
686 {
687 if (equal_function(get(i), element))
688 return true;
689 }
690 return false;
691 }
692 [[nodiscard]] bool contains(const t_type& element, t_index_type start) const
693 {
694 lib_assert(start < size(), "buffer contains using bad start value");
695 for (t_index_type i = start; i < size(); ++i)
696 {
697 if (get(i) == element)
698 return true;
699 }
700 return false;
701 }
702 [[nodiscard]] bool contains(const t_type& element, t_index_type start, t_index_type search_size) const
703 {
704 lib_assert(search_size + start <= size(), "buffer contains using bad start value");
705 for (t_index_type i = start; i < start + search_size; ++i)
706 {
707 if (get(i) == element)
708 return true;
709 }
710 return false;
711 }
712
713 /**--------------------------------------------------------------------------------------------------
714 Fn: t_index_type Buffer::count(const t_type& element) const
715
716 Counts the given element.
717
718 Author: Tyler Parke
719
720 Date: 2017-11-19
721
722 Parameters:
723 element - The element.
724
725 Returns: An t_index_type.
726 *-----------------------------------------------------------------------------------------------**/
727
728 [[nodiscard]] t_index_type count(const t_type& element) const
729 {
730 return m_memory_interface.count(element);
731 }
732
733 /**--------------------------------------------------------------------------------------------------
734 Fn: Buffer::decltype(auto) end()
735
736 Constructor.
737
738 Author: Tyler Parke
739
740 Date: 2017-11-19
741
742 Parameters:
743 parameter1 - The first parameter.
744 *-----------------------------------------------------------------------------------------------**/
745
746 decltype(auto) end()
747 {
748 return m_memory_interface.end();
749 }
750
751 /**--------------------------------------------------------------------------------------------------
752 Fn: Buffer::decltype(auto) end() const
753
754 Constructor.
755
756 Author: Tyler Parke
757
758 Date: 2017-11-19
759
760 Parameters:
761 parameter1 - The first parameter.
762 *-----------------------------------------------------------------------------------------------**/
763
764 [[nodiscard]] decltype(auto) end() const
765 {
766 return m_memory_interface.end();
767 }
768
769 /**--------------------------------------------------------------------------------------------------
770 Fn: Buffer::decltype(auto) end(t_index_type index)
771
772 Constructor.
773
774 Author: Tyler Parke
775
776 Date: 2017-11-19
777
778 Parameters:
779 parameter1 - The first parameter.
780 *-----------------------------------------------------------------------------------------------**/
781
782 decltype(auto) end(t_index_type index)
783 {
784 return m_memory_interface.end(index);
785 }
786
787 /**--------------------------------------------------------------------------------------------------
788 Fn: void Buffer::ensureCapacity(t_index_type new_capacity, bool ensure_not_greater = false,
789 bool ensure_not_less = true)
790
791 Ensures that capacity.
792
793 Author: Tyler Parke
794
795 Date: 2017-11-19
796
797 Parameters:
798 new_capacity - The new capacity.
799 ensure_not_greater - (Optional) True to ensure not greater.
800 ensure_not_less - (Optional) True to ensure not less.
801 *-----------------------------------------------------------------------------------------------**/
802
803 void ensureCapacity(t_index_type new_capacity, bool ensure_not_greater = false, bool ensure_not_less = true)
804 {
805 if (new_capacity == capacity())
806 return;
807 if (new_capacity > capacity())
808 {
809 if (ensure_not_less)
810 m_memory_interface.resizeSpace(new_capacity);
811 }
812 else if (ensure_not_greater)
813 {
814 lib_assert(new_capacity >= size(), "Array can't have capacity less than size");
815 m_memory_interface.resizeSpace(new_capacity);
816 }
817 }
818
819 /**--------------------------------------------------------------------------------------------------
820 Fn: bool Buffer::equals(const Buffer& buffer) const
821
822 Tests if this const Buffer&amp; is considered equal to another.
823
824 Author: Tyler Parke
825
826 Date: 2017-11-19
827
828 Parameters:
829 buffer - The constant buffer&amp; to compare to this object.
830
831 Returns: True if the objects are considered equal, false if they are not.
832 *-----------------------------------------------------------------------------------------------**/
833
834 [[nodiscard]] bool equals(const Buffer& buffer) const
835 {
836 if (size() == buffer.size())
838 else
839 return false;
840 }
841
842
843
844 /**--------------------------------------------------------------------------------------------------
845 Fn: Buffer::decltype(auto) get(t_index_type index)
846
847 Constructor.
848
849 Author: Tyler Parke
850
851 Date: 2017-11-19
852
853 Parameters:
854 parameter1 - The first parameter.
855 *-----------------------------------------------------------------------------------------------**/
856
857 [[nodiscard]] decltype(auto) get(t_index_type index)
858 {
859 lib_assert(index < size(), "Buffer out of bounds");
860 return m_memory_interface.get(index);
861 }
862
863 /**--------------------------------------------------------------------------------------------------
864 Fn: Buffer::decltype(auto) get(t_index_type index) const
865
866 Constructor.
867
868 Author: Tyler Parke
869
870 Date: 2017-11-19
871
872 Parameters:
873 parameter1 - The first parameter.
874 *-----------------------------------------------------------------------------------------------**/
875
876 [[nodiscard]] decltype(auto) get(t_index_type index) const
877 {
878 lib_assert(index < size(), "Buffer out of bounds");
879 return m_memory_interface.get(index);
880 }
881
882
883 [[nodiscard]] Buffer getAll(t_index_type start, t_index_type size)
884 {
885 Buffer buff(size);
886 buff.addAll(begin(start), size);
887 return buff;
888 }
889
890 /**--------------------------------------------------------------------------------------------------
891 Fn: t_index_type Buffer::indexOf(const t_type& element) const
892
893 Index of the given element.
894
895 Author: Tyler Parke
896
897 Date: 2017-11-19
898
899 Parameters:
900 element - The element.
901
902 Returns: An t_index_type.
903 *-----------------------------------------------------------------------------------------------**/
904
905 [[nodiscard]] t_index_type indexOf(const t_type& element) const
906 {
907 for (t_index_type i = 0; i < size(); ++i)
908 {
909 if (get(i) == element) return i;
910 }
912 }
913
914 /**--------------------------------------------------------------------------------------------------
915 Fn: t_index_type Buffer::indexOf(const t_type& element, t_index_type start_pos) const
916
917 Searches for the first match.
918
919 Author: Tyler Parke
920
921 Date: 2017-11-19
922
923 Parameters:
924 element - The element.
925 start_pos - The start position.
926
927 Returns: An t_index_type.
928 *-----------------------------------------------------------------------------------------------**/
929
930 [[nodiscard]] t_index_type indexOf(const t_type& element, t_index_type start_pos) const
931 {
932 for (t_index_type i = start_pos; i < size(); ++i)
933 {
934 if (get(i) == element) return i;
935 }
937 }
938 [[nodiscard]] t_index_type indexOf(const t_type& element, t_index_type start_pos, t_index_type search_size) const
939 {
940 lib_assert(search_size + start_pos <= size(), "bad index of in equation");
941 for (t_index_type i = start_pos; i < start_pos + search_size; ++i)
942 {
943 if (get(i) == element) return i;
944 }
946 }
947
948 /**--------------------------------------------------------------------------------------------------
949 Fn: bool Buffer::isEmpty() const
950
951 Query if this object is empty.
952
953 Author: Tyler Parke
954
955 Date: 2017-11-19
956
957 Returns: True if empty, false if not.
958 *-----------------------------------------------------------------------------------------------**/
959
960 [[nodiscard]] bool isEmpty() const
961 {
962 return size() == 0;
963 }
964
965 /**--------------------------------------------------------------------------------------------------
966 Fn: t_type& Buffer::last()
967
968 Gets the last.
969
970 Author: Tyler Parke
971
972 Date: 2017-11-19
973
974 Returns: A reference to a t_type.
975 *-----------------------------------------------------------------------------------------------**/
976
977 [[nodiscard]] decltype(auto) last()
978 {
979 lib_assert(size() != 0, "Cannot get last: out of bounds");
980 return get(size() - 1);
981 }
982
983 /**--------------------------------------------------------------------------------------------------
984 Fn: const t_type& Buffer::last() const
985
986 Gets the last.
987
988 Author: Tyler Parke
989
990 Date: 2017-11-19
991
992 Returns: A reference to a const t_type.
993 *-----------------------------------------------------------------------------------------------**/
994
995 [[nodiscard]] decltype(auto) last() const
996 {
997 lib_assert(size() != 0, "Cannot get last: out of bounds");
998 return get(size() - 1);
999 }
1000
1001 /**--------------------------------------------------------------------------------------------------
1002 Fn: t_index_type Buffer::lastIndexOf(const t_type& element) const
1003
1004 Last index of the given element.
1005
1006 Author: Tyler Parke
1007
1008 Date: 2017-11-19
1009
1010 Parameters:
1011 element - The element.
1012
1013 Returns: An t_index_type.
1014 *-----------------------------------------------------------------------------------------------**/
1015
1016 [[nodiscard]] t_index_type lastIndexOf(const t_type& element) const
1017 {
1018 for (t_index_type i = size() - 1; i < size(); --i)
1019 {
1020 if (get(i) == element) return i;
1021 }
1023 }
1024
1025 /**--------------------------------------------------------------------------------------------------
1026 Fn: void Buffer::removeIndex(t_index_type location)
1027
1028 Removes the index described by location.
1029
1030 Author: Tyler Parke
1031
1032 Date: 2017-11-19
1033
1034 Parameters:
1035 location - The location to erase.
1036 *-----------------------------------------------------------------------------------------------**/
1037 void removeIndex(t_index_type location)
1038 {
1039 lib_assert(location < size(), "Out of bounds remove");
1040 t_memory_allocator::template deallocate<t_type>(m_memory_interface, location, 1);
1041 m_memory_interface.removeIndex(location);
1042 }
1043
1044 /**--------------------------------------------------------------------------------------------------
1045 Fn: void Buffer::removeIndex(t_index_type location)
1046
1047 Useful for unorded buffers, removes element by swapping with back. Saves big memmove
1048
1049 Author: Tyler Parke
1050
1051 Date: 2024-06-04
1052
1053 Parameters:
1054 location - The location to erase.
1055 *-----------------------------------------------------------------------------------------------**/
1056 void removeIndexBackSwap(t_index_type location)
1057 {
1058 swapIndices(location, size() - 1);//swap with back
1059 removeLast();//remove back
1060 }
1061 /**--------------------------------------------------------------------------------------------------
1062 Fn: bool Buffer::removeElement(const t_type& element)
1063
1064 Removes the element described by element.
1065
1066 Author: Tyler Parke
1067
1068 Date: 2017-11-19
1069
1070 Parameters:
1071 element - The element.
1072
1073 Returns: True if it succeeds, false if it fails.
1074 *-----------------------------------------------------------------------------------------------**/
1075
1076 bool removeElement(const t_type& element)
1077 {
1078 for (t_index_type i = size() - 1; !isNaN(i); --i)//start at last element as less expensive to removeRows
1079 {
1080 if (get(i) == element)
1081 {
1082 removeIndex(i);
1083 return true;
1084 }
1085 }
1086 return false;
1087 }
1088
1089 /**--------------------------------------------------------------------------------------------------
1090 Fn: void Buffer::removeLast()
1091
1092 Removes the last.
1093
1094 Author: Tyler Parke
1095
1096 Date: 2017-11-19
1097 *-----------------------------------------------------------------------------------------------**/
1098
1100 {
1101 lib_assert(size() > 0, "Cannot remove last from empty buffer");
1102 t_memory_allocator::template deallocate<t_type>(m_memory_interface, size() - 1, 1);
1103 m_memory_interface.removeLast();
1104 }
1105
1106 /**--------------------------------------------------------------------------------------------------
1107 Fn: void Buffer::removeAllUnordered(const t_type& object)
1108
1109 Removes all unordered described by object. This function does not preserve the order of the buffer.
1110
1111 Author: Tyler Parke
1112
1113 Date: 2017-11-19
1114
1115 Parameters:
1116 object - The object.
1117 *-----------------------------------------------------------------------------------------------**/
1118
1119 void removeAllUnordered(const t_type& object)
1120 {
1121 t_index_type end = size();
1122 for (t_index_type i = 0; i < end; i++)
1123 {
1124 if (get(i) == object)
1125 {
1126 swapIndices(i, --end);
1127 --i;
1128 }
1129 }
1130 if (end != size())
1132 }
1133
1134 /**--------------------------------------------------------------------------------------------------
1135 Fn: void Buffer::removeAllUnordered(const t_type& object)
1136
1137 Removes all unordered described by object given a functor. The functor should return true if the
1138 item is deleted or false if the item should stay in the array. This function does not preserve the
1139 order of the buffer.
1140
1141 Author: Tyler Parke
1142
1143 Date: 2017-11-19
1144
1145 Parameters:
1146 functor - A functor that takes a value and returns true if value is to be removed, or false otherwise.
1147 *-----------------------------------------------------------------------------------------------**/
1148 template<class t_functor>
1149 void removeAllUnordered(const t_functor& functor)
1150 {
1151 t_index_type end = size();
1152 for (t_index_type i = 0; i < end; i++)
1153 {
1154 if (functor(get(i)))
1155 {
1156 swapIndices(i--, --end);
1157 }
1158 }
1159 if (end != size())
1161 }
1162
1163 /**--------------------------------------------------------------------------------------------------
1164 Fn: void Buffer::removeAllOrdered(const t_type& object)
1165
1166 Removes all ordered described by object. Preserves the order of the buffer
1167
1168 Author: Tyler Parke
1169
1170 Date: 2017-11-19
1171
1172 Parameters:
1173 object - The object.
1174 *-----------------------------------------------------------------------------------------------**/
1175
1176 void removeAllOrdered(const t_type& object)
1177 {
1178 t_index_type start = 0;
1179 for (t_index_type i = 0; i < size(); i++)
1180 {
1181 get(start) = get(i);
1182 if (get(i) != object)
1183 {
1184 ++start;
1185 }
1186 }
1187 if (start != size())
1188 m_memory_interface.removeAllIndex(start, size());
1189 }
1190
1191 /**--------------------------------------------------------------------------------------------------
1192 Fn: void Buffer::removeAllIndex(t_index_type start, t_index_type end)
1193
1194 Removes all index.
1195
1196 Author: Tyler Parke
1197
1198 Date: 2017-11-19
1199
1200 Parameters:
1201 start - The start.
1202 end - The end.
1203 *-----------------------------------------------------------------------------------------------**/
1204
1205 void removeAllIndex(t_index_type start, t_index_type end)
1206 {
1207 if (start == end)
1208 return;
1209 lib_assert(start <= end, "remove all start > end");
1210 t_memory_allocator::template deallocate<t_type>(m_memory_interface, start, end - start);
1211 m_memory_interface.removeAllIndex(start, end);
1212 }
1213 template<class t_range_buffer>
1214 void removeAllIndices(const t_range_buffer& ranges)
1215 {
1216#ifdef _DEBUG
1217 uint04 last_delete_index = 0;
1218#endif
1219 for (const auto& range : ranges)
1220 {
1221 t_index_type start = range.first;
1222 t_index_type end = range.second;
1223#ifdef _DEBUG
1224 lib_assert(last_delete_index <= start, "ranges must be ordered");
1225 lib_assert(start <= end, "remove all start > end");
1226 last_delete_index = end;
1227#endif
1228 t_memory_allocator::template deallocate<t_type>(m_memory_interface, start, end - start);
1229 }
1230 m_memory_interface.removeAllIndices(ranges);
1231 }
1232 /**--------------------------------------------------------------------------------------------------
1233 Fn: void Buffer::replaceAll(const t_type& var, const t_type& replacement)
1234
1235 Replace all.
1236
1237 Author: Tyler Parke
1238
1239 Date: 2017-11-19
1240
1241 Parameters:
1242 var - The variable.
1243 replacement - The replacement.
1244 *-----------------------------------------------------------------------------------------------**/
1245
1246 void replaceAll(const t_type& var, const t_type& replacement)
1247 {
1248 for (t_index_type i = 0; i < size(); i++)
1249 {
1250 if (get(i) == var)
1251 get(i) = replacement;
1252 }
1253 }
1254
1255 /**--------------------------------------------------------------------------------------------------
1256 Fn: void Buffer::reverse()
1257
1258 Reverses this object.
1259
1260 Author: Tyler Parke
1261
1262 Date: 2017-11-19
1263 *-----------------------------------------------------------------------------------------------**/
1264
1265 void reverse()
1266 {
1267 for (t_index_type i = 0; i < size() / 2; i++)
1268 {
1269 m_memory_interface.swap(i, size() - i - 1);
1270 }
1271 }
1272
1273 /**--------------------------------------------------------------------------------------------------
1274 Fn: void Buffer::reverse(t_index_type start, t_index_type end)
1275
1276 Reverses.
1277
1278 Author: Tyler Parke
1279
1280 Date: 2017-11-19
1281
1282 Parameters:
1283 start - The start.
1284 end - The end.
1285 *-----------------------------------------------------------------------------------------------**/
1286
1287 void reverse(const t_index_type start, const t_index_type end)
1288 {
1289 const t_index_type size = end - start;
1290 for (t_index_type i = 0; i < size / 2; i++)
1291 {
1292 m_memory_interface.swap(start + i, (start + size) - i - 1);
1293 }
1294 }
1295
1296
1297
1298 /**--------------------------------------------------------------------------------------------------
1299 Fn: void Buffer::setAll(const t_o_type* src, t_index_type offset, t_index_type size)
1300
1301 Sets all.
1302
1303 Author: Tyler Parke
1304
1305 Date: 2017-11-19
1306
1307 Parameters:
1308 src - Source for the.
1309 offset - The offset.
1310 size - The size.
1311 *-----------------------------------------------------------------------------------------------**/
1312 template<class t_o_type>
1313 void setAll(const t_o_type* src, t_index_type offset, t_index_type size)
1314 {
1315 m_memory_interface.template setAll<t_memory_allocator::isPrimitive()>(src, offset, size);
1316 }
1317
1318 /**--------------------------------------------------------------------------------------------------
1319 Fn: void Buffer::setAll(const Buffer& buffer, t_index_type offset, t_index_type size)
1320
1321 Sets all.
1322
1323 Author: Tyler Parke
1324
1325 Date: 2017-11-19
1326
1327 Parameters:
1328 buffer - The buffer.
1329 offset - The offset.
1330 size - The size.
1331 *-----------------------------------------------------------------------------------------------**/
1332
1333 void setAll(const Buffer& buffer, t_index_type offset, t_index_type size)
1334 {
1335 lib_assert(buffer.size() >= size, "too small of buffer being used in set all");
1336 m_memory_interface.template setAll<t_memory_allocator::isPrimitive()>(buffer.m_memory_interface, offset, 0, size);
1337 }
1338
1339 /**--------------------------------------------------------------------------------------------------
1340 Fn: void Buffer::setAll(const Buffer& buffer, t_index_type offset, t_index_type other_offset, t_index_type size)
1341
1342 Sets all.
1343
1344 Author: Tyler Parke
1345
1346 Date: 2017-11-19
1347
1348 Parameters:
1349 buffer - The buffer.
1350 offset - The offset.
1351 other_offset - The other offset.
1352 size - The size.
1353 *-----------------------------------------------------------------------------------------------**/
1354
1355 void setAll(const Buffer& buffer, t_index_type offset, t_index_type other_offset, t_index_type size)
1356 {
1357 lib_assert(buffer.size() >= size, "too small of buffer being used in set all");
1358 m_memory_interface.template setAll<t_memory_allocator::isPrimitive()>(buffer.m_memory_interface, offset, other_offset, size);
1359 }
1360
1361
1362 /**--------------------------------------------------------------------------------------------------
1363 Fn: void Buffer::setAllToValue(const t_o_type& fill_element, t_index_type offset = 0,
1364 t_index_type fill_size = Constant<t_index_type>::NaN)
1365
1366 Sets all to value.
1367
1368 Author: Tyler Parke
1369
1370 Date: 2017-11-19
1371
1372 Parameters:
1373 fill_element - The fill element.
1374 offset - (Optional) The offset.
1375 fill_size - (Optional) Size of the fill.
1376 *-----------------------------------------------------------------------------------------------**/
1377 template<class t_o_type>
1378 void setAllToValue(const t_o_type& fill_element, const t_index_type offset = 0, t_index_type fill_size = Constant<t_index_type>::NaN)
1379 {
1380 const t_index_type old_size = size();
1381 if (isNaN(fill_size))
1382 fill_size = old_size - offset;
1383 else if(offset + fill_size > old_size)
1384 {
1385 m_memory_interface.setSize(offset + fill_size);
1386 }
1387 const t_type value(fill_element);
1388 if (t_memory_allocator::isPrimitive() || size() == old_size)
1389 {
1390 m_memory_interface.setAll(value, offset, fill_size);
1391 }
1392 else
1393 {
1394 t_index_type size_a = old_size - offset;
1395 m_memory_interface.setAll(value, offset, size_a);
1396 t_memory_allocator::allocateElement(m_memory_interface, offset + size_a, fill_size - size_a, value);
1397 }
1398 }
1399
1400 /**--------------------------------------------------------------------------------------------------
1401 Fn: void Buffer::setSize(t_index_type new_size)
1402
1403 Sets a size.
1404
1405 Author: Tyler Parke
1406
1407 Date: 2017-11-19
1408
1409 Parameters:
1410 new_size - Size of the new.
1411 *-----------------------------------------------------------------------------------------------**/
1412
1413 void setSize(t_index_type new_size)
1414 {
1415 if(new_size < size())
1416 t_memory_allocator::template deallocate<t_type>(m_memory_interface, new_size, size() - new_size);
1417 const t_index_type old_size = size();
1418 m_memory_interface.setSize(new_size);
1419 if (old_size < size())
1420 t_memory_allocator::template allocate<t_type>(m_memory_interface, old_size, size() - old_size);
1421 }
1422 //Keep std::vector compliant
1423 void resize(t_index_type new_size)
1424 {
1425 setSize(new_size);
1426 }
1427
1428 /**--------------------------------------------------------------------------------------------------
1429 Fn: void Buffer::setSize(t_index_type new_size, const t_type& fill_element)
1430
1431 Sets a size, where new elements have a default value.
1432
1433 Author: Derek Fox / Tyler Parke
1434
1435 Date: 2019-02-06
1436
1437 Parameters:
1438 new_size - Size of the new.
1439 fill_element - Default value of new elements.
1440 *-----------------------------------------------------------------------------------------------**/
1441
1442 void setSize(const t_index_type new_size, const t_type& fill_element)
1443 {
1444 const t_index_type old_size = size();
1445 setSize(new_size);
1446 if(old_size < new_size)
1447 setAllToValue(fill_element, old_size);
1448 }
1449 /**--------------------------------------------------------------------------------------------------
1450 Fn: t_index_type Buffer::size() const
1451
1452 Gets the size.
1453
1454 Author: Tyler Parke
1455
1456 Date: 2017-11-19
1457
1458 Returns: An t_index_type.
1459 *-----------------------------------------------------------------------------------------------**/
1460
1461 [[nodiscard]] constexpr t_index_type size() const
1462 {
1463 return m_memory_interface.filledSize();
1464 }
1465
1466 /**--------------------------------------------------------------------------------------------------
1467 Fn: void Buffer::swapElements(const t_type& element1, const t_type& element2)
1468
1469 Swap elements.
1470
1471 Author: Tyler Parke
1472
1473 Date: 2017-11-19
1474
1475 Parameters:
1476 element1 - The first element.
1477 element2 - The second element.
1478 *-----------------------------------------------------------------------------------------------**/
1479
1480 void swapElements(const t_type& element1, const t_type& element2)
1481 {
1482 if (element1 == element2)
1483 return;
1484 t_index_type index_1 = Constant<t_index_type>::NaN;
1485 t_index_type index_2 = Constant<t_index_type>::NaN;
1486 for (t_index_type i = 0; i < size(); i++)
1487 {
1488 if (get(i) == element1)
1489 {
1490 index_1 = i;
1491 if (!isNaN(index_2))
1492 {
1493 swapIndices(index_1, index_2);
1494 return;
1495 }
1496 }
1497 else if (get(i) == element2)
1498 {
1499 index_2 = i;
1500 if (!isNaN(index_1))
1501 {
1502 swapIndices(index_1, index_2);
1503 return;
1504 }
1505 }
1506 }
1507 }
1508
1509 /**--------------------------------------------------------------------------------------------------
1510 Fn: void Buffer::swapAllElements(const t_type& element1, const t_type& element2)
1511
1512 Swap all elements.
1513
1514 Author: Tyler Parke
1515
1516 Date: 2017-11-19
1517
1518 Parameters:
1519 element1 - The first element.
1520 element2 - The second element.
1521 *-----------------------------------------------------------------------------------------------**/
1522
1523 void swapAllElements(const t_type& element1, const t_type& element2)
1524 {
1525 if (element1 == element2)
1526 return;
1527 for (t_index_type ii = 0; ii < size(); ++ii)
1528 {
1529 if (get(ii) == element1)
1530 get(ii) = element2;
1531 else if (get(ii) == element2)
1532 get(ii) = element1;
1533 }
1534 }
1535
1536 /**--------------------------------------------------------------------------------------------------
1537 Fn: void Buffer::swapIndices(t_index_type index_1, t_index_type index_2)
1538
1539 Swap indices.
1540
1541 Author: Tyler Parke
1542
1543 Date: 2017-11-19
1544
1545 Parameters:
1546 index_1 - The first index.
1547 index_2 - The second index.
1548 *-----------------------------------------------------------------------------------------------**/
1549
1550 void swapIndices(t_index_type index_1, t_index_type index_2)
1551 {
1552 std::swap(get(index_1), get(index_2));
1553 }
1554
1555 /**--------------------------------------------------------------------------------------------------
1556 Fn: void Buffer::move(t_index_type from, t_index_type to)
1557
1558 Moves an element to a new index
1559
1560 Author: Tyler Parke
1561
1562 Date: 2018-12-17
1563
1564 Parameters:
1565 from - The original index.
1566 to - The new index.
1567 *-----------------------------------------------------------------------------------------------**/
1568 void move(t_index_type from, t_index_type to)
1569 {
1570 if (to == from)
1571 return;
1572 t_type object = get(from);
1573 removeIndex(from);
1574 if (to > from)
1575 to--;
1576 add(to, object);
1577 }
1578
1579 /**--------------------------------------------------------------------------------------------------
1580 Fn: void Buffer::primitiveSort()
1581
1582 Primitive sort.
1583
1584 Author: Tyler Parke
1585
1586 Date: 2017-11-19
1587 *-----------------------------------------------------------------------------------------------**/
1588
1589 void primitiveSort()
1590 {
1591 struct primitiveUniqueDataComparison
1592 {
1593 bool operator()(const t_type& lhs, const t_type& rhs)
1594 {
1595 return memcmp(&lhs, &rhs, sizeof(t_type)) > 0;
1596 }
1597 };
1598 std::sort(begin(), end(), primitiveUniqueDataComparison());
1599 }
1600
1601 /**--------------------------------------------------------------------------------------------------
1602 Fn: void Buffer::sort()
1603
1604 Sorts this object.
1605
1606 Author: Tyler Parke
1607
1608 Date: 2017-11-19
1609 *-----------------------------------------------------------------------------------------------**/
1610
1611 void sort()
1612 {
1613 std::sort(begin(), end());
1614 }
1615
1616 /**--------------------------------------------------------------------------------------------------
1617 Fn: void Buffer::sortRange(t_index_type start, t_index_type end)
1618
1619 Sort range.
1620
1621 Author: Tyler Parke
1622
1623 Date: 2017-11-19
1624
1625 Parameters:
1626 start - The start.
1627 end - The end.
1628 *-----------------------------------------------------------------------------------------------**/
1629
1630 void sortRange(t_index_type start, t_index_type end)
1631 {
1632 std::sort(begin() + start, begin() + end);
1633 }
1634
1635 /**--------------------------------------------------------------------------------------------------
1636 Fn: void Buffer::setUnique()
1637
1638 Sets the unique.
1639
1640 Author: Tyler Parke
1641
1642 Date: 2017-11-19
1643 *-----------------------------------------------------------------------------------------------**/
1644
1645 void setUnique()
1646 {
1647 if constexpr (t_memory_allocator::isPrimitive())
1648 primitiveSort();
1649 else
1650 sort();
1651 removeAllIndex(cast<t_index_type>(std::unique(begin(), end()) - begin()), size());
1652 }
1653
1654 /**--------------------------------------------------------------------------------------------------
1655 Fn: void Buffer::setUniquePresorted()
1656
1657 Sets unique presorted.
1658
1659 Author: Tyler Parke
1660
1661 Date: 2017-11-19
1662 *-----------------------------------------------------------------------------------------------**/
1663
1664 void setUniquePresorted()
1665 {
1666 removeAllIndex(cast<t_index_type>(std::unique(begin(), end()) - begin()), size());
1667 }
1668
1669 /**--------------------------------------------------------------------------------------------------
1670 Fn: t_index_type Buffer::sortAboutValue(t_index_type value_index)
1671
1672 Sort about value.
1673
1674 Author: Tyler Parke
1675
1676 Date: 2017-11-19
1677
1678 Parameters:
1679 value_index - Zero-based index of the value.
1680
1681 Returns: The sorted about value.
1682 *-----------------------------------------------------------------------------------------------**/
1683
1684 t_index_type sortAboutValue(t_index_type value_index)
1685 {
1686 return sortAboutValue(value_index, 0, size());
1687 }
1688
1689 /**--------------------------------------------------------------------------------------------------
1690 Fn: t_index_type Buffer::sortAboutValue(t_index_type value_index, t_index_type start, t_index_type end)
1691
1692 Sort about value.
1693
1694 Author: Tyler Parke
1695
1696 Date: 2017-11-19
1697
1698 Parameters:
1699 value_index - Zero-based index of the value.
1700 start - The start.
1701 end - The end.
1702
1703 Returns: The sorted about value.
1704 *-----------------------------------------------------------------------------------------------**/
1705
1706 t_index_type sortAboutValue(t_index_type value_index, t_index_type start, t_index_type end)
1707 {
1708 const t_type value = get(value_index);
1709 std::swap(get(value_index), get(--end));
1710 t_index_type store = start;
1711 for (t_index_type idx = start; idx < end; ++idx)
1712 {
1713 if (get(idx) <= value)
1714 {
1715 std::swap(get(idx), get(store));
1716 store++;
1717 }
1718 }
1719 std::swap(get(end), get(store));
1720 return store;
1721 }
1722
1723
1724 /**--------------------------------------------------------------------------------------------------
1725 Fn: t_type Buffer::average() const
1726
1727 Determines the average value.
1728
1729 Author: Tyler Parke
1730
1731 Date: 2017-11-19
1732
1733 Returns: The average value.
1734 *-----------------------------------------------------------------------------------------------**/
1735 /*template<class ttype = t_type>
1736 [[nodiscard]] typename std::enable_if<ObjectInfo<ttype>::Number, ttype>::type average() const
1737 {
1738 return cast<t_type>(summation() / t_type(size()));
1739 }*/
1740
1741 /**--------------------------------------------------------------------------------------------------
1742 Fn: t_type Buffer::summation() const
1743
1744 Gets the summation.
1745
1746 Author: Tyler Parke
1747
1748 Date: 2017-11-19
1749
1750 Returns: A t_type.
1751 *-----------------------------------------------------------------------------------------------**/
1752 /*template<class ttype = t_type>
1753 [[nodiscard]] typename std::enable_if<ObjectInfo<ttype>::Number, ttype>::type summation() const
1754 {
1755 return std::accumulate<const t_type*, t_type>(begin(), end(), t_type(0));
1756 }*/
1757
1758 /**--------------------------------------------------------------------------------------------------
1759 Fn: inline Buffer& Buffer::operator=(const Buffer& buffer)
1760
1761 Assignment operator.
1762
1763 Author: Tyler Parke
1764
1765 Date: 2017-11-19
1766
1767 Parameters:
1768 buffer - The buffer.
1769
1770 Returns: A shallow copy of this object.
1771 *-----------------------------------------------------------------------------------------------**/
1772
1773 inline Buffer& operator=(const Buffer& buffer)
1774 {
1775 if (&(buffer) == this)
1776 return *this;//check to ensure not setting equal to ourselves
1777 clear();
1778 addAll(buffer);
1779 return *this;
1780 }
1781
1782 /**--------------------------------------------------------------------------------------------------
1783 Fn: inline Buffer& Buffer::operator=(Buffer&& buffer)
1784
1785 Move assignment operator.
1786
1787 Author: Tyler Parke
1788
1789 Date: 2017-11-19
1790
1791 Parameters:
1792 buffer - [in,out] The buffer.
1793
1794 Returns: A shallow copy of this object.
1795 *-----------------------------------------------------------------------------------------------**/
1796
1797 inline Buffer& operator=(Buffer&& buffer) noexcept
1798 {
1799 std::swap(m_memory_interface, buffer.m_memory_interface);
1800 return *this;
1801 }
1802
1803 /**--------------------------------------------------------------------------------------------------
1804 Fn: inline bool Buffer::operator==(const Buffer& buffer) const
1805
1806 Equality operator.
1807
1808 Author: Tyler Parke
1809
1810 Date: 2017-11-19
1811
1812 Parameters:
1813 buffer - The buffer.
1814
1815 Returns: True if the parameters are considered equivalent.
1816 *-----------------------------------------------------------------------------------------------**/
1817
1818 [[nodiscard]] inline bool operator==(const Buffer& buffer) const
1819 {
1820 return equals(buffer);
1821 }
1822
1823 /**--------------------------------------------------------------------------------------------------
1824 Fn: inline bool Buffer::operator>(const Buffer& buffer) const
1825
1826 Greater-than comparison operator.
1827
1828 Author: Tyler Parke
1829
1830 Date: 2017-11-19
1831
1832 Parameters:
1833 buffer - The buffer.
1834
1835 Returns: True if the first parameter is greater than to the second.
1836 *-----------------------------------------------------------------------------------------------**/
1837
1838 [[nodiscard]] inline bool operator>(const Buffer& buffer) const
1839 {
1840 return compare(buffer) > 0;
1841 }
1842/** . */
1843 [[nodiscard]] inline bool operator<(const Buffer& buffer) const
1844 {
1845 return compare(buffer) < 0;
1846 }
1847
1848 /**--------------------------------------------------------------------------------------------------
1849 Fn: inline bool Buffer::operator>=(const Buffer& buffer) const
1850
1851 Greater-than-or-equal comparison operator.
1852
1853 Author: Tyler Parke
1854
1855 Date: 2017-11-19
1856
1857 Parameters:
1858 buffer - The buffer.
1859
1860 Returns: True if the first parameter is greater than or equal to the second.
1861 *-----------------------------------------------------------------------------------------------**/
1862
1863 [[nodiscard]] inline bool operator>=(const Buffer& buffer) const
1864 {
1865 return compare(buffer) >= 0;
1866 }
1867/** . */
1868 [[nodiscard]] inline bool operator<=(const Buffer& buffer) const
1869 {
1870 return compare(buffer) <= 0;
1871 }
1872
1873 /**--------------------------------------------------------------------------------------------------
1874 Fn: inline bool Buffer::operator!=(const Buffer& buffer) const
1875
1876 Inequality operator.
1877
1878 Author: Tyler Parke
1879
1880 Date: 2017-11-19
1881
1882 Parameters:
1883 buffer - The buffer.
1884
1885 Returns: True if the parameters are not considered equivalent.
1886 *-----------------------------------------------------------------------------------------------**/
1887
1888 [[nodiscard]] inline bool operator!=(const Buffer& buffer) const
1889 {
1890 return !equals(buffer);
1891 }
1892
1893 /**--------------------------------------------------------------------------------------------------
1894 Fn: Buffer::decltype(auto) operator[](const t_index_type index)
1895
1896 Constructor.
1897
1898 Author: Tyler Parke
1899
1900 Date: 2017-11-19
1901
1902 Parameters:
1903 parameter1 - The first parameter.
1904 *-----------------------------------------------------------------------------------------------**/
1905 [[nodiscard]] decltype(auto) operator[](const t_index_type index)
1906 {
1907 return get(index);
1908 }
1909 [[nodiscard]] decltype(auto) operator[](const int index)
1910 {
1911 return get(cast<t_index_type>(index));
1912 }
1913
1914 /**--------------------------------------------------------------------------------------------------
1915 Fn: Buffer::decltype(auto) operator[](const t_index_type value) const
1916
1917 Constructor.
1918
1919 Author: Tyler Parke
1920
1921 Date: 2017-11-19
1922
1923 Parameters:
1924 parameter1 - The first parameter.
1925 *-----------------------------------------------------------------------------------------------**/
1926
1927 [[nodiscard]] decltype(auto) operator[](const t_index_type value) const
1928 {
1929 return get(value);
1930 }
1931 [[nodiscard]] decltype(auto) operator[](const int index) const
1932 {
1933 return get(cast<t_index_type>(index));
1934 }
1935 /**--------------------------------------------------------------------------------------------------
1936 Fn: Buffer Buffer::operator+(const t_type& element) const
1937
1938 Addition operator.
1939
1940 Author: Tyler Parke
1941
1942 Date: 2017-11-19
1943
1944 Parameters:
1945 element - The element.
1946
1947 Returns: The result of the operation.
1948 *-----------------------------------------------------------------------------------------------**/
1949
1950 [[nodiscard]] Buffer operator+(const t_type& element) const
1951 {
1952 Buffer<t_type, t_memory_allocator, t_memory_manager> buffer(1 + size());
1953 buffer.addAll(*this);
1954 buffer.add(element);
1955 return buffer;
1956 }
1957
1958 /**--------------------------------------------------------------------------------------------------
1959 Fn: const Buffer& Buffer::operator+=(const t_type& element)
1960
1961 Addition assignment operator.
1962
1963 Author: Tyler Parke
1964
1965 Date: 2017-11-19
1966
1967 Parameters:
1968 element - The element.
1969
1970 Returns: The result of the operation.
1971 *-----------------------------------------------------------------------------------------------**/
1972
1973 const Buffer& operator+=(const t_type& element)
1974 {
1975 add(element);
1976 return *this;
1977 }
1978
1979 /**--------------------------------------------------------------------------------------------------
1980 Fn: inline Buffer& Buffer::operator+=(const Buffer& value)
1981
1982 Addition assignment operator.
1983
1984 Author: Tyler Parke
1985
1986 Date: 2017-11-19
1987
1988 Parameters:
1989 value - The value.
1990
1991 Returns: The result of the operation.
1992 *-----------------------------------------------------------------------------------------------**/
1993
1994 inline Buffer& operator+=(const Buffer& value)
1995 {
1996 addAll(value);
1997 return (*this);
1998 }
1999
2000 /**--------------------------------------------------------------------------------------------------
2001 Fn: inline Buffer Buffer::operator+(const Buffer& value) const
2002
2003 Addition operator.
2004
2005 Author: Tyler Parke
2006
2007 Date: 2017-11-19
2008
2009 Parameters:
2010 value - The value.
2011
2012 Returns: The result of the operation.
2013 *-----------------------------------------------------------------------------------------------**/
2014
2015 [[nodiscard]] inline Buffer operator+(const Buffer& value) const
2016 {
2017 Buffer buffer(size() + value.size());
2018 buffer.addAll(*this);
2019 buffer.addAll(value);
2020 return buffer;
2021 }
2022
2023 /**--------------------------------------------------------------------------------------------------
2024 Fn: Buffer Buffer::isSorted() const
2025
2026 Uses the '>' operator, parsing over the whole buffer to check if the buffer is ordered or not
2027
2028 Author: Tyler Parke
2029
2030 Date: 2019-11-9
2031
2032 Parameters:
2033 value - The value.
2034
2035 Returns: true if the list is sorted, false if it is unsorted
2036 *-----------------------------------------------------------------------------------------------**/
2037 [[nodiscard]] bool isSorted() const
2038 {
2039 if (size() <= 1)
2040 return true;
2041 for (t_index_type i = 0; i < size() - 1; i++)
2042 {
2043 if (get(i) >= get(i + 1))
2044 return false;
2045 }
2046 return true;
2047 }
2048
2049 /**--------------------------------------------------------------------------------------------------
2050 Fn: Buffer Buffer::isSorted() const
2051
2052 Uses the '>=' operator, parsing over the whole buffer to check if the buffer is ordered or not
2053
2054 Author: Tyler Parke
2055
2056 Date: 2019-11-9
2057
2058 Parameters:
2059 value - The value.
2060
2061 Returns: true if the list is sorted, false if it is unsorted
2062 *-----------------------------------------------------------------------------------------------**/
2063 [[nodiscard]] bool isSortedSet() const
2064 {
2065 for (t_index_type i = 0; i < size() - 1; i++)
2066 {
2067 if (get(i) > get(i + 1))
2068 return false;
2069 }
2070 return true;
2071 }
2072 [[nodiscard]] constexpr static t_type Type() { t_type* ptr = nullptr; return *ptr; }
2073
2074 [[nodiscard]] constexpr t_memory_manager& memoryInterface()
2075 {
2076 return m_memory_interface;
2077 }
2078 [[nodiscard]] constexpr const t_memory_manager& memoryInterface() const
2079 {
2080 return m_memory_interface;
2081 }
2082 protected:
2083 /**--------------------------------------------------------------------------------------------------
2084 Fn: template<bool t_is_primitive> bool Buffer::_equals(const Buffer& buffer) const;
2085
2086 Equals the given buffer.
2087
2088 Author: Tyler Parke
2089
2090 Date: 2017-11-19
2091
2092 Typeparams:
2093 t_is_primitive - Type of the is primitive.
2094 Parameters:
2095 buffer - The buffer.
2096
2097 Returns: True if the objects are considered equal, false if they are not.
2098 *-----------------------------------------------------------------------------------------------**/
2099
2100 template<bool t_is_primitive>
2101 [[nodiscard]] bool _equals(const Buffer& buffer) const
2102 {
2103 if (t_is_primitive)
2104 {
2105 return compare(buffer) == 0;
2106 }
2107 else
2108 {
2109 for (t_index_type i = 0; i < size(); i++)
2110 {
2111 if (get(i) != buffer.get(i))
2112 return false;
2113 }
2114 return true;
2115 }
2116 }
2117 protected:
2118 /** The memory interface. */
2119 t_memory_manager m_memory_interface;
2120 };
2121 template<class t_type, class t_memory_allocator, class t_memory_manager>
2122 struct Constant<Buffer<t_type, t_memory_allocator, t_memory_manager>>
2123 {
2124 /** The NaN value. This is the value that means the vector is 'not valid' or has an unset dimension. */
2125 inline const static Buffer<t_type, t_memory_allocator, t_memory_manager> NaN{};
2126 };
2127}
2128#include "BoolBuffer.hpp"
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:70
Definition Pointer.hpp:297
Definition ObjectInfo.h:44
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:64
void add(t_index_type location, t_type &&object)
Definition Buffer.hpp:229
decltype(auto) get(t_index_type index) const
Definition Buffer.hpp:876
constexpr Buffer()
Definition Buffer.hpp:66
bool equals(const Buffer &buffer) const
Definition Buffer.hpp:834
void add(t_type &&object)
Definition Buffer.hpp:199
sint04 compare(const Buffer &value) const
Definition Buffer.hpp:613
constexpr t_memory_manager & memoryInterface()
Definition Buffer.hpp:2074
void insert(t_index_type offset, const Buffer &buffer)
Definition Buffer.hpp:332
void addSpace(t_index_type space_to_add)
Definition Buffer.hpp:440
void add(const t_type &object)
Definition Buffer.hpp:204
bool contains(const t_type &element) const
Definition Buffer.hpp:674
t_index_type lastIndexOf(const t_type &element) const
Definition Buffer.hpp:1016
void addAll(const t_type *buffer)
Definition Buffer.hpp:305
decltype(auto) begin(t_index_type index) const
Definition Buffer.hpp:540
void removeAllUnordered(const t_type &object)
Definition Buffer.hpp:1119
void addAndFillSpace(t_index_type space_to_add, const t_type &fill_object)
Definition Buffer.hpp:447
t_index_type capacity() const
Definition Buffer.hpp:557
void setAll(const t_o_type *src, t_index_type offset, t_index_type size)
Definition Buffer.hpp:1313
Buffer getAll(t_index_type start, t_index_type size)
Definition Buffer.hpp:883
constexpr t_index_type size() const
Definition Buffer.hpp:1461
void removeAllOrdered(const t_type &object)
Definition Buffer.hpp:1176
decltype(auto) ptr()
Definition Buffer.hpp:489
constexpr Buffer(Buffer &&buffer) noexcept
Definition Buffer.hpp:74
void removeAllIndex(t_index_type start, t_index_type end)
Definition Buffer.hpp:1205
Buffer(std::initializer_list< t_type > l)
Definition Buffer.hpp:97
t_index_type indexOf(const t_type &element, t_index_type start_pos, t_index_type search_size) const
Definition Buffer.hpp:938
decltype(auto) last()
Definition Buffer.hpp:977
void addAll(const Buffer< t_type, t_other_index_type, t_other_memory_allocator, t_other_memory_manager > &buffer)
Definition Buffer.hpp:248
void addSpace(t_index_type location, t_index_type size)
Definition Buffer.hpp:468
void removeIndexBackSwap(t_index_type location)
Definition Buffer.hpp:1056
decltype(auto) end()
Definition Buffer.hpp:746
void setSize(t_index_type new_size)
Definition Buffer.hpp:1413
void replaceIndexRange(t_index_type offset, t_index_type replace_size, const Buffer &buffer)
Definition Buffer.hpp:346
t_index_type indexOf(const t_type &element) const
Definition Buffer.hpp:905
Buffer(const t_iterator &begin, const t_iterator &end)
Definition Buffer.hpp:104
void removeAllUnordered(const t_functor &functor)
Definition Buffer.hpp:1149
decltype(auto) end(t_index_type index)
Definition Buffer.hpp:782
void swapIndices(t_index_type index_1, t_index_type index_2)
Definition Buffer.hpp:1550
void insert(t_index_type offset, const t_type *const buffer, t_index_type buffer_size)
Definition Buffer.hpp:384
sint04 compare(const Buffer &value, t_index_type start, t_index_type end) const
Definition Buffer.hpp:644
void removeIndex(t_index_type location)
Definition Buffer.hpp:1037
Buffer(t_index_type size)
Definition Buffer.hpp:78
void add(t_index_type location, const t_type &object)
Definition Buffer.hpp:224
void ensureCapacity(t_index_type new_capacity, bool ensure_not_greater=false, bool ensure_not_less=true)
Definition Buffer.hpp:803
decltype(auto) ptr() const
Definition Buffer.hpp:494
Buffer< t_other_type, t_other_index_type, t_other_memory_allocator, t_other_memory_manager > getAs() const
Definition Buffer.hpp:177
void clear(t_index_type new_capacity)
Definition Buffer.hpp:591
decltype(auto) begin()
Definition Buffer.hpp:504
bool _equals(const Buffer &buffer) const
Definition Buffer.hpp:2101
t_index_type memSize() const
Definition Buffer.hpp:499
t_memory_manager m_memory_interface
Definition Buffer.hpp:2119
Buffer(t_index_type size, const t_type &fill_object)
Definition Buffer.hpp:90
~Buffer()
Definition Buffer.hpp:125
bool contains(const t_type &element, const std::function< bool(const t_type &, const t_type &)> &equal_function) const
Definition Buffer.hpp:683
void clear()
Definition Buffer.hpp:572
Buffer(const t_type *buffer, t_index_type size)
Definition Buffer.hpp:83
decltype(auto) begin() const
Definition Buffer.hpp:522
bool isEmpty() const
Definition Buffer.hpp:960
decltype(auto) last() const
Definition Buffer.hpp:995
decltype(auto) get(t_index_type index)
Definition Buffer.hpp:857
bool contains(const t_type &element, t_index_type start) const
Definition Buffer.hpp:692
decltype(auto) end() const
Definition Buffer.hpp:764
void insert(const t_index_type location, const t_type *buffer)
Definition Buffer.hpp:414
bool removeElement(const t_type &element)
Definition Buffer.hpp:1076
t_index_type indexOf(const t_type &element, t_index_type start_pos) const
Definition Buffer.hpp:930
t_index_type count(const t_type &element) const
Definition Buffer.hpp:728
void removeLast()
Definition Buffer.hpp:1099
void addAll(const t_type *buffer, t_index_type buffer_size)
Definition Buffer.hpp:277
bool contains(const t_type &element, t_index_type start, t_index_type search_size) const
Definition Buffer.hpp:702
constexpr Buffer(const Buffer &buffer)
Definition Buffer.hpp:69
Definition ACIColor.h:37
int32_t sint04
-Defines an alias representing a 4 byte, signed integer. -Can represent exact integer values -2147483...
Definition BaseValues.hpp:76
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
constexpr bool isNaN(const t_type &value)
Query if 'value' is valid or invalid.
Definition BaseFunctions.hpp:200
Definition BaseValues.hpp:272