| 19 | template <class Key, class T, class IgnoredLess = std::less<Key>, |
| 20 | class Allocator = std::allocator<std::pair<const Key, T>>> |
| 21 | struct ordered_map : std::vector<std::pair<const Key, T>, Allocator> |
| 22 | { |
| 23 | using key_type = Key; |
| 24 | using mapped_type = T; |
| 25 | using Container = std::vector<std::pair<const Key, T>, Allocator>; |
| 26 | using typename Container::iterator; |
| 27 | using typename Container::const_iterator; |
| 28 | using typename Container::size_type; |
| 29 | using typename Container::value_type; |
| 30 | |
| 31 | // Explicit constructors instead of `using Container::Container` |
| 32 | // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4) |
| 33 | ordered_map(const Allocator& alloc = Allocator()) : Container{alloc} {} |
| 34 | template <class It> |
| 35 | ordered_map(It first, It last, const Allocator& alloc = Allocator()) |
| 36 | : Container{first, last, alloc} {} |
| 37 | ordered_map(std::initializer_list<T> init, const Allocator& alloc = Allocator() ) |
| 38 | : Container{init, alloc} {} |
| 39 | |
| 40 | std::pair<iterator, bool> emplace(const key_type& key, T&& t) |
| 41 | { |
| 42 | for (auto it = this->begin(); it != this->end(); ++it) |
| 43 | { |
| 44 | if (it->first == key) |
| 45 | { |
| 46 | return {it, false}; |
| 47 | } |
| 48 | } |
| 49 | Container::emplace_back(key, t); |
| 50 | return {--this->end(), true}; |
| 51 | } |
| 52 | |
| 53 | T& operator[](const Key& key) |
| 54 | { |
| 55 | return emplace(key, T{}).first->second; |
| 56 | } |
| 57 | |
| 58 | const T& operator[](const Key& key) const |
| 59 | { |
| 60 | return at(key); |
| 61 | } |
| 62 | |
| 63 | T& at(const Key& key) |
| 64 | { |
| 65 | for (auto it = this->begin(); it != this->end(); ++it) |
| 66 | { |
| 67 | if (it->first == key) |
| 68 | { |
| 69 | return it->second; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | JSON_THROW(std::out_of_range("key not found")); |
| 74 | } |
| 75 | |
| 76 | const T& at(const Key& key) const |
| 77 | { |
| 78 | for (auto it = this->begin(); it != this->end(); ++it) |
nothing calls this directly
no test coverage detected