SimpleWindow
载入中...
搜索中...
未找到
Dictionary.h
1#pragma once
2
3#include "IComparable.h"
4#include "IToString.h"
5#include "Utils.h"
6#include <map>
7#include <memory>
8#include <string>
9
10namespace sw
11{
12 template <typename TKey, typename TVal>
13 class Dictionary; // 向前声明
14
18 template <typename TVal>
19 using StrDictionary = Dictionary<std::wstring, TVal>;
20
24 template <typename TKey, typename TVal>
25 class Dictionary : public IToString<Dictionary<TKey, TVal>>,
26 public IEqualityComparable<Dictionary<TKey, TVal>>
27 {
28 private:
32 std::shared_ptr<std::map<TKey, TVal>> _pMap;
33
34 public:
39 : _pMap(std::make_shared<std::map<TKey, TVal>>())
40 {
41 }
42
46 Dictionary(std::initializer_list<std::pair<const TKey, TVal>> list)
47 : _pMap(std::make_shared<std::map<TKey, TVal>>(list))
48 {
49 }
50
54 auto begin() const
55 {
56 return this->_pMap->begin();
57 }
58
62 auto end() const
63 {
64 return this->_pMap->end();
65 }
66
70 auto rbegin() const
71 {
72 return this->_pMap->rbegin();
73 }
74
78 auto rend() const
79 {
80 return this->_pMap->rend();
81 }
82
87 auto &operator[](const TKey &key) const
88 {
89 return this->_pMap->at(key);
90 }
91
95 bool Equals(const Dictionary &other) const
96 {
97 return this->_pMap == other._pMap;
98 }
99
103 int Count() const
104 {
105 return (int)this->_pMap->size();
106 }
107
111 bool IsEmpty() const
112 {
113 return this->_pMap->empty();
114 }
115
120 auto Add(const TKey &key, const TVal &value) const
121 {
122 this->_pMap->insert(std::make_pair(key, value));
123 return *this;
124 }
125
130 bool ContainsKey(const TKey &key) const
131 {
132 return this->_pMap->count(key);
133 }
134
139 bool ContainsValue(const TVal &value) const
140 {
141 for (const auto &pair : *this->_pMap) {
142 if (pair.second == value) {
143 return true;
144 }
145 }
146 return false;
147 }
148
153 void Remove(const TKey &key) const
154 {
155 this->_pMap->erase(key);
156 }
157
161 void Clear() const
162 {
163 this->_pMap->clear();
164 }
165
170 {
171 Dictionary dic;
172 dic._pMap->insert(this->_pMap->begin(), this->_pMap->end());
173 return dic;
174 }
175
179 std::map<TKey, TVal> &GetStdMap() const
180 {
181 return *this->_pMap;
182 }
183
187 std::wstring ToString() const
188 {
189 return Utils::BuildStr(*this->_pMap);
190 }
191 };
192}
字典类,内部维护了一个指向std::map的智能指针
Definition Dictionary.h:27
void Clear() const
清空字典
Definition Dictionary.h:161
auto Add(const TKey &key, const TVal &value) const
添加键值对到字典
Definition Dictionary.h:120
bool IsEmpty() const
字典是否为空
Definition Dictionary.h:111
Dictionary(std::initializer_list< std::pair< const TKey, TVal > > list)
使用初始化列表
Definition Dictionary.h:46
bool Equals(const Dictionary &other) const
判断是否为同一个字典
Definition Dictionary.h:95
auto & operator[](const TKey &key) const
获取或设置值
Definition Dictionary.h:87
std::map< TKey, TVal > & GetStdMap() const
获取字典内部维护的std::map
Definition Dictionary.h:179
bool ContainsKey(const TKey &key) const
是否存在某个键值
Definition Dictionary.h:130
Dictionary()
初始化字典
Definition Dictionary.h:38
std::wstring ToString() const
获取描述当前对象的字符串
Definition Dictionary.h:187
int Count() const
获取键值对个数
Definition Dictionary.h:103
auto end() const
正向迭代器结束
Definition Dictionary.h:62
auto begin() const
正向迭代器开始
Definition Dictionary.h:54
bool ContainsValue(const TVal &value) const
遍历字典,查询是否存在某个值
Definition Dictionary.h:139
auto rbegin() const
反向迭代器开始
Definition Dictionary.h:70
auto rend() const
反向迭代器结束
Definition Dictionary.h:78
Dictionary Copy() const
复制当前字典
Definition Dictionary.h:169
void Remove(const TKey &key) const
移除指定键值对
Definition Dictionary.h:153
相等性比较接口
Definition IComparable.h:14
为支持ToString方法的类提供统一接口
Definition IToString.h:13
static std::wstring BuildStr(const Args &...args)
拼接字符串,也可使用此函数将其他类型转为wstring
Definition Utils.h:113