libspatialindex API Reference  (git-trunk)
rtree/Leaf.cc
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 #include <cstring>
29 
31 
32 #include "RTree.h"
33 #include "Node.h"
34 #include "Index.h"
35 #include "Leaf.h"
36 
37 using namespace SpatialIndex;
38 using namespace SpatialIndex::RTree;
39 
41 = default;
42 
43 Leaf::Leaf(SpatialIndex::RTree::RTree* pTree, id_type id): Node(pTree, id, 0, pTree->m_leafCapacity)
44 {
45 }
46 
47 NodePtr Leaf::chooseSubtree(const Region&, uint32_t, std::stack<id_type>&)
48 {
49  // should make sure to relinquish other PoolPointer lists that might be pointing to the
50  // same leaf.
51  return NodePtr(this, &(m_pTree->m_leafPool));
52 }
53 
54 NodePtr Leaf::findLeaf(const Region& mbr, id_type id, std::stack<id_type>&)
55 {
56  for (uint32_t cChild = 0; cChild < m_children; ++cChild)
57  {
58  // should make sure to relinquish other PoolPointer lists that might be pointing to the
59  // same leaf.
60  if (m_pIdentifier[cChild] == id && mbr == *(m_ptrMBR[cChild])) return NodePtr(this, &(m_pTree->m_leafPool));
61  }
62 
63  return NodePtr();
64 }
65 
66 void Leaf::split(uint32_t dataLength, uint8_t* pData, Region& mbr, id_type id, NodePtr& pLeft, NodePtr& pRight)
67 {
68  ++(m_pTree->m_stats.m_u64Splits);
69 
70  std::vector<uint32_t> g1, g2;
71 
72  switch (m_pTree->m_treeVariant)
73  {
74  case RV_LINEAR:
75  case RV_QUADRATIC:
76  rtreeSplit(dataLength, pData, mbr, id, g1, g2);
77  break;
78  case RV_RSTAR:
79  rstarSplit(dataLength, pData, mbr, id, g1, g2);
80  break;
81  default:
82  throw Tools::NotSupportedException("Leaf::split: Tree variant not supported.");
83  }
84 
85  pLeft = m_pTree->m_leafPool.acquire();
86  pRight = m_pTree->m_leafPool.acquire();
87 
88  if (pLeft.get() == nullptr) pLeft = NodePtr(new Leaf(m_pTree, -1), &(m_pTree->m_leafPool));
89  if (pRight.get() == nullptr) pRight = NodePtr(new Leaf(m_pTree, -1), &(m_pTree->m_leafPool));
90 
91  pLeft->m_nodeMBR = m_pTree->m_infiniteRegion;
92  pRight->m_nodeMBR = m_pTree->m_infiniteRegion;
93 
94  uint32_t cIndex;
95 
96  for (cIndex = 0; cIndex < g1.size(); ++cIndex)
97  {
98  pLeft->insertEntry(m_pDataLength[g1[cIndex]], m_pData[g1[cIndex]], *(m_ptrMBR[g1[cIndex]]), m_pIdentifier[g1[cIndex]]);
99  // we don't want to delete the data array from this node's destructor!
100  m_pData[g1[cIndex]] = nullptr;
101  }
102 
103  for (cIndex = 0; cIndex < g2.size(); ++cIndex)
104  {
105  pRight->insertEntry(m_pDataLength[g2[cIndex]], m_pData[g2[cIndex]], *(m_ptrMBR[g2[cIndex]]), m_pIdentifier[g2[cIndex]]);
106  // we don't want to delete the data array from this node's destructor!
107  m_pData[g2[cIndex]] = nullptr;
108  }
109 }
110 
111 void Leaf::deleteData(const Region& mbr, id_type id, std::stack<id_type>& pathBuffer)
112 {
113  uint32_t child;
114 
115  for (child = 0; child < m_children; ++child)
116  {
117  if (m_pIdentifier[child] == id && mbr == *(m_ptrMBR[child])) break;
118  }
119 
120  deleteEntry(child);
121  m_pTree->writeNode(this);
122 
123  std::stack<NodePtr> toReinsert;
124  NodePtr ptrThis(this, &(m_pTree->m_leafPool));
125  condenseTree(toReinsert, pathBuffer, ptrThis);
126  ptrThis.relinquish();
127 
128  // re-insert eliminated nodes.
129  while (! toReinsert.empty())
130  {
131  NodePtr n = toReinsert.top(); toReinsert.pop();
132  m_pTree->deleteNode(n.get());
133 
134  for (uint32_t cChild = 0; cChild < n->m_children; ++cChild)
135  {
136  // keep this in the for loop. The tree height might change after insertions.
137  uint8_t* overflowTable = new uint8_t[m_pTree->m_stats.m_u32TreeHeight];
138  memset(overflowTable, 0, m_pTree->m_stats.m_u32TreeHeight);
139  m_pTree->insertData_impl(n->m_pDataLength[cChild], n->m_pData[cChild], *(n->m_ptrMBR[cChild]), n->m_pIdentifier[cChild], n->m_level, overflowTable);
140  n->m_pData[cChild] = nullptr;
141  delete[] overflowTable;
142  }
143  if (n.get() == this) n.relinquish();
144  }
145 }
NodePtr findLeaf(const Region &mbr, id_type id, std::stack< id_type > &pathBuffer) override
Definition: rtree/Leaf.cc:54
NodePtr chooseSubtree(const Region &mbr, uint32_t level, std::stack< id_type > &pathBuffer) override
Definition: rtree/Leaf.cc:47
X * get() const noexcept
Definition: PoolPointer.h:55
Leaf(RTree *pTree, id_type id)
Definition: rtree/Leaf.cc:43
Tools::PoolPointer< Node > NodePtr
Definition: rtree/Node.h:40
void split(uint32_t dataLength, uint8_t *pData, Region &mbr, id_type id, NodePtr &left, NodePtr &right) override
Definition: rtree/Leaf.cc:66
int64_t id_type
Definition: SpatialIndex.h:41
void relinquish() noexcept
Definition: PoolPointer.h:57
virtual void deleteData(const Region &mbr, id_type id, std::stack< id_type > &pathBuffer)
Definition: rtree/Leaf.cc:111