-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVector.h
69 lines (57 loc) · 1.28 KB
/
Vector.h
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
#ifndef VECTOR_H
#define VECTOR_H
#include <numeric>
#include <initializer_list>
#include <algorithm>
#include <memory>
template<typename T>
class immutable_vector
{
public:
immutable_vector()
: m_size(0)
{
}
immutable_vector(T &&value)
: m_size(1)
, m_storage(new T[1])
{
memcpy(m_storage.get(), &value, sizeof(T));
}
immutable_vector(const std::initializer_list<T> &&initializer)
: m_size(initializer.size())
, m_storage(new T[m_size])
{
std::copy(begin(initializer), end(initializer), m_storage.get());
}
template<typename ...TVector>
immutable_vector(const TVector &... vectors)
: m_size((vectors.size() + ...))
, m_storage(new T[m_size])
{
copy(m_storage.get(), vectors...);
}
std::size_t size() const
{
return m_size;
}
T *data() const
{
return m_storage.get();
}
private:
template<typename THead, typename ...TTail>
static void copy(T *target, const THead &head, const TTail &...tail)
{
memcpy(target, head.data(), head.size() * sizeof(T));
copy(target + head.size(), tail...);
}
template<typename THead>
static void copy(T *target, const THead &head)
{
memcpy(target, head.data(), head.size() * sizeof(T));
}
std::size_t m_size;
std::shared_ptr<T> m_storage;
};
#endif // VECTOR_H