YSTest: container.hpp 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
container.hpp
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 2012 - 2013.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YB_INC_YSTDEX_CONTAINER_HPP_
29 #define YB_INC_YSTDEX_CONTAINER_HPP_ 1
30
31 #include "functional.hpp"
32 #include <array> // for std::array;
33 #include <algorithm> // for std::copy_n;
34
35 namespace ystdex
36 {
37
44 template<class _tContainer, typename... _tParams>
45 inline void
46 assign(_tContainer& con, _tParams&&... args)
47 {
48 con.assign(yforward(args)...);
49 }
50 template<class _tContainer, typename _type, size_t _vN>
51 inline void
52 assign(_tContainer& con, const _type(&arr)[_vN])
53 {
54 con.assign(arr, arr + _vN);
55 }
57
58
65 template<class _tContainer, typename _tKey>
66 auto
67 at(_tContainer& con, const _tKey& k)
68 -> decltype(con.at(k))
69 {
70 return con.at(k);
71 }
72 template<class _tContainer, typename _tKey>
73 const _tKey&
74 at(const _tContainer& con, const _tKey& k)
75 {
76 const auto i(con.find(k));
77
78 if(i != end(con))
79 return *i;
80 throw std::out_of_range("ystdex::at");
81 }
83
84
90 template<typename _tContainer>
91 class container_inserter
92 {
93 public:
94 typedef _tContainer container_type;
95
96 protected:
97 _tContainer* container;
98
99 public:
100 container_inserter(_tContainer& cont)
101 : container(&cont)
102 {}
103
104 template<typename... _tParams>
105 auto
106 operator()(_tParams&&... args)
107 -> decltype(container->insert(std::forward<_tParams>(args)...))
108 // NOTE: Nested %decltype could cause crashing of devkitPro G++ 4.7.1.
109 {
110 yassume(container);
111
112 return container->insert(yforward(args)...);
113 }
114 };
115
120 template<typename _tContainer, typename... _tParams>
121 inline void
122 seq_insert(_tContainer& cont, _tParams&&... args)
123 {
124 ystdex::seq_apply(container_inserter<_tContainer>(cont), yforward(args)...);
125 }
126
127
134 template<typename _tContainer>
135 void
136 erase_all(_tContainer& c, const typename _tContainer::value_type& val)
137 {
138 c.erase(std::remove(begin(c), end(c), val), end(c));
139 }
146 template<typename _tContainer, typename _tIn, typename _tValue>
147 void
148 erase_all(_tContainer& c, _tIn first, _tIn last, const _tValue& value)
149 {
150 while(first != last)
151 if(*first == value)
152 c.erase(first++);
153 else
154 ++first;
155 }
156
163 template<typename _tRange, typename _fPredicate>
164 void
165 erase_all_if(_tRange& c, _fPredicate pred)
166 {
167 c.erase(std::remove_if(begin(c), end(c), pred), end(c));
168 }
175 template<typename _tContainer, typename _tIn, typename _fPredicate>
176 void
177 erase_all_if(_tContainer& c, _tIn first, _tIn last, _fPredicate pred)
178 {
179 while(first != last)
180 if(pred(*first))
181 c.erase(first++);
182 else
183 ++first;
184 }
185
186
195 template<class _tMap>
196 std::pair<typename _tMap::iterator, bool>
197 search_map(_tMap& m, const typename _tMap::key_type& k)
198 {
199 const auto i(m.lower_bound(k));
200
201 return std::make_pair(i, (i == m.end() || m.key_comp()(k, i->first)));
202 }
203
204
209 template<typename _type, typename... _tParams>
210 inline std::array<_type, sizeof...(_tParams)>
211 make_array(_tParams&&... args)
212 {
213 // TODO: Use one pair of braces (depending on G++).
214 return {{decay_copy(yforward(args))...}};
215 }
216
222 template<typename _type, size_t _vN, typename _tSrc>
223 yconstfn std::array<_type, _vN>
224 to_array(const _tSrc& src)
225 {
226 return std::array<_type, _vN>(src);
227 }
228 template<typename _type, size_t _vN>
229 yconstfn std::array<_type, _vN>
230 to_array(const std::array<_type, _vN>& src)
231 {
232 return src;
233 }
234 template<typename _type, size_t _vN, typename _tSrcElement>
235 inline std::array<_type, _vN>
236 to_array(const _tSrcElement(&src)[_vN])
237 {
238 std::array<_type, _vN> arr;
239
240 std::copy_n(std::addressof(src[0]), _vN, std::addressof(arr[0]));
241 return std::move(arr);
242 }
243 template<typename _type, size_t _vN, typename _tSrcElement>
244 inline std::array<_type, _vN>
245 to_array(_tSrcElement(&&src)[_vN])
246 {
247 std::array<_type, _vN> arr;
248
249 std::copy_n(std::make_move_iterator(std::addressof(src[0])), _vN,
250 std::addressof(arr[0]));
251 return std::move(arr);
252 }
254
255 } // namespace ystdex;
256
257 #endif
258