10#ifndef COMMON_LIST_HPP
11#define COMMON_LIST_HPP
36class List :
public std::vector<T> {
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;
53 List(
const std::vector<T>& list);
65 template <
typename FUNC>
67 template <
typename U,
typename FUNC>
68 U
Reduce(
const FUNC& func, U initial_value = U{})
const;
69 template <
typename U = T,
typename FUNC>
72 template <
typename FUNC>
74 template <
typename U,
typename FUNC>
75 U
MutReduce(
const FUNC& func, U initial_value = U{});
76 template <
typename U = T,
typename FUNC>
79 const_iterator
Max()
const;
80 template <
typename FUNC>
81 const_iterator
Max(
const FUNC& func)
const;
83 const_iterator
Min()
const;
84 template <
typename FUNC>
85 const_iterator
Min(
const FUNC& func)
const;
88 template <
typename FUNC>
92 template <
typename FUNC>
93 iterator
Order(
size_t index,
const FUNC& func);
96 template <
typename FUNC>
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;
107 template <
typename FUNC>
108 size_t Count(
const FUNC& func)
const;
161 this->insert(this->end(), list.begin(), list.end());
195 new_list.insert(new_list.end(), list2.begin(), list2.end());
197 return std::move(new_list);
216 int list_len = list.size();
222 for (
int i = 1; i < list_len; ++i)
223 out <<
", " << list[i];
269template <
typename FUNC>
274 for (
const T& item : *
this)
276 new_list.emplace_back(item);
278 return std::move(new_list);
321template <
typename U,
typename FUNC>
324 for (
const T& item : *
this)
325 initial_value = std::move(func(initial_value, item));
327 return initial_value;
369template <
typename U,
typename FUNC>
374 for (
const T& item : *
this)
375 new_list.emplace_back(func(item));
377 return std::move(new_list);
426template <
typename FUNC>
431 for (T& item : *
this)
433 new_list.emplace_back(item);
435 return std::move(new_list);
483template <
typename U,
typename FUNC>
486 for (T& item : *
this)
487 initial_value = std::move(func(initial_value, item));
489 return initial_value;
533template <
typename U,
typename FUNC>
538 for (T& item : *
this)
539 new_list.emplace_back(func(item));
541 return std::move(new_list);
573 size_t list_len = this->size();
578 const_iterator max_value_iter = this->begin();
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;
585 return max_value_iter;
628template <
typename FUNC>
630 size_t list_len = this->size();
635 const_iterator max_value_iter = this->begin();
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;
642 return max_value_iter;
674 size_t list_len = this->size();
679 const_iterator min_value_iter = this->begin();
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;
686 return min_value_iter;
729template <
typename FUNC>
731 size_t list_len = this->size();
736 const_iterator min_value_iter = this->begin();
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;
743 return min_value_iter;
768 std::sort(this->begin(), this->end());
816template <
typename FUNC>
818 std::sort(this->begin(), this->end(), func);
860 if (index >= this->size())
866 std::nth_element(begin_iter, begin_iter + index, end_iter);
869 return begin_iter + index;
916template <
typename FUNC>
921 if (index >= this->size())
927 std::nth_element(begin_iter, begin_iter + index, end_iter, func);
930 return begin_iter + index;
962 return this->Index(value) < this->size();
1007template <
typename T>
1008template <
typename FUNC>
1010 return this->Index(func) < this->size();
1039template <
typename T>
1041 size_t list_len = this->size();
1044 for (
size_t i = from_index; i < list_len; ++i)
1045 if ((*
this)[i] == value)
1091template <
typename T>
1092template <
typename FUNC>
1094 size_t list_len = this->size();
1097 for (
size_t i = from_index; i < list_len; ++i)
1098 if (func((*
this)[i]))
1130template <
typename T>
1132 size_t list_len = this->size();
1135 for (
int i = std::min(
int(list_len) - 1,
int(from_index)); i >= 0; --i)
1136 if ((*
this)[i] == value)
1182template <
typename T>
1183template <
typename FUNC>
1185 size_t list_len = this->size();
1188 for (
int i = std::min(
int(list_len) - 1,
int(from_index)); i >= 0; --i)
1189 if (func((*
this)[i]))
1220template <
typename T>
1226 for (
const T& item : *
this)
1272template <
typename T>
1273template <
typename FUNC>
1279 for (
const T& item : *
this)
1304template <
typename T>
1308 size_t list_len = this->size();
1309 size_t swap_len = list_len / 2;
1312 for (
size_t i = 0; i < swap_len; ++i)
1313 std::swap(this_list[i], this_list[list_len - i - 1]);
1327template <
typename T>
1329 auto& this_list = *
this;
1330 int list_len = this->size();
1336 std::srand((
unsigned int)time(NULL));
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]);
1373template <
typename T>
1376 for (T& item : *
this)
1410template <
typename T>
1412 const List<T>& this_list = *
this;
1415 size_t list_len = this->size();
1418 end = std::min(end, list_len - 1);
1421 for (
size_t i = start; i < end; ++i)
1422 new_list.emplace_back(this_list[i]);
1424 return std::move(new_list);
List< T > & Sort(const FUNC &func)
对列表进行排序
List< T > MutFilter(const FUNC &func)
过滤列表项
List< U > Map(const FUNC &func) const
列表项映射
List< T > & Fill(const T &value)
将列表中所有元素填充为指定值
friend List< U > operator+(const List< U > &list1, const List< U > &list2)
列表类重载加法运算符
size_t LastIndex(const FUNC &func, size_t from_index=INT_MAX) const
查找指定值在列表中的索引
List< T > Slice(size_t start, size_t end=UINT_MAX) const
切片提取列表中指定范围的元素
U MutReduce(const FUNC &func, U initial_value=U{})
累计列表项
const_iterator Min() const
求列表中的最小值
List< T > & Shuffle()
对列表中的元素进行随机打乱
List< T > Filter(const FUNC &func) const
过滤列表项
size_t Count(const T &value) const
统计列表中指定值的出现次数
U Reduce(const FUNC &func, U initial_value=U{}) const
累计列表项
const_iterator Min(const FUNC &func) const
求列表中的最小值
List< U > MutMap(const FUNC &func)
列表项映射
const_iterator Max(const FUNC &func) const
求列表中的最大值
List(const List< T > &list)=default
列表类的拷贝构造函数
bool Include(const T &value) const
判断列表中是否包含指定值
List< T > & operator+=(const List< T > &list)
列表类重载加等于运算符
List(const std::vector< T > &list)
列表类的带参构造函数
size_t Index(const FUNC &func, size_t from_index=0) const
查找指定值在列表中的索引
size_t Count(const FUNC &func) const
统计满足指定条件的元素个数
List< T > & Reverse()
对列表中的元素进行反转
iterator Order(size_t index)
按序获取列表中指定索引的元素
const_iterator Max() const
求列表中的最大值
size_t LastIndex(const T &value, size_t from_index=INT_MAX) const
查找指定值在列表中的索引
iterator Order(size_t index, const FUNC &func)
按序获取列表中指定索引的元素
bool Include(const FUNC &func) const
判断列表中是否包含满足指定条件的值
friend std::ostream & operator<<(std::ostream &out, const List< U > &list)
列表类重载输出流运算符
size_t Index(const T &value, size_t from_index=0) const
查找指定值在列表中的索引
List< T > & Sort()
对列表进行排序
std::ostream & operator<<(std::ostream &out, const Interval &interval)
区间类重载输出流运算符
List< U > operator+(const List< U > &list1, const List< U > &list2)
列表类重载加法运算符