summaryrefslogtreecommitdiffhomepage
path: root/src/RTree.cpp
blob: f77d06978d3bcdb6b794534d663d56c79b470dd7 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#define RTREE_NO_EXTERN_TEMPLATE
#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 {

#ifdef RTREE_POOL_DEBUG
static size_t fresh_counter, reuse_counter, dtor_counter; // NOLINT
#endif

template<typename T> rtree_pool<T>::rtree_pool() noexcept = default;

template<typename T>
rtree_pool<T>::~rtree_pool()
{
#ifdef RTREE_POOL_DEBUG
    auto last = dtor_counter;
#endif
    while (free_list)
    {
#ifdef RTREE_POOL_DEBUG
        ++dtor_counter;
#endif
        auto* p = free_list;
        free_list = free_list->next;
        p->data.~T();
        delete p;
    }
#ifdef RTREE_POOL_DEBUG
    if (dtor_counter != last) { Debug{} << "rtree-pool: dtor" << dtor_counter; std::fflush(stdout); }
#endif
}

template<typename T> T* rtree_pool<T>::construct()
{
    if (!free_list)
    {
#ifdef RTREE_POOL_DEBUG
        Debug{} << "rtree-pool: fresh"_s << ++fresh_counter; std::fflush(stdout);
#endif
        auto* ptr = new node_u;
        auto* ret = new(&ptr->data) T;
        return ret;
    }
    else
    {
        auto* ret = free_list;
        free_list = free_list->next;
        new (&ret->data) T();
#ifdef RTREE_POOL_DEBUG
        Debug{} << "rtree-pool: reused"_s << ++reuse_counter; std::fflush(stdout);
#endif
        return &ret->data;
    }
}

template<typename T> void rtree_pool<T>::free(T* ptr)
{
    ptr->~T();
    node_p p = {.ptr = ptr };
    node_u* n = p.data_ptr;
    n->next = free_list;
    free_list = n;
}

} // namespace floormat::detail

using my_rtree = RTree<floormat::uint64_t, float, 2, float>;
template struct floormat::detail::rtree_pool<my_rtree::Node>;
template struct floormat::detail::rtree_pool<my_rtree::ListNode>;
template<> floormat::detail::rtree_pool<my_rtree::Node> my_rtree::node_pool = {}; // NOLINT
template<> floormat::detail::rtree_pool<my_rtree::ListNode> my_rtree::list_node_pool = {}; // NOLINT
template class RTree<floormat::uint64_t, float, 2, float>;