summaryrefslogtreecommitdiffhomepage
path: root/src/RTree.cpp
blob: bee4641c16f991d145ab110d6b4d77c10adc7eb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#define RTREE_NO_EXTERN_TEMPLATE_POOL
#include "RTree.hpp"

//#define RTREE_POOL_DEBUG

#ifdef RTREE_POOL_DEBUG
#include <cstdio>
#include <Corrade/Containers/StringView.h>
#include <Corrade/Utility/Debug.h>
#endif

namespace floormat::detail {

template<typename T> rtree_pool<T>::rtree_pool()
{
    free_list.reserve(128);
}

template<typename T> rtree_pool<T>::~rtree_pool()
{
    for (auto* ptr : free_list)
        ::operator delete(ptr);
    free_list.clear();
    free_list.shrink_to_fit();
}

template<typename T> T* rtree_pool<T>::construct()
{
    if (free_list.empty())
    {
#ifdef RTREE_POOL_DEBUG
        static unsigned i = 0; Debug{} << "rtree-pool: fresh"_s << ++i; std::fflush(stdout);
#endif
        return new T;
    }
    else
    {
        auto* ret = free_list.back();
        free_list.pop_back();
        new (ret) T();
#ifdef RTREE_POOL_DEBUG
        static unsigned i = 0; Debug{} << "rtree-pool: reused"_s << ++i; std::fflush(stdout);
#endif
        return ret;
    }
}

template<typename T> void rtree_pool<T>::free(T* ptr)
{
    ptr->~T();
    free_list.push_back(ptr);
}

using my_rtree = RTree<uint64_t, float, 2, float>;

template<> std::vector<my_rtree::Node*> rtree_pool<my_rtree::Node>::free_list = {}; // NOLINT
template<> std::vector<my_rtree::ListNode*> rtree_pool<my_rtree::ListNode>::free_list = {}; // NOLINT

template struct floormat::detail::rtree_pool<my_rtree::Node>;
template struct floormat::detail::rtree_pool<my_rtree::ListNode>;

} // namespace floormat::detail

template class RTree<floormat::uint64_t, float, 2, float>;