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
5#include <algorithm.h>
6#include <msl_memory.h>
7#include <stdint.h>
8
9namespace JGadget {
10namespace vector {
12};
13
14typedef u32 (*extendFunc)(u32, u32, u32);
15
16template <typename T, typename Allocator = JGadget::TAllocator<T> >
17struct TVector {
20 mAllocator = &allocator;
21 mPtr = ptr;
22 }
23
25
26 void set(T* ptr) { mPtr = ptr; }
27
28 Allocator* mAllocator;
29 T* mPtr;
30 };
31
32 TVector(Allocator const& allocator) {
33 mAllocator = allocator;
34 pBegin_ = NULL;
35 pEnd_ = pBegin_;
36 mCapacity = 0;
38 }
39
41 clear();
42 mAllocator.deallocate(pBegin_, 0);
43 }
44
45 void insert(T* pos, u32 count, const T& val) {
46 if (count != 0) {
47 T* ptr = Insert_raw(pos, count);
48 if (ptr == end()) {
49 /* JGadget_outMessage sp120(JGadget_outMessage::warning, __FILE__, 0x141);
50 sp120 << "can't allocate memory"; */
51 } else {
52 std::uninitialized_fill_n(ptr, count, val);
53 }
54 }
55 }
56
57 T* Insert_raw(T* pIt, u32 pCount) {
58 JUT_ASSERT(446, (pBegin_ <= pIt) && (pIt <= pEnd_));
59
60 T* const pFirst = pIt;
61
62 if (pCount == 0) {
63 return pIt;
64 }
65
66 if (pCount + size() <= mCapacity) {
67 void** newEnd = pFirst + pCount;
68
69 if (newEnd < pEnd_) {
70 void** pOverwrittenValues = pEnd_ - pCount;
71 std::uninitialized_copy(pOverwrittenValues, pEnd_, pEnd_);
72 std::copy_backward(pFirst, pOverwrittenValues, pEnd_);
73 DestroyElement_(pFirst, newEnd);
74
75 pEnd_ += pCount;
76 return pIt;
77 } else {
78 std::uninitialized_copy(pFirst, pEnd_, newEnd);
79 DestroyElement_(pFirst, pEnd_);
80
81 pEnd_ += pCount;
82 return pIt;
83 }
84 }
85
86 u32 newSize = GetSize_extend_(pCount);
87
88 void** newDataPointer = mAllocator.allocate(newSize, 0);
89 if (!newDataPointer) {
90 return end();
91 }
92
93 TDestructed_deallocate_ destructionDeallocator(mAllocator, newDataPointer);
94
95 void** const endOfCopy = std::uninitialized_copy(pBegin_, pFirst, newDataPointer);
96 std::uninitialized_copy(pFirst, pEnd_, endOfCopy + pCount);
97
99 destructionDeallocator.set(pBegin_);
100
101 pEnd_ = newDataPointer + (pEnd_ - pBegin_ + pCount);
102 pBegin_ = newDataPointer;
103 mCapacity = newSize;
104
105 return endOfCopy;
106 }
107
108 T* insert(T* pos, const T& val) {
109 u32 diff = (int)((uintptr_t)pos - (uintptr_t)begin()) / 4;
110 insert(pos, 1, val);
111 return pBegin_ + diff;
112 }
113
114 T* begin() const { return pBegin_; }
115
116 T* end() const { return pEnd_; }
117
118 u32 size() const {
119 if (pBegin_ == 0) {
120 return 0;
121 }
122 return (int)((uintptr_t)pEnd_ - (uintptr_t)pBegin_) / 4;
123 }
124
125 u32 capacity() { return mCapacity; }
126
128 JUT_ASSERT(0x22B, pfnExtend_!=NULL);
129
130 u32 oldSize = size();
131 u32 neededNewSpace = oldSize + count;
132 u32 extendedSize = pfnExtend_(capacity(), oldSize, count);
133
134 return neededNewSpace > extendedSize ? neededNewSpace : extendedSize;
135 }
136
137 void DestroyElement_(T* start, T* end) {
138 for (; start != end; start++) {
139 mAllocator.destroy(start);
140 }
141 }
142
144
145 T* erase(T* start, T* end) {
146 T* vectorEnd = pEnd_;
147 T* ppvVar3 = std::copy(end, vectorEnd, start);
148 DestroyElement_(ppvVar3, pEnd_);
149 pEnd_ = ppvVar3;
150 return start;
151 }
152
153 void clear() { erase(begin(), end()); }
154
155 /* 0x00 */ Allocator mAllocator;
156 /* 0x04 */ T* pBegin_;
157 /* 0x08 */ T* pEnd_;
158 /* 0x0C */ u32 mCapacity;
160};
161
162struct TVector_pointer_void : public TVector<void*, TAllocator<void*> > {
165 const JGadget::TAllocator<void*>& allocator);
166
168
169 void insert(void**, void* const&);
170 void** erase(void**, void**);
171
172 void clear() { erase(begin(), end()); }
173 void push_back(void* const& value) { insert(end(), (void* const&)value); }
174};
175
176template <typename T>
177struct TVector_pointer : TVector_pointer_void {
180
181 const T* begin() const { return (const T*)TVector_pointer_void::begin(); }
182 T* begin() { return (T*)TVector_pointer_void::begin(); }
183
184 const T* end() const { return (const T*)TVector_pointer_void::end(); }
185 T* end() { return (T*)TVector_pointer_void::end(); }
186
187 void push_back(const T& ref) {
188 static_cast<TVector_pointer_void*>(this)->push_back((void* const&)ref);
189 }
190};
191
192} // namespace JGadget
193
194#endif /* STD_VECTOR_H */
unsigned long u32
Definition types.h:12
u32 extend_default(u32, u32, u32)
Definition std-vector.cpp:5
Definition binary.h:8
u32(* extendFunc)(u32, u32, u32)
Definition std-vector.h:14
T * uninitialized_copy(T *first, T *last, T *result)
Definition msl_memory.h:45
OutputIt copy(InputIt first, InputIt last, OutputIt d_first)
Definition algorithm.h:131
BidirectionalIterator2 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result)
Definition algorithm.h:140
ForwardIt uninitialized_fill_n(ForwardIt first, Size count, const T &value)
Definition msl_memory.h:7
@ start
Definition strtoul.c:8
Definition std-memory.h:8
Allocator * mAllocator
Definition std-vector.h:28
T * mPtr
Definition std-vector.h:29
TDestructed_deallocate_(JGadget::TAllocator< T > &allocator, T *ptr)
Definition std-vector.h:19
void set(T *ptr)
Definition std-vector.h:26
~TDestructed_deallocate_()
Definition std-vector.h:24
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:172
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:12
void push_back(void *const &value)
Definition std-vector.h:173
T * begin()
Definition std-vector.h:182
const T * begin() const
Definition std-vector.h:181
T * end()
Definition std-vector.h:185
~TVector_pointer()
Definition std-vector.h:179
const T * end() const
Definition std-vector.h:184
void push_back(const T &ref)
Definition std-vector.h:187
TVector_pointer(const TAllocator< void * > &allocator)
Definition std-vector.h:178
Definition vector.h:18
u32 GetSize_extend_(u32 count)
Definition std-vector.h:127
void insert(T *pos, u32 count, const T &val)
Definition std-vector.h:45
TVector(Allocator const &allocator)
Definition std-vector.h:32
T * Insert_raw(T *pIt, u32 pCount)
Definition std-vector.h:57
void DestroyElement_(T *start, T *end)
Definition std-vector.h:137
Allocator mAllocator
Definition std-vector.h:155
T * insert(T *pos, const T &val)
Definition std-vector.h:108
u32 capacity()
Definition std-vector.h:125
T * pEnd_
Definition std-vector.h:157
u32 mCapacity
Definition std-vector.h:158
void DestroyElement_all_()
Definition std-vector.h:143
void clear()
Definition std-vector.h:153
u32 size() const
Definition std-vector.h:118
T * begin() const
Definition std-vector.h:114
T * end() const
Definition std-vector.h:116
T * pBegin_
Definition std-vector.h:156
T * erase(T *start, T *end)
Definition std-vector.h:145
~TVector()
Definition std-vector.h:40
extendFunc pfnExtend_
Definition std-vector.h:159