2017-01-25 01:44:22 +01:00
|
|
|
/* OctaSTD extensions for std::vector.
|
2015-04-06 01:05:21 +02:00
|
|
|
*
|
2015-04-12 22:41:02 +02:00
|
|
|
* This file is part of OctaSTD. See COPYING.md for futher information.
|
2015-04-06 01:05:21 +02:00
|
|
|
*/
|
|
|
|
|
2015-07-13 21:08:55 +02:00
|
|
|
#ifndef OSTD_VECTOR_HH
|
|
|
|
#define OSTD_VECTOR_HH
|
2015-04-06 01:05:21 +02:00
|
|
|
|
2017-01-25 01:44:22 +01:00
|
|
|
#include <vector>
|
2017-02-01 20:56:19 +01:00
|
|
|
#include <memory>
|
2017-02-01 18:28:57 +01:00
|
|
|
#include <type_traits>
|
2015-04-14 23:16:06 +02:00
|
|
|
|
2015-07-13 21:08:55 +02:00
|
|
|
#include "ostd/range.hh"
|
2015-04-06 01:05:21 +02:00
|
|
|
|
2015-07-13 21:07:14 +02:00
|
|
|
namespace ostd {
|
2015-06-04 01:06:43 +02:00
|
|
|
|
2017-02-01 20:56:19 +01:00
|
|
|
template<typename T, typename A>
|
|
|
|
struct ranged_traits<std::vector<T, A>> {
|
|
|
|
static PointerRange<T> iter(std::vector<T, A> &v) {
|
2017-02-09 22:48:28 +01:00
|
|
|
return PointerRange<T>{v.data(), v.data() + v.size()};
|
2017-01-26 00:27:54 +01:00
|
|
|
}
|
|
|
|
};
|
2015-07-13 01:24:14 +02:00
|
|
|
|
2017-02-01 20:56:19 +01:00
|
|
|
template<typename T, typename A>
|
|
|
|
struct ranged_traits<std::vector<T, A> const> {
|
|
|
|
static PointerRange<T const> iter(std::vector<T, A> const &v) {
|
2017-02-09 22:48:28 +01:00
|
|
|
return PointerRange<T const>{v.data(), v.data() + v.size()};
|
2017-01-26 00:27:54 +01:00
|
|
|
}
|
|
|
|
};
|
2015-07-13 01:24:14 +02:00
|
|
|
|
2017-02-01 20:56:19 +01: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};
|
2017-02-01 18:28:57 +01:00
|
|
|
using C = RangeCategory<R>;
|
|
|
|
if constexpr(std::is_convertible_v<C, FiniteRandomAccessRangeTag>) {
|
|
|
|
/* finite random access or contiguous */
|
|
|
|
auto h = range.half();
|
2017-02-01 20:56:19 +01:00
|
|
|
ret.reserve(range.size());
|
2017-02-01 18:28:57 +01:00
|
|
|
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 01:44:22 +01:00
|
|
|
}
|
|
|
|
return ret;
|
2015-07-13 01:24:14 +02:00
|
|
|
}
|
|
|
|
|
2017-02-01 20:56:19 +01:00
|
|
|
template<typename R, typename A = std::allocator<RangeValue<R>>>
|
|
|
|
inline std::vector<RangeValue<R>, A> make_vector(
|
|
|
|
R &&range, A const &alloc = A{}
|
|
|
|
) {
|
|
|
|
return make_vector<RangeValue<R>, A>(std::forward<R>(range), alloc);
|
2015-07-13 01:24:14 +02:00
|
|
|
}
|
|
|
|
|
2015-07-13 21:07:14 +02:00
|
|
|
} /* namespace ostd */
|
2015-04-06 01:05:21 +02:00
|
|
|
|
2016-02-07 22:17:15 +01:00
|
|
|
#endif
|