Skip to content

Commit b2fc0c0

Browse files
committed
Hierarchical index for inverses
1 parent 2d5883f commit b2fc0c0

2 files changed

Lines changed: 57 additions & 57 deletions

File tree

‎src/ifcparse/parse.cpp‎

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -640,11 +640,11 @@ void ifcopenshell::impl::in_memory_file_storage::try_read_semicolon(ifcopenshell
640640

641641
void ifcopenshell::impl::in_memory_file_storage::register_inverse(unsigned id_from, const ifcopenshell::entity* from_entity, int inst_id, int attribute_index) {
642642
// Assume a check on token type has already been performed
643-
byref_excl_[{inst_id, from_entity->index_in_schema(), attribute_index}].push_back(id_from);
643+
byref_excl_[inst_id][{from_entity->index_in_schema(), attribute_index}].push_back(id_from);
644644
}
645645

646646
void ifcopenshell::impl::in_memory_file_storage::unregister_inverse(unsigned id_from, const ifcopenshell::entity* from_entity, const express::Base& inst, int attribute_index) {
647-
auto& ids = byref_excl_[{inst.id(), from_entity->index_in_schema(), attribute_index}];
647+
auto& ids = byref_excl_[inst.id()][{from_entity->index_in_schema(), attribute_index}];
648648
auto iter = std::find(ids.begin(), ids.end(), id_from);
649649
if (iter == ids.end()) {
650650
// @todo inverses also need to be populated when multiple instances are added to a new file.
@@ -2363,9 +2363,7 @@ void ifcopenshell::impl::in_memory_file_storage::process_deletion_inverse(const
23632363
auto id = entity.id();
23642364

23652365
// Delete inverses into entity
2366-
byref_excl_.erase(
2367-
byref_excl_.lower_bound({ id, -1, -1 }),
2368-
byref_excl_.upper_bound({ id, std::numeric_limits<short>::max(), std::numeric_limits<short>::max() }));
2366+
byref_excl_.erase(id);
23692367

23702368
// This is based on traversal which needs instances to still be contained in the map.
23712369
// another option would be to keep byid intact for the remainder of this loop
@@ -2380,12 +2378,11 @@ void ifcopenshell::impl::in_memory_file_storage::process_deletion_inverse(const
23802378
if (name != 0) {
23812379
// Find instances entity -> other
23822380
// and update inverses from entity into other
2383-
auto lower = byref_excl_.lower_bound({ name, -1, -1 });
2384-
auto upper = byref_excl_.upper_bound({ name, std::numeric_limits<short>::max(), std::numeric_limits<short>::max() });
2385-
2386-
for (auto byref_it = lower; byref_it != upper; ++byref_it) {
2387-
auto& ids = byref_it->second;
2388-
ids.erase(std::remove(ids.begin(), ids.end(), id), ids.end());
2381+
auto submap = byref_excl_.find(name);
2382+
if (submap != byref_excl_.end()) {
2383+
for (auto& [key, ids] : submap->second) {
2384+
ids.erase(std::remove(ids.begin(), ids.end(), id), ids.end());
2385+
}
23892386
}
23902387
}
23912388
}
@@ -2458,10 +2455,12 @@ std::vector<express::Base> file::instances_by_reference(int t) {
24582455
std::vector<express::Base> ret;
24592456
std::visit([this, t, &ret](auto& x) {
24602457
if constexpr (std::is_same_v<std::decay_t<decltype(x)>, impl::in_memory_file_storage>) {
2461-
auto lower = x.byref_excl_.lower_bound({ t, -1, -1 });
2462-
auto upper = x.byref_excl_.upper_bound({ t, std::numeric_limits<short>::max(), std::numeric_limits<short>::max() });
2463-
for (auto it = lower; it != upper; ++it) {
2464-
for (auto& i : it->second) {
2458+
auto submap = x.byref_excl_.find(t);
2459+
if (submap == x.byref_excl_.end()) {
2460+
return;
2461+
}
2462+
for (auto& [key, ids] : submap->second) {
2463+
for (auto& i : ids) {
24652464
ret.push_back(instance_by_id(i));
24662465
}
24672466
}
@@ -2618,11 +2617,13 @@ std::vector<int> file::get_inverse_indices(int instance_id) {
26182617
std::visit([&mapping, instance_id](const auto& x) {
26192618
if constexpr (std::is_same_v<std::decay_t<decltype(x)>, std::monostate>) {
26202619
} else if constexpr (std::is_same_v<std::decay_t<decltype(x)>, impl::in_memory_file_storage>) {
2621-
auto lower = x.byref_excl_.lower_bound({ instance_id, -1, -1 });
2622-
auto upper = x.byref_excl_.upper_bound({ instance_id, std::numeric_limits<short>::max(), std::numeric_limits<short>::max() });
2623-
for (auto it = lower; it != upper; ++it) {
2624-
for (auto& i : it->second) {
2625-
mapping[i].push_back(std::get<2>(it->first));
2620+
auto submap = x.byref_excl_.find(instance_id);
2621+
if (submap == x.byref_excl_.end()) {
2622+
return;
2623+
}
2624+
for (auto& [key, ids] : submap->second) {
2625+
for (auto& i : ids) {
2626+
mapping[i].push_back(std::get<1>(key));
26262627
}
26272628
}
26282629
} else if constexpr (std::is_same_v<std::decay_t<decltype(x)>, impl::rocks_db_file_storage>) {
@@ -2678,31 +2679,34 @@ std::vector<express::Entity> file::get_inverse(int instance_id, const ifcopenshe
26782679
return return_value;
26792680
}
26802681

2681-
visit_subtypes(type->as_entity(), [this, attribute_index, instance_id, &return_value](const ifcopenshell::declaration* ent) {
2682-
2683-
std::visit([&return_value, this, attribute_index, instance_id, ent](const auto& x) {
2684-
if constexpr (std::is_same_v<std::decay_t<decltype(x)>, std::monostate>) {
2685-
} else if constexpr (std::is_same_v<std::decay_t<decltype(x)>, impl::in_memory_file_storage>) {
2686-
if (attribute_index == -1) {
2687-
auto lower = x.byref_excl_.lower_bound({ instance_id, ent->index_in_schema(), -1 });
2688-
auto upper = x.byref_excl_.upper_bound({ instance_id, ent->index_in_schema(), std::numeric_limits<short>::max() });
2689-
2690-
for (auto it = lower; it != upper; ++it) {
2691-
for (auto& i : it->second) {
2692-
return_value.push_back(instance_by_id(i).template as<express::Entity>());
2682+
std::visit([&return_value, this, attribute_index, instance_id, type](const auto& x) {
2683+
if constexpr (std::is_same_v<std::decay_t<decltype(x)>, std::monostate>) {
2684+
} else if constexpr (std::is_same_v<std::decay_t<decltype(x)>, impl::in_memory_file_storage>) {
2685+
auto submap = x.byref_excl_.find(instance_id);
2686+
if (submap != x.byref_excl_.end()) {
2687+
visit_subtypes(type->as_entity(), [this, attribute_index, instance_id, &return_value, &submap](const ifcopenshell::declaration* ent) {
2688+
if (attribute_index == -1) {
2689+
auto lower = submap->second.lower_bound({ent->index_in_schema(), std::numeric_limits<short>::min()});
2690+
auto upper = submap->second.upper_bound({ent->index_in_schema(), std::numeric_limits<short>::max()});
2691+
for (auto it = lower; it != upper; ++it) {
2692+
for (auto& i : it->second) {
2693+
return_value.push_back(instance_by_id(i).template as<express::Entity>());
2694+
}
26932695
}
2694-
}
2695-
} else {
2696-
auto it = x.byref_excl_.find({ instance_id, ent->index_in_schema(), attribute_index });
2697-
if (it != x.byref_excl_.end()) {
2698-
for (auto& i : it->second) {
2699-
return_value.push_back(instance_by_id(i).template as<express::Entity>());
2696+
} else {
2697+
auto it = submap->second.find({ent->index_in_schema(), attribute_index});
2698+
if (it != submap->second.end()) {
2699+
for (auto& i : it->second) {
2700+
return_value.push_back(instance_by_id(i).template as<express::Entity>());
2701+
}
27002702
}
27012703
}
2702-
}
2704+
});
27032705
}
2704-
#ifdef IFOPSH_WITH_ROCKSDB
2705-
else if constexpr (std::is_same_v<std::decay_t<decltype(x)>, impl::rocks_db_file_storage>) {
2706+
}
2707+
#ifdef IFOPSH_WITH_ROCKSDB
2708+
else if constexpr (std::is_same_v<std::decay_t<decltype(x)>, impl::rocks_db_file_storage>) {
2709+
visit_subtypes(type->as_entity(), [this, attribute_index, instance_id, &return_value, &x](const ifcopenshell::declaration* ent) {
27062710
if (attribute_index == -1) {
27072711
// @todo no lower/upper_bounds() implemented yet
27082712
auto prefix = "v|" + std::to_string(instance_id) + "|" + std::to_string(ent->index_in_schema()) + "|";
@@ -2717,17 +2721,17 @@ std::vector<express::Entity> file::get_inverse(int instance_id, const ifcopenshe
27172721
it->Next();
27182722
}
27192723
} else {
2720-
auto it = x.byref_excl_.find({ instance_id, ent->index_in_schema(), attribute_index });
2724+
auto it = x.byref_excl_.find({instance_id, ent->index_in_schema(), attribute_index});
27212725
if (it != x.byref_excl_.end()) {
27222726
for (auto& i : it->second) {
27232727
return_value.push_back(instance_by_id(i).template as<express::Entity>());
27242728
}
27252729
}
27262730
}
2727-
}
2731+
});
2732+
}
27282733
#endif
2729-
}, storage_);
2730-
});
2734+
}, storage_);
27312735

27322736
return return_value;
27332737
}
@@ -2738,10 +2742,12 @@ size_t file::get_total_inverses(int instance_id) {
27382742
std::visit([&counted_ids, instance_id](const auto& x) {
27392743
if constexpr (std::is_same_v<std::decay_t<decltype(x)>, std::monostate>) {
27402744
} else if constexpr (std::is_same_v<std::decay_t<decltype(x)>, impl::in_memory_file_storage>) {
2741-
auto lower = x.byref_excl_.lower_bound({ instance_id, -1, -1 });
2742-
auto upper = x.byref_excl_.upper_bound({ instance_id, std::numeric_limits<short>::max(), std::numeric_limits<short>::max() });
2743-
for (auto it = lower; it != upper; ++it) {
2744-
for (auto& i : it->second) {
2745+
auto submap = x.byref_excl_.find(instance_id);
2746+
if (submap == x.byref_excl_.end()) {
2747+
return;
2748+
}
2749+
for (auto& [key, ids] : submap->second) {
2750+
for (auto& i : ids) {
27452751
counted_ids.insert(i);
27462752
}
27472753
}
@@ -2842,7 +2848,7 @@ void ifcopenshell::file::build_inverses_(const express::Base& inst) {
28422848
std::visit([entity_attribute_id, decl, idx, inst](auto& x) {
28432849
if constexpr (std::is_same_v<std::decay_t<decltype(x)>, std::monostate>) {
28442850
} else if constexpr (std::is_same_v<std::decay_t<decltype(x)>, impl::in_memory_file_storage>) {
2845-
x.byref_excl_[{entity_attribute_id, decl->index_in_schema(), idx}].push_back(inst.id());
2851+
x.byref_excl_[entity_attribute_id][{decl->index_in_schema(), idx}].push_back(inst.id());
28462852
} else if constexpr (std::is_same_v<std::decay_t<decltype(x)>, impl::rocks_db_file_storage>) {
28472853
// @todo
28482854
}

‎src/ifcparse/storage.h‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,7 @@ namespace ifcopenshell {
301301
typedef map_transformer<entity_instance_by_name_storage_t, std::function<express::Base(std::shared_ptr<instance_data>)>> entity_instance_by_name_t;
302302
typedef boost::unordered_map<uint32_t, std::shared_ptr<instance_data>> type_instance_by_name_t;
303303
typedef std::map<std::string, express::Base> entity_instance_by_guid_t;
304-
typedef std::tuple<int, short, short> inverse_attr_record;
305-
enum INVERSE_ATTR {
306-
INSTANCE_ID,
307-
INSTANCE_TYPE,
308-
ATTRIBUTE_INDEX
309-
};
310-
typedef std::map<inverse_attr_record, std::vector<uint32_t>> entities_by_ref_t;
304+
typedef std::unordered_map<int, std::map<std::tuple<short, short>, std::vector<uint32_t>>> entities_by_ref_t;
311305
typedef entity_instance_by_name_t::iterator iterator;
312306

313307
in_memory_file_storage(ifcopenshell::file* owner_file = nullptr) : file(owner_file), schema(nullptr), byid_read_(&byid_, [this](const std::shared_ptr<instance_data>& data) { return express::Base(data); }) {};

0 commit comments

Comments
 (0)