forked from MSRevive/MasterSwordRebirth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVGUI_Dar.h
More file actions
194 lines (179 loc) · 3.62 KB
/
Copy pathVGUI_Dar.h
File metadata and controls
194 lines (179 loc) · 3.62 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
//========= Copyright © 1996-2002, Valve LLC, All rights reserved. ============
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================
#ifndef VGUI_DAR_H
#define VGUI_DAR_H
#include<stdlib.h>
#include<string.h>
#include<VGUI.h>
namespace vgui
{
//Simple lightweight dynamic array implementation
template<class ELEMTYPE> class VGUIAPI Dar
{
public:
Dar()
{
_count=0;
_capacity=0;
_data=null;
ensureCapacity(4);
}
Dar(int initialCapacity)
{
_count=0;
_capacity=0;
_data=null;
ensureCapacity(initialCapacity);
}
public:
void ensureCapacity(int wantedCapacity)
{
if(wantedCapacity<=_capacity){return;}
//double capacity until it is >= wantedCapacity
//this could be done with math, but iterative is just so much more fun
int newCapacity=_capacity;
if(newCapacity==0){newCapacity=1;}
while(newCapacity<wantedCapacity){newCapacity*=2;}
//allocate and zero newData
ELEMTYPE* newData=new ELEMTYPE[newCapacity];
if(newData==null){exit(0);return;}
memset(newData,0,sizeof(ELEMTYPE)*newCapacity);
_capacity=newCapacity;
//copy data into newData
for(int i=0;i<_count;i++){newData[i]=_data[i];}
delete[] _data;
_data=newData;
}
void setCount(int count)
{
if((count<0)||(count>_capacity))
{
return;
}
_count=count;
}
unsigned int getCount()
{
return _count;
}
void addElement(ELEMTYPE elem)
{
ensureCapacity(_count+1);
_data[_count]=elem;
_count++;
}
bool hasElement(ELEMTYPE elem)
{
for(int i=0;i<_count;i++)
{
if(_data[i]==elem)
{
return true;
}
}
return false;
}
void putElement(ELEMTYPE elem)
{
if(hasElement(elem))
{
return;
}
addElement(elem);
}
void insertElementAt(ELEMTYPE elem,int index)
{
if((index<0)||(index>_count))
{
return;
}
if((index==_count)||(_count==0))
{
addElement(elem);
}
else
{
addElement(elem); //just to make sure it is big enough
for(int i=_count-1;i>index;i--)
{
_data[i]=_data[i-1];
}
_data[index]=elem;
}
}
void setElementAt(ELEMTYPE elem,int index)
{
if((index<0)||(index>=_count))
{
return;
}
_data[index]=elem;
}
void removeElementAt(int index)
{
if((index<0)||(index>=_count))
{
return;
}
//slide everything to the right of index, left one.
for(int i=index;i<(_count-1);i++)
{
_data[i]=_data[i+1];
}
_count--;
}
void removeElement(ELEMTYPE elem)
{
for(int i=0;i<_count;i++)
{
if(_data[i]==elem)
{
removeElementAt(i);
break;
}
}
}
void removeAll()
{
_count=0;
}
ELEMTYPE operator[](int index)
{
if((index<0)||(index>=_count))
{
return null;
}
return _data[index];
}
protected:
int _count;
int _capacity;
ELEMTYPE* _data;
};
#ifdef _WIN32
//forward referencing all the template types used so they get exported
template class VGUIAPI Dar<char>;
template class VGUIAPI Dar<char*>;
template class VGUIAPI Dar<int>;
template class VGUIAPI Dar<class Button*>;
template class VGUIAPI Dar<class SurfaceBase*>;
template class VGUIAPI Dar<class InputSignal*>;
template class VGUIAPI Dar<class FocusChangeSignal*>;
template class VGUIAPI Dar<class FrameSignal*>;
template class VGUIAPI Dar<class ActionSignal*>;
template class VGUIAPI Dar<class IntChangeSignal*>;
template class VGUIAPI Dar<class TickSignal*>;
template class VGUIAPI Dar<class Dar<char>*>;
template class VGUIAPI Dar<class Frame*>;
template class VGUIAPI Dar<class DesktopIcon*>;
template class VGUIAPI Dar<class ChangeSignal*>;
template class VGUIAPI Dar<class Panel*>;
template class VGUIAPI Dar<class Label*>;
template class VGUIAPI Dar<class RepaintSignal*>;
#endif
}
#endif