Skip to content

Commit c4470e4

Browse files
committed
inplace_vector
1 parent d844173 commit c4470e4

File tree

1 file changed

+87
-4
lines changed

1 file changed

+87
-4
lines changed

octree.h

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,88 @@ namespace OrthoTree
339339
assert(e >= 0 && e < (sizeof(TOut) * CHAR_BIT));
340340
return TOut{ 1 } << e;
341341
}
342+
343+
template<typename T, std::size_t N>
344+
class inplace_vector
345+
{
346+
private:
347+
using container = std::array<T, N>;
348+
349+
public:
350+
using value_type = typename container::value_type;
351+
using reference = T&;
352+
using const_reference = const T&;
353+
using size_type = typename std::size_t;
354+
using iterator = container::iterator;
355+
using const_iterator = container::const_iterator;
356+
357+
public:
358+
constexpr inplace_vector() = default;
359+
360+
template<typename... TVals>
361+
constexpr T& emplace_back(TVals&&... value)
362+
{
363+
assert(m_size < N);
364+
m_stack[m_size] = T(std::forward<TVals>(value)...);
365+
return m_stack[m_size++];
366+
}
367+
368+
369+
constexpr void push_back(const T& value)
370+
{
371+
m_stack[m_size] = value;
372+
++m_size;
373+
}
374+
375+
constexpr T& operator[](std::size_t index) { return m_stack[index]; }
376+
377+
constexpr T const& operator[](std::size_t index) const { return m_stack[index]; }
378+
379+
constexpr std::size_t size() const { return m_size; }
380+
381+
constexpr bool empty() const noexcept { return m_size == 0; }
382+
383+
constexpr iterator insert(iterator whereIt, T const& val)
384+
{
385+
for (auto it = m_stack.begin() + m_size; it != whereIt; --it)
386+
{
387+
*it = std::move(*(it - 1));
388+
}
389+
*whereIt = std::move(val);
390+
++m_size;
391+
return whereIt;
392+
}
393+
394+
constexpr bool erase(iterator it)
395+
{
396+
if (it < begin() || it >= end())
397+
{
398+
return false;
399+
}
400+
401+
for (auto it2 = it; it2 != end() - 1; ++it2)
402+
{
403+
*it2 = std::move(*(it2 + 1));
404+
}
405+
406+
--m_size;
407+
return true;
408+
}
409+
410+
constexpr void clear() { m_size = 0; }
411+
412+
constexpr iterator begin() { return m_stack.begin(); }
413+
414+
constexpr iterator end() { return m_stack.begin() + m_size; }
415+
416+
constexpr const_iterator begin() const { return m_stack.begin(); }
417+
418+
constexpr const_iterator end() const { return m_stack.begin() + m_size; }
419+
420+
private:
421+
container m_stack;
422+
std::size_t m_size = 0;
423+
};
342424
} // namespace detail
343425

344426
#ifdef _MSC_VER
@@ -1824,11 +1906,12 @@ namespace OrthoTree
18241906
{
18251907
public:
18261908
using EntityContainer = typename std::vector<TEntityID>;
1909+
using ChildContainer = typename std::conditional_t<DIMENSION_NO < 4, detail::inplace_vector<MortonNodeID, SI::CHILD_NO>, std::vector<MortonNodeID>>;
18271910

18281911
private:
1829-
EntityContainer m_entities = {};
1912+
EntityContainer m_entities;
1913+
ChildContainer m_children;
18301914

1831-
std::vector<MortonNodeID> m_children;
18321915
#ifndef ORTHOTREE__DISABLED_NODECENTER
18331916
IGM::Vector m_center;
18341917
#endif
@@ -1929,7 +2012,7 @@ namespace OrthoTree
19292012

19302013
inline constexpr bool IsAnyChildExist() const noexcept { return !m_children.empty(); }
19312014

1932-
inline constexpr std::vector<MortonNodeID> const& GetChildren() const noexcept { return m_children; }
2015+
inline constexpr auto const& GetChildren() const noexcept { return m_children; }
19332016
};
19342017

19352018
protected: // Aid struct to partitioning and distance ordering
@@ -3375,7 +3458,7 @@ namespace OrthoTree
33753458
farestEntityDistance = GetFarestDistance(neighborEntities, neighborNo);
33763459
return true;
33773460
},
3378-
[&](std::vector<MortonNodeID> const& originalChildren) {
3461+
[&](Node::ChildContainer const& originalChildren) {
33793462
auto childrenDistance = std::vector<std::pair<MortonNodeID, TGeometry>>();
33803463
for (MortonLocationIDCR childNodeKey : originalChildren)
33813464
{

0 commit comments

Comments
 (0)