Skip to content

Commit 80d2f33

Browse files
committed
filter duplicate items, fixes #66
1 parent 503a0b1 commit 80d2f33

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

importer/src/hierarchyitem.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ bool HierarchyItem::is_duplicate(std::shared_ptr<HierarchyItem> item) const
9999
if (s_priority_types.count(m_type) > 0)
100100
return false;
101101

102-
if (m_name != item->m_name || m_country != item->m_country || m_postcode != item->m_postcode)
102+
if (m_name != item->m_name || m_postcode != item->m_postcode)
103103
return false;
104104

105105
if (m_type == item->m_type || same_starts_with("building", m_type, item->m_type)
@@ -149,29 +149,32 @@ void HierarchyItem::set_parent(hindex parent, bool force)
149149
// c->set_parent(m_id, force);
150150
}
151151

152-
void HierarchyItem::cleanup_children()
152+
void HierarchyItem::cleanup_children(bool duplicate_only)
153153
{
154154
// as a result of this run, children that are supposed to be kept are staying in children
155155
// property. all disposed ones are still pointed to via Hierarchy map, but should not be accessed
156156
// while moving along hierarchy for indexing or writing it
157-
std::deque<std::shared_ptr<HierarchyItem> > children;
158-
for (auto item : m_children)
159-
{
160-
item->cleanup_children();
161-
if (item->keep())
162-
children.push_back(item);
163-
else
164-
children.insert(children.end(), item->m_children.begin(), item->m_children.end());
165-
}
166-
m_children = children;
157+
{
158+
std::deque<std::shared_ptr<HierarchyItem> > children;
159+
for (auto item : m_children)
160+
{
161+
item->cleanup_children();
162+
if (item->keep())
163+
children.push_back(item);
164+
else
165+
children.insert(children.end(), item->m_children.begin(), item->m_children.end());
166+
}
167+
m_children = children;
168+
}
167169

168170
// check for duplicates
171+
bool had_duplicates = false;
169172
for (size_t child_index = 0; child_index < m_children.size(); ++child_index)
170173
{
171174
std::shared_ptr<HierarchyItem> item = m_children[child_index];
175+
std::deque<std::shared_ptr<HierarchyItem> > children;
172176
std::deque<std::shared_ptr<HierarchyItem> > duplicates;
173177

174-
children.clear();
175178
children.insert(children.end(), m_children.begin(), m_children.begin() + child_index + 1);
176179

177180
for (size_t i = child_index + 1; i < m_children.size(); ++i)
@@ -183,13 +186,17 @@ void HierarchyItem::cleanup_children()
183186
// merge duplicates
184187
for (auto &i : duplicates)
185188
{
189+
had_duplicates = true;
186190
item->add_linked(i);
187191
item->m_children.insert(item->m_children.end(), i->m_children.begin(),
188192
i->m_children.end());
189193
for (auto &i_children : i->m_children)
190194
i_children->set_parent(item->m_id, true);
191195
}
192196

197+
if (had_duplicates)
198+
item->cleanup_children(true);
199+
193200
m_children = children;
194201
}
195202

importer/src/hierarchyitem.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@ class HierarchyItem
2020
hindex linked_id() const { return m_linked_id; }
2121
hindex parent_id() const { return m_parent_id; }
2222
const std::string &country() const { return m_country; }
23-
24-
bool keep() const;
25-
bool is_duplicate(std::shared_ptr<HierarchyItem> item) const;
23+
bool keep() const;
2624

2725
const std::deque<std::shared_ptr<HierarchyItem> > &children() { return m_children; }
2826

2927
void add_child(std::shared_ptr<HierarchyItem> child);
3028
void add_linked(std::shared_ptr<HierarchyItem> linked);
3129
void set_parent(hindex parent, bool force = false);
32-
void cleanup_children();
30+
void cleanup_children(bool duplicate_only = false);
3331
sqlid index(sqlid idx, sqlid parent);
3432
void write(sqlite3pp::database &db) const;
3533

@@ -41,6 +39,7 @@ class HierarchyItem
4139

4240
protected:
4341
void set_names();
42+
bool is_duplicate(std::shared_ptr<HierarchyItem> item) const;
4443

4544
private:
4645
hindex m_id;

0 commit comments

Comments
 (0)