Skip to content

Commit 0ceef8f

Browse files
committed
Use constant time parent enumeration lookup
1 parent 28560d1 commit 0ceef8f

File tree

10 files changed

+103
-1292
lines changed

10 files changed

+103
-1292
lines changed

src/ifcexpressparser/implementation.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,15 @@ def get_attribute_index(entity, attr_name):
167167
'name' : name,
168168
'padding' : ' ' * (max_len - len(name))
169169
} for name in enumerable_types]
170+
171+
enumeration_index_by_str = dict((j,i) for i,j in enumerate(enumerable_types))
172+
def get_parent_id(s):
173+
e = mapping.schema.entities.get(s)
174+
if e and e.supertypes:
175+
return enumeration_index_by_str[e.supertypes[0]]
176+
else: return -1
170177

171-
parent_type_statements = [templates.parent_type_stmt % {
172-
'name' : name,
173-
'parent' : type.supertypes[0],
174-
'padding' : ' ' * (max_len - len(name))
175-
} for name, type in mapping.schema.entities.items() if type.supertypes and len(type.supertypes) == 1]
178+
parent_type_statements = ",".join(map(str, map(get_parent_id, enumerable_types)))
176179

177180
max_id = len(enumerable_types)
178181

@@ -225,7 +228,7 @@ def compose(params):
225228
'type_name_strings' : type_name_strings,
226229
'string_map_statements' : catnl(string_map_statements),
227230
'simple_type_statement' : simple_type_statements,
228-
'parent_type_statements' : catnl(parent_type_statements),
231+
'parent_type_statements' : parent_type_statements,
229232
'entity_implementations' : catnl(entity_implementations),
230233
'simple_type_impl' : catnl(simple_type_impl)
231234
}

src/ifcexpressparser/templates.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
#ifndef %(schema_name_upper)sENUM_H
6363
#define %(schema_name_upper)sENUM_H
6464
65+
#include <boost/optional.hpp>
66+
6567
#define IfcSchema %(schema_name)s
6668
6769
namespace %(schema_name)s {
@@ -70,7 +72,7 @@
7072
typedef enum {
7173
%(types)s, UNDEFINED
7274
} Enum;
73-
Enum Parent(Enum v);
75+
boost::optional<Enum> Parent(Enum v);
7476
Enum FromString(const std::string& s);
7577
std::string ToString(Enum v);
7678
bool IsSimple(Enum v);
@@ -146,10 +148,14 @@
146148
else return it->second;
147149
}
148150
149-
Type::Enum Type::Parent(Enum v){
150-
if (v < 0 || v >= %(max_id)d) return (Enum)-1;
151-
%(parent_type_statements)s
152-
return (Enum)-1;
151+
static int parent_map[] = {%(parent_type_statements)s};
152+
boost::optional<Type::Enum> Type::Parent(Enum v){
153+
const int p = parent_map[static_cast<int>(v)];
154+
if (p >= 0) {
155+
return static_cast<Type::Enum>(p);
156+
} else {
157+
return boost::none;
158+
}
153159
}
154160
155161
bool Type::IsSimple(Enum v) {
@@ -282,7 +288,13 @@
282288
return jt->second;
283289
}
284290
}
285-
if ((t = Parent(t)) == -1) break;
291+
boost::optional<Enum> pt = Parent(t);
292+
if (pt) {
293+
t = *pt;
294+
}
295+
else {
296+
break;
297+
}
286298
}
287299
throw IfcException("Attribute not found");
288300
}
@@ -301,7 +313,13 @@
301313
return_value.insert(jt->first);
302314
}
303315
}
304-
if ((t = Parent(t)) == -1) break;
316+
boost::optional<Enum> pt = Parent(t);
317+
if (pt) {
318+
t = *pt;
319+
}
320+
else {
321+
break;
322+
}
305323
}
306324
307325
return return_value;

src/ifcparse/Ifc2x3-latebound.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4258,7 +4258,13 @@ std::pair<Type::Enum, unsigned> Type::GetInverseAttribute(Enum t, const std::str
42584258
return jt->second;
42594259
}
42604260
}
4261-
if ((t = Parent(t)) == -1) break;
4261+
boost::optional<Enum> pt = Parent(t);
4262+
if (pt) {
4263+
t = *pt;
4264+
}
4265+
else {
4266+
break;
4267+
}
42624268
}
42634269
throw IfcException("Attribute not found");
42644270
}
@@ -4277,7 +4283,13 @@ std::set<std::string> Type::GetInverseAttributeNames(Enum t) {
42774283
return_value.insert(jt->first);
42784284
}
42794285
}
4280-
if ((t = Parent(t)) == -1) break;
4286+
boost::optional<Enum> pt = Parent(t);
4287+
if (pt) {
4288+
t = *pt;
4289+
}
4290+
else {
4291+
break;
4292+
}
42814293
}
42824294

42834295
return return_value;

src/ifcparse/Ifc2x3.cpp

Lines changed: 8 additions & 555 deletions
Large diffs are not rendered by default.

src/ifcparse/Ifc2x3enum.h

Lines changed: 3 additions & 1 deletion
Large diffs are not rendered by default.

src/ifcparse/Ifc4-latebound.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4952,7 +4952,13 @@ std::pair<Type::Enum, unsigned> Type::GetInverseAttribute(Enum t, const std::str
49524952
return jt->second;
49534953
}
49544954
}
4955-
if ((t = Parent(t)) == -1) break;
4955+
boost::optional<Enum> pt = Parent(t);
4956+
if (pt) {
4957+
t = *pt;
4958+
}
4959+
else {
4960+
break;
4961+
}
49564962
}
49574963
throw IfcException("Attribute not found");
49584964
}
@@ -4971,7 +4977,13 @@ std::set<std::string> Type::GetInverseAttributeNames(Enum t) {
49714977
return_value.insert(jt->first);
49724978
}
49734979
}
4974-
if ((t = Parent(t)) == -1) break;
4980+
boost::optional<Enum> pt = Parent(t);
4981+
if (pt) {
4982+
t = *pt;
4983+
}
4984+
else {
4985+
break;
4986+
}
49754987
}
49764988

49774989
return return_value;

0 commit comments

Comments
 (0)