forked from ovr/StaticScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.h
More file actions
42 lines (33 loc) · 775 Bytes
/
array.h
File metadata and controls
42 lines (33 loc) · 775 Bytes
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
//
// Created by Dmitry Patsura on 2018-12-20.
//
#ifndef HLVM_RUNTIME_ARRAY_H
#define HLVM_RUNTIME_ARRAY_H
#include "library.h"
#include <cstdint>
#include <cstdlib>
template<typename T>
class Array {
public:
Array() : elements(nullptr), capacity(0) {}
Array(int32_t capacity) {
this->capacity = capacity;
this->elements = new T[capacity];
}
void push(T value) {
if (size == capacity) {
this->expand();
}
this->size++;
this->elements[size] = value;
}
private:
T* elements;
int32_t size = 0;
int32_t capacity;
void expand() {
this->size *= 2;
this->elements = std::realloc(this->elements, this->size * sizeof(T));
}
};
#endif //HLVM_RUNTIME_ARRAY_H