From bbb920113f84ab5778784dfce40161a8f59cfa59 Mon Sep 17 00:00:00 2001 From: Thomas Holder Date: Sat, 22 Dec 2018 21:14:32 +0100 Subject: sp_attribute_lookup with std::map - replace linear array search with std::map lookup - remove duplicated name lookup logic in SPStylePropHelper --- src/attributes.cpp | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'src/attributes.cpp') diff --git a/src/attributes.cpp b/src/attributes.cpp index 1222d2669..d7081d4c9 100644 --- a/src/attributes.cpp +++ b/src/attributes.cpp @@ -9,6 +9,9 @@ * Released under GNU GPL v2+, read the file 'COPYING' for more information. */ +#include +#include + #include // g_assert() #include "attributes.h" @@ -555,15 +558,34 @@ static SPStyleProp const props[] = { #define n_attrs (sizeof(props) / sizeof(props[0])) -/** Returns an SPAttributeEnum; SP_ATTR_INVALID (of value 0) if key isn't recognized. */ +/** + * Inverse to the \c props array for lookup by name. + */ +struct AttributeLookupImpl { + struct cstrless { + bool operator()(char const *lhs, char const *rhs) const { return std::strcmp(lhs, rhs) < 0; } + }; + + std::map m_map; + + AttributeLookupImpl() + { + for (unsigned int i = 1; i < n_attrs; i++) { + // sanity check: order of props array must match SPAttributeEnum + g_assert(props[i].code == i); + + m_map[props[i].name] = props[i].code; + } + } +}; + SPAttributeEnum sp_attribute_lookup(gchar const *key) { - for (unsigned int i = 1; i < n_attrs; i++) { - g_assert(props[i].code == static_cast< gint >(i) ); - // If this g_assert fails, then the sort order of SPAttributeEnum does not match the order in props[]! - if(g_str_equal(const_cast(static_cast(props[i].name)), key)) - return props[i].code; + static AttributeLookupImpl const _instance; + auto it = _instance.m_map.find(key); + if (it != _instance.m_map.end()) { + return it->second; } // std::cerr << "sp_attribute_lookup: invalid attribute: " // << (key?key:"Null") << std::endl; -- cgit v1.2.3