Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

map.hpp
Go to the documentation of this file.
1 //
2 // immer: immutable data structures for C++
3 // Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
4 //
5 // This software is distributed under the Boost Software License, Version 1.0.
6 // See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
7 //
8 
9 #pragma once
10 
11 #include <immer/memory_policy.hpp>
14 
15 #include <functional>
16 
17 namespace immer {
18 
19 template <typename K,
20  typename T,
21  typename Hash,
22  typename Equal,
23  typename MemoryPolicy,
26 
58 template <typename K,
59  typename T,
60  typename Hash = std::hash<K>,
61  typename Equal = std::equal_to<K>,
62  typename MemoryPolicy = default_memory_policy,
64 class map
65 {
66  using value_t = std::pair<K, T>;
67 
69  {
70  const T& operator() (const value_t& v) const noexcept
71  {
72  return v.second;
73  }
74  };
75 
77  {
78  const T* operator() (const value_t& v) const noexcept
79  {
80  return &v.second;
81  }
82  };
83 
85  {
86  template <typename Kf, typename Tf>
87  value_t operator() (Kf&& k, Tf&& v) const
88  {
89  return { std::forward<Kf>(k), std::forward<Tf>(v) };
90  }
91  };
92 
94  {
95  const T& operator() () const
96  {
97  static T v{};
98  return v;
99  }
100  };
101 
102  struct error_value
103  {
104  const T& operator() () const
105  {
106  throw std::out_of_range{"key not found"};
107  }
108  };
109 
110  struct hash_key
111  {
112  auto operator() (const value_t& v)
113  { return Hash{}(v.first); }
114 
115  auto operator() (const K& v)
116  { return Hash{}(v); }
117  };
118 
119  struct equal_key
120  {
121  auto operator() (const value_t& a, const value_t& b)
122  { return Equal{}(a.first, b.first); }
123 
124  auto operator() (const value_t& a, const K& b)
125  { return Equal{}(a.first, b); }
126  };
127 
128  struct equal_value
129  {
130  auto operator() (const value_t& a, const value_t& b)
131  { return Equal{}(a.first, b.first) && a.second == b.second; }
132  };
133 
135  value_t, hash_key, equal_key, MemoryPolicy, B>;
136 
137 public:
138  using key_type = K;
139  using mapped_type = T;
140  using value_type = std::pair<K, T>;
142  using diference_type = std::ptrdiff_t;
143  using hasher = Hash;
144  using key_equal = Equal;
145  using reference = const value_type&;
146  using const_reference = const value_type&;
147 
149  value_t, hash_key, equal_key, MemoryPolicy, B>;
151 
153 
158  map() = default;
159 
165  iterator begin() const { return {impl_}; }
166 
171  iterator end() const { return {impl_, typename iterator::end_t{}}; }
172 
177  size_type size() const { return impl_.size; }
178 
184  size_type count(const K& k) const
185  { return impl_.template get<detail::constantly<size_type, 1>,
187 
194  const T& operator[] (const K& k) const
195  { return impl_.template get<project_value, default_value>(k); }
196 
203  const T& at(const K& k) const
204  { return impl_.template get<project_value, error_value>(k); }
205 
206 
236  const T* find(const K& k) const
237  { return impl_.template get<project_value_ptr,
239 
243  bool operator==(const map& other) const
244  { return impl_.template equals<equal_value>(other.impl_); }
245  bool operator!=(const map& other) const
246  { return !(*this == other); }
247 
254  map insert(value_type value) const
255  { return impl_.add(std::move(value)); }
256 
263  map set(key_type k, mapped_type v) const
264  { return impl_.add({std::move(k), std::move(v)}); }
265 
273  template <typename Fn>
274  map update(key_type k, Fn&& fn) const
275  {
276  return impl_
277  .template update<project_value, default_value, combine_value>(
278  std::move(k), std::forward<Fn>(fn));
279  }
280 
286  map erase(const K& k) const
287  { return impl_.sub(k); }
288 
293  transient_type transient() const&
294  { return transient_type{ impl_ }; }
295  transient_type transient() &&
296  { return transient_type{ std::move(impl_) }; }
297 
298  // Semi-private
299  const impl_t& impl() const { return impl_; }
300 
301 private:
303 
305  : impl_(std::move(impl))
306  {}
307 
309 };
310 
311 } // namespace immer
const T & operator()(const value_t &v) const noexcept
Definition: map.hpp:70
auto operator()(const value_t &a, const value_t &b)
Definition: map.hpp:130
const auto default_bits
Definition: config.hpp:63
impl_t impl_
Definition: map.hpp:308
std::equal_to< uint256 > key_equal
Definition: map.hpp:144
friend transient_type
Definition: map.hpp:302
const T * find(const K &k) const
Definition: map.hpp:236
Definition: box.hpp:161
const T * operator()(const value_t &v) const noexcept
Definition: map.hpp:78
auto operator()(const value_t &v)
Definition: map.hpp:112
size_type count(const K &k) const
Definition: map.hpp:184
bool operator==(const map &other) const
Definition: map.hpp:243
map(impl_t impl)
Definition: map.hpp:304
std::pair< uint256, CDeterministicMNCPtr > value_type
Definition: map.hpp:140
std::uint32_t bits_t
Definition: bits.hpp:23
const T & at(const K &k) const
Definition: map.hpp:203
champ add(T v) const
Definition: champ.hpp:219
std::size_t size_t
Definition: bits.hpp:21
detail::hamts::champ_iterator< value_t, hash_key, equal_key, MemoryPolicy, B > iterator
Definition: map.hpp:149
iterator begin() const
Definition: map.hpp:165
memory_policy< default_heap_policy, default_refcount_policy > default_memory_policy
iterator end() const
Definition: map.hpp:171
map update(key_type k, Fn &&fn) const
Definition: map.hpp:274
champ sub(const K &k) const
Definition: champ.hpp:407
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:84
detail::hamts::champ< value_t, hash_key, equal_key, MemoryPolicy, B > impl_t
Definition: map.hpp:135
value_t operator()(Kf &&k, Tf &&v) const
Definition: map.hpp:87
256-bit opaque blob.
Definition: uint256.h:123
size_type size() const
Definition: map.hpp:177
const T & operator()() const
Definition: map.hpp:104
const T & operator()() const
Definition: map.hpp:95
bool operator!=(const map &other) const
Definition: map.hpp:245
auto operator()(const value_t &a, const value_t &b)
Definition: map.hpp:121
std::pair< uint256, CDeterministicMNCPtr > value_t
Definition: map.hpp:66
const impl_t & impl() const
Definition: map.hpp:299
const T & operator[](const K &k) const
Definition: map.hpp:194
map()=default
map erase(const K &k) const
Definition: map.hpp:286
map insert(value_type value) const
Definition: map.hpp:254
Released under the MIT license