cpp-stat-bench 0.24.0
Benchmark library with statistics for C++.
Loading...
Searching...
No Matches
ordered_map.h
Go to the documentation of this file.
1/*
2 * Copyright 2025 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <algorithm>
23#include <cstddef>
24#include <initializer_list>
25#include <unordered_map>
26#include <utility>
27#include <vector>
28
29namespace stat_bench {
30namespace util {
31
38template <typename Key, typename MappedValue>
40public:
42 using iterator =
43 typename std::vector<std::pair<Key, MappedValue>>::iterator;
44
47 typename std::vector<std::pair<Key, MappedValue>>::const_iterator;
48
52 OrderedMap() = default;
53
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));
63 }
64 }
65
72 [[nodiscard]] auto empty() const noexcept -> bool {
73 return key_to_index_.empty();
74 }
75
81 [[nodiscard]] auto size() const noexcept -> std::size_t {
82 return key_to_index_.size();
83 }
84
91 [[nodiscard]] auto count(const Key& key) const -> std::size_t {
92 return key_to_index_.count(key);
93 }
94
100 void reserve(std::size_t size) {
101 key_to_index_.reserve(size);
102 pairs_.reserve(size);
103 }
104
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);
117 if (!success) {
118 pairs_.pop_back();
119 }
120 return {pairs_.begin() + static_cast<std::ptrdiff_t>(iter->second),
121 success};
122 }
123
132 template <typename... Args>
133 auto try_emplace(const Key& key, Args&&... 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),
138 false};
139 }
140 pairs_.emplace_back(key, MappedValue{std::forward<Args>(args)...});
141 key_to_index_[key] = pairs_.size() - 1;
142 return {pairs_.end() - 1, true};
143 }
144
152 const auto index = iter - pairs_.begin();
153 key_to_index_.erase(pairs_[index].first);
154 pairs_.erase(iter);
155 for (std::size_t i = index; i < pairs_.size(); ++i) {
156 key_to_index_[pairs_[i].first] = i;
157 }
158 return pairs_.begin() + static_cast<std::ptrdiff_t>(index);
159 }
160
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;
175 }
176 return pairs_[iter->second].second;
177 }
178
187 [[nodiscard]] auto at(const Key& key) const -> const MappedValue& {
188 return pairs_[key_to_index_.at(key)].second;
189 }
190
197 [[nodiscard]] auto find(const Key& key) const -> const_iterator {
198 const auto iter = key_to_index_.find(key);
199 if (iter == key_to_index_.end()) {
200 return pairs_.end();
201 }
202 return pairs_.begin() + static_cast<std::ptrdiff_t>(iter->second);
203 }
204
210 [[nodiscard]] auto begin() -> iterator { return pairs_.begin(); }
211
217 [[nodiscard]] auto begin() const -> const_iterator {
218 return pairs_.begin();
219 }
220
226 [[nodiscard]] auto end() -> iterator { return pairs_.end(); }
227
233 [[nodiscard]] auto end() const -> const_iterator { return pairs_.end(); }
234
242 [[nodiscard]] auto operator==(const OrderedMap& rhs) const -> bool {
243 if (key_to_index_.size() != rhs.key_to_index_.size()) {
244 return false;
245 }
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;
252 });
253 }
254
262 [[nodiscard]] auto operator!=(const OrderedMap& rhs) const -> bool {
263 return !(*this == rhs);
264 }
265
271 [[nodiscard]] auto pairs() const noexcept
272 -> const std::vector<std::pair<Key, MappedValue>>& {
273 return pairs_;
274 }
275
276private:
278 std::vector<std::pair<Key, MappedValue>> pairs_;
279
281 std::unordered_map<Key, std::size_t> key_to_index_;
282};
283
284} // namespace util
285} // namespace stat_bench
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.
Definition ordered_map.h:46
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.
Definition ordered_map.h:72
auto count(const Key &key) const -> std::size_t
Count the number of pairs with a key.
Definition ordered_map.h:91
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.
Definition ordered_map.h:42
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.
Definition ordered_map.h:81
OrderedMap(std::initializer_list< std::pair< Key, MappedValue > > initializer_list)
Constructor.
Definition ordered_map.h:59
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.
STL namespace.