libostd/ostd/vector.hh

45 lines
1.2 KiB
C++
Raw Normal View History

2017-01-25 00:44:22 +00:00
/* OctaSTD extensions for std::vector.
2015-04-05 23:05:21 +00:00
*
2015-04-12 20:41:02 +00:00
* This file is part of OctaSTD. See COPYING.md for futher information.
2015-04-05 23:05:21 +00:00
*/
2015-07-13 19:08:55 +00:00
#ifndef OSTD_VECTOR_HH
#define OSTD_VECTOR_HH
2015-04-05 23:05:21 +00:00
2017-01-25 00:44:22 +00:00
#include <vector>
#include <memory>
#include <type_traits>
2015-04-14 21:16:06 +00:00
2015-07-13 19:08:55 +00:00
#include "ostd/range.hh"
2015-04-05 23:05:21 +00:00
2015-07-13 19:07:14 +00:00
namespace ostd {
2015-06-03 23:06:43 +00:00
template<typename T, typename A = std::allocator<T>, typename R>
inline std::vector<T, A> make_vector(R range, A const &alloc = A{}) {
std::vector<T, A> ret{alloc};
using C = range_category_t<R>;
if constexpr(std::is_convertible_v<C, finite_random_access_range_tag>) {
/* finite random access or contiguous */
auto h = range.half();
ret.reserve(range.size());
ret.insert(ret.end(), h, h + range.size());
} else {
/* infinite random access and below */
for (; !range.empty(); range.pop_front()) {
ret.push_back(range.front());
}
2017-01-25 00:44:22 +00:00
}
return ret;
2015-07-12 23:24:14 +00:00
}
template<typename R, typename A = std::allocator<range_value_t<R>>>
inline std::vector<range_value_t<R>, A> make_vector(
R &&range, A const &alloc = A{}
) {
return make_vector<range_value_t<R>, A>(std::forward<R>(range), alloc);
2015-07-12 23:24:14 +00:00
}
2015-07-13 19:07:14 +00:00
} /* namespace ostd */
2015-04-05 23:05:21 +00:00
2016-02-07 21:17:15 +00:00
#endif