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 /src/attributes.cpp | |
| 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
Diffstat (limited to 'src/attributes.cpp')
| -rw-r--r-- | src/attributes.cpp | 34 |
1 files changed, 28 insertions, 6 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; |
