forked from NTDLS/NSWFL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSWFL_StringBuilderArray.H
342 lines (295 loc) · 8.01 KB
/
NSWFL_StringBuilderArray.H
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright © NetworkDLS 2023, All rights reserved
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _NSWFL_STRINGBUILDERARRAY_H_
#define _NSWFL_STRINGBUILDERARRAY_H_
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "NSWFL.H"
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace NSWFL {
namespace String {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class StringBuilderArray
{
public:
StringBuilder **_Items;
int _Allocated;
int _Used;
public:
StringBuilderArray()
{
this->_Allocated = 10;
this->_Used = 0;
this->_Items = (StringBuilder **)calloc(this->_Allocated, sizeof(StringBuilder *));
}
~StringBuilderArray()
{
this->Clear();
this->_Allocated = 0;
this->_Used = 0;
free(this->_Items);
}
//Returns the count of used allocation (slots).
int Count()
{
return this->_Used;
}
//Returns the number of allocations (slots).
int Allocated()
{
return this->_Allocated;
}
//Adds a new item to the vector.
StringBuilder *Add(char *value)
{
return AddGetIndex(value, (int)strlen(value), NULL);
}
//Adds a new item to the vector.
StringBuilder *Add(char *value, int length)
{
return AddGetIndex(value, length, NULL);
}
//Adds a new item to the vector.
StringBuilder *AddEmpty(int *outIndex)
{
if (this->_Used == this->_Allocated)
{
//Keep a 10 percent overage for allocation efficiency.
this->_Allocated += (int)(this->_Allocated > 100 ? (this->_Allocated / 10.0f) : 10.0f);
this->_Items = (StringBuilder **)realloc(this->_Items, this->_Allocated * sizeof(StringBuilder *));
}
this->_Items[this->_Used] = (StringBuilder *)calloc(1, sizeof(StringBuilder *));
if (outIndex)
{
*outIndex = this->_Used;
}
this->_Items[this->_Used] = (StringBuilder *)calloc(1, sizeof(StringBuilder *));
return this->_Items[this->_Used++];
}
//Adds a new item to the vector.
StringBuilder *AddGetIndex(char *value, int length, int *outIndex)
{
if (this->_Used == this->_Allocated)
{
//Keep a 10 percent overage for allocation efficiency.
this->_Allocated += (int)(this->_Allocated > 100 ? (this->_Allocated / 10.0f) : 10.0f);
this->_Items = (StringBuilder **)realloc(this->_Items, this->_Allocated * sizeof(StringBuilder *));
}
this->_Items[this->_Used] = (StringBuilder *)calloc(length + 1, 1);
if (value)
{
this->_Items[this->_Used] = new StringBuilder(value, length);
}
else
{
this->_Items[this->_Used] = new StringBuilder();
}
if (outIndex)
{
*outIndex = this->_Used;
}
return this->_Items[this->_Used++];
}
void Compact()
{
this->_Allocated = this->_Used;
this->_Items = (StringBuilder **)realloc(this->_Items, this->_Allocated * sizeof(StringBuilder *));
}
//Deletes all values from the vector matching on value.
//Returns the count of items which were deleted.
int DeleteAll(char *value)
{
int iCount = 0;
for (int i = this->_Used - 1; i >= 0 && this->_Used > 0;)
{
if (strcmp(value, this->_Items[i]->Buffer) == 0)
{
this->DeleteAt(i);
iCount++;
}
else
{
i--;
}
}
return iCount;
}
//Deletes a value from the vector.
//Returns true if an item was found and removed.
bool DeleteOne(char *value)
{
for (int i = this->_Used - 1; i >= 0; i--)
{
if (strcmp(value, this->_Items[i]->Buffer) == 0)
{
this->DeleteAt(i);
return true;
}
}
return false;
}
//Determines if a given value is in the vector.
//Returns true if an item was found, otherwise false.
bool Contiains(char *value)
{
for (int i = this->_Used - 1; i >= 0; i--)
{
if (strcmp(value, this->_Items[i]->Buffer) == 0)
{
return true;
}
}
return false;
}
//Determines if a given value is in the vector.
//Returns true if an item was found, otherwise false.
bool ContiainsI(char *value)
{
for (int i = this->_Used - 1; i >= 0; i--)
{
if (_strcmpi(value, this->_Items[i]->Buffer) == 0)
{
return true;
}
}
return false;
}
//Determines if a given value is in the vector.
//Returns true if an item was found, otherwise false.
bool ContiainsNI(char *value, int iLenght)
{
for (int i = this->_Used - 1; i >= 0; i--)
{
if (_strnicmp(value, this->_Items[i]->Buffer, iLenght) == 0)
{
return true;
}
}
return false;
}
//Determines if a given value is in the vector.
//Returns true if an item was found, otherwise false.
bool ContiainsN(char *value, int iLenght)
{
for (int i = this->_Used - 1; i >= 0; i--)
{
if (strncmp(value, this->_Items[i]->Buffer, iLenght) == 0)
{
return true;
}
}
return false;
}
//Finds the index of a given value.
//Returns the index of the found value, otherwise returns -1.
int IndexOf(char *value)
{
for (int i = this->_Used - 1; i >= 0; i--)
{
if (strcmp(value, this->_Items[i]->Buffer) == 0)
{
return i;
}
}
return -1;
}
//Deletes a value from the vector by it's index.
//Returns true if an item was deleted.
bool DeleteAt(int index)
{
if (index >= 0 && index < this->_Used)
{
delete this->_Items[index];
this->_Used--;
if (this->_Used > 0)
{
for (int i = index; i < this->_Used; i++)
{
this->_Items[i] = this->_Items[i + 1];
}
}
}
return false;
}
//Deletes all items in the vector.
void Clear()
{
while (this->_Used != 0)
{
delete this->_Items[this->_Used - 1];
this->_Used--;
}
}
//Returns a pointer to the value by index.
StringBuilder *at(int index)
{
if (index >= 0 && index < this->_Used)
{
return this->_Items[index];
}
return (StringBuilder *)NULL;
}
char **ToArray()
{
return ToStringArray(NULL);
}
char **ToStringArray(int *iNumberOfElements)
{
char **sArray = (char **)calloc(this->_Used, sizeof(char *));
if (iNumberOfElements != NULL)
{
*iNumberOfElements = this->_Used;
}
for (int i = 0; i < _Used; i++)
{
sArray[i] = (char *)calloc(this->_Items[i]->Length + 1, 1);
strcpy_s(sArray[i], this->_Items[i]->Length + 1, this->_Items[i]->Buffer);
}
return sArray;
}
void SortAscending()
{
bool bItemsMoved = true;
while (bItemsMoved)
{
bItemsMoved = false;
for (int i = 0; i < this->_Used - 1; i++)
{
if (this->_Items[i]->StrCmp(this->_Items[i + 1]) > 0)
{
StringBuilder *temp = this->_Items[i];
this->_Items[i] = this->_Items[i + 1];
this->_Items[i + 1] = temp;
bItemsMoved = true;
}
}
}
}
void SortDecending()
{
bool bItemsMoved = true;
while (bItemsMoved)
{
bItemsMoved = false;
for (int i = 0; i < this->_Used - 1; i++)
{
if (this->_Items[i]->StrCmp(this->_Items[i + 1]) < 0)
{
StringBuilder *temp = this->_Items[i];
this->_Items[i] = this->_Items[i + 1];
this->_Items[i + 1] = temp;
bItemsMoved = true;
}
}
}
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} //namespace::String
} //namespace::NSWFL
#endif