-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_storage.h
89 lines (78 loc) · 2.49 KB
/
dynamic_storage.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef DYNAMIC_STORAGE_H
#define DYNAMIC_STORAGE_H
#include <cstdlib>
#include <initializer_list>
#include <memory>
#include <algorithm>
template<typename T>
struct dynamic_storage {
public:
typedef size_t size_type;
typedef T* iterator;
typedef T const* const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
dynamic_storage();
explicit dynamic_storage(size_type n, T value = T());
dynamic_storage(dynamic_storage const& other);
dynamic_storage(std::initializer_list<T> init);
~dynamic_storage();
dynamic_storage& operator=(dynamic_storage const &other);
template<class... Args>
void emplace_back(Args&&... args);
void push_back(T value);
void pop_back();
bool empty() const noexcept;
T& back() noexcept;
T const& back() const noexcept;
T& operator[](size_type n) noexcept;
T const& operator[](size_type n) const noexcept;
size_type size() const noexcept;
size_type capacity() const noexcept;
void reserve(size_type n);
void shrink_to_fit();
void swap(dynamic_storage& other) noexcept;
iterator begin() noexcept;
const_iterator begin() const noexcept;
const_iterator cbegin() const noexcept;
iterator end() noexcept;
const_iterator end() const noexcept;
const_iterator cend() const noexcept;
reverse_iterator rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;
const_reverse_iterator crbegin() const noexcept;
reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;
const_reverse_iterator crend() const noexcept;
private:
size_type _size;
struct big_data {
struct deleter {
deleter();
~deleter();
void operator()(T* p);
};
size_type capacity;
std::shared_ptr<T> data;
big_data();
big_data(size_type capacity, T* data);
big_data(big_data const& other);
~big_data();
};
struct small_data {
static constexpr size_t capacity = std::min(sizeof(big_data) / sizeof(T), static_cast<size_t>(1));
T data[capacity];
};
union any_data {
small_data small;
big_data big;
any_data();
~any_data();
} _data;
T* current;
void set_capacity(size_type capacity);
void prepare_for_modification();
bool is_data_big() const noexcept;
};
#include "dynamic_storage.tpp"
#endif //DYNAMIC_STORAGE_H