forked from luanhailiang/mudos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.c
More file actions
2220 lines (1919 loc) · 51 KB
/
array.c
File metadata and controls
2220 lines (1919 loc) · 51 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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "std.h"
#include "lpc_incl.h"
#include "comm.h"
#include "regexp.h"
#include "backend.h"
#include "qsort.h"
#include "md.h"
#include "efun_protos.h"
/*
* This file contains functions used to manipulate arrays.
* Some of them are connected to efuns, and some are only used internally
* by the MudOS driver.
*/
#ifdef ARRAY_STATS
int num_arrays;
int total_array_size;
#endif
INLINE_STATIC int builtin_sort_array_cmp_fwd PROT((svalue_t *, svalue_t *));
INLINE_STATIC int builtin_sort_array_cmp_rev PROT((svalue_t *, svalue_t *));
INLINE_STATIC int sort_array_cmp PROT((svalue_t *, svalue_t *));
#ifndef NO_ENVIRONMENT
static int deep_inventory_count PROT((object_t *));
static void deep_inventory_collect PROT((object_t *, array_t *, int *));
#endif
INLINE_STATIC int alist_cmp PROT((svalue_t *, svalue_t *));
/*
* Make an empty array for everyone to use, never to be deallocated.
* It is cheaper to reuse it, than to use MALLOC() and allocate.
*/
array_t the_null_array =
{
1, /* Ref count, which will ensure that it will
* never be deallocated */
#ifdef DEBUG
1, /* extra ref */
#endif
0, /* size */
};
#ifdef PACKAGE_MUDLIB_STATS
static void ms_setup_stats P1(array_t *, p) {
if (current_object) {
assign_stats(&p->stats, current_object);
add_array_size(&p->stats, p->size);
} else {
null_stats(&p->stats);
}
}
#define ms_remove_stats(p) add_array_size(&(p)->stats, -(int)((p)->size))
#define ms_add_array_size(p, n) add_array_size(p, n)
#else
#define ms_setup_stats(x)
#define ms_remove_stats(x)
#define ms_add_array_size(p, n)
#endif
/* Array allocation routines:
*
* the prefix int_ indicates error checking is not performed. It is up to
* the caller to guarantee 0 < n <= max_array_size [note that the first
* inequality is strict].
*
* _empty_ indicates that the internal svalues are not initialized to zero;
* this saves some time if the calling routine is simply going to overwrite
* the entries. In the case of free_ and dealloc_, it means the entries
* have already been moved somewhere else or freed, and should be ignored.
*
* Note that we rely a bit on gcc automatically inlining small routines.
* Speed maniacs may wish to add INLINE liberally.
*/
static array_t *int_allocate_empty_array P1(int, n) {
array_t *p;
#ifdef ARRAY_STATS
num_arrays++;
total_array_size += sizeof(array_t) + sizeof(svalue_t) * (n-1);
#endif
p = ALLOC_ARRAY(n);
p->ref = 1;
p->size = n;
ms_setup_stats(p);
return p;
}
array_t *allocate_empty_array P1(int, n)
{
if (n < 0 || n > max_array_size)
error("Illegal array size.\n");
if (!n) return &the_null_array;
return int_allocate_empty_array(n);
}
static array_t *int_allocate_array P1(int, n)
{
array_t *p = int_allocate_empty_array(n);
while (n--)
p->item[n] = const0;
return p;
}
array_t *allocate_array P1(int, n)
{
array_t *p = allocate_empty_array(n);
while (n--)
p->item[n] = const0;
return p;
}
array_t *allocate_array2 P2(int, n, svalue_t *, svp) {
int i;
array_t *ret;
if (svp->type == T_FUNCTION) {
ret = allocate_array(n);
for (i = 0; i < n; i++) {
svalue_t *r;
push_number(i);
r = call_function_pointer(svp->u.fp, 1);
ret->item[i] = *r;
r->type = T_NUMBER;
}
} else {
ret = allocate_empty_array(n);
for (i = 0; i < n; i++)
assign_svalue_no_free(ret->item + i, svp);
}
return ret;
}
static void dealloc_empty_array P1(array_t *, p) {
ms_remove_stats(p);
#ifdef ARRAY_STATS
num_arrays--;
total_array_size -= sizeof(array_t) + sizeof(svalue_t) *
(p->size - 1);
#endif
FREE((char *) p);
}
void dealloc_array P1(array_t *, p)
{
int i;
for (i = p->size; i--;)
free_svalue(&p->item[i], "free_array");
dealloc_empty_array(p);
}
void free_array P1(array_t *, p)
{
if (--(p->ref) > 0 || (p == &the_null_array))
return;
dealloc_array(p);
}
void free_empty_array P1(array_t *, p)
{
if ((--(p->ref) > 0) || (p == &the_null_array))
return;
dealloc_empty_array(p);
}
/* Finish setting up an array allocated with ALLOC_ARRAY, resizing it to
size n */
static array_t *fix_array P2(array_t *, p, int, n) {
#ifdef ARRAY_STATS
num_arrays++;
total_array_size += sizeof(array_t) + sizeof(svalue_t) * (n-1);
#endif
p->size = n;
p->ref = 1;
ms_setup_stats(p);
return RESIZE_ARRAY(p, n);
}
INLINE array_t *resize_array P2(array_t *, p, int, n) {
#ifdef ARRAY_STATS
total_array_size += (n - p->size) * sizeof(svalue_t);
#endif
ms_remove_stats(p);
p = RESIZE_ARRAY(p, n);
if (!p)
fatal("Out of memory.\n");
p->size = n;
ms_setup_stats(p);
return p;
}
array_t *explode_string P4(char *, str, int, slen, char *, del, int, len)
{
char *p, *beg, *lastdel = 0;
int num, j, limit;
array_t *ret;
char *buff, *tmp;
unsigned short sz;
if (!slen)
return &the_null_array;
/* return an array of length strlen(str) -w- one character per element */
if (len == 0) {
sz = 1;
if (slen > max_array_size) {
slen = max_array_size;
}
ret = int_allocate_empty_array(slen);
for (j = 0; j < slen; j++) {
ret->item[j].type = T_STRING;
ret->item[j].subtype = STRING_MALLOC;
ret->item[j].u.string = tmp = new_string(1, "explode_string: tmp");
tmp[0] = str[j];
tmp[1] = '\0';
}
return ret;
}
if (len == 1) {
char delimeter;
delimeter = *del;
#ifndef REVERSIBLE_EXPLODE_STRING
/*
* Skip leading 'del' strings, if any.
*/
while (*str == delimeter) {
str++;
slen--;
if (str[0] == '\0') {
return &the_null_array;
}
# ifdef SANE_EXPLODE_STRING
break;
# endif
}
#endif
/*
* Find number of occurences of the delimiter 'del'.
*/
for (p = str, num = 0; *p;) {
if (*p == delimeter) {
num++;
lastdel = p;
}
p++;
}
/*
* Compute number of array items. It is either number of delimiters,
* or, one more.
*/
limit = max_array_size;
#ifdef REVERSIBLE_EXPLODE_STRING
num++;
limit--;
#else
if (lastdel != (str + slen - 1)) {
num++;
limit--;
}
#endif
if (num > max_array_size) {
num = max_array_size;
}
ret = int_allocate_empty_array(num);
for (p = str, beg = str, num = 0; *p && (num < limit);) {
if (*p == delimeter) {
DEBUG_CHECK(num >= ret->size, "Index out of bounds in explode!\n");
sz = p - beg;
ret->item[num].type = T_STRING;
ret->item[num].subtype = STRING_MALLOC;
ret->item[num].u.string = buff = new_string(sz, "explode_string: buff");
strncpy(buff, beg, sz);
buff[sz] = '\0';
num++;
beg = ++p;
} else {
p++;
}
}
#ifdef REVERSIBLE_EXPLODE_STRING
ret->item[num].type = T_STRING;
ret->item[num].subtype = STRING_MALLOC;
ret->item[num].u.string = string_copy(beg, "explode_string: last, len == 1");
#else
/* Copy last occurence, if there was not a 'del' at the end. */
if (*beg != '\0' && num != limit) {
ret->item[num].type = T_STRING;
ret->item[num].subtype = STRING_MALLOC;
ret->item[num].u.string = string_copy(beg, "explode_string: last, len == 1");
}
#endif
return ret;
} /* len == 1 */
#ifndef REVERSIBLE_EXPLODE_STRING
/*
* Skip leading 'del' strings, if any.
*/
while (strncmp(str, del, len) == 0) {
str += len;
slen -= len;
if (str[0] == '\0') {
return &the_null_array;
}
# ifdef SANE_EXPLODE_STRING
break;
# endif
}
#endif
/*
* Find number of occurences of the delimiter 'del'.
*/
for (p = str, num = 0; *p;) {
if (strncmp(p, del, len) == 0) {
num++;
lastdel = p;
p += len;
} else {
p++;
}
}
/*
* Compute number of array items. It is either number of delimiters, or,
* one more.
*/
#ifdef REVERSIBLE_EXPLODE_STRING
num++;
#else
if (lastdel != (str + slen - len)) {
num++;
}
#endif
if (num > max_array_size) {
num = max_array_size;
}
ret = int_allocate_empty_array(num);
limit = max_array_size - 1; /* extra element can be added after loop */
for (p = str, beg = str, num = 0; *p && (num < limit);) {
if (strncmp(p, del, len) == 0) {
if (num >= ret->size)
fatal("Index out of bounds in explode!\n");
ret->item[num].type = T_STRING;
ret->item[num].subtype = STRING_MALLOC;
ret->item[num].u.string = buff = new_string(p - beg,
"explode_string: buff");
strncpy(buff, beg, p - beg);
buff[p - beg] = '\0';
num++;
beg = p + len;
p = beg;
} else {
p++;
}
}
/* Copy last occurence, if there was not a 'del' at the end. */
#ifdef REVERSIBLE_EXPLODE_STRING
ret->item[num].type = T_STRING;
ret->item[num].subtype = STRING_MALLOC;
ret->item[num].u.string = string_copy(beg, "explode_string: last, len != 1");
#else
if (*beg != '\0' && num != limit) {
ret->item[num].type = T_STRING;
ret->item[num].subtype = STRING_MALLOC;
ret->item[num].u.string = string_copy(beg, "explode_string: last, len != 1");
}
#endif
return ret;
}
char *implode_string P3(array_t *, arr, char *, del, int, del_len)
{
int size, i, num;
char *p, *q;
for (i = arr->size, size = 0, num = 0; i--;) {
if (arr->item[i].type == T_STRING) {
size += SVALUE_STRLEN(&arr->item[i]);
num++;
}
}
if (num == 0)
return string_copy("", "implode_string");
p = new_string(size + (num - 1) * del_len, "implode_string: p");
q = p;
for (i = 0, num = 0; i < arr->size; i++) {
if (arr->item[i].type == T_STRING) {
if (num) {
strncpy(p, del, del_len);
p += del_len;
}
size = SVALUE_STRLEN(&arr->item[i]);
strncpy(p, arr->item[i].u.string, size);
p += size;
num++;
}
}
*p = 0;
return q;
}
void implode_array P4(funptr_t *, fp, array_t *, arr,
svalue_t *, dest, int, first_on_stack) {
int i = 0, n;
svalue_t *v;
if (first_on_stack) {
if (!(n = arr->size)) {
*dest = *sp--;
return;
}
} else {
if (!(n = arr->size)) {
*dest = const0;
return;
} else if (n == 1) {
assign_svalue_no_free(dest, &arr->item[0]);
return;
}
}
if (!first_on_stack)
push_svalue(&arr->item[i++]);
while (1) {
push_svalue(&arr->item[i++]);
v = call_function_pointer(fp, 2);
if (!v) {
*dest = const0;
return;
}
if (i < n)
push_svalue(v);
else
break;
}
assign_svalue_no_free(dest, v);
}
array_t *users()
{
register object_t *ob;
int i, j;
array_t *ret;
#ifdef F_SET_HIDE
int display_hidden = 0;
if (num_hidden_users > 0) {
if (current_object->flags & O_HIDDEN) {
display_hidden = 1;
} else {
display_hidden = valid_hide(current_object);
}
}
ret = allocate_empty_array(num_user - (display_hidden ? 0 : num_hidden_users));
#else
ret = allocate_empty_array(num_user);
#endif
for (i = j = 0; i < max_users; i++) {
if (!all_users[i]) {
continue;
}
ob = all_users[i]->ob;
#ifdef F_SET_HIDE
if (!display_hidden && (ob->flags & O_HIDDEN))
continue;
#endif
ret->item[j].type = T_OBJECT;
ret->item[j].u.ob = ob;
add_ref(ob, "users");
j++;
}
return ret;
}
/*
* Slice of an array.
* It now frees the passed array
*/
array_t *slice_array P3(array_t *, p, int, from, int, to)
{
int cnt;
svalue_t *sv1, *sv2;
if (from < 0)
from = 0;
if (to >= p->size)
to = p->size - 1;
if (from > to) {
free_array(p);
return &the_null_array;
}
if (!(--p->ref)) {
if (from) {
sv1 = p->item + from;
cnt = from;
while (cnt--) free_svalue(--sv1, "slice_array:2");
cnt = to - from + 1;
sv1 = p->item;
sv2 = p->item + from;
while (cnt--) *sv1++ = *sv2++;
} else {
sv2 = p->item + to + 1;
}
cnt = (p->size - 1) - to;
while (cnt--) free_svalue(sv2++, "slice_array:3");
p = resize_array(p, to-from+1);
p->ref = 1;
return p;
} else {
array_t *d;
d = int_allocate_empty_array(to - from + 1);
sv1 = d->item - from;
sv2 = p->item;
for (cnt = from; cnt <= to; cnt++)
assign_svalue_no_free(sv1 + cnt, sv2 + cnt);
return d;
}
}
/*
* Copy of an array
*/
static array_t *copy_array P1(array_t *, p)
{
array_t *d;
int n;
svalue_t *sv1 = p->item, *sv2;
d = allocate_empty_array(n = p->size);
sv2 = d->item;
while (n--)
assign_svalue_no_free(sv2 + n, sv1 + n);
return d;
}
#ifdef F_COMMANDS
array_t *commands P1(object_t *, ob)
{
sentence_t *s;
array_t *v, *p;
int cnt = 0;
svalue_t *sv;
for (s = ob->sent; s && s->verb; s = s->next) {
if (++cnt == max_array_size) break;
}
v = allocate_empty_array(cnt);
sv = v->item;
for (s = ob->sent; cnt-- && s && s->verb; s = s->next) {
sv->type = T_ARRAY;
(sv++)->u.arr = p = int_allocate_empty_array(4);
p->item[0].type = T_STRING;
p->item[0].u.string = ref_string(s->verb); /* the verb is shared */
p->item[0].subtype = STRING_SHARED;
p->item[1].type = T_NUMBER;
p->item[1].u.number = s->flags;
p->item[2].type = T_OBJECT;
p->item[2].u.ob = s->ob;
p->item[3].type = T_STRING;
if (s->flags & V_FUNCTION) {
p->item[3].u.string = "<function>";
p->item[3].subtype = STRING_CONSTANT;
} else {
p->item[3].u.string = ref_string(s->function.s);
p->item[3].subtype = STRING_SHARED;
}
add_ref(s->ob, "commands");
}
return v;
}
#endif
/* EFUN: filter_array
Runs all elements of an array through ob->func()
and returns an array holding those elements that ob->func
returned 1 for.
*/
#ifdef F_FILTER
void
filter_array P2(svalue_t *, arg, int, num_arg)
{
array_t *vec = arg->u.arr, *r;
int size;
if ((size = vec->size) < 1) {
pop_n_elems(num_arg - 1);
return;
}
else {
char *flags;
svalue_t *v;
int res = 0, cnt;
function_to_call_t ftc;
process_efun_callback(1, &ftc, F_FILTER);
flags = new_string(size, "TEMP: filter: flags");
push_malloced_string(flags);
for (cnt = 0; cnt < size; cnt++) {
push_svalue(vec->item + cnt);
v = call_efun_callback(&ftc, 1);
if (!IS_ZERO(v)) {
flags[cnt] = 1;
res++;
} else
flags[cnt] = 0;
}
r = allocate_empty_array(res);
if (res) {
while (cnt--) {
if (flags[cnt])
assign_svalue_no_free(&r->item[--res], vec->item+cnt);
}
}
FREE_MSTR(flags);
sp--;
pop_n_elems(num_arg - 1);
free_array(vec);
sp->u.arr = r;
}
}
void
filter_string P2(svalue_t *, arg, int, num_arg)
{
if (arg->u.string[0] == 0) {
pop_n_elems(num_arg - 1);
return;
}
else {
int size;
svalue_t *v;
int idx = 0, cnt;
function_to_call_t ftc;
char *str;
unlink_string_svalue(arg);
size = SVALUE_STRLEN(arg);
str = arg->u.string;
process_efun_callback(1, &ftc, F_FILTER);
for (cnt = 0; cnt < size; cnt++) {
push_number(str[cnt]);
v = call_efun_callback(&ftc, 1);
if (!IS_ZERO(v))
str[idx++] = str[cnt];
}
if (idx != cnt)
arg->u.string = extend_string(arg->u.string, idx);
pop_n_elems(num_arg - 1);
}
}
#endif
/* Unique maker
These routines takes an array of objects and calls the function 'func'
in them. The return values are used to decide which of the objects are
unique. Then an array on the below form are returned:
({
({Same1:1, Same1:2, Same1:3, .... Same1:N }),
({Same2:1, Same2:2, Same2:3, .... Same2:N }),
({Same3:1, Same3:2, Same3:3, .... Same3:N }),
....
....
({SameM:1, SameM:2, SameM:3, .... SameM:N }),
})
i.e an array of arrays consisting of lists of objectpointers
to all the nonunique objects for each unique set of objects.
The basic purpose of this routine is to speed up the preparing of the
array used for describing.
*/
/* nonstatic, is used in mappings too */
int sameval P2(svalue_t *, arg1, svalue_t *, arg2)
{
DEBUG_CHECK(!arg1 || !arg2, "Null pointer passed to sameval.\n");
switch (arg1->type | arg2->type) {
case T_NUMBER:
return arg1->u.number == arg2->u.number;
case T_ARRAY:
case T_CLASS:
return arg1->u.arr == arg2->u.arr;
case T_STRING:
if (SVALUE_STRLEN_DIFFERS(arg1, arg2)) return 0;
return !strcmp(arg1->u.string, arg2->u.string);
case T_OBJECT:
return arg1->u.ob == arg2->u.ob;
case T_MAPPING:
return arg1->u.map == arg2->u.map;
case T_FUNCTION:
return arg1->u.fp == arg2->u.fp;
case T_REAL:
return arg1->u.real == arg2->u.real;
#ifndef NO_BUFFER_TYPE
case T_BUFFER:
return arg1->u.buf == arg2->u.buf;
#endif
}
return 0;
}
#ifdef F_UNIQUE_ARRAY
typedef struct unique_s {
svalue_t mark;
int count;
struct unique_s *next;
int *indices;
} unique_t;
typedef struct unique_list_s {
unique_t *head;
struct unique_list_s *next;
} unique_list_t;
static unique_list_t *g_u_list = 0;
static void unique_array_error_handler PROT((void)) {
unique_list_t *unlist = g_u_list;
unique_t *uptr = unlist->head, *nptr;
g_u_list = g_u_list->next;
while (uptr) {
nptr = uptr->next;
FREE((char *) uptr->indices);
free_svalue(&uptr->mark, "unique_array_error_handler");
FREE((char *) uptr);
uptr = nptr;
}
FREE((char *)unlist);
}
void f_unique_array PROT((void)) {
array_t *v, *ret;
int size, i, numkeys = 0, *ind, num_arg = st_num_arg;
svalue_t *skipval, *sv, *svp;
unique_list_t *unlist;
unique_t **head, *uptr, *nptr;
funptr_t *fp = 0;
char *func;
size = (v = (sp - num_arg + 1)->u.arr)->size;
if (!size) {
if (num_arg == 3) free_svalue(sp--, "f_unique_array");
free_svalue(sp--, "f_unique_array");
return;
}
if (num_arg == 3) {
skipval = sp;
if ((sp-1)->type == T_FUNCTION) fp = (sp-1)->u.fp;
else func = (sp-1)->u.string;
}
else {
skipval = &const0;
if (sp->type == T_FUNCTION) fp = sp->u.fp;
else func = sp->u.string;
}
unlist = ALLOCATE(unique_list_t, TAG_TEMPORARY, "f_unique_array:1");
unlist->next = g_u_list;
unlist->head = 0;
head = &unlist->head;
g_u_list = unlist;
STACK_INC;
sp->type = T_ERROR_HANDLER;
sp->u.error_handler = unique_array_error_handler;
for (i = 0; i < size; i++) {
if (fp) {
push_svalue(v->item + i);
sv = call_function_pointer(fp, 1);
} else if ((v->item + i)->type == T_OBJECT) {
sv = apply(func, (v->item + i)->u.ob, 0, ORIGIN_EFUN);
} else sv = 0;
if (sv && !sameval(sv, skipval)) {
uptr = *head;
while (uptr) {
if (sameval(sv, &uptr->mark)) {
uptr->indices = RESIZE(uptr->indices, uptr->count + 1, int,
TAG_TEMPORARY, "f_unique_array:2");
uptr->indices[uptr->count++] = i;
break;
}
uptr = uptr->next;
}
if (!uptr) {
numkeys++;
uptr = ALLOCATE(unique_t, TAG_TEMPORARY, "f_unique_array:3");
uptr->indices = ALLOCATE(int, TAG_TEMPORARY, "f_unique_array:4");
uptr->count = 1;
uptr->indices[0] = i;
uptr->next = *head;
assign_svalue_no_free(&uptr->mark, sv);
*head = uptr;
}
}
}
ret = allocate_empty_array(numkeys);
uptr = *head;
svp = v->item;
while (numkeys--) {
nptr = uptr->next;
(sv = ret->item + numkeys)->type = T_ARRAY;
sv->u.arr = allocate_empty_array(i = uptr->count);
skipval = sv->u.arr->item + i;
ind = uptr->indices;
while (i--) {
assign_svalue_no_free(--skipval, svp + ind[i]);
}
FREE((char *)ind);
free_svalue(&uptr->mark, "f_unique_array");
FREE((char *)uptr);
uptr = nptr;
}
unlist = g_u_list->next;
FREE((char *)g_u_list);
g_u_list = unlist;
sp--;
pop_n_elems(num_arg - 1);
free_array(v);
sp->u.arr = ret;
}
/*
* End of Unique maker
*************************
*/
#endif
/* Concatenation of two arrays into one
*/
array_t *add_array P2(array_t *, p, array_t *, r)
{
int cnt, res;
array_t *d; /* destination */
/*
* have to be careful with size zero arrays because they could be
* the_null_array. REALLOC(the_null_array, ...) is bad :(
*/
if (p->size == 0) {
p->ref--;
return r->ref > 1 ? (r->ref--, copy_array(r)) : r;
}
if (r->size == 0) {
r->ref--;
return p->ref > 1 ? (p->ref--, copy_array(p)) : p;
}
res = p->size + r->size;
if (res < 0 || res > max_array_size)
error("result of array addition is greater than maximum array size.\n");
/* x += x */
if ((p == r) && (p->ref == 2)) {
int osize = p->size;
p->ref = 1;
d = resize_array(p, res);
/* copy myself */
for (cnt = osize; cnt--; )
assign_svalue_no_free(&d->item[--res], &d->item[cnt]);
return d;
}
/* transfer svalues for ref 1 target array */
if (p->ref == 1) {
d = resize_array(p, res);
/* note that d->ref is already 1, which is right */
} else {
d = int_allocate_empty_array(res);
for (cnt = p->size; cnt--;)
assign_svalue_no_free(&d->item[cnt], &p->item[cnt]);
p->ref--;
}
/* transfer svalues from ref 1 source array */
if (r->ref == 1) {
for (cnt = r->size; cnt--;)
d->item[--res] = r->item[cnt];
dealloc_empty_array(r);
} else {
for (cnt = r->size; cnt--;)
assign_svalue_no_free(&d->item[--res], &r->item[cnt]);
r->ref--;
}
return d;
}
#ifndef NO_ENVIRONMENT
/* Returns an array of all objects contained in 'ob' */
array_t *all_inventory P2(object_t *, ob, int, override)
{
array_t *d;
object_t *cur;
int cnt, res;
int display_hidden;
if (override) {
display_hidden = 1;
} else {
display_hidden = -1;
}
cnt = 0;
for (cur = ob->contains; cur; cur = cur->next_inv) {
#ifdef F_SET_HIDE
if (cur->flags & O_HIDDEN) {
if (display_hidden == -1) {
display_hidden = valid_hide(current_object);
}
if (display_hidden)
cnt++;
} else
#endif
cnt++;
}
if (!cnt)
return &the_null_array;
d = int_allocate_empty_array(cnt);
cur = ob->contains;
for (res = 0; res < cnt; res++) {
#ifdef F_SET_HIDE
if ((cur->flags & O_HIDDEN) && !display_hidden) {
cur = cur->next_inv;
res--;
continue;
}
#endif
d->item[res].type = T_OBJECT;
d->item[res].u.ob = cur;
add_ref(cur, "all_inventory");
cur = cur->next_inv;
}
return d;
}
#endif
/* Runs all elements of an array through ob::func
and replaces each value in arr by the value returned by ob::func
*/
#ifdef F_MAP
void
map_array P2(svalue_t *, arg, int, num_arg)
{
array_t *arr = arg->u.arr;
array_t *r;