1///////////////////////////////////////////////////////////////////////////////
3// Module : dictionary.h
4// Copyright : (c) Andy Arvanitis 2018
7// Maintainer : Andy Arvanitis
8// Stability : experimental
11// A simple dictionary optimized for this runtime's particular use –
12// *not* intended for general use or as a replacement for containers
13// such as std::map or std::unordered_map. It relies on the following
16// 1) Keys are C-type string literals. No copying or (memory management)
18// 2) The number of elements in the container is generally small – say,
19// under 20. There is no sorting and a linear search is used to
22///////////////////////////////////////////////////////////////////////////////
24#ifndef purescript_dictionary_H
25#define purescript_dictionary_H
35 namespace _template_ {
38 class dict_t : public std::vector<std::pair<const char *, T>> {
40 static auto null() -> const T& {
46 using std::vector<std::pair<const char *, T>>::vector;
48 auto operator[](const char key[]) const -> const T& {
49 for (auto it = this->cbegin(), end=this->cend(); it != end; it++) {
50 const auto& elem_key = it->first;
51 if (elem_key == key || !std::strcmp(elem_key, key)) {
55#if !defined(NDEBUG) && !defined(PURESCRIPT_DISABLE_EXCEPTIONS)
56 throw std::runtime_error("dictionary key \"" + std::string(key) + "\" not found");
61 auto at(const char key[]) const -> const T& {
65 auto operator[](const char key[]) -> T& {
66 const auto end = this->end();
67 for (auto it = this->begin(); it != end; it++) {
68 const auto& elem_key = it->first;
69 if (elem_key == key || !std::strcmp(elem_key, key)) {
73 return this->emplace(end, key, T())->second;
76 auto contains(const char key[]) const -> bool {
77 for (auto it = this->cbegin(), end=this->cend(); it != end; it++) {
78 const auto& elem_key = it->first;
79 if (elem_key == key || !std::strcmp(elem_key, key)) {
88 } // namespace _template_
89} // namespace purescript
91#endif // purescript_dictionary_H
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list