25#ifndef __BITPIT_FLAT_VECTOR_2D_TPP__
26#define __BITPIT_FLAT_VECTOR_2D_TPP__
33#include "binary_stream.hpp"
46OBinaryStream&
operator<<(OBinaryStream &buffer,
const FlatVector2D<T> &vector)
48 buffer << vector.m_index;
63IBinaryStream& operator>>(IBinaryStream &buffer, FlatVector2D<T> &vector)
65 buffer >> vector.m_index;
76 : m_index(initialize ? 1 : 0, 0L)
90 initialize(sizes.size(), sizes.data(), 1, &value, 0);
104 initialize(nVectors, &size, 0, &value, 0);
118 initialize(nVectors, sizes, 1, &value, 0);
131 initialize(nVectors, sizes, 1, values, 1);
143 initialize(vector2D);
154 return (!m_index.empty());
167 initialize(sizes.size(), sizes.data(), 1, &value, 1);
181 initialize(nVectors, &size, 0, &value, 0);
195 initialize(nVectors, sizes, 1, &value, 0);
209 initialize(nVectors, sizes, 1, values, 1);
223 const std::size_t *sizes, std::size_t sizesStride,
224 const T *values, std::size_t valuesStride)
226 std::size_t nItems = 0;
227 for (std::size_t i = 0; i < nVectors; ++i) {
228 nItems += *(sizes + i * sizesStride);
236 bool reallocateIndex = (nVectors != size());
237 bool reallocateValues = (nItems != getItemCount());
245 if (reallocateIndex || reallocateValues) {
246 destroy(reallocateIndex, reallocateValues);
250 if (reallocateIndex) {
251 m_index.resize(nVectors + 1);
255 for (std::size_t i = 0; i < nVectors; ++i) {
256 m_index[i+1] = m_index[i] + *(sizes + i * sizesStride);
260 if (valuesStride == 0) {
261 if (reallocateValues) {
262 m_v.assign(nItems, *values);
264 for (std::size_t k = 0; k < nItems; ++k) {
269 if (reallocateValues) {
274 for (std::size_t i = 0; i < nVectors; ++i) {
275 std::size_t subArraySize = *(sizes + i * sizesStride);
276 for (std::size_t j = 0; j < subArraySize; ++j) {
277 m_v[k++] = *(values + i * valuesStride + j);
292 std::size_t nVectors = vector2D.size();
294 std::size_t nItems = 0;
295 for (std::size_t i = 0; i < nVectors; ++i) {
296 nItems += vector2D[i].size();
304 bool reallocateIndex = (nVectors != size());
305 bool reallocateValues = (nItems != getItemCount());
313 if (reallocateIndex || reallocateValues) {
314 destroy(reallocateIndex, reallocateValues);
318 if (reallocateIndex) {
319 m_index.resize(nVectors + 1);
323 for (std::size_t i = 0; i < nVectors; ++i) {
324 m_index[i+1] = m_index[i] + vector2D[i].size();
328 if (reallocateValues) {
333 for (std::size_t i = 0; i < nVectors; ++i) {
334 std::size_t subArraySize = vector2D[i].size();
335 for (std::size_t j = 0; j < subArraySize; ++j) {
336 m_v[k++] = vector2D[i][j];
350 m_v.assign(other.m_v.begin(), other.m_v.end());
351 m_index.assign(other.m_index.begin(), other.m_index.end());
380 m_index.shrink_to_fit();
403 m_index.reserve(nVectors + 1);
417 m_index.swap(other.m_index);
430 std::fill(m_v.begin(), m_v.end(), value);
441 return m_index == rhs.m_index && m_v == rhs.m_v;
469 std::vector<T>(0).swap(m_v);
471 std::vector<size_t>(1, 0L).swap(m_index);
493 std::size_t nVectors = size();
495 std::vector<T>(0).swap(m_v);
497 std::vector<size_t>(nVectors + 1, 0L).swap(m_index);
501 m_index.assign(nVectors + 1, 0L);
514 m_index.shrink_to_fit();
527 return m_index.data();
543 return (m_index.data() + i);
614 std::size_t previousLastIndex = m_index.back();
615 m_index.emplace_back(previousLastIndex + subArraySize);
617 m_v.resize(m_v.size() + subArraySize, value);
631 pushBack(subArray.size(), subArray.data());
646 std::size_t previousLastIndex = m_index.back();
647 m_index.emplace_back(previousLastIndex + subArraySize);
649 m_v.insert(m_v.end(), subArray, subArray + subArraySize);
664 m_v.emplace_back(value);
679 m_v.emplace_back(std::move(value));
693 assert(isIndexValid(i));
695 m_v.insert(m_v.begin() + m_index[i+1], value);
697 std::size_t nIndexes = m_index.size();
698 for (std::size_t k = i + 1; k < nIndexes; ++k) {
715 assert(isIndexValid(i));
717 m_v.insert(m_v.begin() + m_index[i+1], std::move(value));
719 std::size_t nIndexes = m_index.size();
720 for (std::size_t k = i + 1; k < nIndexes; ++k) {
739 m_v.resize(m_index.back() + 1);
750 if (getItemCount(size() - 1) == 0) {
755 m_v.resize(m_index.back() + 1);
768 assert(isIndexValid(i));
770 if (getItemCount(i) == 0) {
774 m_v.erase(m_v.begin() + m_index[i+1] - 1);
776 std::size_t nIndexes = m_index.size();
777 for (std::size_t k = i + 1; k < nIndexes; ++k) {
793 assert(isIndexValid(i));
795 m_v.erase(m_v.begin() + m_index[i], m_v.begin() + m_index[i+1] - 1);
796 m_index.erase(m_index.begin() + i + 1);
808 assert(isIndexValid(i, j));
810 m_v.erase(m_v.begin() + m_index[i] + j);
812 std::size_t nIndexes = m_index.size();
813 for (std::size_t k = i + 1; k < nIndexes; ++k) {
828 assert(isIndexValid(i, j));
829 (*this)[i][j] = value;
842 assert(isIndexValid(i, j));
843 (*this)[i][j] = std::move(value);
856 assert(isIndexValid(i, j));
857 return (*
this)[i][j];
870 assert(isIndexValid(i, j));
871 return (*
this)[i][j];
884 assert(isIndexValid(i));
898 assert(isIndexValid(i));
923 m_v[k] = std::move(value);
958 return get(size() - 1);
980 if (!isInitialized()) {
984 return (m_index.size() - 1);
997 if (!isInitialized()) {
1001 return m_index.capacity() - 1;
1014 m_index[1] = m_index.back();
1026 if (!isInitialized()) {
1042 if (!isInitialized()) {
1046 return m_index[i + 1] - m_index[i];
1059 return m_v.capacity();
1070 return ((2 + m_index.size())*
sizeof(
size_t) + m_v.size() *
sizeof(T));
1082 assert(isIndexValid(i));
1088 std::size_t index = m_index[i];
1099T* FlatVector2D<T>::operator[](std::size_t i)
1101 assert(isIndexValid(i));
1107 std::size_t index = m_index[i];
1118bool FlatVector2D<T>::isIndexValid(std::size_t i)
const
1120 return (i < size());
1131bool FlatVector2D<T>::isIndexValid(std::size_t i, std::size_t j)
const
1133 if (!isIndexValid(i)) {
1137 return (j < (m_index[i+1] - m_index[i]));
Metafunction for generation of a flattened vector of vectors.
const std::size_t * indices() const noexcept
bool isInitialized() const
void clearItems(bool release=true)
T & getItem(std::size_t i, std::size_t j)
bool operator==(const FlatVector2D &rhs) const
void swap(FlatVector2D &other) noexcept
T & rawGetItem(std::size_t k)
const std::vector< T > & vector() const
std::size_t getItemCapacity() const
FlatVector2D(bool initialize=true)
void rawSetItem(std::size_t k, const T &value)
void clear(bool release=true)
void reserve(std::size_t nVectors, std::size_t nItems=0)
std::size_t getItemCount() const
void initialize(const std::vector< std::size_t > &sizes, const T &value=T())
void eraseItem(std::size_t i, std::size_t j)
void erase(std::size_t i)
const T * get(std::size_t i) const
std::size_t capacity() const
std::size_t getBinarySize() const
void setItem(std::size_t i, std::size_t j, const T &value)
void pushBackItem(const T &value)
Logger & operator<<(Logger &logger, LoggerManipulator< T > &&m)