Skip to content

Commit a8afc3b

Browse files
committed
added remove function that does not need rect
closes #27
1 parent cf826fb commit a8afc3b

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

RTree.h

+15-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ class RTree
8888
/// \param a_dataId Positive Id of data. Maybe zero, but negative numbers not allowed.
8989
void Insert(const ELEMTYPE a_min[NUMDIMS], const ELEMTYPE a_max[NUMDIMS], const DATATYPE& a_dataId);
9090

91-
/// Remove entry
91+
/// Remove entry (traverse the whole tree and removes every occurrence)
92+
/// \param a_dataId Positive Id of data. Maybe zero, but negative numbers not allowed.
93+
void Remove(const DATATYPE& a_dataId);
94+
95+
/// Remove entry (only if inside the given rect)
9296
/// \param a_min Min of bounding rect
9397
/// \param a_max Max of bounding rect
9498
/// \param a_dataId Positive Id of data. Maybe zero, but negative numbers not allowed.
@@ -529,6 +533,13 @@ void RTREE_QUAL::Insert(const ELEMTYPE a_min[NUMDIMS], const ELEMTYPE a_max[NUMD
529533
}
530534

531535

536+
RTREE_TEMPLATE
537+
void RTREE_QUAL::Remove(const DATATYPE& a_dataId)
538+
{
539+
RemoveRect(NULL, a_dataId, &m_root);
540+
}
541+
542+
532543
RTREE_TEMPLATE
533544
void RTREE_QUAL::Remove(const ELEMTYPE a_min[NUMDIMS], const ELEMTYPE a_max[NUMDIMS], const DATATYPE& a_dataId)
534545
{
@@ -1578,7 +1589,7 @@ void RTREE_QUAL::Classify(int a_index, int a_group, PartitionVars* a_parVars)
15781589
RTREE_TEMPLATE
15791590
bool RTREE_QUAL::RemoveRect(Rect* a_rect, const DATATYPE& a_id, Node** a_root)
15801591
{
1581-
RTREE_ASSERT(a_rect && a_root);
1592+
RTREE_ASSERT(a_root);
15821593
RTREE_ASSERT(*a_root);
15831594

15841595
ListNode* reInsertList = NULL;
@@ -1632,14 +1643,14 @@ bool RTREE_QUAL::RemoveRect(Rect* a_rect, const DATATYPE& a_id, Node** a_root)
16321643
RTREE_TEMPLATE
16331644
bool RTREE_QUAL::RemoveRectRec(Rect* a_rect, const DATATYPE& a_id, Node* a_node, ListNode** a_listNode)
16341645
{
1635-
RTREE_ASSERT(a_rect && a_node && a_listNode);
1646+
RTREE_ASSERT(a_node && a_listNode);
16361647
RTREE_ASSERT(a_node->m_level >= 0);
16371648

16381649
if(a_node->IsInternalNode()) // not a leaf node
16391650
{
16401651
for(int index = 0; index < a_node->m_count; ++index)
16411652
{
1642-
if(Overlap(a_rect, &(a_node->m_branch[index].m_rect)))
1653+
if(a_rect == NULL || Overlap(a_rect, &(a_node->m_branch[index].m_rect)))
16431654
{
16441655
if(!RemoveRectRec(a_rect, a_id, a_node->m_branch[index].m_child, a_listNode))
16451656
{

tests/basics.cpp

+35-1
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,38 @@ TEST(BasicTests, BasicTests) {
111111
}
112112

113113
EXPECT_THAT(values, UnorderedElementsAreArray(collectedValues));
114-
}
114+
}
115+
116+
117+
TEST(BasicTests, RemoveTests) {
118+
typedef RTree<ValueType, int, 2, float> MyTree;
119+
MyTree tree;
120+
121+
std::vector<ValueType> v;
122+
123+
for (size_t i = 0; i < 1000; i++) {
124+
const Rect rect(i, i, i, i);
125+
tree.Insert(rect.min, rect.max, i);
126+
v.push_back(i);
127+
}
128+
129+
for (size_t i = 0; i < v.size(); i++) {
130+
tree.Remove(v.back());
131+
132+
std::vector<ValueType> collectedValues = std::vector<ValueType>();
133+
MyTree::Iterator it;
134+
tree.GetFirst(it);
135+
136+
while (!it.IsNull()) {
137+
int value = *it;
138+
++it;
139+
collectedValues.push_back(value);
140+
}
141+
142+
v.pop_back();
143+
144+
ASSERT_EQ(v.size(), collectedValues.size());
145+
EXPECT_THAT(v, UnorderedElementsAreArray(collectedValues));
146+
}
147+
148+
}

0 commit comments

Comments
 (0)