libspatialindex API Reference  (git-trunk)
rtree/PointerPoolNode.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Project: libspatialindex - A C++ library for spatial indexing
3  * Author: Marios Hadjieleftheriou, mhadji@gmail.com
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 #ifndef __spatialindex_rtree_pointer_pool_node_h
29 #define __spatialindex_rtree_pointer_pool_node_h
30 
31 #include "Node.h"
32 
33 namespace Tools
34 {
35  using namespace SpatialIndex;
36  template<> class PointerPool<RTree::Node>
37  {
38  public:
39  explicit PointerPool(uint32_t capacity) : m_capacity(capacity)
40  {
41  }
42 
44  {
45  assert(m_pool.size() <= m_capacity);
46 
47  while (! m_pool.empty())
48  {
49  RTree::Node* x = m_pool.top(); m_pool.pop();
50  delete x;
51  }
52  }
53 
55  {
56  if (! m_pool.empty())
57  {
58  RTree::Node* p = m_pool.top(); m_pool.pop();
59  return PoolPointer<RTree::Node>(p, this);
60  }
61  return PoolPointer<RTree::Node>();
62  }
63 
65  {
66  if (p != nullptr)
67  {
68  if (m_pool.size() < m_capacity)
69  {
70  if (p->m_pData != nullptr)
71  {
72  for (uint32_t cChild = 0; cChild < p->m_children; ++cChild)
73  {
74  // there is no need to set the pointer to zero, after deleting it,
75  // since it will be redeleted only if it is actually initialized again,
76  // a fact that will be depicted by variable m_children.
77  if (p->m_pData[cChild] != nullptr) delete[] p->m_pData[cChild];
78  }
79  }
80 
81  p->m_level = 0;
82  p->m_identifier = -1;
83  p->m_children = 0;
84  p->m_totalDataLength = 0;
85 
86  m_pool.push(p);
87  }
88  else
89  {
90  delete p;
91  }
92 
93  assert(m_pool.size() <= m_capacity);
94  }
95  }
96 
97  uint32_t getCapacity() const { return m_capacity; }
98  void setCapacity(uint32_t c)
99  {
100  assert (c >= 0);
101  m_capacity = c;
102  }
103 
104  protected:
105  uint32_t m_capacity;
106  std::stack<RTree::Node*> m_pool;
107 
108  };
109 }
110 
111 #endif /* __spatialindex_rtree_pointer_pool_node_h */
PoolPointer< RTree::Node > acquire()
std::stack< RTree::Node * > m_pool