2015-12-27 17:58:49 +01:00
|
|
|
#pragma once
|
|
|
|
|
2016-01-07 12:04:40 +01:00
|
|
|
// std
|
2015-12-27 17:58:49 +01:00
|
|
|
#include <algorithm>
|
2015-12-28 13:21:02 +01:00
|
|
|
#include <functional>
|
|
|
|
#include <type_traits>
|
2015-12-27 17:58:49 +01:00
|
|
|
|
|
|
|
namespace DOS
|
|
|
|
{
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct wrapped_array {
|
|
|
|
wrapped_array(T* first, T* last) : begin_ {first}, end_ {last} {}
|
|
|
|
wrapped_array(T* first, std::ptrdiff_t size)
|
|
|
|
: wrapped_array {first, first + size} {}
|
|
|
|
|
|
|
|
T* begin() const noexcept { return begin_; }
|
|
|
|
T* end() const noexcept { return end_; }
|
|
|
|
|
|
|
|
T* begin_;
|
|
|
|
T* end_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
wrapped_array<T> wrap_array(T* first, std::ptrdiff_t size) noexcept
|
|
|
|
{ return {first, size}; }
|
|
|
|
|
|
|
|
template <typename T, typename G>
|
2015-12-28 13:21:02 +01:00
|
|
|
std::vector<T> toVector(G* first, std::ptrdiff_t size) noexcept
|
2015-12-27 17:58:49 +01:00
|
|
|
{
|
2015-12-28 13:21:02 +01:00
|
|
|
const wrapped_array<G> array = wrap_array(first, size);
|
2015-12-27 17:58:49 +01:00
|
|
|
std::vector<T> result;
|
2016-01-02 16:48:16 +01:00
|
|
|
for (auto it = array.begin(); it != array.end(); ++it)
|
|
|
|
result.emplace_back(T(*it));
|
2015-12-27 17:58:49 +01:00
|
|
|
return result;
|
|
|
|
}
|
2015-12-28 13:21:02 +01:00
|
|
|
|
|
|
|
template <typename T, typename K, typename R = typename std::result_of<K(T)>::type>
|
|
|
|
std::vector<R> toVector(T* first, std::ptrdiff_t size, K f) noexcept
|
|
|
|
{
|
|
|
|
wrapped_array<T> array = wrap_array<T>(first, size);
|
|
|
|
std::vector<R> result;
|
2016-01-02 16:48:16 +01:00
|
|
|
for (auto it = array.begin(); it != array.end(); ++it)
|
|
|
|
result.emplace_back(R(f(*it)));
|
2015-12-28 13:21:02 +01:00
|
|
|
return result;
|
|
|
|
}
|
2015-12-27 17:58:49 +01:00
|
|
|
}
|