forked from starokurov/otus-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
206 lines (173 loc) · 6.54 KB
/
main.cpp
File metadata and controls
206 lines (173 loc) · 6.54 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <iostream>
#include <map>
#include <vector>
#include <memory>
#include <stdexcept>
#include <algorithm>
#include <iterator>
#include "lib.h"
// ============================================================
// Аллокатор с фиксированным количеством элементов (статический пул)
// ============================================================
template<typename T, size_t N>
class FixedAllocator {
struct Pool {
T* storage;
std::vector<size_t> free_list;
Pool() {
storage = static_cast<T*>(::operator new(sizeof(T) * N));
for (size_t i = N; i > 0; --i)
free_list.push_back(i - 1);
}
~Pool() {
::operator delete(storage);
}
Pool(const Pool&) = delete;
Pool& operator=(const Pool&) = delete;
};
static Pool& get_pool() {
static Pool pool;
return pool;
}
public:
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using size_type = size_t;
using difference_type = ptrdiff_t;
FixedAllocator() = default;
template<typename U>
FixedAllocator(const FixedAllocator<U, N>&) {}
pointer allocate(size_type n) {
if (n != 1) throw std::bad_alloc();
Pool& pool = get_pool();
if (pool.free_list.empty()) throw std::bad_alloc();
size_t idx = pool.free_list.back();
pool.free_list.pop_back();
return pool.storage + idx;
}
void deallocate(pointer p, size_type n) noexcept {
if (p == nullptr || n == 0) return;
Pool& pool = get_pool();
size_t idx = p - pool.storage;
pool.free_list.push_back(idx);
}
template<typename U, typename... Args>
void construct(U* p, Args&&... args) {
::new(static_cast<void*>(p)) U(std::forward<Args>(args)...);
}
template<typename U>
void destroy(U* p) {
p->~U();
}
template<typename U>
struct rebind {
using other = FixedAllocator<U, N>;
};
bool operator==(const FixedAllocator&) const { return true; }
bool operator!=(const FixedAllocator&) const { return false; }
};
// ============================================================
// Собственный контейнер (односвязный список) с аллокатором
// ============================================================
template<typename T, typename Allocator = std::allocator<T>>
class MyContainer {
public:
using value_type = T;
using allocator_type = Allocator;
using size_type = size_t;
private:
struct Node {
T value;
Node* next;
};
using NodeAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<Node>;
NodeAllocator node_alloc_;
Node* head_;
Node* tail_;
size_type size_;
public:
MyContainer() : node_alloc_(), head_(nullptr), tail_(nullptr), size_(0) {}
explicit MyContainer(const Allocator& alloc) : node_alloc_(alloc), head_(nullptr), tail_(nullptr), size_(0) {}
~MyContainer() {
clear();
}
void push_back(const T& value) {
Node* new_node = node_alloc_.allocate(1);
std::allocator_traits<NodeAllocator>::construct(node_alloc_, new_node, Node{value, nullptr});
if (tail_) {
tail_->next = new_node;
tail_ = new_node;
} else {
head_ = tail_ = new_node;
}
++size_;
}
size_type size() const { return size_; }
bool empty() const { return size_ == 0; }
class iterator {
public:
using iterator_category = std::forward_iterator_tag;
using value_type = T;
using difference_type = ptrdiff_t;
using pointer = T*;
using reference = T&;
iterator(Node* node) : node_(node) {}
reference operator*() const { return node_->value; }
pointer operator->() const { return &node_->value; }
iterator& operator++() { node_ = node_->next; return *this; }
iterator operator++(int) { iterator tmp = *this; ++(*this); return tmp; }
bool operator==(const iterator& other) const { return node_ == other.node_; }
bool operator!=(const iterator& other) const { return !(*this == other); }
private:
Node* node_;
};
iterator begin() { return iterator(head_); }
iterator end() { return iterator(nullptr); }
private:
void clear() {
Node* cur = head_;
while (cur) {
Node* next = cur->next;
std::allocator_traits<NodeAllocator>::destroy(node_alloc_, cur);
node_alloc_.deallocate(cur, 1);
cur = next;
}
head_ = tail_ = nullptr;
size_ = 0;
}
};
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; ++i) result *= i;
return result;
}
int main() {
// 1. std::map с обычным аллокатором
std::map<int, int> map1;
for (int i = 0; i < 10; ++i) map1[i] = factorial(i);
// 2. std::map с нашим аллокатором, ограниченным 11 элементами (учитываем служебный узел)
FixedAllocator<std::pair<const int, int>, 11> alloc;
std::map<int, int, std::less<int>, FixedAllocator<std::pair<const int, int>, 11>> map2(alloc);
for (int i = 0; i < 10; ++i) map2[i] = factorial(i);
std::cout << "std::map with default allocator:\n";
for (const auto& p : map1) std::cout << p.first << " " << p.second << "\n";
std::cout << "\nstd::map with FixedAllocator<11>:\n";
for (const auto& p : map2) std::cout << p.first << " " << p.second << "\n";
// 3. Свой контейнер с обычным аллокатором
MyContainer<int> cont1;
for (int i = 0; i < 10; ++i) cont1.push_back(i);
// 4. Свой контейнер с нашим аллокатором для int (10 элементов)
FixedAllocator<int, 10> int_alloc;
MyContainer<int, FixedAllocator<int, 10>> cont2(int_alloc);
for (int i = 0; i < 10; ++i) cont2.push_back(i);
std::cout << "\nMyContainer with default allocator:\n";
for (auto val : cont1) std::cout << val << " ";
std::cout << "\n";
std::cout << "\nMyContainer with FixedAllocator<int,10>:\n";
for (auto val : cont2) std::cout << val << " ";
std::cout << "\n";
return 0;
}