-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArray.h
More file actions
249 lines (217 loc) · 5.39 KB
/
Array.h
File metadata and controls
249 lines (217 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// ======================================================================================
// File : Array.h
// Author : Li Chen
// Last Change : 07/29/2010 | 15:45:30 PM | Thursday,July
// Description :
// ======================================================================================
#ifndef __GAMESERVICE_ARRAY_H__
#define __GAMESERVICE_ARRAY_H__
#include "Customize/CusMemory.h"
// ========================================================
// Trigger
// if use vector choose std or other
// ========================================================
#undef GS_USE_STD_VECTOR
#define GS_USE_STD_VECTOR
// define vector:
#ifdef GS_USE_STD_VECTOR
// use std::vector
#include <vector>
#if defined(_PS3)
//typename std::vector<T>;
#define GS_VECTOR typename std::vector<T>
#else
#define GS_VECTOR std::vector<T>
#endif
#else
// or engine vector
#include <efd/Foundation.h>
#if defined(_PS3)
#define GS_VECTOR typename efd::vector<T>
#else
#define GS_VECTOR efd::vector<T>
#endif
#endif
namespace GameService
{
#if defined(_XBOX) || defined(_XENON)
#define _FORCE_INLINE //__forceinline
#elif defined(_PS3)
#define _FORCE_INLINE inline
#else
#define _FORCE_INLINE __forceinline
#endif
enum {INDEX_NONE = -1 };
template< class T > class TArray
{
public:
GS_VECTOR& GetData()
{
return m_Vector;
}
_FORCE_INLINE TArray()
{
m_Vector.clear();
}
_FORCE_INLINE TArray( GS_INT InNum )
{
m_Vector.reserve(InNum);
}
_FORCE_INLINE TArray(const TArray& Other) : TArray()
{
Copy(Other);
}
virtual ~TArray()
{
m_Vector.clear();
}
// common functions
_FORCE_INLINE GS_DWORD GetTypeSize() const
{
return sizeof(T);
}
_FORCE_INLINE T& operator()( GS_UINT i )
{
GS_Assert((i>=0));
GS_Assert(i<m_Vector.size()||m_Vector.size()==0); // m_ArrayNum==0 is workaround for &MyArray(0) abuse
return m_Vector[i];
}
_FORCE_INLINE const T& operator()( GS_UINT i ) const
{
GS_Assert(i>=0);
GS_Assert(i<m_Vector.size()||m_Vector.size()==0); // m_ArrayNum==0 is workaround for &MyArray(0) abuse
return m_Vector[i];
}
_FORCE_INLINE T& Last( GS_INT c=0 )
{
GS_Assert(m_Vector.size()>0);
GS_Assert(c<m_Vector.size());
return m_Vector.back();
}
_FORCE_INLINE const T& Last( GS_INT c=0 ) const
{
GS_Assert(m_Vector.size()>0);
GS_Assert(c<m_Vector.size());
return m_Vector.back();
}
_FORCE_INLINE GS_BOOL FindItem( const T& Item, GS_INT& Index ) const
{
GS_VECTOR::iterator it;
for( it=m_Vector.begin(); it != m_Vector.end(); it++ )
if( *it == Item )
return 1;
return 0;
}
_FORCE_INLINE GS_INT FindItemIndex( const T& Item ) const
{
GS_VECTOR::iterator it;
GS_INT index = 0;
for( it=m_Vector.begin(); it != m_Vector.end(); it++,index++ )
if( *it == Item )
return index;
return INDEX_NONE;
}
_FORCE_INLINE GS_BOOL ContainsItem( const T& Item ) const
{
return ( FindItemIndex(Item) != INDEX_NONE );
}
GS_BOOL operator==(const TArray<T>& OtherArray) const
{
return (m_Vector == OtherArray.m_Vector);
}
GS_BOOL operator!=(const TArray<T>& OtherArray) const
{
return (m_Vector == OtherArray.m_Vector);
}
TArray<T>& operator+=( const TArray<T>& Other )
{
Append( Other );
return *this;
}
TArray<T>& operator=( const TArray<T>& Other )
{
Copy( Other );
return *this;
}
// Add, Insert, Remove, Empty interface.
_FORCE_INLINE GS_BOOL IsValidIndex( GS_INT i ) const
{
return i>=0 && i<m_Vector.size();
}
_FORCE_INLINE GS_UINT Num() const
{
GS_Assert(m_Vector.size()>=0);
return m_Vector.size();
}
void Shrink( GS_INT ElementSize )
{
GS_Assert(m_Vector.size() > 0);
if (m_Vector.size() != m_Vector.capacity())
{
m_Vector.resize(m_Vector.size());
}
}
void Remove( GS_UINT Index, GS_UINT Count=1 )
{
GS_Assert(Index>=0);
GS_Assert(Index<=m_Vector.size());
GS_Assert(Index+Count<=m_Vector.size());
for (GS_UINT i=0;i<Count; i++)
{
m_Vector.erase(m_Vector.begin()+Index);
}
}
void Empty( GS_INT Slack=0 )
{
m_Vector.clear();
GS_Assert(m_Vector.empty());
}
GS_BOOL IsEmpty()
{
return (m_Vector.empty());
}
void Append(const TArray<T>& Source)
{
for (GS_INT i=0;i<Source.Num();i++)
{
m_Vector.push_back(Source(i));
}
}
GS_INT AddItem( const T& Item )
{
m_Vector.push_back(Item);
return m_Vector.size()-1;
}
GS_INT AddUniqueItem( const T& Item )
{
GS_INT index=0;
GS_VECTOR::iterator it;
for( it=m_Vector.begin(); it!=m_Vector.end(); it++,index++ )
if( *it==Item )
return index;
return AddItem( Item );
}
void Reserve(GS_INT Number)
{
m_Vector.reserve(Number);
}
GS_INT RemoveItem( const T& Item )
{
GS_INT OriginalNum=m_Vector.size();
GS_INT index=0;
GS_VECTOR::iterator it;
for( it=m_Vector.begin(); it!=m_Vector.end(); it++,index++ )
if( *it==Item )
m_Vector.erase(m_Vector.begin()+index);
return OriginalNum - m_Vector.size();
}
protected:
void Copy(const TArray<T>& Source)
{
m_Vector = Source.m_Vector;
}
private:
std::vector<T> m_Vector;
};
} // namespace GameService
#endif // __GAMESERVICE_ARRAY_H__