acme
Foundational cross-platform layer of the ca2 framework
Loading...
Searching...
No Matches
simple_array.h
1// Created by camilo on 2023-11-22 12:47 <3ThomasBorregaardSorensen
2#pragma once
3
4
5#include "acme/memory/::system()->m_pheapmanagement->memory(::heap::e_memory_main)->allocate.h"
6
7
8// currently simple_array doesn't call any constructors/destructors on elements
9template < typename T >
10class simple_array
11{
12public:
13
14 T* m_p;
15 ::i32 m_c;
16 ::i32 m_cAlloc;
17
18
19 simple_array() { m_p = nullptr; m_c = 0; m_cAlloc = 0; }
20 ~simple_array() { if (m_p) ::system()->m_pheapmanagement->memory(::heap::e_memory_main)->free(m_p); m_p = nullptr; m_c = 0; m_cAlloc = 0; }
21
22 void allocate_for_size(::i32 s)
23 {
24
25 if (s > m_cAlloc)
26 {
27
28 m_cAlloc = s + 8;
29
30 auto pNew = (T*)::system()->m_pheapmanagement->memory(::heap::e_memory_main)->allocate(m_cAlloc * sizeof(T));
31
32 memory_copy(pNew, m_p, m_c * sizeof(T));
33
34 ::system()->m_pheapmanagement->memory(::heap::e_memory_main)->free(m_p);
35
36 m_p = pNew;
37
38 }
39
40 }
41
42 void add(const T& t)
43 {
44
45 allocate_for_size(m_c + 1);
46
47 ::new (m_p + m_c) T(t);
48
49 m_c++;
50
51 }
52
53 void erase_at(::collection::index i)
54 {
55
56 memory_transfer(m_p + i, m_p + i + 1, (m_c - i - 1) * sizeof(T));
57
58 m_c--;
59
60 }
61
62};
63
64
65template < typename T >
66bool erase_first(simple_array < T >& a, const T& t)
67{
68
69 for (::collection::index i = 0; i < a.m_c; i++)
70 {
71
72 if (a.m_p[i] == t)
73 {
74
75 a.erase_at(i);
76
77 return true;
78
79 }
80
81 }
82
83 return false;
84
85}
86
87
88
89