-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
176 lines (132 loc) · 3.92 KB
/
main.cpp
File metadata and controls
176 lines (132 loc) · 3.92 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
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
template<typename T, size_t R>
struct logging_allocator {
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
std::size_t _byte_reserved = sizeof(T)*R;
pointer _reserved_memory_ptr = nullptr;
pointer _free_memory_ptr = nullptr;
logging_allocator() {
_reserved_memory_ptr = reinterpret_cast<T *>(std::malloc(_byte_reserved));
_free_memory_ptr = _reserved_memory_ptr;
}
~logging_allocator() {
std::free(_reserved_memory_ptr);
}
template<typename U>
struct rebind {
using other = logging_allocator<U, R>;
};
T *allocate(std::size_t n) {
auto p = _free_memory_ptr;
if (!p)
throw std::bad_alloc();
_free_memory_ptr += n;
return reinterpret_cast<T *>(p);
}
void deallocate(T *p, std::size_t n) { ; }
template<typename U, typename ...Args>
void construct(U *p, Args &&...args) {
new(p) U(std::forward<Args>(args)...);
}
void destroy(T *p) {
if(p != nullptr)
p->~T();
}
};
template<typename T, typename U>
struct MapNode {
T key;
U value;
MapNode* next;
MapNode(T t, U u) : key(t), value(u) { ; }
};
template<typename T, typename U, typename Allocator>
struct myMap {
using k_type = T;
using v_type = U;
typedef typename Allocator::template rebind<MapNode<T,U>>::other MyMapAllocator;
MyMapAllocator _allocator;
MapNode<T,U>* _headElement;
MapNode<T,U>* _tailElement;
size_t _size = 0;
~myMap() {
auto temp = _headElement;
for(size_t i = 0; i < _size; i++) {
auto temp1 = temp->next;
_allocator.destroy(temp);
_allocator.deallocate(temp, sizeof(temp));
temp = temp1;
}
}
void add(const k_type&& key_, v_type&& value_) {
MapNode<T,U>* temp = _allocator.allocate(1);
_allocator.construct(temp, std::forward<const k_type>(key_), std::forward<v_type>(value_));
if(_size == 0)
_headElement = temp;
else
_tailElement->next = temp;
_tailElement = temp;
_size++;
}
v_type get(const k_type key_) {
auto temp = _headElement;
for(size_t i = 0; i < _size; i++) {
if(temp->key == key_)
return temp->value;
temp = temp->next;
}
return _headElement->value;
}
};
void f1() {
std::cout << " === std::map and std::allocator ===" << std::endl;
auto m = std::map<int, int, std::less<int>>{};
for (size_t i = 0; i < 10; ++i) {
m[i] = std::tgamma(i+1);
std::cout << i << " " << m[i] << std::endl;
}
}
void f2() {
std::cout << " === std::map and my allocator ===" << std::endl;
auto n = std::map<int, int, std::less<int>, logging_allocator<std::pair<const int, int>, 10>>{};
for (size_t i = 0; i < 10; ++i) {
n[i] = std::tgamma(i+1);
std::cout << i << " " << n[i] << std::endl;
}
}
void f3() {
std::cout << " === my map and std::allocator ===" << std::endl;
auto r = myMap<int, int, std::allocator<MapNode<const int, int>>>{};
for (size_t i = 0; i < 10; ++i) {
r.add(std::move(i), std::move(std::tgamma(i+1)));
std::cout << i << " " << r.get(i) << std::endl;
}
}
void f4() {
std::cout << " === my map and my allocator ===" << std::endl;
auto p = myMap<int, int, logging_allocator<std::pair<const int, int>, 10>>{};
for (size_t i = 0; i < 10; ++i) {
p.add(std::move(i), std::move(std::tgamma(i+1)));
std::cout << i << " " << p.get(i) << std::endl;
}
}
int main(int, char *[]) {
try {
f1();
f2();
f3();
f4();
}
catch(...)
{
std::cout << "Unknown error" << std::endl;
}
return 0;
}