forked from OpenKore/openkore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataStructures.pm
More file actions
495 lines (457 loc) · 11.2 KB
/
DataStructures.pm
File metadata and controls
495 lines (457 loc) · 11.2 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#########################################################################
# OpenKore - Utility Functions
#
# Copyright (c) 2004,2005,2006,2007 OpenKore Development Team
#
# This software is open source, licensed under the GNU General Public
# License, version 2.
# Basically, this means that you're allowed to modify and distribute
# this software. However, if you distribute modified versions, you MUST
# also distribute the source code.
# See http://www.gnu.org/licenses/gpl.html for the full license.
#########################################################################
##
# MODULE DESCRIPTION: Utility functions for data structure manipulation
#
# Various functions for manipulating data structures.
package Utils::DataStructures;
use strict;
use Exporter;
use base qw(Exporter);
use FastUtils;
our %EXPORT_TAGS = (
# Manipulation functions for arrays which can contain "holes".
arrays => [qw( binAdd binFind binFindReverse binRemove binRemoveAndShift
binRemoveAndShiftByIndex binSize )],
# Functions for searching in arrays and nested data structures.
finders => [qw( existsInList findIndex findIndexString findIndexString_lc findIndexString_lc_not_equip
findIndexStringList_lc findLastIndex findKey findKeyString )],
# Misc array functions.
misc => [qw( compactArray hashCopyByKey minHeapAdd shuffleArray )]
);
our @EXPORT_OK = (
@{$EXPORT_TAGS{arrays}},
@{$EXPORT_TAGS{finders}},
@{$EXPORT_TAGS{misc}}
);
$EXPORT_TAGS{all} = \@EXPORT_OK;
##
# void binAdd(Array* array, ID)
# array: a reference to an array.
# ID: the element to add to @array.
# Requires: defined($array)
# Returns: the index of the added element.
#
# Add $ID to the first empty slot (that is, a slot which is set to undef)
# in @array, or append it to the end of @array if there are no empty
# slots.
#
# Example:
# @list = ("ID1", undef, "ID2");
# binAdd(\@list, "New"); # --> Returns: 1
# # Result:
# # $list[0] eq "ID1"
# # $list[1] eq "New"
# # $list[2] eq "ID2"
#
# @list = ("ID1", "ID2");
# binAdd(\@list, "New"); # --> Returns: 2
# # Result:
# # $list[0] eq "ID1"
# # $list[1] eq "ID2"
# # $list[2] eq "New"
sub binAdd {
my $r_array = shift;
my $ID = shift;
my $i;
for ($i = 0; $i <= @{$r_array};$i++) {
if ($$r_array[$i] eq "") {
$$r_array[$i] = $ID;
return $i;
}
}
}
##
# binFind(Array *array, ID)
# array: a reference to an array.
# ID: the element to search for.
# Returns: the index of the element for $ID, or undef is $ID
# is not an element in $array.
#
# Look for element $ID in $array.
#
# Example:
# our @array = ("hello", "world", "!");
# binFind(\@array, "world"); # => 1
# binFind(\@array, "?"); # => undef
# This function is written in src/auto/XSTools/misc/fastutils.xs
##
# binFindReverse(Array *array, ID)
#
# Same as binFind() but starts looking from the end of the array
# instead of from the beginning.
sub binFindReverse {
my $r_array = shift;
my $ID = shift;
my $i;
for ($i = @{$r_array} - 1; $i >= 0;$i--) {
if ($$r_array[$i] eq $ID) {
return $i;
}
}
}
##
# void binRemove(Array *array, ID)
# array: a reference to an array
# ID: the element to remove.
#
# Find a value in $array which has the same value as $ID,
# and remove it.
#
# Example:
# our @array = ("hello", "world", "!");
# # Same as: delete $array[1];
# binRemove(\@array, "world");
sub binRemove {
my $r_array = shift;
my $ID = shift;
my $i;
for ($i = 0; $i < @{$r_array};$i++) {
if ($$r_array[$i] eq $ID) {
# "WARNING: Calling delete on array values is deprecated and likely to be removed in a future version of Perl."
delete $$r_array[$i];
last;
}
}
}
sub binRemoveAndShift {
my $r_array = shift;
my $ID = shift;
my $found;
my $i;
my @newArray;
for ($i = 0; $i < @{$r_array};$i++) {
if ($$r_array[$i] ne $ID || $found ne "") {
push @newArray, $$r_array[$i];
} else {
$found = $i;
}
}
@{$r_array} = @newArray;
return $found;
}
sub binRemoveAndShiftByIndex {
my $r_array = shift;
my $index = shift;
my $found;
my $i;
my @newArray;
for ($i = 0; $i < @{$r_array};$i++) {
if ($i != $index) {
push @newArray, $$r_array[$i];
} else {
$found = 1;
}
}
@{$r_array} = @newArray;
return $found;
}
##
# int binSize(Array *array)
# r_array: a reference to an array.
#
# Calculates the size of $array, excluding undefined values.
#
# Example:
# our @array = ("hello", undef, "world");
# scalar @array; # -> 3
# binSize(\@array); # -> 2
sub binSize {
my $r_array = shift;
return 0 if !defined $r_array;
my $found = 0;
my $i;
for ($i = 0; $i < @{$r_array};$i++) {
if ($$r_array[$i] ne "") {
$found++;
}
}
return $found;
}
##
# void compactArray(Array *array)
#
# Resize an array by removing undefined items.
sub compactArray {
my ($array) = @_;
for (my $i = $#{$array}; $i >= 0; $i--) {
if (!defined $array->[$i]) {
splice @{$array}, $i, 1;
}
}
}
##
# boolean existsInList(list, val)
# list: a string containing a list of comma-seperated items.
# val: the value to check for.
#
# Check whether $val exists in $list. This function is case-insensitive.
#
# Example:
# my $list = "Apple,Orange Juice";
# existsInList($list, "apple"); # => 1
# existsInList($list, "Orange Juice"); # => 1
# existsInList($list, "juice"); # => 0
sub existsInList {
my ($list, $val) = @_;
return 0 if ($val eq "");
my @array = split / *, */, $list;
$val = lc($val);
foreach (@array) {
s/^\s+//;
s/\s+$//;
s/\s+/ /g;
next if ($_ eq "");
return 1 if (lc($_) eq $val);
}
return 0;
}
##
# findIndex(r_array, key, [num])
# r_array: A reference to an array, which contains a hash in each element.
# key: The name of the key to lookup.
# num: The number to compare with.
# Returns: The index in r_array of the found item, or undef if not found. Or, if not found and $num is not given: returns the index of first undefined array element, or the index of the last array element + 1.
#
# This function loops through the entire array, looking for a hash item whose
# value equals $num.
#
# Example:
# my @inventory;
# $inventory[0]{amount} = 50;
# $inventory[1]{amount} = 100;
# $inventory[2] = undef;
# $inventory[3]{amount} = 200;
# findIndex(\@inventory, "amount", 100); # 1
# findIndex(\@inventory, "amount", 99); # undef
# findIndex(\@inventory, "amount"); # 2
#
# @inventory = ();
# $inventory[0]{amount} = 50;
# findIndex(\@inventory, "amount"); # 1
sub findIndex {
my $r_array = shift;
return undef if !$r_array;
my $key = shift;
my $num = shift;
if ($num ne "") {
my $max = @{$r_array};
for (my $i = 0; $i < $max; $i++) {
return $i if (exists $r_array->[$i] && $r_array->[$i]{$key} == $num);
}
return undef;
} else {
my $max = @{$r_array};
my $i;
for ($i = 0; $i < $max; $i++) {
return $i if (!$r_array->[$i] || !%{$r_array->[$i]});
}
return $i;
}
}
##
# findIndexString(r_array, key, [str])
#
# Same as findIndex(), except this function compares strings, not numbers.
sub findIndexString {
my $r_array = shift;
return undef if !$r_array;
my $key = shift;
my $str = shift;
if ($str ne "") {
my $max = @{$r_array};
for (my $i = 0; $i < $max; $i++) {
if (exists $r_array->[$i] && $r_array->[$i]{$key} eq $str) {
return $i;
}
}
return undef;
} else {
my $max = @{$r_array};
my $i;
for ($i = 0; $i < $max; $i++) {
return $i if (!$r_array->[$i] || !%{$r_array->[$i]});
}
return $i;
}
}
##
# findIndexString_lc(r_array, key, [str])
#
# Same has findIndexString(), except this function does case-insensitive string comparisons.
sub findIndexString_lc {
my $r_array = shift;
return undef if !$r_array;
my $key = shift;
my $str = shift;
if ($str ne "") {
$str = lc $str;
my $max = @{$r_array};
for (my $i = 0; $i < $max; $i++) {
return $i if (exists $r_array->[$i] && lc($r_array->[$i]{$key}) eq $str);
}
return undef;
} else {
my $max = @{$r_array};
my $i;
for ($i = 0; $i < $max; $i++) {
return $i if (!$r_array->[$i] || !%{$r_array->[$i]});
}
return $i;
}
}
our %findIndexStringList_lc_cache;
sub findIndexStringList_lc {
my $r_array = shift;
return undef if !defined $r_array;
my $match = shift;
my $ID = shift;
my $skipID = shift;
my $max = @{$r_array};
my ($i, $arr);
if (exists $findIndexStringList_lc_cache{$ID}) {
$arr = $findIndexStringList_lc_cache{$ID};
} else {
my @tmp = split / *, */, lc($ID);
$arr = \@tmp;
%findIndexStringList_lc_cache = () if (scalar(keys %findIndexStringList_lc_cache) > 30);
$findIndexStringList_lc_cache{$ID} = $arr;
}
foreach (@{$arr}) {
for ($i = 0; $i < $max; $i++) {
next if $i eq $skipID;
if (exists $r_array->[$i] && lc($r_array->[$i]{$match}) eq $_) {
return $i;
}
}
}
if ($ID eq "") {
return $i;
} else {
return undef;
}
}
##
# findIndexString_lc_not_equip(r_array, key, [str])
#
# Same has findIndexString(), except this function does case-insensitive string comparisons.
# It only finds items which are not equipped AND are able to be equipped (identified).
sub findIndexString_lc_not_equip {
my $r_array = shift;
return undef if !defined $r_array;
my $match = shift;
my $ID = lc(shift);
my $i;
for ($i = 0; $i < @{$r_array} ;$i++) {
if ((exists $r_array->[$i] && lc($$r_array[$i]{$match}) eq $ID && ($$r_array[$i]{'identified'}) && !($$r_array[$i]{'equipped'}))
|| (!$$r_array[$i] && $ID eq "")) {
return $i;
}
}
if ($ID eq "") {
return $i;
} else {
return undef;
}
}
sub findLastIndex {
my $r_array = shift;
return undef if !$r_array;
my $key = shift;
my $num = shift;
if ($num ne "") {
my $max = @{$r_array};
for (my $i = $max-1; $i > -1; $i--) {
return $i if (exists $r_array->[$i] && $r_array->[$i]{$key} == $num);
}
return undef;
} else {
my $max = @{$r_array};
my $i;
for ($i = $max-1; $i > -1; $i--) {
return $i if (!$r_array->[$i] || !%{$r_array->[$i]});
}
return $i;
}
}
sub findKey {
my $r_hash = shift;
my $match = shift;
my $ID = shift;
foreach (keys %{$r_hash}) {
if (exists $r_hash->{$_} && $r_hash->{$_}{$match} == $ID) {
return $_;
}
}
}
sub findKeyString {
my $r_hash = shift;
my $match = shift;
my $ID = shift;
foreach (keys %{$r_hash}) {
if (ref($r_hash->{$_}) && $r_hash->{$_}{$match} eq $ID) {
return $_;
}
}
}
##
# hashCopyByKey(Hash* target, Hash* source, keys...)
#
# Copies each key from the target to the source.
#
# For example,
# <pre class="example">
# hashCopyByKey(\%target, \%source, qw(some keys));
# </pre>
# would copy 'some' and 'keys' from the source hash to the
# target hash.
sub hashCopyByKey {
my ($target, $source, @keys) = @_;
foreach (@keys) {
$target->{$_} = $source->{$_} if exists $source->{$_};
}
}
sub minHeapAdd {
my $r_array = shift;
my $r_hash = shift;
my $match = shift;
my $i;
my $found;
my @newArray;
for ($i = 0; $i < @{$r_array};$i++) {
if (!$found && exists $r_array->[$i] && $$r_hash{$match} < $$r_array[$i]{$match}) {
push @newArray, $r_hash;
$found = 1;
}
push @newArray, $$r_array[$i];
}
if (!$found) {
push @newArray, $r_hash;
}
@{$r_array} = @newArray;
}
##
# void shuffleArray(Array* array)
# array: A reference to an array.
#
# Randomize the order of the items in the array.
sub shuffleArray {
my $r_array = shift;
my $i = @{$r_array};
my $j;
while ($i--) {
$j = int rand ($i+1);
@$r_array[$i,$j] = @$r_array[$j,$i];
}
}
1;