SimpleWindow
载入中...
搜索中...
未找到
List.h
1#pragma once
2
3#include "IList.h"
4#include "IToString.h"
5#include "Utils.h"
6#include <algorithm>
7#include <initializer_list>
8#include <stdexcept>
9#include <utility>
10
11namespace sw
12{
17 template <typename T>
18 class List final : public IListT<T>,
19 public IToString<List<T>>
20 {
21 private:
25 std::vector<T> _data;
26
27 public:
31 List() = default;
32
37 List(std::initializer_list<T> list)
38 : _data(list)
39 {
40 }
41
46 explicit List(int capacity)
47 : _data()
48 {
49 _data.reserve(static_cast<size_t>(capacity));
50 }
51
57 : _data(other._data)
58 {
59 }
60
66 : _data(std::move(other._data))
67 {
68 }
69
76 {
77 if (this != &other) {
78 _data = other._data;
79 }
80 return *this;
81 }
82
89 {
90 if (this != &other) {
91 _data = std::move(other._data);
92 }
93 return *this;
94 }
95
102 T &operator[](int index)
103 {
104 return GetAt(index);
105 }
106
113 const T &operator[](int index) const
114 {
115 return GetAt(index);
116 }
117
123 {
124 return static_cast<int>(_data.capacity());
125 }
126
132 {
133 if (newCapacity > Capacity()) {
134 _data.reserve(static_cast<size_t>(newCapacity));
135 }
136 }
137
141 void Clear()
142 {
143 _data.clear();
144 }
145
150 void Add(const T &value)
151 {
152 _data.push_back(value);
153 }
154
159 void Add(T &&value)
160 {
161 _data.push_back(std::move(value));
162 }
163
169 void RemoveAt(int index)
170 {
172 throw std::out_of_range("Index out of range in List::RemoveAt.");
173 }
174 _data.erase(_data.begin() + static_cast<size_t>(index));
175 }
176
183 void Insert(int index, const T &value)
184 {
186 throw std::out_of_range("Index out of range in List::Insert.");
187 }
188 _data.insert(_data.begin() + static_cast<size_t>(index), value);
189 }
190
197 void Insert(int index, T &&value)
198 {
200 throw std::out_of_range("Index out of range in List::Insert.");
201 }
202 _data.insert(_data.begin() + static_cast<size_t>(index), std::move(value));
203 }
204
210 int IndexOf(const T &value) const
211 {
212 auto it = std::find(_data.begin(), _data.end(), value);
213 return it != _data.end() ? static_cast<int>(std::distance(_data.begin(), it)) : -1;
214 }
215
221 int LastIndexOf(const T &value) const
222 {
223 auto it = std::find(_data.rbegin(), _data.rend(), value);
224 return it != _data.rend() ? static_cast<int>(std::distance(it, _data.rend()) - 1) : -1;
225 }
226
232 bool Contains(const T &value) const
233 {
234 return std::find(_data.begin(), _data.end(), value) != _data.end();
235 }
236
242 bool Remove(const T &value)
243 {
244 int index = IndexOf(value);
245 if (index == -1) {
246 return false;
247 } else {
248 RemoveAt(index);
249 return true;
250 }
251 }
252
257 std::wstring ToString() const
258 {
259 return Utils::BuildStr(_data);
260 }
261
266 std::vector<T> &GetInternalVector() noexcept
267 {
268 return _data;
269 }
270
275 const std::vector<T> &GetInternalVector() const noexcept
276 {
277 return _data;
278 }
279
280 private:
287 template <typename U = T>
288 auto SetAtImpl(int index, const T &value)
289 -> typename std::enable_if<std::is_copy_assignable<U>::value>::type
290 {
292 throw std::out_of_range("Index out of range in List::SetAt.");
293 }
294 _data[static_cast<size_t>(index)] = value;
295 }
296
303 template <typename U = T>
304 auto SetAtImpl(int index, const T &value)
305 -> typename std::enable_if<!std::is_copy_assignable<U>::value>::type
306 {
307 throw std::logic_error("Type T must be copy assignable to use SetAt in List.");
308 }
309
316 template <typename U = T>
317 auto SetAtImpl(int index, T &&value)
318 -> typename std::enable_if<std::is_move_assignable<U>::value>::type
319 {
320 if (index < 0 || index >= Count()) {
321 throw std::out_of_range("Index out of range in List::SetAt.");
322 }
323 _data[static_cast<size_t>(index)] = std::move(value);
324 }
325
332 template <typename U = T>
333 auto SetAtImpl(int index, T &&value)
334 -> typename std::enable_if<!std::is_move_assignable<U>::value>::type
335 {
336 throw std::logic_error("Type T must be move assignable to use move SetAt in List.");
337 }
338
339 public:
344 virtual int Count() const noexcept override
345 {
346 return static_cast<int>(_data.size());
347 }
348
355 virtual T &GetAt(int index) override
356 {
358 throw std::out_of_range("Index out of range in List::GetAt.");
359 }
360 return _data[static_cast<size_t>(index)];
361 }
362
369 virtual const T &GetAt(int index) const override
370 {
372 throw std::out_of_range("Index out of range in List::GetAt.");
373 }
374 return _data[static_cast<size_t>(index)];
375 }
376
384 virtual void SetAt(int index, const T &value) override
385 {
386 SetAtImpl(index, value);
387 }
388
396 virtual void SetAt(int index, T &&value) override
397 {
398 SetAtImpl(index, std::move(value));
399 }
400 };
401}
类型安全的列表接口,继承IList并提供类型化的元素访问
Definition IList.h:71
为支持ToString方法的类提供统一接口
Definition IToString.h:13
值转换器接口
Definition IValueConverter.h:14
基于std::vector的泛型列表,实现IListT接口
Definition List.h:20
void RemoveAt(int index)
移除指定索引处的元素
Definition List.h:169
const T & operator[](int index) const
获取指定索引处的const元素引用
Definition List.h:113
const std::vector< T > & GetInternalVector() const noexcept
获取底层std::vector的const引用
Definition List.h:275
void Reserve(int newCapacity)
预留至少指定数量的元素空间
Definition List.h:131
void Add(const T &value)
在列表末尾追加元素
Definition List.h:150
void Add(T &&value)
在列表末尾追加元素(移动语义)
Definition List.h:159
virtual T & GetAt(int index) override
获取指定索引处的元素引用
Definition List.h:355
virtual int Count() const noexcept override
返回列表中的元素数量
Definition List.h:344
bool Contains(const T &value) const
判断列表是否包含指定值
Definition List.h:232
T & operator[](int index)
获取指定索引处的元素引用
Definition List.h:102
virtual void SetAt(int index, const T &value) override
设置指定索引处的元素值
Definition List.h:384
List(int capacity)
指定初始容量构造
Definition List.h:46
int IndexOf(const T &value) const
查找指定值在列表中首次出现的索引
Definition List.h:210
void Insert(int index, const T &value)
在指定索引处插入元素
Definition List.h:183
List(List< T > &&other) noexcept
移动构造函数
Definition List.h:65
List()=default
默认构造函数,创建空列表
virtual const T & GetAt(int index) const override
获取指定索引处的const元素引用
Definition List.h:369
List< T > & operator=(List< T > &&other) noexcept
移动赋值运算符
Definition List.h:88
int LastIndexOf(const T &value) const
查找指定值在列表中最后出现的索引
Definition List.h:221
std::wstring ToString() const
将列表转换为字符串表示
Definition List.h:257
List(const List< T > &other)
拷贝构造函数
Definition List.h:56
bool Remove(const T &value)
移除列表中首次出现的指定值
Definition List.h:242
List(std::initializer_list< T > list)
使用初始化列表构造
Definition List.h:37
List< T > & operator=(const List< T > &other)
拷贝赋值运算符
Definition List.h:75
int Capacity() const noexcept
获取当前分配的容量
Definition List.h:122
std::vector< T > & GetInternalVector() noexcept
获取底层std::vector的引用
Definition List.h:266
void Insert(int index, T &&value)
在指定索引处插入元素(移动语义)
Definition List.h:197
virtual void SetAt(int index, T &&value) override
设置指定索引处的元素值(移动语义)
Definition List.h:396
void Clear()
清空列表中的所有元素
Definition List.h:141
static std::wstring BuildStr(const Args &...args)
拼接字符串,也可使用此函数将其他类型转为wstring
Definition Utils.h:139
SimpleWindow框架的顶级命名空间,所有公共类型、控件、枚举和工具函数均定义于此。
Definition Alignment.h:4