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>
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>>
97 Buffer(std::initializer_list<t_type> l)
103 template<
class t_iterator>
109 for (
auto iter =
begin; iter !=
end; iter++)
111 get(index++) = *iter;
142 operator const t_type* ()
const
180 for (t_index_type i = 0; i <
size(); i++)
182 other.
add(t_other_type(
get(i)));
204 void add(
const t_type&
object)
224 void add(t_index_type location,
const t_type&
object)
229 void add(t_index_type location, t_type&&
object)
232 t_memory_allocator::template allocateElement(
m_memory_interface, location, std::move(
object));
247 template<
class t_other_memory_allocator,
class t_other_index_type,
class t_other_memory_manager>
250 t_index_type offset =
size();
252 if constexpr (t_memory_allocator::isPrimitive())
258 for (t_index_type i = 0; i < buffer.
size(); i++)
259 t_memory_allocator::template allocateElement(
m_memory_interface, i + offset, buffer[i]);
277 void addAll(
const t_type* buffer, t_index_type buffer_size)
279 t_index_type offset =
size();
281 if constexpr (t_memory_allocator::isPrimitive())
287 for (t_index_type i = 0; i < buffer_size; i++)
307 t_index_type
size = 0;
311 if (memcmp(&buffer[
size], &loc,
sizeof(t_type)) == 0)
335 if constexpr (t_memory_allocator::isPrimitive())
341 for (t_index_type i = 0; i < buffer.
size(); i++)
348 lib_assert(offset + replace_size <=
size(),
"end > size in buffer replace");
349 if (replace_size < buffer.
size())
351 t_index_type size_dff = buffer.
size() - replace_size;
353 if constexpr (!t_memory_allocator::isPrimitive())
355 for (t_index_type i = 0; i < size_dff; i++)
359 else if (replace_size > buffer.
size())
381 #pragma clang diagnostic push
382 #pragma clang diagnostic ignored "-Wunused-value"
384 void insert(t_index_type offset,
const t_type*
const buffer, t_index_type buffer_size)
387 if constexpr (t_memory_allocator::isPrimitive())
393 for (t_index_type i = 0; i < buffer_size; i++)
398 #pragma clang diagnostic pop
414 void insert(
const t_index_type location,
const t_type* buffer)
416 t_index_type
size = 0;
420 if (memcmp(&buffer[
size], &loc,
sizeof(t_type)) == 0)
439 template<
bool t_managed>
442 const t_index_type offset =
size();
446 template<
bool t_managed = false>
449 const t_index_type offset =
size();
451 t_memory_allocator::template allocateElement<t_type>(
m_memory_interface, offset,
size() - offset, fill_object);
467 template<
bool t_managed>
470 lib_assert(location <= this->
size(),
"Tried to insert into a bad location");
489 [[nodiscard]]
decltype(
auto)
ptr()
494 [[nodiscard]]
decltype(
auto)
ptr()
const
504 [[nodiscard]]
decltype(
auto)
begin()
522 [[nodiscard]]
decltype(
auto)
begin()
const
540 [[nodiscard]]
decltype(
auto)
begin(t_index_type index)
const
591 void clear(t_index_type new_capacity)
674 [[nodiscard]]
bool contains(
const t_type& element)
const
676 for (t_index_type i = 0; i <
size(); ++i)
678 if (
get(i) == element)
683 [[nodiscard]]
bool contains(
const t_type& element,
const std::function<
bool(
const t_type&,
const t_type&)>& equal_function)
const
687 if (equal_function(
get(i), element))
692 [[nodiscard]]
bool contains(
const t_type& element, t_index_type start)
const
694 lib_assert(start <
size(),
"buffer contains using bad start value");
695 for (t_index_type i = start; i <
size(); ++i)
697 if (
get(i) == element)
702 [[nodiscard]]
bool contains(
const t_type& element, t_index_type start, t_index_type search_size)
const
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)
707 if (
get(i) == element)
728 [[nodiscard]] t_index_type
count(
const t_type& element)
const
764 [[nodiscard]]
decltype(
auto)
end()
const
782 decltype(
auto)
end(t_index_type index)
803 void ensureCapacity(t_index_type new_capacity,
bool ensure_not_greater =
false,
bool ensure_not_less =
true)
812 else if (ensure_not_greater)
814 lib_assert(new_capacity >=
size(),
"Array can't have capacity less than size");
857 [[nodiscard]]
decltype(
auto)
get(t_index_type index)
876 [[nodiscard]]
decltype(
auto)
get(t_index_type index)
const
905 [[nodiscard]] t_index_type
indexOf(
const t_type& element)
const
907 for (t_index_type i = 0; i <
size(); ++i)
909 if (
get(i) == element)
return i;
930 [[nodiscard]] t_index_type
indexOf(
const t_type& element, t_index_type start_pos)
const
932 for (t_index_type i = start_pos; i <
size(); ++i)
934 if (
get(i) == element)
return i;
938 [[nodiscard]] t_index_type
indexOf(
const t_type& element, t_index_type start_pos, t_index_type search_size)
const
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)
943 if (
get(i) == element)
return i;
977 [[nodiscard]]
decltype(
auto)
last()
995 [[nodiscard]]
decltype(
auto)
last()
const
1018 for (t_index_type i =
size() - 1; i <
size(); --i)
1020 if (
get(i) == element)
return i;
1078 for (t_index_type i =
size() - 1; !
isNaN(i); --i)
1080 if (
get(i) == element)
1122 for (t_index_type i = 0; i <
end; i++)
1124 if (
get(i) ==
object)
1148 template<
class t_functor>
1152 for (t_index_type i = 0; i <
end; i++)
1154 if (functor(
get(i)))
1178 t_index_type start = 0;
1179 for (t_index_type i = 0; i <
size(); i++)
1182 if (
get(i) !=
object)
1187 if (start !=
size())
1210 t_memory_allocator::template deallocate<t_type>(m_memory_interface, start, end - start);
1211 m_memory_interface.removeAllIndex(start, end);
1213 template<class t_range_buffer>
1214 void removeAllIndices(const t_range_buffer& ranges)
1217 uint04 last_delete_index = 0;
1219 for (const auto& range : ranges)
1221 t_index_type start = range.first;
1222 t_index_type end = range.second;
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;
1228 t_memory_allocator::template deallocate<t_type>(m_memory_interface, start, end - start);
1230 m_memory_interface.removeAllIndices(ranges);
1246 void replaceAll(const t_type& var, const t_type& replacement)
1248 for (t_index_type i = 0; i < size(); i++)
1251 get(i) = replacement;
1267 for (t_index_type i = 0; i < size() / 2; i++)
1269 m_memory_interface.swap(i, size() - i - 1);
1287 void reverse(const t_index_type start, const t_index_type end)
1289 const t_index_type size = end - start;
1290 for (t_index_type i = 0; i < size / 2; i++)
1292 m_memory_interface.swap(start + i, (start + size) - i - 1);
1312 template<class t_o_type>
1313 void setAll(const t_o_type* src, t_index_type offset, t_index_type size)
1315 m_memory_interface.template setAll<t_memory_allocator::isPrimitive()>(src, offset, size);
1333 void setAll(const Buffer& buffer, t_index_type offset, t_index_type size)
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);
1355 void setAll(const Buffer& buffer, t_index_type offset, t_index_type other_offset, t_index_type size)
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);
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)
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)
1385 m_memory_interface.setSize(offset + fill_size);
1387 const t_type value(fill_element);
1388 if (t_memory_allocator::isPrimitive() || size() == old_size)
1390 m_memory_interface.setAll(value, offset, fill_size);
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);
1413 void setSize(t_index_type new_size)
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);
1422 //Keep std::vector compliant
1423 void resize(t_index_type new_size)
1442 void setSize(const t_index_type new_size, const t_type& fill_element)
1444 const t_index_type old_size = size();
1446 if(old_size < new_size)
1447 setAllToValue(fill_element, old_size);
1461 [[nodiscard]] constexpr t_index_type size() const
1463 return m_memory_interface.filledSize();
1480 void swapElements(const t_type& element1, const t_type& element2)
1482 if (element1 == element2)
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++)
1488 if (get(i) == element1)
1491 if (!isNaN(index_2))
1493 swapIndices(index_1, index_2);
1497 else if (get(i) == element2)
1500 if (!isNaN(index_1))
1502 swapIndices(index_1, index_2);
1523 void swapAllElements(const t_type& element1, const t_type& element2)
1525 if (element1 == element2)
1527 for (t_index_type ii = 0; ii < size(); ++ii)
1529 if (get(ii) == element1)
1531 else if (get(ii) == element2)
1550 void swapIndices(t_index_type index_1, t_index_type index_2)
1552 std::swap(get(index_1), get(index_2));
1568 void move(t_index_type from, t_index_type to)
1572 t_type object = get(from);
1589 void primitiveSort()
1591 struct primitiveUniqueDataComparison
1593 bool operator()(const t_type& lhs, const t_type& rhs)
1595 return memcmp(&lhs, &rhs, sizeof(t_type)) > 0;
1598 std::sort(begin(), end(), primitiveUniqueDataComparison());
1613 std::sort(begin(), end());
1630 void sortRange(t_index_type start, t_index_type end)
1632 std::sort(begin() + start, begin() + end);
1647 if constexpr (t_memory_allocator::isPrimitive())
1651 removeAllIndex(cast<t_index_type>(std::unique(begin(), end()) - begin()), size());
1664 void setUniquePresorted()
1666 removeAllIndex(cast<t_index_type>(std::unique(begin(), end()) - begin()), size());
1684 t_index_type sortAboutValue(t_index_type value_index)
1686 return sortAboutValue(value_index, 0, size());
1706 t_index_type sortAboutValue(t_index_type value_index, t_index_type start, t_index_type end)
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)
1713 if (get(idx) <= value)
1715 std::swap(get(idx), get(store));
1719 std::swap(get(end), get(store));
1735 /*template<class ttype = t_type>
1736 [[nodiscard]] typename std::enable_if<ObjectInfo<ttype>::Number, ttype>::type average() const
1738 return cast<t_type>(summation() / t_type(size()));
1752 /*template<class ttype = t_type>
1753 [[nodiscard]] typename std::enable_if<ObjectInfo<ttype>::Number, ttype>::type summation() const
1755 return std::accumulate<const t_type*, t_type>(begin(), end(), t_type(0));
1773 inline Buffer& operator=(const Buffer& buffer)
1775 if (&(buffer) == this)
1776 return *this;//check to ensure not setting equal to ourselves
1797 inline Buffer& operator=(Buffer&& buffer) noexcept
1799 std::swap(m_memory_interface, buffer.m_memory_interface);
1818 [[nodiscard]] inline bool operator==(const Buffer& buffer) const
1820 return equals(buffer);
1838 [[nodiscard]] inline bool operator>(const Buffer& buffer) const
1840 return compare(buffer) > 0;
1843 [[nodiscard]] inline bool operator<(const Buffer& buffer) const
1845 return compare(buffer) < 0;
1863 [[nodiscard]] inline bool operator>=(const Buffer& buffer) const
1865 return compare(buffer) >= 0;
1868 [[nodiscard]] inline bool operator<=(const Buffer& buffer) const
1870 return compare(buffer) <= 0;
1888 [[nodiscard]] inline bool operator!=(const Buffer& buffer) const
1890 return !equals(buffer);
1905 [[nodiscard]] decltype(auto) operator[](const t_index_type index)
1909 [[nodiscard]] decltype(auto) operator[](const int index)
1911 return get(cast<t_index_type>(index));
1927 [[nodiscard]] decltype(auto) operator[](const t_index_type value) const
1931 [[nodiscard]] decltype(auto) operator[](const int index) const
1933 return get(cast<t_index_type>(index));
1950 [[nodiscard]] Buffer operator+(const t_type& element) const
1952 Buffer<t_type, t_memory_allocator, t_memory_manager> buffer(1 + size());
1953 buffer.addAll(*this);
1954 buffer.add(element);
1973 const Buffer& operator+=(const t_type& element)
1994 inline Buffer& operator+=(const Buffer& value)
2015 [[nodiscard]] inline Buffer operator+(const Buffer& value) const
2017 Buffer buffer(size() + value.size());
2018 buffer.addAll(*this);
2019 buffer.addAll(value);
2037 [[nodiscard]] bool isSorted() const
2041 for (t_index_type i = 0; i < size() - 1; i++)
2043 if (get(i) >= get(i + 1))
2063 [[nodiscard]] bool isSortedSet() const
2065 for (t_index_type i = 0; i < size() - 1; i++)
2067 if (get(i) > get(i + 1))
2072 [[nodiscard]] constexpr static t_type Type() { t_type* ptr = nullptr; return *ptr; }
2074 [[nodiscard]] constexpr t_memory_manager& memoryInterface()
2076 return m_memory_interface;
2078 [[nodiscard]] constexpr const t_memory_manager& memoryInterface() const
2080 return m_memory_interface;
2100 template<bool t_is_primitive>
2101 [[nodiscard]] bool _equals(const Buffer& buffer) const
2105 return compare(buffer) == 0;
2109 for (t_index_type i = 0; i < size(); i++)
2111 if (get(i) != buffer.get(i))
2119 t_memory_manager m_memory_interface;
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>>
2125 inline const static Buffer<t_type, t_memory_allocator, t_memory_manager> NaN{};
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
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