1//////////////////////////////////////////////////////////////////////
2// This file is part of Remere's Map Editor
3//////////////////////////////////////////////////////////////////////
4// Remere's Map Editor is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Remere's Map Editor is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//////////////////////////////////////////////////////////////////////
17
18#ifndef RME_CON_VECTOR_H_
19#define RME_CON_VECTOR_H_
20
21#define REALLOC_INCREASE 600
22
23template <class T> // This only really works with pointers.. hrhr "T" might be abit misleading.. :o
24class contigous_vector {
25 T __pointer_check(T t) {
26 return *t;
27 } // If this fails, you have tried using this class with a non-pointer type, DONT
28public:
29 contigous_vector(size_t start_size = 7) {
30 start = reinterpret_cast<T*>(malloc(sizeof(T) * start_size));
31 memset(start, 0, sizeof(T) * start_size);
32 sz = start_size;
33 }
34 ~contigous_vector() {
35 free(start);
36 }
37
38 void resize(size_t new_size) {
39 size_t old_size = sz;
40 start = reinterpret_cast<T*>(realloc(start, sizeof(T) * new_size));
41 memset(start + old_size, 0, sizeof(T) * (new_size - old_size));
42 sz = new_size;
43 }
44 size_t size() {
45 return sz;
46 }
47
48 T& locate(size_t index) {
49 // Masterly inefficient!
50 while (index >= sz) {
51 resize(sz + REALLOC_INCREASE);
52 }
53 return start[index];
54 }
55
56 T at(size_t index) const {
57 if (index >= sz) {
58 return nullptr;
59 }
60 return start[index];
61 }
62
63 void set(size_t index, T value) {
64 locate(index) = value;
65 }
66
67 T operator[](size_t index) {
68 return at(index);
69 }
70 const T operator[](size_t index) const {
71 return at(index);
72 }
73
74private:
75 T* start;
76 size_t sz;
77};
78
79#endif
80