diff options
| author | Thomas Holder <thomas@thomas-holder.de> | 2018-12-22 20:14:32 +0000 |
|---|---|---|
| committer | Thomas Holder <thomas@thomas-holder.de> | 2018-12-24 12:21:19 +0000 |
| commit | bbb920113f84ab5778784dfce40161a8f59cfa59 (patch) | |
| tree | 0f8ca88addb5dd2220b93d8b7f59d496559a21b2 | |
| parent | Standard: Cpp11 (diff) | |
| download | inkscape-bbb920113f84ab5778784dfce40161a8f59cfa59.tar.gz inkscape-bbb920113f84ab5778784dfce40161a8f59cfa59.zip | |
sp_attribute_lookup with std::map
- replace linear array search with std::map lookup
- remove duplicated name lookup logic in SPStylePropHelper
| -rw-r--r-- | src/attributes.cpp | 34 | ||||
| -rw-r--r-- | src/attributes.h | 7 | ||||
| -rw-r--r-- | src/style.cpp | 15 |
3 files changed, 38 insertions, 18 deletions
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 <cstring> +#include <map> + #include <glib.h> // 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<char const *, SPAttributeEnum, cstrless> 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<void *>(static_cast<void const *>(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; diff --git a/src/attributes.h b/src/attributes.h index 1f0178fae..512c00499 100644 --- a/src/attributes.h +++ b/src/attributes.h @@ -564,7 +564,14 @@ enum SPAttributeEnum : unsigned { SP_PROP_PATH_EFFECT, }; +/** + * Get attribute id by name. Return SP_ATTR_INVALID for invalid names. + */ SPAttributeEnum sp_attribute_lookup(gchar const *key); + +/** + * Get attribute name by id. Return NULL for invalid ids. + */ gchar const *sp_attribute_name(SPAttributeEnum id); #endif diff --git a/src/style.cpp b/src/style.cpp index aab8f2695..b11a92749 100644 --- a/src/style.cpp +++ b/src/style.cpp @@ -76,7 +76,7 @@ static CRSelEng *sp_repr_sel_eng(); class SPStylePropHelper { SPStylePropHelper() { #define REGISTER_PROPERTY(id, member, name) \ - _register(reinterpret_cast<SPIBasePtr>(&SPStyle::member), id, name) + _register(reinterpret_cast<SPIBasePtr>(&SPStyle::member), id) /* name unused */ // SVG 2: Attributes promoted to properties REGISTER_PROPERTY(SP_ATTR_D, d, "d"); @@ -215,11 +215,7 @@ public: * Get property pointer by name */ SPIBase *get(SPStyle *style, const std::string &name) { - auto it = m_name_map.find(name); - if (it != m_name_map.end()) { - return _get(style, it->second); - } - return nullptr; + return get(style, sp_attribute_lookup(name.c_str())); } /** @@ -238,19 +234,14 @@ public: private: SPIBase *_get(SPStyle *style, SPIBasePtr ptr) { return &(style->*ptr); } - void _register(SPIBasePtr ptr, SPAttributeEnum id, const char *name) { + void _register(SPIBasePtr ptr, SPAttributeEnum id) { m_vector.push_back(ptr); if (id != SP_ATTR_INVALID) { m_id_map[id] = ptr; } - - if (name[0]) { - m_name_map[name] = ptr; - } } - std::unordered_map<std::string, SPIBasePtr> m_name_map; std::unordered_map</* SPAttributeEnum */ int, SPIBasePtr> m_id_map; std::vector<SPIBasePtr> m_vector; }; |
