QCAD
Open Source 2D CAD
Loading...
Searching...
No Matches
PointerPoolNode.h
Go to the documentation of this file.
1/******************************************************************************
2 * Project: libspatialindex - A C++ library for spatial indexing
3 * Author: Marios Hadjieleftheriou, [email protected]
4 ******************************************************************************
5 * Copyright (c) 2002, Marios Hadjieleftheriou
6 *
7 * All rights reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26******************************************************************************/
27
28#pragma once
29
30#include "Node.h"
31
32namespace Tools
33{
34 using namespace SpatialIndex;
36 {
37 public:
38 explicit PointerPool(uint32_t capacity) : m_capacity(capacity)
39 {
40 #ifndef NDEBUG
41 m_hits = 0;
42 m_misses = 0;
44 #endif
45 }
46
48 {
49 assert(m_pool.size() <= m_capacity);
50
51 while (! m_pool.empty())
52 {
53 TPRTree::Node* x = m_pool.top(); m_pool.pop();
54 #ifndef NDEBUG
56 #endif
57 delete x;
58 }
59
60 #ifndef NDEBUG
61 std::cerr << "Lost pointers: " << m_pointerCount << std::endl;
62 #endif
63 }
64
66 {
67 if (! m_pool.empty())
68 {
69 TPRTree::Node* p = m_pool.top(); m_pool.pop();
70 #ifndef NDEBUG
71 ++m_hits;
72 #endif
73
74 return PoolPointer<TPRTree::Node>(p, this);
75 }
76 #ifndef NDEBUG
77 else
78 {
79 // fixme: well sort of...
81 ++m_misses;
82 }
83 #endif
84
86 }
87
89 {
90 if (p != 0)
91 {
92 if (m_pool.size() < m_capacity)
93 {
94 if (p->m_pData != 0)
95 {
96 for (uint32_t cChild = 0; cChild < p->m_children; ++cChild)
97 {
98 if (p->m_pData[cChild] != 0) delete[] p->m_pData[cChild];
99 }
100 }
101
102 p->m_level = 0;
103 p->m_identifier = -1;
104 p->m_children = 0;
105 p->m_totalDataLength = 0;
106
107 m_pool.push(p);
108 }
109 else
110 {
111 #ifndef NDEBUG
113 #endif
114 delete p;
115 }
116
117 assert(m_pool.size() <= m_capacity);
118 }
119 }
120
121 uint32_t getCapacity() const { return m_capacity; }
122 void setCapacity(uint32_t c)
123 {
124 assert (c >= 0);
125 m_capacity = c;
126 }
127
128 protected:
129 uint32_t m_capacity;
130 std::stack<TPRTree::Node*> m_pool;
131
132 #ifndef NDEBUG
133 public:
134 uint64_t m_hits;
135 uint64_t m_misses;
137 #endif
138 };
139}
140
Definition Node.h:42
uint32_t m_level
Definition Node.h:99
id_type m_identifier
Definition Node.h:103
uint32_t m_totalDataLength
Definition Node.h:126
byte ** m_pData
Definition Node.h:115
uint32_t m_children
Definition Node.h:106
~PointerPool()
Definition PointerPoolNode.h:47
uint64_t m_pointerCount
Definition PointerPoolNode.h:136
std::stack< TPRTree::Node * > m_pool
Definition PointerPoolNode.h:130
uint64_t m_misses
Definition PointerPoolNode.h:135
PointerPool(uint32_t capacity)
Definition PointerPoolNode.h:38
uint32_t getCapacity() const
Definition PointerPoolNode.h:121
PoolPointer< TPRTree::Node > acquire()
Definition PointerPoolNode.h:65
uint64_t m_hits
Definition PointerPoolNode.h:134
void release(TPRTree::Node *p)
Definition PointerPoolNode.h:88
void setCapacity(uint32_t c)
Definition PointerPoolNode.h:122
uint32_t m_capacity
Definition PointerPoolNode.h:129
Definition PointerPool.h:35
uint32_t m_capacity
Definition PointerPool.h:112
uint64_t m_hits
Definition PointerPool.h:117
uint64_t m_pointerCount
Definition PointerPool.h:119
std::stack< X * > m_pool
Definition PointerPool.h:113
uint64_t m_misses
Definition PointerPool.h:118
Definition PoolPointer.h:37
Definition CustomStorage.h:34
Definition PointerPool.h:33