-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.hpp
67 lines (49 loc) · 1.54 KB
/
stack.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
#ifndef SLIP_STACK_HPP
#define SLIP_STACK_HPP
#include <vector>
#include <cassert>
#include <type_traits>
template<class T, std::size_t align=alignof(T)>
class stack {
using value_type = typename std::aligned_union<0, T>::type;
using storage_type = std::vector<value_type>;
storage_type storage;
std::size_t sp;
public:
stack(const stack& other) = default;
stack(stack&& other) = default;
T* next() { return reinterpret_cast<T*>(&storage[sp]); }
std::size_t size() const { return sp; }
stack(std::size_t size) : storage(size), sp(0) { }
T* allocate(std::size_t n) {
T* res = next();
sp += n;
assert(sp <= storage.size() && "stack overflow");
return res;
}
void deallocate(T* ptr, std::size_t n) {
assert(n <= sp && "stack corruption");
sp -= n;
assert(ptr == next() && "stack corruption");
}
template<class U=T>
struct allocator {
static_assert(sizeof(U) >= sizeof(T), "size error");
using storage_type = stack;
storage_type& storage;
using value_type = U;
template<class V> struct rebind { using other = allocator<V>; };
U* allocate(std::size_t n) {
std::size_t m = (sizeof(U) * n) / sizeof(T);
m += (m * sizeof(T) < n * sizeof(U));
assert(m * sizeof(T) >= n * sizeof(U));
return reinterpret_cast<U*>(storage.allocate(m));
}
void deallocate(U* ptr, std::size_t n) {
return storage.deallocate(ptr, n);
}
};
};
template<class T, class U=T>
using stack_allocator = typename stack<T>::template allocator<U>;
#endif