Wrath of Zeus
Made by Torchlight Games for CSE 125 SP24
Loading...
Searching...
No Matches
smartvector.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <vector>
6#include <unordered_set>
7
15template<typename T>
17public:
22 explicit SmartVector(size_t max_size) {
23 //this->max_size = max_size;
24 this->wrapped_vector.reserve(max_size);
25 }
26
28
35 size_t push(T object) {
36
37 // See if there's an empty index
38 size_t index;
39 auto it = freelist.begin();
40 if (it != freelist.end()) {
41 index = *it;
42 freelist.erase(index);
43
44 wrapped_vector[index] = object;
45 }
46 else {
47 index = wrapped_vector.size();
48
49 // Check that the wrapped vector can be safely pushed to (without
50 // causing reallocation)
51 //assert(this->max_size > index);
52
53 wrapped_vector.push_back(object);
54 }
55
56 return index;
57 }
58
67 bool set(T object, size_t index) {
68 if (index >= wrapped_vector.size())
69 return false;
70
71 // Remove index from free list if the specified index is currently
72 // empty
73 auto it = freelist.find(index);
74
75 if (it != freelist.end()) {
76 freelist.erase(*it);
77 }
78
79 wrapped_vector[index] = object;
80
81 return true;
82 }
83
90 size_t pushEmpty() {
91 size_t index = wrapped_vector.size();
92
93 // Check that the wrapped vector can be safely pushed to (without
94 // causing reallocation)
95 //assert(this->max_size > index);
96
97 freelist.insert(index);
98 wrapped_vector.push_back({});
99
100 return index;
101 }
102
108 bool remove(size_t index) {
109 // Check that index is in bounds
110 if (index < wrapped_vector.size()) {
111 // Add index to freelist
112 freelist.insert(index);
113
114 return true;
115 }
116
117 return false;
118 }
119
126 const T get(size_t index) const {
127 // Check that the index is in bounds
128 if (index >= wrapped_vector.size())
129 {
130 // Index is out of bounds
131 return nullptr;
132 }
133 // Check that index isn't free
134 if (freelist.find(index) != freelist.end()) {
135 // Index is free
136 return nullptr;
137 }
138
139 // Return pointer to object stored at the given index
140 return &wrapped_vector[index];
141 }
142
143 // https://stackoverflow.com/questions/38790352/how-can-you-return-a-non-const-reference-to-an-element-in-a-stdvector-data-mem
144 T get(size_t index) {
145 // Check that the index is in bounds
146 if (index >= wrapped_vector.size())
147 {
148 // Index is out of bounds
149 return nullptr;
150 }
151 // Check that index isn't free
152 if (freelist.find(index) != freelist.end()) {
153 // Index is free
154 return nullptr;
155 }
156
157 // Return pointer to object stored at the given index
158 return wrapped_vector[index];
159 }
160
166 size_t size() const {
167 return wrapped_vector.size();
168 }
169
175 size_t numElements() const {
176 return wrapped_vector.size() - freelist.size();
177 }
178
179 DEF_SERIALIZE(Archive& ar, const unsigned int version) {
180 ar& wrapped_vector& freelist;
181 }
182
183private:
190 //size_t max_size;
191 std::vector<T> wrapped_vector;
192 std::unordered_set<size_t> freelist;
193};
SmartVector is a wrapper for the std::vector class that adds objects either to the end of the wrapped...
Definition: smartvector.hpp:16
size_t numElements() const
Returns the number of elements in the wrapped vector (not including gaps)
Definition: smartvector.hpp:175
bool remove(size_t index)
Attempts to remove the given object at the given index of the wrapped vector.
Definition: smartvector.hpp:108
SmartVector()
Definition: smartvector.hpp:27
size_t pushEmpty()
Adds an empty object to the end of the wrapped vector (by adding the new index to the freelist.
Definition: smartvector.hpp:90
const T get(size_t index) const
Returns the object stored at the given index if it exists, and returns nullptr otherwise.
Definition: smartvector.hpp:126
T get(size_t index)
Definition: smartvector.hpp:144
size_t size() const
Returns the number of elements allocated in the wrapped vector (including gaps)
Definition: smartvector.hpp:166
bool set(T object, size_t index)
Overwrites the object at the specified index with the given object.
Definition: smartvector.hpp:67
size_t push(T object)
Pushes the given object to the end of the wrapped vector if it is currently full, or to an existing e...
Definition: smartvector.hpp:35
SmartVector(size_t max_size)
Creates a SmartVector instance with a maximum wrapped vector size of the given value.
Definition: smartvector.hpp:22
DEF_SERIALIZE(Archive &ar, const unsigned int version)
Definition: smartvector.hpp:179
GLuint index
Definition: glad.h:1846
@ T
Definition: mazegenerator.hpp:44