-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_util.hpp
98 lines (80 loc) · 1.91 KB
/
container_util.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#pragma once
#include <type_traits>
#include <utility>
#include <iterator>
#include <algorithm>
template<typename T>
concept ContainerType = requires(T a) {
std::begin(a);
std::end(a);
};
template<typename T>
concept STLContainerType = requires(T a) {
a.begin();
a.end();
a.size();
std::back_inserter(a);
};
template<STLContainerType Container>
auto concat(Container&& list1, Container&& list2)
{
std::decay_t<Container> ret(list1.get_allocator());
ret.reserve(list1.size() + list2.size());
std::copy(std::begin(list1), std::end(list1), std::back_inserter(ret));
std::copy(std::begin(list2), std::end(list2), std::back_inserter(ret));
return ret;
}
template<template<typename...> typename ResultContainer, ContainerType Container, typename Transformer>
auto map(Container&& list1, Transformer&& transformer)
{
ResultContainer<std::invoke_result_t<Transformer, typename std::decay_t<Container>::value_type> > ret;
std::transform(std::begin(list1), std::end(list1), std::back_inserter(ret), std::forward<Transformer>(transformer));
return ret;
}
template<STLContainerType Container>
auto as_container(Container&& c, int size)
{
return std::forward<Container>(c);
}
template<typename ElementType>
auto as_container(ElementType * c, int size)
{
struct string_array_wrap
{
ElementType* _array;
int _size;
using value_type = ElementType;
struct iterator
{
string_array_wrap& parent;
int cur_pos;
ElementType& operator * ()
{
return parent._array[cur_pos];
}
bool operator == (const iterator& other)
{
return cur_pos == other.cur_pos;
}
iterator& operator +(int inc) {
cur_pos += inc;
return *this;
}
iterator& operator ++() {
cur_pos ++;
return *this;
}
};
iterator begin()
{
return iterator{*this, 0};
}
iterator end()
{
return iterator{*this, _size};
}
int size() const { return _size;}
};
return string_array_wrap{c, size};
}