Twilight Princess
Decompilation of The Legend of Zelda: Twilight Princess
Loading...
Searching...
No Matches
std-vector.h
Go to the documentation of this file.
1#ifndef STD_VECTOR_H
2#define STD_VECTOR_H
3
6#include <algorithm>
7#include <memory>
8#include <stdint.h>
9
10namespace JGadget {
11namespace vector {
13};
14
15typedef u32 (*extendFunc)(u32, u32, u32);
16
17template <typename T, typename Allocator = JGadget::TAllocator<T> >
18struct TVector {
21 mAllocator = &allocator;
22 mPtr = ptr;
23 }
24
26
27 void set(T* ptr) { mPtr = ptr; }
28
29 Allocator* mAllocator;
30 T* mPtr;
31 };
32
33 typedef T* iterator;
34 typedef const T* const_iterator;
35
36 TVector(Allocator const& allocator) {
37#if PLATFORM_GCN
38 mAllocator = allocator;
39#endif
40 pBegin_ = NULL;
41 pEnd_ = pBegin_;
42 mCapacity = 0;
44 }
45
47#if DEBUG
48 Confirm();
49#endif
50 clear();
51 JGADGET_ASSERTWARN(250, size()==0);
52 mAllocator.deallocate(pBegin_, 0);
53 }
54
55 void insert(T* pos, u32 count, const T& val) {
56 if (count == 0) {
57 return;
58 }
59
60 T* ptr = Insert_raw(pos, count);
61 if (ptr == end()) {
62 JGADGET_WARNMSG(321, "can't allocate memory");
63 } else {
64 std::uninitialized_fill_n(ptr, count, val);
65 }
66 }
67
68 T* Insert_raw(T* pFirst, u32 pCount) {
69 T* const pIt = pFirst;
70 JUT_ASSERT(446, (pBegin_<=pIt)&&(pIt<=pEnd_));
71
72 if (pCount == 0) {
73 return pFirst;
74 }
75
76 if (pCount + size() <= mCapacity) {
77 void** newEnd = pIt + pCount;
78
79 if (newEnd < pEnd_) {
80 void** pOverwrittenValues = pEnd_ - pCount;
81 std::uninitialized_copy(pOverwrittenValues, pEnd_, pEnd_);
82 std::copy_backward(pIt, pOverwrittenValues, pEnd_);
83 DestroyElement_(pIt, newEnd);
84
85 pEnd_ += pCount;
86 return pFirst;
87 } else {
88 std::uninitialized_copy(pIt, pEnd_, newEnd);
90
91 pEnd_ += pCount;
92 return pFirst;
93 }
94 }
95
96 u32 newSize = GetSize_extend_(pCount);
97
98 void** newDataPointer = mAllocator.allocate(newSize, 0);
99 if (!newDataPointer) {
100 return end();
101 }
102
103 TDestructed_deallocate_ destructionDeallocator(mAllocator, newDataPointer);
104
105 void** const endOfCopy = std::uninitialized_copy(pBegin_, pIt, newDataPointer);
106 std::uninitialized_copy(pIt, pEnd_, endOfCopy + pCount);
107
109 destructionDeallocator.set(pBegin_);
110
111 pEnd_ = newDataPointer + (pEnd_ - pBegin_ + pCount);
112 pBegin_ = newDataPointer;
113 mCapacity = newSize;
114
115 return endOfCopy;
116 }
117
118 T* insert(T* pos, const T& val) {
119 u32 diff = (int)((uintptr_t)pos - (uintptr_t)begin()) / 4;
120 insert(pos, 1, val);
121 return pBegin_ + diff;
122 }
123
124 iterator begin() { return pBegin_; }
125 const_iterator begin() const { return pBegin_; }
126 iterator end() { return pEnd_; }
127 const_iterator end() const { return pEnd_; }
128
129 u32 size() const {
130 if (pBegin_ == 0) {
131 return 0;
132 }
133 return (int)((uintptr_t)pEnd_ - (uintptr_t)pBegin_) / 4;
134 }
135
136 u32 capacity() const { return mCapacity; }
137
138 bool Confirm() const {
139 if (size() > mCapacity) {
140 JGADGET_WARNMSG(507, "illegal size");
141 return false;
142 }
143 return true;
144 }
145
146 u32 GetSize_extend_(u32 count) const {
147 JUT_ASSERT(555, pfnExtend_!=NULL);
148
149 u32 oldSize = size();
150 u32 neededNewSpace = oldSize + count;
151 u32 extendedSize = pfnExtend_(capacity(), oldSize, count);
152
153 return neededNewSpace > extendedSize ? neededNewSpace : extendedSize;
154 }
155
156 void DestroyElement_(T* pFirst, T* pLast) {
157 JUT_ASSERT(536, (pBegin_<=pFirst)&&(pFirst<=pEnd_));
158 JUT_ASSERT(537, (pBegin_<=pLast)&&(pLast<=pEnd_));
159 T* it = pFirst;
160 for (; it != pLast; it++) {
161 mAllocator.destroy(it);
162 }
163 }
164
166
167 T* erase(T* pFirst, T* pLast) {
168 iterator pItFirst = pFirst;
169 iterator pItLast = pLast;
170 JUT_ASSERT(347, (pBegin_<=pItFirst)&&(pItFirst<=pEnd_))
171 JUT_ASSERT(348, (pBegin_<=pItLast)&&(pItLast<=pEnd_));
172 JUT_ASSERT(349, pItFirst<=pItLast);
173 T* vectorEnd = pEnd_; // DEBUG NONMATCHING
174 T* ppvVar3 = std::copy(pItLast, vectorEnd, pItFirst);
175 DestroyElement_(ppvVar3, pEnd_);
176 pEnd_ = ppvVar3;
177 return pFirst;
178 }
179
180 void clear() { erase(begin(), end()); }
181
182 /* 0x00 */ Allocator mAllocator;
183 /* 0x04 */ T* pBegin_;
184 /* 0x08 */ T* pEnd_;
185 /* 0x0C */ u32 mCapacity;
187};
188
189struct TVector_pointer_void : public TVector<void*, TAllocator<void*> > {
192 const JGadget::TAllocator<void*>& allocator);
193
195
196 void insert(void**, void* const&);
197 void** erase(void**, void**);
198
199 void clear() { erase(begin(), end()); }
200 void push_back(void* const& value) { insert(end(), (void* const&)value); }
201};
202
203template <typename T>
204struct TVector_pointer : TVector_pointer_void {
207
208 typedef T* iterator;
209 typedef const T* const_iterator;
210
211 const T* begin() const { return (const T*)TVector_pointer_void::begin(); }
212 T* begin() { return (T*)TVector_pointer_void::begin(); }
213
214 const T* end() const { return (const T*)TVector_pointer_void::end(); }
215 T* end() { return (T*)TVector_pointer_void::end(); }
216
217 void push_back(const T& ref) {
218 static_cast<TVector_pointer_void*>(this)->push_back((void* const&)ref);
219 }
220};
221
222} // namespace JGadget
223
224#endif /* STD_VECTOR_H */
int diff
Definition e_fmod.c:41
unsigned long u32
Definition types.h:12
u32 extend_default(u32, u32, u32)
Definition std-vector.cpp:5
Definition linklist.cpp:6
u32(* extendFunc)(u32, u32, u32)
Definition std-vector.h:15
Definition std-memory.h:8
Allocator * mAllocator
Definition std-vector.h:29
T * mPtr
Definition std-vector.h:30
TDestructed_deallocate_(JGadget::TAllocator< T > &allocator, T *ptr)
Definition std-vector.h:20
void set(T *ptr)
Definition std-vector.h:27
~TDestructed_deallocate_()
Definition std-vector.h:25
Definition vector.h:47
void insert(void **, void *const &)
Definition std-vector.cpp:19
void ** erase(void **, void **)
Definition std-vector.cpp:23
void clear()
Definition std-vector.h:199
TVector_pointer_void(u32, void *const &, const JGadget::TAllocator< void * > &allocator)
TVector_pointer_void(const JGadget::TAllocator< void * > &allocator)
Definition std-vector.cpp:9
~TVector_pointer_void()
Definition std-vector.cpp:16
void push_back(void *const &value)
Definition std-vector.h:200
const T * const_iterator
Definition std-vector.h:209
T * begin()
Definition std-vector.h:212
const T * begin() const
Definition std-vector.h:211
T * end()
Definition std-vector.h:215
~TVector_pointer()
Definition std-vector.h:206
const T * end() const
Definition std-vector.h:214
void push_back(const T &ref)
Definition std-vector.h:217
TVector_pointer(const TAllocator< void * > &allocator)
Definition std-vector.h:205
T * iterator
Definition std-vector.h:208
Definition vector.h:18
u32 capacity() const
Definition std-vector.h:136
const_iterator end() const
Definition std-vector.h:127
T * erase(T *pFirst, T *pLast)
Definition std-vector.h:167
void insert(T *pos, u32 count, const T &val)
Definition std-vector.h:55
T * Insert_raw(T *pFirst, u32 pCount)
Definition std-vector.h:68
TVector(Allocator const &allocator)
Definition std-vector.h:36
Allocator mAllocator
Definition std-vector.h:182
T * insert(T *pos, const T &val)
Definition std-vector.h:118
T * pEnd_
Definition std-vector.h:184
const T * const_iterator
Definition std-vector.h:34
iterator begin()
Definition std-vector.h:124
T * iterator
Definition std-vector.h:33
u32 mCapacity
Definition std-vector.h:185
void DestroyElement_(T *pFirst, T *pLast)
Definition std-vector.h:156
void DestroyElement_all_()
Definition std-vector.h:165
const_iterator begin() const
Definition std-vector.h:125
void clear()
Definition std-vector.h:180
u32 size() const
Definition std-vector.h:129
u32 GetSize_extend_(u32 count) const
Definition std-vector.h:146
iterator end()
Definition std-vector.h:126
bool Confirm() const
Definition std-vector.h:138
T * pBegin_
Definition std-vector.h:183
~TVector()
Definition std-vector.h:46
extendFunc pfnExtend_
Definition std-vector.h:186