blob: aaa777f589c8c174ec6d7b29d02070d6c61afda9 (
plain) (
blame)
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
|
using namespace CMem; //strlen and memcpy
template <typename T>
Vector<T>::Vector() {
m_data = 0;
m_size = 0;
}
template <typename T>
Vector<T>::Vector(u32int size) {
m_data = new T[size];
m_size = size;
}
template <typename T>
Vector<T>::Vector(u32int size, T value) {
//m_data = (T*)Mem::kalloc(size * sizeof(T));
m_data = new T[size](value);
m_size = size;
/*for (u32int i = 0; i < size; i++) {
m_data[i] = new(&m_data[i]) T(value);
}*/
}
template <typename T>
Vector<T>::Vector(const Vector<T> &other) {
m_size = other.m_size;
m_data = (T*)Mem::kalloc(m_size * sizeof(T));
for (u32int i = 0; i < m_size; i++) {
new(&m_data[i]) T(other.m_data[i]);
}
}
template <typename T>
Vector<T>::~Vector() {
if (m_data != 0) delete[] m_data;
}
template <typename T>
T& Vector<T>::operator[] (u32int index) {
return m_data[index];
}
template <typename T>
void Vector<T>::push(T element) {
T* newdata = (T*)Mem::kalloc((m_size + 1) * sizeof(T));
memcpy((u8int*)newdata, (const u8int*) m_data, m_size * sizeof(T));
new(&newdata[m_size]) T(element); //Construct by copy
m_size++;
Mem::kfree(m_data);
m_data = newdata;
}
/* template <typename T>
void Vector<T>::push(T& element) {
T* newdata = (T*)Memory::alloc((m_size + 1) * sizeof(T));
memcpy((u8int*)newdata, (const u8int*) m_data, m_size * sizeof(T));
//memcpy((u8int*)newdata + (m_size * sizeof(T)), (const u8int*) element, sizeof(T)); //Copy
new(&newdata[m_size]) T(element); //Construct by copy
m_size++;
Memory::free(m_data);
m_data = newdata;
} */
template <typename T>
void Vector<T>::pop() {
m_size--;
//delete(&m_data[m_size], &m_data[m_size]); //implicitly call destructor with placement delete
m_data[m_size].~T(); //Call destructor
T* newdata = (T*)Mem::kalloc(m_size * sizeof(T));
memcpy((u8int*)newdata, (const u8int*) m_data, m_size * sizeof(T));
Mem::kfree(m_data);
m_data = newdata;
}
template <typename T>
T& Vector<T>::back() {
return m_data[m_size - 1];
}
template <typename T>
T& Vector<T>::front() {
return m_data[0];
}
template <typename T>
u32int Vector<T>::size() {
return m_size;
}
template <typename T>
bool Vector<T>::empty() {
return m_size == 0;
}
template <typename T>
void Vector<T>::clear() {
if (empty()) return;
Mem::kfree(m_data);
m_data = 0;
m_size = 0;
}
|