24#include <initializer_list>
25#include <unordered_map>
38template <
typename Key,
typename MappedValue>
43 typename std::vector<std::pair<Key, MappedValue>>
::iterator;
60 std::initializer_list<std::pair<Key, MappedValue>> initializer_list) {
61 for (
auto& pair : initializer_list) {
62 emplace(std::move(pair.first), std::move(pair.second));
72 [[nodiscard]]
auto empty() const noexcept ->
bool {
73 return key_to_index_.empty();
81 [[nodiscard]]
auto size() const noexcept ->
std::
size_t {
82 return key_to_index_.size();
91 [[nodiscard]]
auto count(
const Key& key)
const -> std::size_t {
92 return key_to_index_.count(key);
101 key_to_index_.reserve(
size);
102 pairs_.reserve(
size);
112 template <
typename... Args>
113 auto emplace(Args&&... args) -> std::pair<iterator, bool> {
114 pairs_.emplace_back(std::forward<Args>(args)...);
115 const auto [iter, success] =
116 key_to_index_.emplace(pairs_.back().first, pairs_.size() - 1);
120 return {pairs_.begin() +
static_cast<std::ptrdiff_t
>(iter->second),
132 template <
typename... Args>
134 -> std::pair<iterator, bool> {
135 const auto iter = key_to_index_.find(key);
136 if (iter != key_to_index_.end()) {
137 return {pairs_.begin() +
static_cast<std::ptrdiff_t
>(iter->second),
140 pairs_.emplace_back(key, MappedValue{std::forward<Args>(args)...});
141 key_to_index_[key] = pairs_.size() - 1;
142 return {pairs_.end() - 1,
true};
152 const auto index = iter - pairs_.begin();
153 key_to_index_.erase(pairs_[index].first);
155 for (std::size_t i = index; i < pairs_.size(); ++i) {
156 key_to_index_[pairs_[i].first] = i;
158 return pairs_.begin() +
static_cast<std::ptrdiff_t
>(index);
169 [[nodiscard]]
auto operator[](
const Key& key) -> MappedValue& {
170 const auto iter = key_to_index_.find(key);
171 if (iter == key_to_index_.end()) {
172 pairs_.emplace_back(key, MappedValue{});
173 key_to_index_[key] = pairs_.size() - 1;
174 return pairs_.back().second;
176 return pairs_[iter->second].second;
187 [[nodiscard]]
auto at(
const Key& key)
const ->
const MappedValue& {
188 return pairs_[key_to_index_.at(key)].second;
198 const auto iter = key_to_index_.find(key);
199 if (iter == key_to_index_.end()) {
202 return pairs_.begin() +
static_cast<std::ptrdiff_t
>(iter->second);
218 return pairs_.begin();
243 if (key_to_index_.size() != rhs.key_to_index_.size()) {
246 return std::all_of(key_to_index_.begin(), key_to_index_.end(),
247 [
this, &rhs](
const auto& pair) {
248 const auto rhs_iter = rhs.key_to_index_.find(pair.first);
249 return rhs_iter != rhs.key_to_index_.end() &&
250 pairs_[pair.second].second ==
251 rhs.pairs_[rhs_iter->second].second;
263 return !(*
this == rhs);
271 [[nodiscard]]
auto pairs() const noexcept
272 -> const
std::vector<
std::pair<Key, MappedValue>>& {
278 std::vector<std::pair<Key, MappedValue>> pairs_;
281 std::unordered_map<Key, std::size_t> key_to_index_;
auto end() const -> const_iterator
Get an iterator pointing to the end of pairs.
auto at(const Key &key) const -> const MappedValue &
Access a mapped value.
auto erase(const_iterator iter) -> iterator
Erase a pair.
auto find(const Key &key) const -> const_iterator
Find a pair with a key.
typename std::vector< std::pair< Key, MappedValue > >::const_iterator const_iterator
Type of iterators.
auto begin() -> iterator
Get an iterator pointing to the beginning of pairs.
auto try_emplace(const Key &key, Args &&... args) -> std::pair< iterator, bool >
Insert a pair if the key does not exist.
auto empty() const noexcept -> bool
Check whether this mapping is empty.
auto count(const Key &key) const -> std::size_t
Count the number of pairs with a key.
auto begin() const -> const_iterator
Get an iterator pointing to the beginning of pairs.
auto emplace(Args &&... args) -> std::pair< iterator, bool >
Insert a pair.
void reserve(std::size_t size)
Reserve memory space.
auto pairs() const noexcept -> const std::vector< std::pair< Key, MappedValue > > &
Get pairs.
auto operator==(const OrderedMap &rhs) const -> bool
Check whether this is equal to another mapping.
typename std::vector< std::pair< Key, MappedValue > >::iterator iterator
Type of iterators.
auto end() -> iterator
Get an iterator pointing to the end of pairs.
OrderedMap()=default
Constructor.
auto size() const noexcept -> std::size_t
Get the number of pairs.
OrderedMap(std::initializer_list< std::pair< Key, MappedValue > > initializer_list)
Constructor.
auto operator!=(const OrderedMap &rhs) const -> bool
Check whether this is not equal to another mapping.
auto operator[](const Key &key) -> MappedValue &
Access a mapped value.
Namespace of utility functions and classes.
Namespace of stat_bench source codes.