61 (m_children * (m_pTree->m_dimension *
sizeof(
double) * 2 +
sizeof(
id_type) + 2 *
sizeof(
double) +
sizeof(uint32_t))) +
63 (2 * m_pTree->m_dimension *
sizeof(
double)));
68 m_nodeMBR = m_pTree->m_infiniteRegion;
71 ptr +=
sizeof(uint32_t);
73 memcpy(&m_level, ptr,
sizeof(uint32_t));
74 ptr +=
sizeof(uint32_t);
76 memcpy(&m_children, ptr,
sizeof(uint32_t));
77 ptr +=
sizeof(uint32_t);
79 memcpy(&(m_nodeMBR.
m_startTime), ptr,
sizeof(
double));
80 ptr +=
sizeof(double);
81 memcpy(&(m_nodeMBR.
m_endTime), ptr,
sizeof(
double));
82 ptr +=
sizeof(double);
84 for (uint32_t cChild = 0; cChild < m_children; ++cChild)
86 m_ptrMBR[cChild] = m_pTree->m_regionPool.
acquire();
87 *(m_ptrMBR[cChild]) = m_pTree->m_infiniteRegion;
89 memcpy(m_ptrMBR[cChild]->m_pLow, ptr, m_pTree->m_dimension *
sizeof(
double));
90 ptr += m_pTree->m_dimension *
sizeof(double);
91 memcpy(m_ptrMBR[cChild]->m_pHigh, ptr, m_pTree->m_dimension *
sizeof(
double));
92 ptr += m_pTree->m_dimension *
sizeof(double);
93 memcpy(&(m_pIdentifier[cChild]), ptr,
sizeof(
id_type));
95 memcpy(&(m_ptrMBR[cChild]->m_startTime), ptr,
sizeof(
double));
96 ptr +=
sizeof(double);
97 memcpy(&(m_ptrMBR[cChild]->m_endTime), ptr,
sizeof(
double));
98 ptr +=
sizeof(double);
100 memcpy(&(m_pDataLength[cChild]), ptr,
sizeof(uint32_t));
101 ptr +=
sizeof(uint32_t);
103 if (m_pDataLength[cChild] > 0)
105 m_totalDataLength += m_pDataLength[cChild];
106 m_pData[cChild] =
new uint8_t[m_pDataLength[cChild]];
107 memcpy(m_pData[cChild], ptr, m_pDataLength[cChild]);
108 ptr += m_pDataLength[cChild];
112 m_pData[cChild] =
nullptr;
118 memcpy(m_nodeMBR.
m_pLow, ptr, m_pTree->m_dimension *
sizeof(
double));
119 ptr += m_pTree->m_dimension *
sizeof(double);
120 memcpy(m_nodeMBR.
m_pHigh, ptr, m_pTree->m_dimension *
sizeof(
double));
128 *data =
new uint8_t[len];
129 uint8_t* ptr = *data;
136 memcpy(ptr, &nodeType,
sizeof(uint32_t));
137 ptr +=
sizeof(uint32_t);
139 memcpy(ptr, &m_level,
sizeof(uint32_t));
140 ptr +=
sizeof(uint32_t);
142 memcpy(ptr, &m_children,
sizeof(uint32_t));
143 ptr +=
sizeof(uint32_t);
145 memcpy(ptr, &(m_nodeMBR.
m_startTime),
sizeof(
double));
146 ptr +=
sizeof(double);
147 memcpy(ptr, &(m_nodeMBR.
m_endTime),
sizeof(
double));
148 ptr +=
sizeof(double);
150 for (uint32_t cChild = 0; cChild < m_children; ++cChild)
152 memcpy(ptr, m_ptrMBR[cChild]->m_pLow, m_pTree->m_dimension *
sizeof(
double));
153 ptr += m_pTree->m_dimension *
sizeof(double);
154 memcpy(ptr, m_ptrMBR[cChild]->m_pHigh, m_pTree->m_dimension *
sizeof(
double));
155 ptr += m_pTree->m_dimension *
sizeof(double);
156 memcpy(ptr, &(m_pIdentifier[cChild]),
sizeof(
id_type));
158 memcpy(ptr, &(m_ptrMBR[cChild]->m_startTime),
sizeof(
double));
159 ptr +=
sizeof(double);
160 memcpy(ptr, &(m_ptrMBR[cChild]->m_endTime),
sizeof(
double));
161 ptr +=
sizeof(double);
163 memcpy(ptr, &(m_pDataLength[cChild]),
sizeof(uint32_t));
164 ptr +=
sizeof(uint32_t);
166 if (m_pDataLength[cChild] > 0)
168 memcpy(ptr, m_pData[cChild], m_pDataLength[cChild]);
169 ptr += m_pDataLength[cChild];
174 memcpy(ptr, m_nodeMBR.
m_pLow, m_pTree->m_dimension *
sizeof(
double));
175 ptr += m_pTree->m_dimension *
sizeof(double);
176 memcpy(ptr, m_nodeMBR.
m_pHigh, m_pTree->m_dimension *
sizeof(
double));
205 return m_pIdentifier[
index];
219 if (m_pData[index] ==
nullptr)
226 length = m_pDataLength[
index];
227 *data = m_pData[
index];
238 return (m_level == 0);
243 return (m_level != 0);
258 m_capacity(capacity),
261 m_pIdentifier(
nullptr),
262 m_pDataLength(
nullptr),
269 m_pDataLength =
new uint32_t[m_capacity + 2];
270 m_pData =
new uint8_t*[m_capacity + 2];
272 m_pIdentifier =
new id_type[m_capacity + 2];
276 delete[] m_pDataLength;
279 delete[] m_pIdentifier;
286 if (m_pData !=
nullptr)
288 for (uint32_t cChild = 0; cChild < m_children; ++cChild)
290 if (m_pData[cChild] !=
nullptr)
delete[] m_pData[cChild];
294 delete[] m_pDataLength;
297 if (m_ptrMBR !=
nullptr)
delete[] m_ptrMBR;
298 if (m_pIdentifier !=
nullptr)
delete[] m_pIdentifier;
306 void Node::insertEntry(uint32_t dataLength, uint8_t* pData,
TimeRegion& mbr,
id_type id)
308 assert(m_children < m_capacity);
310 m_pDataLength[m_children] = dataLength;
311 m_pData[m_children] = pData;
312 m_ptrMBR[m_children] = m_pTree->m_regionPool.
acquire();
313 *(m_ptrMBR[m_children]) = mbr;
314 m_pIdentifier[m_children] = id;
316 m_totalDataLength += dataLength;
322 bool Node::deleteEntry(uint32_t
index)
324 assert(index >= 0 && index < m_children);
329 m_totalDataLength -= m_pDataLength[
index];
330 if (m_pData[index] !=
nullptr)
delete[] m_pData[
index];
332 if (m_children > 1 && index != m_children - 1)
334 m_pDataLength[
index] = m_pDataLength[m_children - 1];
335 m_pData[
index] = m_pData[m_children - 1];
336 m_ptrMBR[
index] = m_ptrMBR[m_children - 1];
337 m_pIdentifier[
index] = m_pIdentifier[m_children - 1];
346 m_nodeMBR = m_pTree->m_infiniteRegion;
349 else if (m_pTree->m_bTightMBRs && m_nodeMBR.
touchesShape(*ptrR))
351 for (uint32_t cDim = 0; cDim < m_nodeMBR.
m_dimension; ++cDim)
353 m_nodeMBR.
m_pLow[cDim] = std::numeric_limits<double>::max();
354 m_nodeMBR.
m_pHigh[cDim] = -std::numeric_limits<double>::max();
356 for (uint32_t cChild = 0; cChild < m_children; ++cChild)
358 m_nodeMBR.
m_pLow[cDim] = std::min(m_nodeMBR.
m_pLow[cDim], m_ptrMBR[cChild]->
m_pLow[cDim]);
368 bool Node::insertData(
369 uint32_t dataLength, uint8_t* pData,
TimeRegion& mbr,
id_type id, std::stack<id_type>& pathBuffer,
379 if ((! bInsertMbr2) && m_children < m_capacity)
386 insertEntry(dataLength, pData, mbr,
id);
387 m_pTree->writeNode(
this);
391 if ((! b || bForceAdjust) && (! pathBuffer.empty()))
393 id_type cParent = pathBuffer.top(); pathBuffer.pop();
394 NodePtr ptrN = m_pTree->readNode(cParent);
396 p->adjustTree(
this, pathBuffer);
405 bool bIsRoot = pathBuffer.empty();
412 ptrCopy = m_pTree->m_leafPool.
acquire();
413 if (ptrCopy.
get() ==
nullptr) ptrCopy =
NodePtr(
new Leaf(m_pTree, - 1), &(m_pTree->m_leafPool));
414 else ptrCopy->m_nodeMBR = m_pTree->m_infiniteRegion;
418 ptrCopy = m_pTree->m_indexPool.
acquire();
419 if (ptrCopy.
get() ==
nullptr) ptrCopy =
NodePtr(
new Index(m_pTree, -1, m_level), &(m_pTree->m_indexPool));
422 ptrCopy->m_level = m_level;
423 ptrCopy->m_nodeMBR = m_pTree->m_infiniteRegion;
427 for (uint32_t cChild = 0; cChild < m_children; ++cChild)
429 if (! (m_ptrMBR[cChild]->m_endTime < std::numeric_limits<double>::max()))
431 uint8_t* data =
nullptr;
433 if (m_pDataLength[cChild] > 0)
435 data =
new uint8_t[m_pDataLength[cChild]];
436 memcpy(data, m_pData[cChild], m_pDataLength[cChild] *
sizeof(uint8_t));
438 ptrCopy->insertEntry(m_pDataLength[cChild], data, *(m_ptrMBR[cChild]), m_pIdentifier[cChild]);
446 uint32_t children = (bInsertMbr2) ? ptrCopy->m_children + 2 : ptrCopy->m_children + 1;
447 assert(children > 0);
449 if (children >= m_pTree->m_strongVersionOverflow * m_capacity)
454 ptrCopy->split(dataLength, pData, mbr,
id, n, nn, mbr2, id2, bInsertMbr2);
455 assert(n->m_children > 1 && nn->m_children > 1);
460 n->m_level = ptrCopy->m_level;
461 nn->m_level = ptrCopy->m_level;
462 n->m_identifier = -1;
463 nn->m_identifier = -1;
465 m_pTree->writeNode(n.
get());
466 m_pTree->writeNode(nn.
get());
469 if (ptrR.
get() ==
nullptr) ptrR =
NodePtr(
new Index(m_pTree, -1, ptrCopy->m_level + 1), &(m_pTree->m_indexPool));
474 ptrR->m_level = ptrCopy->m_level + 1;
475 ptrR->m_nodeMBR = m_pTree->m_infiniteRegion;
478 ptrR->insertEntry(0,
nullptr, n->m_nodeMBR, n->m_identifier);
479 ptrR->insertEntry(0,
nullptr, nn->m_nodeMBR, nn->m_identifier);
483 ptrR->m_identifier = m_identifier;
484 m_pTree->writeNode(ptrR.
get());
485 m_pTree->m_stats.m_treeHeight[m_pTree->m_stats.m_treeHeight.size() - 1] = ptrR->m_level + 1;
486 m_pTree->m_stats.m_nodesInLevel.at(n->m_level) = m_pTree->m_stats.m_nodesInLevel[n->m_level] + 1;
487 assert(m_pTree->m_roots[m_pTree->m_roots.size() - 1].m_startTime == ptrCopy->m_nodeMBR.
m_startTime &&
488 m_pTree->m_roots[m_pTree->m_roots.size() - 1].m_endTime == ptrCopy->m_nodeMBR.
m_endTime);
492 m_pTree->writeNode(
this);
493 m_pTree->writeNode(ptrR.
get());
495 assert(m_pTree->m_roots[m_pTree->m_roots.size() - 1].m_id == m_identifier);
496 m_pTree->m_roots[m_pTree->m_roots.size() - 1].m_startTime = m_nodeMBR.
m_startTime;
497 m_pTree->m_roots[m_pTree->m_roots.size() - 1].m_endTime = m_nodeMBR.
m_endTime;
498 m_pTree->m_roots.emplace_back(ptrR->m_identifier, ptrR->m_nodeMBR.
m_startTime, ptrR->m_nodeMBR.
m_endTime);
499 m_pTree->m_stats.m_treeHeight.push_back(ptrR->m_level + 1);
500 m_pTree->m_stats.m_nodesInLevel.at(n->m_level) = m_pTree->m_stats.m_nodesInLevel[n->m_level] + 2;
501 if (m_level > 0) ++(m_pTree->m_stats.m_u32DeadIndexNodes);
502 else ++(m_pTree->m_stats.m_u32DeadLeafNodes);
505 if (ptrR->m_level >= m_pTree->m_stats.m_nodesInLevel.size()) m_pTree->m_stats.m_nodesInLevel.push_back(1);
506 else m_pTree->m_stats.m_nodesInLevel.at(ptrR->m_level) = m_pTree->m_stats.m_nodesInLevel[ptrR->m_level] + 1;
514 n->m_level = ptrCopy->m_level;
515 nn->m_level = ptrCopy->m_level;
529 n->m_identifier = -1;
530 nn->m_identifier = -1;
532 m_pTree->m_stats.m_nodesInLevel.at(n->m_level) = m_pTree->m_stats.m_nodesInLevel[n->m_level] + 2;
533 if (m_level > 0) ++(m_pTree->m_stats.m_u32DeadIndexNodes);
534 else ++(m_pTree->m_stats.m_u32DeadLeafNodes);
536 m_pTree->writeNode(n.
get());
537 m_pTree->writeNode(nn.
get());
539 id_type cParent = pathBuffer.top(); pathBuffer.pop();
540 NodePtr ptrN = m_pTree->readNode(cParent);
542 ++(m_pTree->m_stats.m_u64Adjustments);
545 p->insertData(n->m_nodeMBR, n->m_identifier, nn->m_nodeMBR, nn->m_identifier,
this, pathBuffer);
558 ptrCopy->insertEntry(dataLength, pData, mbr,
id);
559 if (bInsertMbr2) ptrCopy->insertEntry(0,
nullptr, mbr2, id2);
565 ptrCopy->m_identifier = m_identifier;
566 m_pTree->writeNode(ptrCopy.
get());
567 assert(m_pTree->m_roots[m_pTree->m_roots.size() - 1].m_startTime == ptrCopy->m_nodeMBR.
m_startTime &&
568 m_pTree->m_roots[m_pTree->m_roots.size() - 1].m_endTime == ptrCopy->m_nodeMBR.
m_endTime);
572 m_pTree->writeNode(ptrCopy.
get());
573 m_pTree->writeNode(
this);
575 assert(m_pTree->m_roots[m_pTree->m_roots.size() - 1].m_id == m_identifier);
576 m_pTree->m_roots[m_pTree->m_roots.size() - 1].m_startTime = m_nodeMBR.
m_startTime;
577 m_pTree->m_roots[m_pTree->m_roots.size() - 1].m_endTime = m_nodeMBR.
m_endTime;
578 m_pTree->m_roots.emplace_back(ptrCopy->m_identifier, ptrCopy->m_nodeMBR.
m_startTime, ptrCopy->m_nodeMBR.
m_endTime);
579 m_pTree->m_stats.m_treeHeight.push_back(ptrCopy->m_level + 1);
581 m_pTree->m_stats.m_nodesInLevel.at(ptrCopy->m_level) = m_pTree->m_stats.m_nodesInLevel[ptrCopy->m_level] + 1;
582 if (m_level > 0) ++(m_pTree->m_stats.m_u32DeadIndexNodes);
583 else ++(m_pTree->m_stats.m_u32DeadLeafNodes);
590 m_pTree->writeNode(ptrCopy.
get());
592 m_pTree->m_stats.m_nodesInLevel.at(ptrCopy->m_level) = m_pTree->m_stats.m_nodesInLevel[ptrCopy->m_level] + 1;
593 if (m_level > 0) ++(m_pTree->m_stats.m_u32DeadIndexNodes);
594 else ++(m_pTree->m_stats.m_u32DeadLeafNodes);
596 id_type cParent = pathBuffer.top(); pathBuffer.pop();
597 NodePtr ptrN = m_pTree->readNode(cParent);
599 ++(m_pTree->m_stats.m_u64Adjustments);
602 for (child = 0; child < p->m_children; ++child)
604 if (p->m_pIdentifier[child] == m_identifier)
break;
609 double st = p->m_ptrMBR[child]->m_startTime;
610 *(p->m_ptrMBR[child]) = m_nodeMBR;
611 p->m_ptrMBR[child]->m_startTime = st;
615 p->insertData(0,
nullptr, ptrCopy->m_nodeMBR, ptrCopy->m_identifier, pathBuffer, m_pTree->m_infiniteRegion, -1,
false);
629 for (child = 0; child < m_children; ++child)
631 if (m_pIdentifier[child] == oldVersion->m_identifier)
break;
635 bool bAdjust =
false;
642 *(m_ptrMBR[child]) = oldVersion->m_nodeMBR;
646 if (m_children < m_capacity - 1)
650 insertEntry(0,
nullptr, mbr1, id1);
651 insertEntry(0,
nullptr, mbr2, id2);
653 m_pTree->writeNode(
this);
657 id_type cParent = pathBuffer.top(); pathBuffer.pop();
658 NodePtr ptrN = m_pTree->readNode(cParent);
660 p->adjustTree(
this, pathBuffer);
667 bool bStored = insertData(0,
nullptr, mbr1, id1, pathBuffer, mbr2, id2,
true);
668 if (! bStored) m_pTree->writeNode(
this);
672 bool Node::deleteData(
id_type id,
double delTime, std::stack<id_type>& pathBuffer,
bool bForceAdjust)
677 uint32_t child = m_capacity;
679 bool bAdjustParent =
false;
681 *oldNodeMBR = m_nodeMBR;
686 for (uint32_t cChild = 0; cChild < m_children; ++cChild)
688 assert(m_level != 0 || (m_ptrMBR[cChild]->m_startTime != m_ptrMBR[cChild]->m_endTime));
689 if (! (m_ptrMBR[cChild]->m_endTime < std::numeric_limits<double>::max())) ++alive;
690 if (m_pIdentifier[cChild] ==
id) child = cChild;
693 assert(child < m_capacity);
697 bool bAdjusted =
false;
699 if (m_level == 0 && m_ptrMBR[child]->m_startTime == delTime)
701 bAdjusted = deleteEntry(child);
702 bAdjustParent = bAdjusted;
712 if ((! bAdjusted) && bForceAdjust)
714 for (uint32_t cDim = 0; cDim < m_nodeMBR.
m_dimension; ++cDim)
716 m_nodeMBR.
m_pLow[cDim] = std::numeric_limits<double>::max();
717 m_nodeMBR.
m_pHigh[cDim] = -std::numeric_limits<double>::max();
719 for (uint32_t cChild = 0; cChild < m_children; ++cChild)
721 m_nodeMBR.
m_pLow[cDim] = std::min(m_nodeMBR.
m_pLow[cDim], m_ptrMBR[cChild]->
m_pLow[cDim]);
726 bAdjustParent =
true;
732 if (alive < m_pTree->m_versionUnderflow * m_capacity && (! pathBuffer.empty()))
739 if (m_level == 0 && m_children < m_capacity)
741 parent = m_pTree->readNode(pathBuffer.top());
745 for (child = 0; child < parent->m_children; ++child)
747 if (parent->m_pIdentifier[child] == m_identifier)
break;
753 double actualNodeStartTime = parent->m_ptrMBR[child]->
m_startTime;
756 for (uint32_t cSibling = 0; cSibling < parent->m_children; ++cSibling)
760 parent->m_pIdentifier[cSibling] != m_identifier &&
761 ! (parent->m_ptrMBR[cSibling]->
m_endTime < std::numeric_limits<double>::max()) &&
764 NodePtr sibling = m_pTree->readNode(parent->m_pIdentifier[cSibling]);
765 std::vector<DeleteDataEntry> toCheck;
769 bool bSingleParent =
true;
771 for (uint32_t cSiblingChild = 0; cSiblingChild < sibling->m_children; ++cSiblingChild)
777 bSingleParent =
false;
783 if (! (sibling->m_ptrMBR[cSiblingChild]->
m_endTime < std::numeric_limits<double>::max()))
786 if (sibling->m_ptrMBR[cSiblingChild]->
m_startTime >= actualNodeStartTime)
792 if (a <= m_nodeMBR.
getArea() * 1.1) toCheck.emplace_back(cSiblingChild, a);
799 if ((! bSingleParent) || toCheck.empty() || alive == m_pTree->m_versionUnderflow * sibling->m_capacity + 1)
continue;
805 for (uint32_t cSiblingChild = 0; cSiblingChild < sibling->m_children; ++cSiblingChild)
807 Si.insert(sibling->m_ptrMBR[cSiblingChild]->
m_startTime);
808 Si.insert(sibling->m_ptrMBR[cSiblingChild]->
m_endTime);
811 uint32_t* SiCounts =
new uint32_t[Si.size() - 1];
812 memset(SiCounts, 0, (Si.size() - 1) *
sizeof(uint32_t));
814 for (uint32_t cSiblingChild = 0; cSiblingChild < sibling->m_children; ++cSiblingChild)
816 std::set<double>::iterator it1 = Si.begin();
817 std::set<double>::iterator it2 = Si.begin();
818 for (
size_t cIndex = 0; cIndex < Si.size() - 1; ++cIndex)
822 sibling->m_ptrMBR[cSiblingChild]->
m_startTime <= *it1 &&
823 sibling->m_ptrMBR[cSiblingChild]->
m_endTime >= *it2
824 ) ++(SiCounts[cIndex]);
829 std::vector<DeleteDataEntry> Sdel;
831 for (
size_t cCheck = 0; cCheck < toCheck.size(); ++cCheck)
836 std::set<double>::iterator it1 = Si.begin();
837 std::set<double>::iterator it2 = Si.begin();
838 for (
size_t cIndex = 0; cIndex < Si.size() - 1; ++cIndex)
842 sibling->m_ptrMBR[toCheck[cCheck].m_index]->
m_startTime <= *it1 &&
843 sibling->m_ptrMBR[toCheck[cCheck].m_index]->
m_endTime >= *it2 &&
844 SiCounts[cIndex] <= m_pTree->m_versionUnderflow * sibling->m_capacity)
851 if (good) Sdel.push_back(toCheck[cCheck]);
856 if (Sdel.empty())
continue;
861 sort(Sdel.begin(), Sdel.end(), DeleteDataEntry::compare);
862 uint32_t entry = Sdel[0].m_index;
863 bool b1 = m_nodeMBR.
containsShape(*(sibling->m_ptrMBR[entry]));
864 bool b2 = sibling->m_nodeMBR.
touchesShape(*(sibling->m_ptrMBR[entry]));
866 insertEntry(sibling->m_pDataLength[entry], sibling->m_pData[entry], *(sibling->m_ptrMBR[entry]), sibling->m_pIdentifier[entry]);
867 sibling->m_pData[entry] =
nullptr;
870 assert(sibling->m_children > 1);
871 sibling->deleteEntry(entry);
873 m_pTree->writeNode(
this);
874 m_pTree->writeNode(sibling.
get());
877 if (((! b1) || bAdjustParent) && b2) p->adjustTree(
this, sibling.
get(), pathBuffer);
878 else if ((! b1) || bAdjustParent) p->adjustTree(
this, pathBuffer);
879 else if (b2) p->adjustTree(sibling.
get(), pathBuffer);
889 m_pTree->writeNode(
this);
890 if (m_level > 0) ++(m_pTree->m_stats.m_u32DeadIndexNodes);
891 else ++(m_pTree->m_stats.m_u32DeadLeafNodes);
893 if (parent.
get() ==
nullptr)
895 parent = m_pTree->readNode(pathBuffer.top());
902 if (child < parent->m_children && m_identifier != parent->m_pIdentifier[child])
904 for (child = 0; child < parent->m_children; ++child)
906 if (parent->m_pIdentifier[child] == m_identifier)
break;
913 double en = parent->m_ptrMBR[child]->
m_endTime;
914 *(parent->m_ptrMBR[child]) = m_nodeMBR;
923 bool bNewRoot = parent->deleteData(m_identifier, delTime, pathBuffer, (bAdjustParent && parent->m_nodeMBR.
touchesShape(*oldNodeMBR)));
932 for (child = 0; child < m_children; ++child)
934 if (! (m_ptrMBR[child]->m_endTime < std::numeric_limits<double>::max()))
936 if (! bNewRoot || m_level == 0)
939 m_pTree->insertData_impl(m_pDataLength[child], m_pData[child], *(m_ptrMBR[child]), m_pIdentifier[child], m_level);
941 m_pData[child] =
nullptr;
945 std::stack<NodePtr> Sins;
946 Sins.push(m_pTree->readNode(m_pIdentifier[child]));
947 while (! Sins.empty())
949 NodePtr p = Sins.top(); Sins.pop();
952 for (uint32_t cIndex= 0; cIndex < p->m_children; ++cIndex)
954 if (! (p->m_ptrMBR[cIndex]->
m_endTime < std::numeric_limits<double>::max()))
957 m_pTree->insertData_impl(p->m_pDataLength[cIndex], p->m_pData[cIndex], *(p->m_ptrMBR[cIndex]), p->m_pIdentifier[cIndex], p->m_level);
959 p->m_pData[cIndex] =
nullptr;
965 for (uint32_t cIndex= 0; cIndex < p->m_children; ++cIndex)
967 if (! (p->m_ptrMBR[cIndex]->
m_endTime < std::numeric_limits<double>::max()))
969 Sins.push(m_pTree->readNode(p->m_pIdentifier[cIndex]));
982 if (alive == 0 && pathBuffer.empty())
988 m_pTree->m_bHasVersionCopied =
false;
992 Leaf root(m_pTree, m_identifier);
993 root.m_nodeMBR.m_startTime = m_nodeMBR.
m_endTime;
994 root.m_nodeMBR.m_endTime = std::numeric_limits<double>::max();
995 m_pTree->writeNode(&root);
997 m_pTree->m_stats.m_treeHeight[m_pTree->m_stats.m_treeHeight.size() - 1] = 1;
998 if (m_pTree->m_stats.m_nodesInLevel.at(m_level) == 1) m_pTree->m_stats.m_nodesInLevel.pop_back();
999 else m_pTree->m_stats.m_nodesInLevel.at(m_level) = m_pTree->m_stats.m_nodesInLevel[m_level] - 1;
1000 m_pTree->m_stats.m_nodesInLevel.at(0) = m_pTree->m_stats.m_nodesInLevel[0] + 1;
1004 m_pTree->writeNode(
this);
1006 if (m_level > 0) ++(m_pTree->m_stats.m_u32DeadIndexNodes);
1007 else ++(m_pTree->m_stats.m_u32DeadLeafNodes);
1009 Leaf root(m_pTree, -1);
1010 root.m_nodeMBR.m_startTime = m_nodeMBR.
m_endTime;
1011 root.m_nodeMBR.m_endTime = std::numeric_limits<double>::max();
1012 m_pTree->writeNode(&root);
1013 assert(m_pTree->m_roots[m_pTree->m_roots.size() - 1].m_id == m_identifier);
1014 m_pTree->m_roots[m_pTree->m_roots.size() - 1].m_startTime = m_nodeMBR.
m_startTime;
1015 m_pTree->m_roots[m_pTree->m_roots.size() - 1].m_endTime = m_nodeMBR.
m_endTime;
1016 m_pTree->m_roots.emplace_back(root.m_identifier, root.m_nodeMBR.m_startTime, root.m_nodeMBR.m_endTime);
1018 m_pTree->m_stats.m_treeHeight.push_back(1);
1019 m_pTree->m_stats.m_nodesInLevel.at(root.m_level) = m_pTree->m_stats.m_nodesInLevel[root.m_level] + 1;
1025 assert(m_level == 0);
1026 m_pTree->writeNode(
this);
1027 m_pTree->m_bHasVersionCopied =
false;
1031 else if (bAdjustParent && (! pathBuffer.empty()))
1034 m_pTree->writeNode(
this);
1035 parent = m_pTree->readNode(pathBuffer.top());
1038 p->adjustTree(
this, pathBuffer);
1042 m_pTree->writeNode(
this);
1049 void Node::rtreeSplit(
1050 uint32_t dataLength, uint8_t* pData,
TimeRegion& mbr,
id_type id, std::vector<uint32_t>& group1, std::vector<uint32_t>& group2,
1054 uint32_t minimumLoad =
static_cast<uint32_t
>(std::floor(m_capacity * m_pTree->m_fillFactor));
1056 uint32_t cTotal = (bInsertMbr2) ? m_children + 2 : m_children + 1;
1059 uint8_t* mask =
new uint8_t[cTotal];
1060 memset(mask, 0, cTotal);
1064 m_pDataLength[m_children] = dataLength;
1065 m_pData[m_children] = pData;
1066 m_ptrMBR[m_children] = m_pTree->m_regionPool.
acquire();
1067 *(m_ptrMBR[m_children]) = mbr;
1068 m_pIdentifier[m_children] = id;
1072 m_pDataLength[m_children + 1] = 0;
1073 m_pData[m_children + 1] =
nullptr;
1074 m_ptrMBR[m_children + 1] = m_pTree->m_regionPool.
acquire();
1075 *(m_ptrMBR[m_children + 1]) = mbr2;
1076 m_pIdentifier[m_children + 1] = id2;
1080 uint32_t seed1, seed2;
1081 pickSeeds(seed1, seed2, cTotal);
1083 group1.push_back(seed1);
1084 group2.push_back(seed2);
1091 *mbrA = *(m_ptrMBR[seed1]);
1093 *mbrB = *(m_ptrMBR[seed2]);
1096 uint32_t cRemaining = cTotal - 2;
1098 while (cRemaining > 0)
1100 if (minimumLoad - group1.size() == cRemaining)
1103 for (cChild = 0; cChild < cTotal; ++cChild)
1105 if (mask[cChild] == 0)
1107 group1.push_back(cChild);
1113 else if (minimumLoad - group2.size() == cRemaining)
1116 for (cChild = 0; cChild < cTotal; ++cChild)
1118 if (mask[cChild] == 0)
1120 group2.push_back(cChild);
1132 double md1 = 0.0, md2 = 0.0;
1133 double m = -std::numeric_limits<double>::max();
1141 for (cChild = 0; cChild < cTotal; ++cChild)
1143 if (mask[cChild] == 0)
1149 d = std::abs(d1 - d2);
1156 if (m_pTree->m_treeVariant==
RV_LINEAR || m_pTree->m_treeVariant ==
RV_RSTAR)
break;
1166 group1.push_back(sel);
1171 group2.push_back(sel);
1176 group1.push_back(sel);
1181 group2.push_back(sel);
1184 else if (group1.size() < group2.size())
1186 group1.push_back(sel);
1189 else if (group2.size() < group1.size())
1191 group2.push_back(sel);
1196 group1.push_back(sel);
1215 void Node::rstarSplit(
1216 uint32_t dataLength, uint8_t* pData,
TimeRegion& mbr,
id_type id, std::vector<uint32_t>& group1, std::vector<uint32_t>& group2,
1219 RstarSplitEntry** dataLow =
nullptr;
1220 RstarSplitEntry** dataHigh =
nullptr;
1222 uint32_t cTotal = (bInsertMbr2) ? m_children + 2 : m_children + 1;
1226 dataLow =
new RstarSplitEntry*[cTotal];
1227 dataHigh =
new RstarSplitEntry*[cTotal];
1235 m_pDataLength[m_children] = dataLength;
1236 m_pData[m_children] = pData;
1237 m_ptrMBR[m_children] = m_pTree->m_regionPool.
acquire();
1238 *(m_ptrMBR[m_children]) = mbr;
1239 m_pIdentifier[m_children] = id;
1243 m_pDataLength[m_children + 1] = 0;
1244 m_pData[m_children + 1] =
nullptr;
1245 m_ptrMBR[m_children + 1] = m_pTree->m_regionPool.
acquire();
1246 *(m_ptrMBR[m_children + 1]) = mbr2;
1247 m_pIdentifier[m_children + 1] = id2;
1250 uint32_t nodeSPF =
static_cast<uint32_t
>(std::floor(cTotal * m_pTree->m_splitDistributionFactor));
1251 uint32_t splitDistribution = cTotal - (2 * nodeSPF) + 2;
1253 uint32_t cChild = 0, cDim, cIndex;
1255 for (cChild = 0; cChild < cTotal; ++cChild)
1259 dataLow[cChild] =
new RstarSplitEntry(m_ptrMBR[cChild].
get(), cChild, 0);
1263 for (uint32_t i = 0; i < cChild; ++i)
delete dataLow[i];
1269 dataHigh[cChild] = dataLow[cChild];
1272 double minimumMargin = std::numeric_limits<double>::max();
1273 uint32_t splitAxis = std::numeric_limits<uint32_t>::max();
1274 uint32_t sortOrder = std::numeric_limits<uint32_t>::max();
1277 for (cDim = 0; cDim < m_pTree->m_dimension; ++cDim)
1281 sizeof(RstarSplitEntry*),
1282 RstarSplitEntry::compareLow);
1286 sizeof(RstarSplitEntry*),
1287 RstarSplitEntry::compareHigh);
1290 double marginl = 0.0;
1291 double marginh = 0.0;
1295 for (cChild = 1; cChild <= splitDistribution; ++cChild)
1297 uint32_t l = nodeSPF - 1 + cChild;
1299 bbl1 = *(dataLow[0]->m_pRegion);
1300 bbh1 = *(dataHigh[0]->m_pRegion);
1302 for (cIndex = 1; cIndex < l; ++cIndex)
1308 bbl2 = *(dataLow[l]->m_pRegion);
1309 bbh2 = *(dataHigh[l]->m_pRegion);
1311 for (cIndex = l + 1; cIndex < cTotal; ++cIndex)
1321 double margin = std::min(marginl, marginh);
1324 if (margin < minimumMargin)
1326 minimumMargin = margin;
1328 sortOrder = (marginl < marginh) ? 0 : 1;
1332 for (cChild = 0; cChild < cTotal; ++cChild)
1334 dataLow[cChild]->m_sortDim = cDim + 1;
1338 for (cChild = 0; cChild < cTotal; ++cChild)
1340 dataLow[cChild]->m_sortDim = splitAxis;
1346 sizeof(RstarSplitEntry*),
1347 (sortOrder == 0) ? RstarSplitEntry::compareLow : RstarSplitEntry::compareHigh);
1349 double ma = std::numeric_limits<double>::max();
1350 double mo = std::numeric_limits<double>::max();
1351 uint32_t splitPoint = std::numeric_limits<uint32_t>::max();
1355 for (cChild = 1; cChild <= splitDistribution; ++cChild)
1357 uint32_t l = nodeSPF - 1 + cChild;
1359 bb1 = *(dataLow[0]->m_pRegion);
1361 for (cIndex = 1; cIndex < l; ++cIndex)
1366 bb2 = *(dataLow[l]->m_pRegion);
1368 for (cIndex = l + 1; cIndex < cTotal; ++cIndex)
1377 splitPoint = cChild;
1387 splitPoint = cChild;
1393 uint32_t l1 = nodeSPF - 1 + splitPoint;
1395 for (cIndex = 0; cIndex < l1; ++cIndex)
1397 group1.push_back(dataLow[cIndex]->m_index);
1398 delete dataLow[cIndex];
1401 for (cIndex = l1; cIndex < cTotal; ++cIndex)
1403 group2.push_back(dataLow[cIndex]->m_index);
1404 delete dataLow[cIndex];
1411 void Node::pickSeeds(uint32_t& index1, uint32_t& index2, uint32_t total)
1413 double separation = -std::numeric_limits<double>::max();
1414 double inefficiency = -std::numeric_limits<double>::max();
1415 uint32_t cDim, cChild, cIndex;
1417 switch (m_pTree->m_treeVariant)
1421 for (cDim = 0; cDim < m_pTree->m_dimension; ++cDim)
1423 double leastLower = m_ptrMBR[0]->
m_pLow[cDim];
1424 double greatestUpper = m_ptrMBR[0]->
m_pHigh[cDim];
1425 uint32_t greatestLower = 0;
1426 uint32_t leastUpper = 0;
1429 for (cChild = 1; cChild < total; ++cChild)
1431 if (m_ptrMBR[cChild]->m_pLow[cDim] > m_ptrMBR[greatestLower]->m_pLow[cDim]) greatestLower = cChild;
1432 if (m_ptrMBR[cChild]->m_pHigh[cDim] < m_ptrMBR[leastUpper]->m_pHigh[cDim]) leastUpper = cChild;
1434 leastLower = std::min(m_ptrMBR[cChild]->m_pLow[cDim], leastLower);
1435 greatestUpper = std::max(m_ptrMBR[cChild]->m_pHigh[cDim], greatestUpper);
1438 width = greatestUpper - leastLower;
1439 if (width <= 0) width = 1;
1441 double f = (m_ptrMBR[greatestLower]->
m_pLow[cDim] - m_ptrMBR[leastUpper]->
m_pHigh[cDim]) / width;
1445 index1 = leastUpper;
1446 index2 = greatestLower;
1451 if (index1 == index2)
1453 if (index2 == 0) ++index2;
1460 for (cChild = 0; cChild < total - 1; ++cChild)
1462 double a = m_ptrMBR[cChild]->
getArea();
1464 for (cIndex = cChild + 1; cIndex < total; ++cIndex)
1473 if (d > inefficiency)
1490 pathBuffer.push(m_identifier);
1492 for (uint32_t cChild = 0; cChild < m_children; ++cChild)
1494 if (m_pIdentifier[cChild] ==
id)
1495 return m_pTree->readNode(m_pIdentifier[cChild]);
1497 if (m_ptrMBR[cChild]->containsShape(mbr))
1499 NodePtr n = m_pTree->readNode(m_pIdentifier[cChild]);
1500 NodePtr l = n->findNode(mbr,
id, pathBuffer);
1501 assert(n.
get() != l.
get());
1502 if (l.
get() !=
nullptr)
return l;
void getChildShape(uint32_t index, IShape **out) const override
IObject * clone() override
void makeInfinite(uint32_t dimension) override
virtual double getIntersectingArea(const Region &in) const
virtual void getCombinedRegion(Region &out, const Region &in) const
bool isIndex() const override
bool touchesShape(const IShape &in) const override
bool containsShape(const IShape &in) const override
Tools::PoolPointer< Node > NodePtr
bool isLeaf() const override
virtual void combineRegionInTime(const TimeRegion &in)
virtual void combineRegion(const Region &in)
void getShape(IShape **out) const override
double getArea() const override
void getChildData(uint32_t index, uint32_t &length, uint8_t **data) const override
bool intersectsShape(const IShape &in) const override
virtual double getMargin() const
uint32_t getLevel() const override
id_type getIdentifier() const override
SpatialIndex::ISpatialIndex & index()
id_type getChildIdentifier(uint32_t index) const override
void storeToByteArray(uint8_t **data, uint32_t &len) override
uint32_t getByteArraySize() override
uint32_t getChildrenCount() const override
void loadFromByteArray(const uint8_t *data) override