表格线识别通用库文档
载入中...
搜索中...
未找到
list.hpp
浏览该文件的文档.
1/*
2 * @Description: 列表类 头文件
3 * @Version:
4 * @Autor: dreamy-xay
5 * @date: 2023-12-03
6 * @LastEditors: dreamy-xay
7 * @LastEditTime: 2024-03-06
8 */
9
10#ifndef COMMON_LIST_HPP
11#define COMMON_LIST_HPP
12
13#include <algorithm>
14#include <climits>
15#include <cstdlib>
16#include <ctime>
17#include <iostream>
18#include <vector>
19
20#include "common/enum.h"
21#include "common/macro.h"
22#include "common/type.h"
23
24namespace cm {
25
35template <typename T>
36class List : public std::vector<T> {
37 public:
39 using typename std::vector<T>::iterator;
41 using typename std::vector<T>::const_iterator;
43 using typename std::vector<T>::reverse_iterator;
45 using typename std::vector<T>::const_reverse_iterator;
47 using std::vector<T>::vector;
48
50 List() = default;
52 List(const List<T>& list) = default;
53 List(const std::vector<T>& list);
55 ~List() = default;
56
57 List<T>& operator+=(const List<T>& list);
58
59 template <typename U>
60 friend List<U> operator+(const List<U>& list1, const List<U>& list2);
61
62 template <typename U>
63 friend std::ostream& operator<<(std::ostream& out, const List<U>& list);
64
65 template <typename FUNC>
66 List<T> Filter(const FUNC& func) const;
67 template <typename U, typename FUNC>
68 U Reduce(const FUNC& func, U initial_value = U{}) const;
69 template <typename U = T, typename FUNC>
70 List<U> Map(const FUNC& func) const;
71
72 template <typename FUNC>
73 List<T> MutFilter(const FUNC& func);
74 template <typename U, typename FUNC>
75 U MutReduce(const FUNC& func, U initial_value = U{});
76 template <typename U = T, typename FUNC>
77 List<U> MutMap(const FUNC& func);
78
79 const_iterator Max() const;
80 template <typename FUNC>
81 const_iterator Max(const FUNC& func) const;
82
83 const_iterator Min() const;
84 template <typename FUNC>
85 const_iterator Min(const FUNC& func) const;
86
88 template <typename FUNC>
89 List<T>& Sort(const FUNC& func);
90
91 iterator Order(size_t index);
92 template <typename FUNC>
93 iterator Order(size_t index, const FUNC& func);
94
95 bool Include(const T& value) const;
96 template <typename FUNC>
97 bool Include(const FUNC& func) const;
98
99 size_t Index(const T& value, size_t from_index = 0) const;
100 template <typename FUNC>
101 size_t Index(const FUNC& func, size_t from_index = 0) const;
102 size_t LastIndex(const T& value, size_t from_index = INT_MAX) const;
103 template <typename FUNC>
104 size_t LastIndex(const FUNC& func, size_t from_index = INT_MAX) const;
105
106 size_t Count(const T& value) const;
107 template <typename FUNC>
108 size_t Count(const FUNC& func) const;
109
111
113
114 List<T>& Fill(const T& value);
115
116 List<T> Slice(size_t start, size_t end = UINT_MAX) const;
117};
118
128template <typename T>
129inline List<T>::List(const std::vector<T>& list) : std::vector<T>(list) {}
130
159template <typename T>
160inline List<T>& List<T>::operator+=(const List<T>& list) {
161 this->insert(this->end(), list.begin(), list.end()); // 插入list内容
162
163 return *this;
164}
165
191template <typename U>
192inline List<U> operator+(const List<U>& list1, const List<U>& list2) {
193 List<U> new_list = list1; // 新列表
194
195 new_list.insert(new_list.end(), list2.begin(), list2.end()); // 插入 list2 内容
196
197 return std::move(new_list);
198}
199
214template <typename U>
215inline std::ostream& operator<<(std::ostream& out, const List<U>& list) {
216 int list_len = list.size(); // 获取列表长度
217
218 out << "["; // 输出列表左括号
219
220 if (list_len) { // 如果列表内存在元素,则打印
221 out << list[0];
222 for (int i = 1; i < list_len; ++i)
223 out << ", " << list[i];
224 }
225
226 out << "]"; // 输出列表右括号
227
228 return out;
229}
230
268template <typename T>
269template <typename FUNC>
270inline List<T> List<T>::Filter(const FUNC& func) const {
271 List<T> new_list; // 新列表
272
273 // 遍历所有项
274 for (const T& item : *this)
275 if (func(item)) // 如果不满足过滤条件则加入新列表
276 new_list.emplace_back(item);
277
278 return std::move(new_list); // 返回过滤后的区间列表
279}
280
320template <typename T>
321template <typename U, typename FUNC>
322inline U List<T>::Reduce(const FUNC& func, U initial_value) const {
323 // 遍历所有项
324 for (const T& item : *this)
325 initial_value = std::move(func(initial_value, item)); // 归并函数 func 每次的返回值
326
327 return initial_value; // 返回归并后的值
328}
329
368template <typename T>
369template <typename U, typename FUNC>
370inline List<U> List<T>::Map(const FUNC& func) const {
371 List<U> new_list; // 新列表
372
373 // 遍历所有项
374 for (const T& item : *this)
375 new_list.emplace_back(func(item));
376
377 return std::move(new_list); // 返回重新映射后的区间列表
378}
379
425template <typename T>
426template <typename FUNC>
427inline List<T> List<T>::MutFilter(const FUNC& func) {
428 List<T> new_list; // 新列表
429
430 // 遍历所有项
431 for (T& item : *this)
432 if (func(item)) // 如果不满足过滤条件则加入新列表
433 new_list.emplace_back(item);
434
435 return std::move(new_list); // 返回过滤后的区间列表
436}
437
482template <typename T>
483template <typename U, typename FUNC>
484inline U List<T>::MutReduce(const FUNC& func, U initial_value) {
485 // 遍历所有项
486 for (T& item : *this)
487 initial_value = std::move(func(initial_value, item)); // 归并函数 get_val 每次的返回值
488
489 return initial_value; // 返回归并后的值
490}
491
532template <typename T>
533template <typename U, typename FUNC>
534inline List<U> List<T>::MutMap(const FUNC& func) {
535 List<U> new_list; // 新列表
536
537 // 遍历所有项
538 for (T& item : *this)
539 new_list.emplace_back(func(item));
540
541 return std::move(new_list); // 返回重新映射后的区间列表
542}
543
571template <typename T>
572inline typename List<T>::const_iterator List<T>::Max() const {
573 size_t list_len = this->size(); // 获取列表长度
574
575 if (!list_len) // 列表长度为0则直接返回结束迭代器
576 return this->end();
577
578 const_iterator max_value_iter = this->begin(); // 最大值的迭代器
579
580 const_iterator cur_value_iter = max_value_iter + 1; // 当前值的迭代器
581 for (size_t i = 1; i < list_len; ++i, ++cur_value_iter)
582 if (*max_value_iter < *cur_value_iter)
583 max_value_iter = cur_value_iter; // 更新最大值迭代器
584
585 return max_value_iter;
586}
587
627template <typename T>
628template <typename FUNC>
629inline typename List<T>::const_iterator List<T>::Max(const FUNC& func) const {
630 size_t list_len = this->size(); // 获取列表长度
631
632 if (!list_len) // 列表长度为0则直接返回结束迭代器
633 return this->end();
634
635 const_iterator max_value_iter = this->begin(); // 最大值的迭代器
636
637 const_iterator cur_value_iter = max_value_iter + 1; // 当前值的迭代器
638 for (size_t i = 1; i < list_len; ++i, ++cur_value_iter)
639 if (func(*max_value_iter, *cur_value_iter))
640 max_value_iter = cur_value_iter; // 更新最大值迭代器
641
642 return max_value_iter;
643}
644
672template <typename T>
673inline typename List<T>::const_iterator List<T>::Min() const {
674 size_t list_len = this->size(); // 获取列表长度
675
676 if (!list_len) // 列表长度为0则直接返回结束迭代器
677 return this->end();
678
679 const_iterator min_value_iter = this->begin(); // 最小值的迭代器
680
681 const_iterator cur_value_iter = min_value_iter + 1; // 当前值的迭代器
682 for (size_t i = 1; i < list_len; ++i, ++cur_value_iter)
683 if (*cur_value_iter < *min_value_iter)
684 min_value_iter = cur_value_iter; // 更新最小值迭代器
685
686 return min_value_iter;
687}
688
728template <typename T>
729template <typename FUNC>
730inline typename List<T>::const_iterator List<T>::Min(const FUNC& func) const {
731 size_t list_len = this->size(); // 获取列表长度
732
733 if (!list_len) // 列表长度为0则直接返回结束迭代器
734 return this->end();
735
736 const_iterator min_value_iter = this->begin(); // 最小值的迭代器
737
738 const_iterator cur_value_iter = min_value_iter + 1; // 当前值的迭代器
739 for (size_t i = 1; i < list_len; ++i, ++cur_value_iter)
740 if (func(*cur_value_iter, *min_value_iter))
741 min_value_iter = cur_value_iter; // 更新最小值迭代器
742
743 return min_value_iter;
744}
745
766template <typename T>
768 std::sort(this->begin(), this->end());
769
770 return *this;
771}
772
815template <typename T>
816template <typename FUNC>
817inline List<T>& List<T>::Sort(const FUNC& func) {
818 std::sort(this->begin(), this->end(), func);
819
820 return *this;
821}
822
855template <typename T>
856inline typename List<T>::iterator List<T>::Order(size_t index) {
857 List<T>::iterator end_iter = this->end(); // 结束迭代器
858
859 // 如果序号大于等于列表项数,
860 if (index >= this->size())
861 return end_iter;
862
863 List<T>::iterator begin_iter = this->begin(); // 开始迭代器
864
865 // 归位第 index 个元素位置
866 std::nth_element(begin_iter, begin_iter + index, end_iter);
867
868 // 返回迭代器
869 return begin_iter + index;
870}
871
915template <typename T>
916template <typename FUNC>
917inline typename List<T>::iterator List<T>::Order(size_t index, const FUNC& func) {
918 List<T>::iterator end_iter = this->end(); // 结束迭代器
919
920 // 如果序号大于等于列表项数,
921 if (index >= this->size())
922 return end_iter;
923
924 List<T>::iterator begin_iter = this->begin(); // 开始迭代器
925
926 // 归位第 index 个元素位置
927 std::nth_element(begin_iter, begin_iter + index, end_iter, func);
928
929 // 返回迭代器
930 return begin_iter + index;
931}
932
960template <typename T>
961inline bool List<T>::Include(const T& value) const {
962 return this->Index(value) < this->size();
963}
964
1007template <typename T>
1008template <typename FUNC>
1009inline bool List<T>::Include(const FUNC& func) const {
1010 return this->Index(func) < this->size();
1011}
1012
1039template <typename T>
1040inline size_t List<T>::Index(const T& value, size_t from_index) const {
1041 size_t list_len = this->size(); // 列表长度
1042
1043 // 从开始索引开始遍历整个列表
1044 for (size_t i = from_index; i < list_len; ++i)
1045 if ((*this)[i] == value) // 找到指定值
1046 return i;
1047
1048 return list_len;
1049}
1050
1091template <typename T>
1092template <typename FUNC>
1093inline size_t List<T>::Index(const FUNC& func, size_t from_index) const {
1094 size_t list_len = this->size(); // 列表长度
1095
1096 // 从开始索引开始遍历整个列表
1097 for (size_t i = from_index; i < list_len; ++i)
1098 if (func((*this)[i])) // 找到指定值
1099 return i;
1100
1101 return list_len;
1102}
1103
1130template <typename T>
1131inline size_t List<T>::LastIndex(const T& value, size_t from_index) const {
1132 size_t list_len = this->size(); // 列表长度
1133
1134 // 从开始索引开始遍历整个列表
1135 for (int i = std::min(int(list_len) - 1, int(from_index)); i >= 0; --i)
1136 if ((*this)[i] == value) // 找到指定值
1137 return i;
1138
1139 return list_len;
1140}
1141
1182template <typename T>
1183template <typename FUNC>
1184inline size_t List<T>::LastIndex(const FUNC& func, size_t from_index) const {
1185 size_t list_len = this->size(); // 列表长度
1186
1187 // 从开始索引开始遍历整个列表
1188 for (int i = std::min(int(list_len) - 1, int(from_index)); i >= 0; --i)
1189 if (func((*this)[i])) // 找到指定值
1190 return i;
1191
1192 return list_len;
1193}
1194
1220template <typename T>
1221inline size_t List<T>::Count(const T& value) const {
1222 // 计数
1223 size_t count = 0;
1224
1225 // 从开始索引开始遍历整个列表
1226 for (const T& item : *this)
1227 if (item == value) // 如果值相等,计数器加1
1228 ++count;
1229
1230 return count;
1231}
1232
1272template <typename T>
1273template <typename FUNC>
1274inline size_t List<T>::Count(const FUNC& func) const {
1275 // 计数
1276 size_t count = 0;
1277
1278 // 从开始索引开始遍历整个列表
1279 for (const T& item : *this)
1280 if (func(item)) // 如果值相等,计数器加1
1281 ++count;
1282
1283 return count;
1284}
1285
1304template <typename T>
1306 List<T>& this_list = *this;
1307
1308 size_t list_len = this->size(); // 列表长度
1309 size_t swap_len = list_len / 2;
1310
1311 // 列表对半交换
1312 for (size_t i = 0; i < swap_len; ++i)
1313 std::swap(this_list[i], this_list[list_len - i - 1]);
1314
1315 return this_list;
1316}
1317
1327template <typename T>
1329 auto& this_list = *this;
1330 int list_len = this->size(); // 列表长度
1331
1332 if (list_len <= 1)
1333 return this_list;
1334
1335 // 随机种子
1336 std::srand((unsigned int)time(NULL)); // flawfinder: ignore
1337
1338 for (size_t i = 0; i < list_len - 1; i++) {
1339 size_t j = i + rand() / (RAND_MAX / (list_len - i) + 1);
1340 std::swap(this_list[i], this_list[j]);
1341 }
1342
1343 return this_list;
1344}
1345
1373template <typename T>
1374inline List<T>& List<T>::Fill(const T& value) {
1375 // 遍历全部值设置为填充值
1376 for (T& item : *this)
1377 item = value;
1378
1379 return *this;
1380}
1381
1410template <typename T>
1411inline List<T> List<T>::Slice(size_t start, size_t end) const {
1412 const List<T>& this_list = *this; // 列表本身
1413 List<T> new_list; // 提取的新列表
1414
1415 size_t list_len = this->size(); // 列表长度
1416
1417 // 结束位置最大不超过列表长度
1418 end = std::min(end, list_len - 1);
1419
1420 // 根据起止信息从原列表中提取出一个新的列表
1421 for (size_t i = start; i < end; ++i)
1422 new_list.emplace_back(this_list[i]);
1423
1424 return std::move(new_list);
1425}
1426
1427} // namespace cm
1428
1429#endif
列表类
Definition list.hpp:36
List< T > & Sort(const FUNC &func)
对列表进行排序
Definition list.hpp:817
List< T > MutFilter(const FUNC &func)
过滤列表项
Definition list.hpp:427
List< U > Map(const FUNC &func) const
列表项映射
Definition list.hpp:370
List< T > & Fill(const T &value)
将列表中所有元素填充为指定值
Definition list.hpp:1374
friend List< U > operator+(const List< U > &list1, const List< U > &list2)
列表类重载加法运算符
Definition list.hpp:192
size_t LastIndex(const FUNC &func, size_t from_index=INT_MAX) const
查找指定值在列表中的索引
Definition list.hpp:1184
List< T > Slice(size_t start, size_t end=UINT_MAX) const
切片提取列表中指定范围的元素
Definition list.hpp:1411
U MutReduce(const FUNC &func, U initial_value=U{})
累计列表项
Definition list.hpp:484
const_iterator Min() const
求列表中的最小值
Definition list.hpp:673
List< T > & Shuffle()
对列表中的元素进行随机打乱
Definition list.hpp:1328
List< T > Filter(const FUNC &func) const
过滤列表项
Definition list.hpp:270
size_t Count(const T &value) const
统计列表中指定值的出现次数
Definition list.hpp:1221
U Reduce(const FUNC &func, U initial_value=U{}) const
累计列表项
Definition list.hpp:322
const_iterator Min(const FUNC &func) const
求列表中的最小值
Definition list.hpp:730
~List()=default
列表类的析构函数
List< U > MutMap(const FUNC &func)
列表项映射
Definition list.hpp:534
const_iterator Max(const FUNC &func) const
求列表中的最大值
Definition list.hpp:629
List(const List< T > &list)=default
列表类的拷贝构造函数
bool Include(const T &value) const
判断列表中是否包含指定值
Definition list.hpp:961
List< T > & operator+=(const List< T > &list)
列表类重载加等于运算符
Definition list.hpp:160
List()=default
列表类的默认构造函数
List(const std::vector< T > &list)
列表类的带参构造函数
Definition list.hpp:129
size_t Index(const FUNC &func, size_t from_index=0) const
查找指定值在列表中的索引
Definition list.hpp:1093
size_t Count(const FUNC &func) const
统计满足指定条件的元素个数
Definition list.hpp:1274
List< T > & Reverse()
对列表中的元素进行反转
Definition list.hpp:1305
iterator Order(size_t index)
按序获取列表中指定索引的元素
Definition list.hpp:856
const_iterator Max() const
求列表中的最大值
Definition list.hpp:572
size_t LastIndex(const T &value, size_t from_index=INT_MAX) const
查找指定值在列表中的索引
Definition list.hpp:1131
iterator Order(size_t index, const FUNC &func)
按序获取列表中指定索引的元素
Definition list.hpp:917
bool Include(const FUNC &func) const
判断列表中是否包含满足指定条件的值
Definition list.hpp:1009
friend std::ostream & operator<<(std::ostream &out, const List< U > &list)
列表类重载输出流运算符
Definition list.hpp:215
size_t Index(const T &value, size_t from_index=0) const
查找指定值在列表中的索引
Definition list.hpp:1040
List< T > & Sort()
对列表进行排序
Definition list.hpp:767
std::ostream & operator<<(std::ostream &out, const Interval &interval)
区间类重载输出流运算符
Definition interval.hpp:328
List< U > operator+(const List< U > &list1, const List< U > &list2)
列表类重载加法运算符
Definition list.hpp:192