Py2Cpp 1.6.3; VERSION ${PROJECT_VERSION}
Loading...
Searching...
No Matches
dict.hpp
Go to the documentation of this file.
1
9#pragma once
10
11#include <cstddef>
12#include <initializer_list>
13#include <unordered_map>
14#include <utility>
15
16// template <typename T> using Value_type = typename T::value_type;
17
18namespace py {
19
28 template <typename Iter> struct key_iterator : Iter {
34 explicit key_iterator(Iter it) : Iter(it) {}
35
41 auto operator*() const -> const auto& { return Iter::operator*().first; }
42
48 auto operator*() -> const auto& { return Iter::operator*().first; }
49
56 Iter::operator++();
57 return *this;
58 }
64 auto operator++(int) -> key_iterator {
65 auto old = *this;
66 ++*this;
67 return old;
68 }
69 };
70
80 template <typename Key, typename T> class dict : public std::unordered_map<Key, T> {
81 using Self = dict<Key, T>;
82 using Base = std::unordered_map<Key, T>;
83
84 public:
85 using value_type = std::pair<const Key, T>;
86 using key_type = Key;
87
93 dict() : Base{} {}
94
102 dict(std::initializer_list<value_type> init) : Base{init} {}
103
110 auto contains(const Key& key) const -> bool { return this->find(key) != this->end(); }
111
122 auto get(const Key& key, const T& default_value) const -> T {
123 if (!contains(key)) {
124 return default_value;
125 }
126 return this->at(key);
127 }
128
136 auto begin() -> key_iterator<decltype(Base::begin())> {
137 return key_iterator<decltype(Base::begin())>{Base::begin()};
138 }
139
147 auto end() -> key_iterator<decltype(Base::end())> {
148 return key_iterator<decltype(Base::end())>{Base::end()};
149 }
150
158 auto begin() const -> key_iterator<decltype(Base::begin())> {
159 return key_iterator<decltype(Base::begin())>{Base::begin()};
160 }
161
169 auto end() const -> key_iterator<decltype(Base::end())> {
170 return key_iterator<decltype(Base::end())>{Base::end()};
171 }
172
178 auto items() -> Base& { return *this; }
179
185 auto items() const -> const Base& { return *this; }
186
192 auto copy() const -> Self { return *this; }
193
203 auto operator[](const Key& k) const -> const T& {
204 return Base::at(k); // luk: a bug in std::unordered_map?
205 }
206
216 auto at(const Key& k) const -> const T& {
217 return Base::at(k); // luk: a bug in std::unordered_map?
218 }
219
229 auto operator[](const Key& k) -> T& { return Base::operator[](k); }
230
236 auto operator=(const Self&) -> Self& = delete;
237
243 auto operator=(Self&&) noexcept -> dict& = default;
244
249 dict(dict<Key, T>&&) noexcept = default;
250
251 ~dict() = default;
252
253 // private:
259 dict(const dict<Key, T>&) = default;
260 };
261
271 template <typename Key, typename T>
272 inline auto operator<(const Key& key, const dict<Key, T>& m) noexcept -> bool {
273 return m.contains(key);
274 }
275
284 template <typename Key, typename T> inline auto len(const dict<Key, T>& m) noexcept -> size_t {
285 return m.size();
286 }
287
294 // template <typename Key, typename T>
295 // dict(std::initializer_list<std::pair<const Key, T>>) -> dict<Key, T>;
296
297 // template <class Sequence>
298 // dict(const Sequence& S)
299 // -> dict<std::remove_cv_t<decltype(*std::begin(S))>, size_t>;
300
301} // namespace py
Python-like dictionary implementation.
Definition dict.hpp:80
Key key_type
Definition dict.hpp:86
auto operator=(Self &&) noexcept -> dict &=default
Move assignment operator.
auto get(const Key &key, const T &default_value) const -> T
Get a value with a default fallback.
Definition dict.hpp:122
auto contains(const Key &key) const -> bool
Check if the dictionary contains a specific key.
Definition dict.hpp:110
auto operator[](const Key &k) const -> const T &
Access value by key (const version)
Definition dict.hpp:203
auto begin() -> key_iterator< decltype(Base::begin())>
Get iterator to the beginning of keys.
Definition dict.hpp:136
std::pair< const Key, T > value_type
Definition dict.hpp:85
auto operator[](const Key &k) -> T &
Access or insert value by key.
Definition dict.hpp:229
dict()
Default constructor.
Definition dict.hpp:93
auto end() -> key_iterator< decltype(Base::end())>
Get iterator to the end of keys.
Definition dict.hpp:147
auto begin() const -> key_iterator< decltype(Base::begin())>
Get const iterator to the beginning of keys.
Definition dict.hpp:158
auto end() const -> key_iterator< decltype(Base::end())>
Get const iterator to the end of keys.
Definition dict.hpp:169
dict(std::initializer_list< value_type > init)
Construct a dict from an initializer list.
Definition dict.hpp:102
auto copy() const -> Self
Create a copy of the dictionary.
Definition dict.hpp:192
auto items() const -> const Base &
Get the underlying map for iteration (const version)
Definition dict.hpp:185
auto items() -> Base &
Get the underlying map for iteration.
Definition dict.hpp:178
auto operator=(const Self &) -> Self &=delete
Copy assignment operator (deleted)
auto at(const Key &k) const -> const T &
Access value by key with bounds checking (const version)
Definition dict.hpp:216
Python-like utilities and data structures for C++.
Definition dict.hpp:18
auto len(const dict< Key, T > &m) noexcept -> size_t
Get the number of key-value pairs in a dictionary.
Definition dict.hpp:284
Iterator adapter for accessing keys in map-like containers.
Definition dict.hpp:28
auto operator*() const -> const auto &
Dereference to get the key (const version)
Definition dict.hpp:41
auto operator*() -> const auto &
Dereference to get the key (non-const version)
Definition dict.hpp:48
key_iterator(Iter it)
Construct a key iterator from an underlying iterator.
Definition dict.hpp:34
auto operator++(int) -> key_iterator
Post-increment operator.
Definition dict.hpp:64
auto operator++() -> key_iterator &
Pre-increment operator.
Definition dict.hpp:55