🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
OrderedMap.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include <vector>
25#include <unordered_map>
26#include <stdexcept> // for std::out_of_range
27#include <utility> // for std::pair
28
29AST_NAMESPACE_BEGIN
30
40template<typename Key, typename Value>
42private:
43 using vector_type = std::vector<std::pair<Key, Value>>;
44 using map_type = std::unordered_map<Key, std::size_t>;
45
46 // 按插入顺序存储键值对
47 vector_type items_;
48 // 键到下标位置的快速映射
49 map_type index_map_;
50public: // ---------- 迭代器支持 ----------
51
52 using iterator = typename vector_type::iterator;
53 using const_iterator = typename vector_type::const_iterator;
54
55 iterator begin() { return items_.begin(); }
56 iterator end() { return items_.end(); }
57 const_iterator begin() const { return items_.begin(); }
58 const_iterator end() const { return items_.end(); }
59 const_iterator cbegin() const { return items_.cbegin(); }
60 const_iterator cend() const { return items_.cend(); }
61
62public: // ---------- 容量 ----------
63
65 bool empty() const noexcept {
66 return items_.empty();
67 }
68
70 std::size_t size() const noexcept {
71 return items_.size();
72 }
73
74public: // ---------- 访问元素 ----------
75
77 Value& at(const Key& key) {
78 auto it = index_map_.find(key);
79 if (it == index_map_.end())
80 throw std::out_of_range("OrderedMap::at: key not found");
81 return items_[it->second].second;
82 }
83
85 const Value& at(const Key& key) const {
86 auto it = index_map_.find(key);
87 if (it == index_map_.end())
88 throw std::out_of_range("OrderedMap::at: key not found");
89 return items_[it->second].second;
90 }
91
92 Value& operator[](const Key& key) {
93 auto it = index_map_.find(key);
94 if (it != index_map_.end()) {
95 return items_[it->second].second;
96 }
97 // 不存在则插入默认构造的值
98 index_map_[key] = items_.size();
99 items_.emplace_back(key, Value{});
100 return items_.back().second;
101 }
102
103public: // ---------- 插入/更新 ----------
104
106 void insert(const Key& key, const Value& value) {
107 auto it = index_map_.find(key);
108 if (it != index_map_.end()) {
109 // 已存在:更新值
110 items_[it->second].second = value;
111 } else {
112 // 不存在:追加到末尾
113 index_map_[key] = items_.size();
114 items_.emplace_back(key, value);
115 }
116 }
117
119 void insert(std::pair<Key, Value>&& pair) {
120 insert(pair.first, std::move(pair.second));
121 }
122
123public: // ---------- 查找 ----------
124
126 iterator find(const Key& key) {
127 auto it = index_map_.find(key);
128 if (it == index_map_.end())
129 return items_.end();
130 return items_.begin() + it->second;
131 }
132
134 const_iterator find(const Key& key) const {
135 auto it = index_map_.find(key);
136 if (it == index_map_.end())
137 return items_.end();
138 return items_.begin() + it->second;
139 }
140
142 bool contains(const Key& key) const {
143 return index_map_.find(key) != index_map_.end();
144 }
145
146public:
148 void clear() {
149 items_.clear();
150 index_map_.clear();
151 }
152
153public:
155 std::vector<Key> keys() const {
156 std::vector<Key> result;
157 result.reserve(items_.size());
158 for (const auto& pair : items_) {
159 result.push_back(pair.first);
160 }
161 return result;
162 }
163
165 std::vector<Value> values() const {
166 std::vector<Value> result;
167 result.reserve(items_.size());
168 for (const auto& pair : items_) {
169 result.push_back(pair.second);
170 }
171 return result;
172 }
173
175 const vector_type& items() const { return items_; }
176};
177
180AST_NAMESPACE_END
181
有序映射类,保持键值对的插入顺序
定义 OrderedMap.hpp:41
void clear()
清空映射
定义 OrderedMap.hpp:148
const_iterator find(const Key &key) const
查找键对应的迭代器(常量版本)
定义 OrderedMap.hpp:134
Value & at(const Key &key)
获取键对应的值
定义 OrderedMap.hpp:77
void insert(const Key &key, const Value &value)
插入或更新键值对
定义 OrderedMap.hpp:106
std::size_t size() const noexcept
获取映射中键值对的数量
定义 OrderedMap.hpp:70
void insert(std::pair< Key, Value > &&pair)
插入或更新键值对(移动语义)
定义 OrderedMap.hpp:119
bool empty() const noexcept
检查映射是否为空
定义 OrderedMap.hpp:65
iterator find(const Key &key)
查找键对应的迭代器
定义 OrderedMap.hpp:126
const Value & at(const Key &key) const
获取键对应的值(常量版本)
定义 OrderedMap.hpp:85
std::vector< Value > values() const
获取所有值
定义 OrderedMap.hpp:165
std::vector< Key > keys() const
获取所有键
定义 OrderedMap.hpp:155
const vector_type & items() const
获取所有键值对
定义 OrderedMap.hpp:175
bool contains(const Key &key) const
检查映射是否包含指定键
定义 OrderedMap.hpp:142
值对象基类
定义 Value.hpp:43