-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_list.h
More file actions
135 lines (113 loc) · 2.62 KB
/
Copy patharray_list.h
File metadata and controls
135 lines (113 loc) · 2.62 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
//
// Created by Alex on 21-Jun-18.
//
#ifndef CPP_GENERIC_HASHMAP_VECTOR_H
#define CPP_GENERIC_HASHMAP_VECTOR_H
#include <initializer_list>
#include <memory.h>
#include <algorithm>
#include <assert.h>
#define INITIAL_CAPACITY 8
#define GROW_FACTOR 2
#define SHRINK_THRESHOLD_RATIO 0.25
#define SHRINK_FACTOR 0.5
template<typename T>
class array_list
{
private:
T* table;
unsigned int capacity;
unsigned int count;
void init(unsigned int capacity, std::initializer_list<T> list)
{
this->capacity = std::max(capacity, list.size() * GROW_FACTOR);
this->count = list.size();
table = (T*)malloc(this->capacity * sizeof(T));
int position = 0;
for (const T& element : list)
{
table[position++] = element;
}
}
void grow()
{
capacity *= GROW_FACTOR;
table = (T*)realloc(table, capacity * sizeof(T));
}
void shrink()
{
// Can't lose elements!
if (capacity / SHRINK_FACTOR >= count) {
capacity /= SHRINK_FACTOR;
table = (T *) realloc(table, capacity * sizeof(T));
}
}
public:
~array_list()
{
free(table);
}
array_list(std::initializer_list<T> list)
{
init(INITIAL_CAPACITY, list);
}
explicit array_list(int count)
{
init(std::max(INITIAL_CAPACITY, count), {});
this->count = count;
}
void clear()
{
for (int i = 0; i < count; i++)
delete &table[i];
count = 0;
}
void push_back(T element)
{
if (count + 1 > capacity)
grow();
table[count++] = element;
}
T pop_back()
{
assert(count > 0);
T element = table[count-1];
count--;
if (count * 1. / capacity <= SHRINK_THRESHOLD_RATIO)
shrink();
return element;
}
void push_front(T element)
{
if (count + 1 > capacity)
grow();
for (int i = count-1; i >= 1; i--)
table[i] = table[i-1];
table[0] = element;
}
T pop_front()
{
assert(count > 0);
T element = table[0];
for (int i = 0; i < count-1; i++)
table[i] = table[i+1];
count--;
if (count * 1. / capacity <= SHRINK_THRESHOLD_RATIO)
shrink();
return element;
}
T& at(int index)
{
assert(index >= 0 && index < count);
return table[index];
}
T& operator[](int index) const
{
return table[index];
}
unsigned int size()
{
return count;
}
};
#endif //CPP_GENERIC_HASHMAP_VECTOR_H