This repository was archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathklib.array.h
More file actions
395 lines (290 loc) · 11.5 KB
/
klib.array.h
File metadata and controls
395 lines (290 loc) · 11.5 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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#ifndef KLIB_ARRAY_H
#define KLIB_ARRAY_H
template<class any>
class Array {
friend bool isArray();
private:
/* The encrypted data of array class. */
any *_proto_;
public:
/* The length property sets or returns the number of elements in an array. */
unsigned length;
Array();
Array(const std::initializer_list<any> &);
Array(const Array<any> &);
~Array();
any& operator[] (const unsigned &);
Array<any> operator= (const std::initializer_list<any> &);
Array<any> operator= (const Array<any> &);
/* The method is used to join two or more arrays. */
Array<any> concat(const Array<any> &);
/* The method copies array elements to another position in the array, overwriting the existing values. */
/* Note: this method overwrites the original array. */
Array<any> copyWithin(unsigned, const unsigned=0, unsigned=-1);
/* The method checks if all elements in an array pass a test (provided as a function). */
bool every(bool (*)(any,unsigned,Array<any>));
/* The method fills the specified elements in an array with a static value. */
Array<any> fill(const any &, const unsigned=0, unsigned=-1);
/* The method creates an array filled with all array elements that pass a test (provided as a function). */
Array<any> filter(bool (*)(any,unsigned,Array<any>), const Array<any> & = {});
/* The method returns the value of the first element in an array that pass a test (provided as a function). */
any find(bool (*)(any,unsigned,Array<any>), const any & = NULL);
/* The method returns the index of the first element in an array that pass a test (provided as a function). */
int findIndex(bool (*)(any,unsigned,Array<any>), const int & = -1);
/* The method determines whether an array contains a specified element. */
bool includes(const any &, const unsigned=0);
/* The method searches the array for the specified item, and returns its position. */
int indexOf(const any &, const unsigned=0);
/* The method returns the array as a string. */
char* join(const char * ="'");
/* The method returns an Array Iterator object with the keys of an array. */
Array<unsigned> keys();
/* The method searches the array for the specified item, and returns its position. */
int lastIndexOf(const any &, unsigned=-1);
/* The method creates a new array with the results of calling a function for every array element. */
Array<any> map(any (*)(any,unsigned,Array<any>), int);
/* The method executes a provided function for each value of the array (from left-to-right). */
any reduce(any (*)(any));
/* The reduce the values of an array to a single value (going right-to-left) */
any reduceRight(any (*)(any));
/* The method reverses the order of the elements in an array. */
Array<any> reverse();
/* The method returns the selected elements in an array, as a new array object. */
Array<any> slice(const unsigned=0, unsigned=-1);
/* The method sorts the items of an array. */
Array<any> sort();
/* The method adds/removes items to/from an array, and returns the removed item(s). */
/* Note: This method changes the original array. */
Array<any> splice(const unsigned &, const Array<any> & = {});
Array<any> splice(const unsigned &, const unsigned &, const Array<any> & = {});
/* The method returns a string with all the array values, separated by commas. */
char* toString();
/* method returns the array. */
Array<any> values();
/* The method adds new items to the end of an array, and returns the new length. */
unsigned push(const Array<any> &);
unsigned push(const any &);
/* The method removes the last element of an array, and returns that element. */
any pop();
/* The method adds new items to the beginning of an array, and returns the new length. */
unsigned unshift(const Array<any> &);
unsigned unshift(const any &);
/* The method removes the first item of an array, and returns that element. */
any shift();
};
/* constructor */
template<class any>
Array<any>::Array() {
length = 0;
_proto_ = NULL;
}
template<class any>
Array<any>::Array(const std::initializer_list<any> &list) {
_proto_ = new any[length = list.size()]; // list.end()-list.begin()
for (unsigned i = 0; i < length; i++) _proto_[i] = *(list.begin()+i);
}
template<class any>
Array<any>::Array(const Array<any> &list) {
_proto_ = new any[length = list.length];
for (unsigned i = 0; i < length; i++) _proto_[i] = list._proto_[i];
}
template<class any>
Array<any>::~Array() {
delete[] _proto_;
}
/* call operators */
template<class any>
any& Array<any>::operator[] (const unsigned &index) { return _proto_[index]; }
/* processing operators: FRIEND */
/* processing operators: OVERLOAD */
template<class any>
Array<any> Array<any>::operator= (const std::initializer_list<any> &list) {
delete[] _proto_;
_proto_ = new any[length = list.size()];
for (unsigned i = 0; i < length; i++) _proto_[i] = *(list.begin()+i);
return *this;
}
template<class any>
Array<any> Array<any>::operator= (const Array<any> &list) {
delete[] _proto_;
_proto_ = new any[length = list.length];
for (unsigned i = 0; i < length; i++) _proto_[i] = list._proto_[i];
return *this;
}
/* class methods: PRIVATE */
/* class methods: FRIEND */
/* class methods: BUILT-IN */
template<class any>
Array<any> Array<any>::concat(const Array<any> &items) {
any result[length+items.length];
for (unsigned i = 0; i < length; i++) result[i] = _proto_[i];
for (unsigned i = 0; i < items.length; i++) result[length+i] = items[i];
return result;
}
template<class any>
Array<any> Array<any>::copyWithin(unsigned target, const unsigned start, unsigned end) {
if (end == -1 || end > length) end = length;
//++ FIX
return *this;
}
template<class any>
bool Array<any>::every(bool (*function)(any,unsigned,Array<any>)) {
for (unsigned i = 0; i < length; i++) if (!(*function)(_proto_[i],i,*this)) return false;
return true;
}
template<class any>
Array<any> Array<any>::fill(const any &value, const unsigned start, unsigned end) {
if (end == -1 || end > length) end = length;
for (unsigned i = start; i < end; i++) _proto_[i] = value;
return *this;
}
template<class any>
Array<any> Array<any>::filter(bool (*function)(any,unsigned,Array<any>), const Array<any> &thisValue) {
unsigned count = 0;
for (unsigned i = 0; i < length; i++) if (!(*function)(_proto_[i],i,*this)) count++;
if (!count) thisValue;
any result[count];
count = 0;
for (unsigned i = 0; i < length; i++) if (!(*function)(_proto_[i],i,*this)) result[count++] = _proto_[i];
return result;
}
template<class any>
any Array<any>::find(bool (*function)(any,unsigned,Array<any>), const any &thisValue) {
for (unsigned i = 0; i < length; i++) if ((*function)(_proto_[i],i,*this)) return _proto_[i];
return thisValue;
}
template<class any>
int Array<any>::findIndex(bool (*function)(any,unsigned,Array<any>), const int &thisValue) {
for (unsigned i = 0; i < length; i++) if ((*function)(_proto_[i],i,*this)) return i;
return thisValue;
}
template<class any>
bool Array<any>::includes(const any &element, const unsigned start) {
for (unsigned i = start; i < length; i++) if (_proto_[i] == element) return true;
return false;
}
template<class any>
int Array<any>::indexOf(const any &item, const unsigned start) {
for (unsigned i = start; i < length; i++) if (_proto_[i] == item) return i;
return -1;
}
template<class any>
char* Array<any>::join(const char *separator) {
}
template<class any>
Array<unsigned> Array<any>::keys() {
}
template<class any>
int Array<any>::lastIndexOf(const any &item, unsigned atlength) {
}
template<class any>
Array<any> Array<any>::map(any (*function)(any,unsigned,Array<any>), int) {
}
template<class any>
any Array<any>::reduce(any (*function)(any)) {
}
template<class any>
any Array<any>::reduceRight(any (*function)(any)) {
}
template<class any>
Array<any> Array<any>::reverse() {
}
template<class any>
Array<any> Array<any>::slice(const unsigned start, unsigned end) {
if (end == -1) end = length;
if (end-start <= 0) return {};
any result[end-start];
unsigned j = 0;
for (unsigned i = start; i < end; i++) result[j++] = _proto_[i];
return result;
}
template<class any>
Array<any> Array<any>::sort() {
}
template<class any>
Array<any> Array<any>::splice(const unsigned &index, const Array<any> &items) {
any *old = _proto_;
_proto_ = new any[length+items.length];
for (unsigned i = 0; i < index; i++) _proto_[i] = old[i];
for (unsigned i = 0; i < items.length; i++) _proto_[index+i] = items[i];
for (unsigned i = index; i < length; i++) _proto_[items.length+i] = old[i];
length += items.length;
delete[] old;
return *this;
}
template<class any>
Array<any> Array<any>::splice(const unsigned &index, const unsigned &howmany, const Array<any> &items) {
any *old = _proto_;
_proto_ = new any[length + items.length - (howmany > (length - index) ? length - index : howmany)];
for (unsigned i = 0; i < index; i++) _proto_[i] = old[i];
for (unsigned i = 0; i < items.length; i++) _proto_[index+i] = items[i];
for (unsigned i = index+howmany; i < length; i++) _proto_[items.length+i] = old[i];
length += items.length - (howmany > (length - index) ? length - index : howmany);
delete[] old;
return *this;
}
template<class any>
char* Array<any>::toString() {
}
template<class any>
Array<any> Array<any>::values() {
}
template<class any>
unsigned Array<any>::push(const Array<any> &items) {
any *old = _proto_;
_proto_ = new any[length+items.length];
for (unsigned i = 0; i < length; i++) _proto_[i] = old[i];
for (unsigned i = 0; i < items.length; i++) _proto_[length+i] = items[i];
length += items.length;
delete[] old;
return length;
}
template<class any>
unsigned Array<any>::push(const any &item) {
any *old = _proto_;
_proto_ = new any[++length];
for (unsigned i = 0; i < length-1; i++) _proto_[i] = old[i];
_proto_[length-1] = item;
delete[] old;
return length;
}
template<class any>
any Array<any>::pop() {
any itemToReturn = _proto_[--length];
any *old = _proto_;
_proto_ = new any[length];
for (unsigned i = 0; i < length; i++) _proto_[i] = old[i];
delete[] old;
return itemToReturn;
}
template<class any>
unsigned Array<any>::unshift(const Array<any> &items) {
any *old = _proto_;
_proto_ = new any[length+items.length];
for (unsigned i = 0; i < items.length; i++) _proto_[i] = items[i];
for (unsigned i = 0; i < length; i++) _proto_[items.length+i] = old[i];
length += items.length;
delete[] old;
return length;
}
template<class any>
unsigned Array<any>::unshift(const any &item) {
any *old = _proto_;
_proto_ = new any[++length];
for (unsigned i = 1; i < length; i++) _proto_[i] = old[i];
_proto_[0] = item;
delete[] old;
return length;
}
template<class any>
any Array<any>::shift() {
any itemToReturn = _proto_[0];
any *old = _proto_;
_proto_ = new any[--length];
for (unsigned i = 0; i < length; i++) _proto_[i] = old[i+1];
delete[] old;
return itemToReturn;
}
template<class any>
using array = Array<any>;
#endif