Skip to content

Commit 88c36b4

Browse files
committed
Add bump_alloc, size_t -> int in stack_union
1 parent c397ff9 commit 88c36b4

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

cp-algo/bump_alloc.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef CP_ALGO_BUMP_ALLOC_HPP
2+
#define CP_ALGO_BUMP_ALLOC_HPP
3+
#include <cstddef>
4+
namespace cp_algo {
5+
char buf[450 << 20] alignas(32);
6+
size_t buf_ind = sizeof buf;
7+
template<class T> struct bump_alloc {
8+
typedef T value_type;
9+
bump_alloc() {}
10+
template<class U> bump_alloc(const U&) {}
11+
T* allocate(size_t n) {
12+
buf_ind -= n * sizeof(T);
13+
buf_ind &= 0 - alignof(T);
14+
return (T*)(buf + buf_ind);
15+
}
16+
void deallocate(T*, size_t) {}
17+
};
18+
}
19+
#endif // CP_ALGO_BUMP_ALLOC_HPP

cp-algo/data_structures/stack_union.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
#ifndef CP_ALGO_DATA_STRUCTURES_STACK_UNION_HPP
22
#define CP_ALGO_DATA_STRUCTURES_STACK_UNION_HPP
3+
#include "../bump_alloc.hpp"
34
#include <cstddef>
45
#include <vector>
56
namespace cp_algo::data_structures {
67
template<class datatype>
78
struct stack_union {
8-
stack_union(size_t n): n(n), head(n), next(1), data(1) {}
9+
stack_union(int n = 0): n(n), head(n), next(1), data(1) {}
910

10-
void push(size_t v, datatype const& vdata) {
11+
void push(int v, datatype const& vdata) {
1112
next.push_back(head[v]);
1213
head[v] = size(next) - 1;
1314
data.push_back(vdata);
1415
}
1516
template<typename... Args>
16-
void emplace(size_t v, Args&&... vdata) {
17+
void emplace(int v, Args&&... vdata) {
1718
next.push_back(head[v]);
1819
head[v] = size(next) - 1;
1920
data.emplace_back(std::forward<Args...>(vdata...));
2021
}
2122

22-
size_t n;
23-
std::vector<size_t> head, next;
23+
int n;
24+
std::vector<int> head, next;
2425
std::vector<datatype> data;
2526
};
2627
}

0 commit comments

Comments
 (0)