add class dictionary to avoid storing strings all the time
This commit is contained in:
32
include/llmr/style/class_dictionary.hpp
Normal file
32
include/llmr/style/class_dictionary.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef LLMR_STYLE_CLASS_DICTIONARY
|
||||
#define LLMR_STYLE_CLASS_DICTIONARY
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace llmr {
|
||||
|
||||
enum class ClassID : uint32_t {
|
||||
Fallback = 0, // These values are from the fallback properties
|
||||
Default = 1, // These values are from the default style for a layer
|
||||
Named = 2 // These values (and all subsequent IDs) are from a named style from the layer
|
||||
};
|
||||
|
||||
class ClassDictionary {
|
||||
public:
|
||||
// Returns an ID for a class name. If the class name does not yet have an ID, one is
|
||||
// auto-generated and stored for future reference.
|
||||
static ClassID Lookup(const std::string &class_name);
|
||||
|
||||
// Returns either Fallback, Default or Named, depending on the type of the class id.
|
||||
static ClassID Normalize(ClassID id);
|
||||
|
||||
private:
|
||||
static std::unordered_map<std::string, ClassID> store;
|
||||
static uint32_t offset;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
29
src/style/class_dictionary.cpp
Normal file
29
src/style/class_dictionary.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <llmr/style/class_dictionary.hpp>
|
||||
|
||||
namespace llmr {
|
||||
|
||||
ClassID ClassDictionary::Lookup(const std::string &class_name) {
|
||||
auto it = store.find(class_name);
|
||||
if (it == store.end()) {
|
||||
// Insert the class name into the store.
|
||||
ClassID id = ClassID(uint32_t(ClassID::Named) + offset++);
|
||||
store.emplace(class_name, id);
|
||||
return id;
|
||||
} else {
|
||||
return it->second;
|
||||
}
|
||||
}
|
||||
|
||||
ClassID ClassDictionary::Normalize(ClassID id) {
|
||||
if (id >= ClassID::Named) {
|
||||
return ClassID::Named;
|
||||
} else {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::unordered_map<std::string, ClassID> ClassDictionary::store = { { "", ClassID::Default } };
|
||||
uint32_t ClassDictionary::offset = 0;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user