@@ -0,0 +1,58 @@
1+#ifndef string_literal_dict_H
2+#define string_literal_dict_H
3+
4+#include <cstring>
5+#include <string>
6+#include <vector>
7+#include <utility>
8+
9+
10+template <typename T>
11+class string_literal_dict_t : private std::vector<std::pair<const char *, T>> {
12+
13+ static auto null() -> const T& {
14+ static const T value;
15+ return value;
16+ }
17+
18+ public:
19+ using std::vector<std::pair<const char *, T>>::vector;
20+
21+ auto operator[](const char key[]) const -> const T& {
22+ for (auto it = this->cbegin(), end=this->cend(); it != end; it++) {
23+ const auto& elem_key = it->first;
24+ if (elem_key == key || !std::strcmp(elem_key, key)) {
25+ return it->second;
26+ }
27+ }
28+#ifndef NDEBUG
29+ throw std::runtime_error("dictionary key \"" + std::string(key) + "\" not found");
30+#endif
31+ return null();
32+ }
33+
34+ auto operator[](const char key[]) -> T& {
35+ const auto end = this->end();
36+ for (auto it = this->begin(); it != end; it++) {
37+ const auto& elem_key = it->first;
38+ if (elem_key == key || !std::strcmp(elem_key, key)) {
39+ return it->second;
40+ }
41+ }
42+ return this->emplace(end, key, T())->second;
43+ }
44+
45+ auto contains(const char key[]) const -> bool {
46+ for (auto it = this->cbegin(), end=this->cend(); it != end; it++) {
47+ const auto& elem_key = it->first;
48+ if (elem_key == key || !std::strcmp(elem_key, key)) {
49+ return true;
50+ }
51+ }
52+ return false;
53+ }
54+
55+};
56+
57+
58+#endif // string_literal_dict_H