-
Notifications
You must be signed in to change notification settings - Fork 1
/
stackbasedvector.h
90 lines (77 loc) · 1.38 KB
/
stackbasedvector.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
90
#ifndef JWUTIL_STACKBASEDVECTOR_H
#define JWUTIL_STACKBASEDVECTOR_H
#include <vector>
// http://stackoverflow.com/questions/354442/looking-for-c-stl-like-vector-class-but-using-stack-storage
// http://stackoverflow.com/questions/8049657/stack-buffer-based-stl-allocator
namespace jw_util
{
template <typename DataType, std::size_t stack_size>
using StackBasedVector = std::vector<DataType>;
/*
template <typename Type, std::size_t stack_size>
class StackBasedVector
{
public:
StackBasedVector()
{
// This class is not tested yet
assert(false);
}
Type &operator[](std::size_t i)
{
if (i < stack_size)
{
// Use stack
return stack[i];
}
else
{
// Use heap
assert(i < size);
return heap[i - stack_size];
}
}
void push_back(Type &val)
{
if (size < stack_size)
{
// Use stack
stack[size] = val;
}
else
{
// Use heap
std::size_t heap_i = size - stack_size;
if (heap_i >= alloc)
{
if (alloc)
{
Heap *new_heap = new Type[alloc * 2];
std::copy_n(heap, new_heap, alloc);
delete[] heap;
heap = new_heap;
alloc *= 2;
}
else
{
alloc = 16;
heap = new Type[alloc];
}
}
heap[heap_i] = val;
}
size++;
}
void pop_back()
{
size--;
}
private:
std::size_t size = 0;
std::size_t alloc = 0;
std::array<Type, stack_size> stack;
Type *heap;
};
*/
}
#endif // JWUTIL_STACKBASEDVECTOR_H