SimpleWindow
载入中...
搜索中...
未找到
List.h
1#pragma once
2
3#include "IComparable.h"
4#include "IToString.h"
5#include "Utils.h"
6#include <algorithm>
7#include <memory>
8#include <string>
9#include <vector>
10
11namespace sw
12{
13 template <typename T>
14 class List; // 向前声明
15
19 using StrList = List<std::wstring>;
20
24 template <typename T>
25 class List : public IToString<List<T>>,
26 public IEqualityComparable<List<T>>
27 {
28 private:
32 std::shared_ptr<std::vector<T>> _pVec;
33
34 public:
39 : _pVec(std::make_shared<std::vector<T>>())
40 {
41 }
42
46 List(std::initializer_list<T> list)
47 : _pVec(std::make_shared<std::vector<T>>(list))
48 {
49 }
50
54 explicit List(int capacity)
55 : List()
56 {
57 this->_pVec->reserve(capacity);
58 }
59
63 auto begin() const
64 {
65 return this->_pVec->begin();
66 }
67
71 auto end() const
72 {
73 return this->_pVec->end();
74 }
75
79 auto rbegin() const
80 {
81 return this->_pVec->rbegin();
82 }
83
87 auto rend() const
88 {
89 return this->_pVec->rend();
90 }
91
95 auto &operator[](int index) const
96 {
97 return this->_pVec->at(index);
98 }
99
103 bool Equals(const List &other) const
104 {
105 return this->_pVec == other._pVec;
106 }
107
111 int Capacity() const
112 {
113 return (int)this->_pVec->capacity();
114 }
115
119 int Count() const
120 {
121 return (int)this->_pVec->size();
122 }
123
127 bool IsEmpty() const
128 {
129 return this->_pVec->empty();
130 }
131
136 auto Append(const T &value) const
137 {
138 this->_pVec->push_back(value);
139 return *this;
140 }
141
146 auto Append(T &&value) const
147 {
148 this->_pVec->push_back(std::forward<T>(value));
149 return *this;
150 }
151
156 auto Insert(int index, const T &value) const
157 {
158 this->_pVec->insert(this->_pVec->begin() + index, value);
159 return *this;
160 }
161
166 auto Insert(int index, T &&value) const
167 {
168 this->_pVec->insert(this->_pVec->begin() + index, std::forward<T>(value));
169 return *this;
170 }
171
176 bool Contains(const T &value) const
177 {
178 return std::find(this->_pVec->begin(), this->_pVec->end(), value) != this->_pVec->end();
179 }
180
186 int IndexOf(const T &value) const
187 {
188 auto it = std::find(this->_pVec->begin(), this->_pVec->end(), value);
189 return it == this->_pVec->end() ? -1 : int(it - this->_pVec->begin());
190 }
191
197 bool Remove(const T &value) const
198 {
199 auto it = std::find(this->_pVec->begin(), this->_pVec->end(), value);
200 if (it == this->_pVec->end())
201 return false;
202 this->_pVec->erase(it);
203 return true;
204 }
205
210 void RemoveAt(int index) const
211 {
212 this->_pVec->erase(this->_pVec->begin() + index);
213 }
214
218 void Clear() const
219 {
220 this->_pVec->clear();
221 }
222
226 List Copy() const
227 {
228 List list((int)this->_pVec->capacity());
229 list._pVec->assign(this->_pVec->begin(), this->_pVec->end());
230 return list;
231 }
232
236 std::vector<T> &GetStdVector() const
237 {
238 return *this->_pVec;
239 }
240
244 std::wstring ToString() const
245 {
246 return Utils::BuildStr(*this->_pVec);
247 }
248 };
249}
相等性比较接口
Definition IComparable.h:14
为支持ToString方法的类提供统一接口
Definition IToString.h:13
列表类,内部维护了一个指向std::vector的智能指针
Definition List.h:27
auto Insert(int index, const T &value) const
在指定位置插入值
Definition List.h:156
auto begin() const
正向迭代器开始
Definition List.h:63
auto Append(T &&value) const
添加一个值到列表末尾
Definition List.h:146
bool Equals(const List &other) const
判断是否为同一个列表
Definition List.h:103
bool Contains(const T &value) const
列表是否包含某个值
Definition List.h:176
List(int capacity)
初始化列表并指定容量
Definition List.h:54
int IndexOf(const T &value) const
查找值在列表中的索引
Definition List.h:186
auto & operator[](int index) const
获取或设置列表中指定位置的值
Definition List.h:95
auto rbegin() const
反向迭代器开始
Definition List.h:79
void RemoveAt(int index) const
移除指定索引处的值
Definition List.h:210
int Capacity() const
列表当前的容量
Definition List.h:111
List Copy() const
复制当前列表
Definition List.h:226
std::wstring ToString() const
获取描述当前对象的字符串
Definition List.h:244
auto rend() const
反向迭代器结束
Definition List.h:87
auto end() const
正向迭代器结束
Definition List.h:71
void Clear() const
清空列表
Definition List.h:218
List(std::initializer_list< T > list)
使用初始化列表
Definition List.h:46
int Count() const
获取元素个数
Definition List.h:119
std::vector< T > & GetStdVector() const
获取列表内部维护的std::vector
Definition List.h:236
auto Append(const T &value) const
添加一个值到列表末尾
Definition List.h:136
auto Insert(int index, T &&value) const
在指定位置插入值
Definition List.h:166
List()
初始化列表
Definition List.h:38
bool Remove(const T &value) const
移除列表中第一个指定的值
Definition List.h:197
bool IsEmpty() const
列表是否为空
Definition List.h:127
static std::wstring BuildStr(const Args &...args)
拼接字符串,也可使用此函数将其他类型转为wstring
Definition Utils.h:113