12#include <initializer_list>
13#include <unordered_map>
41 auto operator*() const -> const auto& {
return Iter::operator*().first; }
48 auto operator*() ->
const auto& {
return Iter::operator*().first; }
80 template <
typename Key,
typename T>
class dict :
public std::unordered_map<Key, T> {
82 using Base = std::unordered_map<Key, T>;
102 dict(std::initializer_list<value_type> init) : Base{init} {}
110 auto contains(
const Key& key)
const ->
bool {
return this->find(key) != this->
end(); }
122 auto get(
const Key& key,
const T& default_value)
const -> T {
124 return default_value;
126 return this->
at(key);
137 return key_iterator<
decltype(Base::begin())>{Base::begin()};
148 return key_iterator<
decltype(Base::end())>{Base::end()};
159 return key_iterator<
decltype(Base::begin())>{Base::begin()};
170 return key_iterator<
decltype(Base::end())>{Base::end()};
178 auto items() -> Base& {
return *
this; }
185 auto items() const -> const Base& {
return *
this; }
216 auto at(
const Key& k)
const ->
const T& {
229 auto operator[](
const Key& k) -> T& {
return Base::operator[](k); }
271 template <typename Key, typename T>
272 inline auto operator<(const Key& key, const
dict<Key, T>& m) noexcept ->
bool {
284 template <
typename Key,
typename T>
inline auto len(
const dict<Key, T>& m)
noexcept ->
size_t {
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