This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make the ansi branch
[perl5.git] / av.c
CommitLineData
a0d0e21e 1/* av.c
79072805 2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
79072805
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
a0d0e21e
LW
8 */
9
10/*
11 * "...for the Entwives desired order, and plenty, and peace (by which they
12 * meant that things should remain where they had set them)." --Treebeard
79072805
LW
13 */
14
15#include "EXTERN.h"
16#include "perl.h"
17
fb73857a 18void
a0d0e21e
LW
19av_reify(av)
20AV* av;
21{
22 I32 key;
23 SV* sv;
fb73857a
PP
24
25 if (AvREAL(av))
26 return;
a0d0e21e
LW
27 key = AvMAX(av) + 1;
28 while (key > AvFILL(av) + 1)
29 AvARRAY(av)[--key] = &sv_undef;
30 while (key) {
31 sv = AvARRAY(av)[--key];
32 assert(sv);
11343788
MB
33 if (sv != &sv_undef) {
34 dTHR;
a0d0e21e 35 (void)SvREFCNT_inc(sv);
11343788 36 }
a0d0e21e 37 }
29de640a
CS
38 key = AvARRAY(av) - AvALLOC(av);
39 while (key)
40 AvALLOC(av)[--key] = &sv_undef;
a0d0e21e
LW
41 AvREAL_on(av);
42}
43
44void
45av_extend(av,key)
46AV *av;
47I32 key;
48{
11343788 49 dTHR; /* only necessary if we have to extend stack */
a0d0e21e
LW
50 if (key > AvMAX(av)) {
51 SV** ary;
52 I32 tmp;
53 I32 newmax;
54
55 if (AvALLOC(av) != AvARRAY(av)) {
56 ary = AvALLOC(av) + AvFILL(av) + 1;
57 tmp = AvARRAY(av) - AvALLOC(av);
58 Move(AvARRAY(av), AvALLOC(av), AvFILL(av)+1, SV*);
59 AvMAX(av) += tmp;
60 SvPVX(av) = (char*)AvALLOC(av);
61 if (AvREAL(av)) {
62 while (tmp)
63 ary[--tmp] = &sv_undef;
64 }
65
66 if (key > AvMAX(av) - 10) {
67 newmax = key + AvMAX(av);
68 goto resize;
69 }
70 }
71 else {
72 if (AvALLOC(av)) {
c07a80fd 73#ifndef STRANGE_MALLOC
4633a7c4 74 U32 bytes;
c07a80fd 75#endif
4633a7c4 76
a0d0e21e
LW
77 newmax = key + AvMAX(av) / 5;
78 resize:
4633a7c4 79#ifdef STRANGE_MALLOC
a0d0e21e 80 Renew(AvALLOC(av),newmax+1, SV*);
4633a7c4
LW
81#else
82 bytes = (newmax + 1) * sizeof(SV*);
83#define MALLOC_OVERHEAD 16
84 tmp = MALLOC_OVERHEAD;
85 while (tmp - MALLOC_OVERHEAD < bytes)
86 tmp += tmp;
87 tmp -= MALLOC_OVERHEAD;
88 tmp /= sizeof(SV*);
89 assert(tmp > newmax);
90 newmax = tmp - 1;
91 New(2,ary, newmax+1, SV*);
92 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
fba3b22e
MB
93 if (AvMAX(av) > 64)
94 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
4633a7c4
LW
95 else
96 Safefree(AvALLOC(av));
97 AvALLOC(av) = ary;
98#endif
a0d0e21e
LW
99 ary = AvALLOC(av) + AvMAX(av) + 1;
100 tmp = newmax - AvMAX(av);
7d55f622 101 if (av == curstack) { /* Oops, grew stack (via av_store()?) */
a0d0e21e
LW
102 stack_sp = AvALLOC(av) + (stack_sp - stack_base);
103 stack_base = AvALLOC(av);
104 stack_max = stack_base + newmax;
105 }
106 }
107 else {
108 newmax = key < 4 ? 4 : key;
109 New(2,AvALLOC(av), newmax+1, SV*);
110 ary = AvALLOC(av) + 1;
111 tmp = newmax;
112 AvALLOC(av)[0] = &sv_undef; /* For the stacks */
113 }
114 if (AvREAL(av)) {
115 while (tmp)
116 ary[--tmp] = &sv_undef;
117 }
118
119 SvPVX(av) = (char*)AvALLOC(av);
120 AvMAX(av) = newmax;
121 }
122 }
123}
124
79072805 125SV**
463ee0b2
LW
126av_fetch(av,key,lval)
127register AV *av;
79072805
LW
128I32 key;
129I32 lval;
130{
131 SV *sv;
132
a0d0e21e
LW
133 if (!av)
134 return 0;
135
8990e307 136 if (SvRMAGICAL(av)) {
463ee0b2 137 if (mg_find((SV*)av,'P')) {
11343788 138 dTHR;
8990e307 139 sv = sv_newmortal();
463ee0b2 140 mg_copy((SV*)av, sv, 0, key);
463ee0b2
LW
141 Sv = sv;
142 return &Sv;
143 }
144 }
145
a0d0e21e
LW
146 if (key < 0) {
147 key += AvFILL(av) + 1;
148 if (key < 0)
149 return 0;
150 }
151 else if (key > AvFILL(av)) {
152 if (!lval)
153 return 0;
154 if (AvREALISH(av))
155 sv = NEWSV(5,0);
156 else
157 sv = sv_newmortal();
158 return av_store(av,key,sv);
79072805 159 }
a0d0e21e 160 if (AvARRAY(av)[key] == &sv_undef) {
4dbf4341 161 emptyness:
79072805
LW
162 if (lval) {
163 sv = NEWSV(6,0);
463ee0b2 164 return av_store(av,key,sv);
79072805
LW
165 }
166 return 0;
167 }
4dbf4341
PP
168 else if (AvREIFY(av)
169 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
170 || SvTYPE(AvARRAY(av)[key]) == SVTYPEMASK)) {
171 AvARRAY(av)[key] = &sv_undef; /* 1/2 reify */
172 goto emptyness;
173 }
463ee0b2 174 return &AvARRAY(av)[key];
79072805
LW
175}
176
177SV**
463ee0b2
LW
178av_store(av,key,val)
179register AV *av;
79072805
LW
180I32 key;
181SV *val;
182{
79072805
LW
183 SV** ary;
184
a0d0e21e
LW
185 if (!av)
186 return 0;
43fcc5d2
CS
187 if (!val)
188 val = &sv_undef;
463ee0b2 189
8990e307 190 if (SvRMAGICAL(av)) {
463ee0b2 191 if (mg_find((SV*)av,'P')) {
43fcc5d2
CS
192 if (val != &sv_undef)
193 mg_copy((SV*)av, val, 0, key);
463ee0b2
LW
194 return 0;
195 }
196 }
197
a0d0e21e
LW
198 if (key < 0) {
199 key += AvFILL(av) + 1;
200 if (key < 0)
201 return 0;
79072805 202 }
43fcc5d2
CS
203 if (SvREADONLY(av) && key >= AvFILL(av))
204 croak(no_modify);
49beac48 205 if (!AvREAL(av) && AvREIFY(av))
a0d0e21e 206 av_reify(av);
a0d0e21e
LW
207 if (key > AvMAX(av))
208 av_extend(av,key);
463ee0b2 209 ary = AvARRAY(av);
a0d0e21e
LW
210 if (AvFILL(av) < key) {
211 if (!AvREAL(av)) {
11343788 212 dTHR;
7d55f622 213 if (av == curstack && key > stack_sp - stack_base)
a0d0e21e
LW
214 stack_sp = stack_base + key; /* XPUSH in disguise */
215 do
216 ary[++AvFILL(av)] = &sv_undef;
217 while (AvFILL(av) < key);
79072805 218 }
a0d0e21e 219 AvFILL(av) = key;
79072805 220 }
a0d0e21e
LW
221 else if (AvREAL(av))
222 SvREFCNT_dec(ary[key]);
79072805 223 ary[key] = val;
8990e307 224 if (SvSMAGICAL(av)) {
a0d0e21e
LW
225 if (val != &sv_undef) {
226 MAGIC* mg = SvMAGIC(av);
227 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
228 }
463ee0b2
LW
229 mg_set((SV*)av);
230 }
79072805
LW
231 return &ary[key];
232}
233
234AV *
235newAV()
236{
463ee0b2 237 register AV *av;
79072805 238
a0d0e21e
LW
239 av = (AV*)NEWSV(3,0);
240 sv_upgrade((SV *)av, SVt_PVAV);
463ee0b2
LW
241 AvREAL_on(av);
242 AvALLOC(av) = 0;
243 SvPVX(av) = 0;
244 AvMAX(av) = AvFILL(av) = -1;
245 return av;
79072805
LW
246}
247
248AV *
249av_make(size,strp)
250register I32 size;
251register SV **strp;
252{
463ee0b2 253 register AV *av;
79072805
LW
254 register I32 i;
255 register SV** ary;
256
a0d0e21e
LW
257 av = (AV*)NEWSV(8,0);
258 sv_upgrade((SV *) av,SVt_PVAV);
a0d0e21e 259 AvFLAGS(av) = AVf_REAL;
573fa4ea
TB
260 if (size) { /* `defined' was returning undef for size==0 anyway. */
261 New(4,ary,size,SV*);
262 AvALLOC(av) = ary;
263 SvPVX(av) = (char*)ary;
264 AvFILL(av) = size - 1;
265 AvMAX(av) = size - 1;
266 for (i = 0; i < size; i++) {
267 assert (*strp);
268 ary[i] = NEWSV(7,0);
269 sv_setsv(ary[i], *strp);
270 strp++;
271 }
79072805 272 }
463ee0b2 273 return av;
79072805
LW
274}
275
276AV *
277av_fake(size,strp)
278register I32 size;
279register SV **strp;
280{
463ee0b2 281 register AV *av;
79072805
LW
282 register SV** ary;
283
a0d0e21e
LW
284 av = (AV*)NEWSV(9,0);
285 sv_upgrade((SV *)av, SVt_PVAV);
79072805 286 New(4,ary,size+1,SV*);
463ee0b2 287 AvALLOC(av) = ary;
79072805 288 Copy(strp,ary,size,SV*);
a0d0e21e 289 AvFLAGS(av) = AVf_REIFY;
463ee0b2
LW
290 SvPVX(av) = (char*)ary;
291 AvFILL(av) = size - 1;
292 AvMAX(av) = size - 1;
79072805 293 while (size--) {
a0d0e21e
LW
294 assert (*strp);
295 SvTEMP_off(*strp);
79072805
LW
296 strp++;
297 }
463ee0b2 298 return av;
79072805
LW
299}
300
301void
463ee0b2
LW
302av_clear(av)
303register AV *av;
79072805
LW
304{
305 register I32 key;
a0d0e21e 306 SV** ary;
79072805 307
7d55f622
PP
308#ifdef DEBUGGING
309 if (SvREFCNT(av) <= 0) {
310 warn("Attempt to clear deleted array");
311 }
312#endif
a0d0e21e 313 if (!av || AvMAX(av) < 0)
79072805
LW
314 return;
315 /*SUPPRESS 560*/
a0d0e21e
LW
316
317 if (AvREAL(av)) {
318 ary = AvARRAY(av);
319 key = AvFILL(av) + 1;
320 while (key) {
321 SvREFCNT_dec(ary[--key]);
322 ary[key] = &sv_undef;
323 }
324 }
463ee0b2
LW
325 if (key = AvARRAY(av) - AvALLOC(av)) {
326 AvMAX(av) += key;
a0d0e21e 327 SvPVX(av) = (char*)AvALLOC(av);
79072805 328 }
463ee0b2 329 AvFILL(av) = -1;
fb73857a
PP
330
331 if (SvRMAGICAL(av))
332 mg_clear((SV*)av);
79072805
LW
333}
334
335void
463ee0b2
LW
336av_undef(av)
337register AV *av;
79072805
LW
338{
339 register I32 key;
340
463ee0b2 341 if (!av)
79072805
LW
342 return;
343 /*SUPPRESS 560*/
a0d0e21e
LW
344 if (AvREAL(av)) {
345 key = AvFILL(av) + 1;
346 while (key)
347 SvREFCNT_dec(AvARRAY(av)[--key]);
348 }
463ee0b2
LW
349 Safefree(AvALLOC(av));
350 AvALLOC(av) = 0;
351 SvPVX(av) = 0;
352 AvMAX(av) = AvFILL(av) = -1;
748a9306
LW
353 if (AvARYLEN(av)) {
354 SvREFCNT_dec(AvARYLEN(av));
355 AvARYLEN(av) = 0;
356 }
79072805
LW
357}
358
a0d0e21e 359void
463ee0b2
LW
360av_push(av,val)
361register AV *av;
79072805
LW
362SV *val;
363{
a0d0e21e
LW
364 if (!av)
365 return;
366 av_store(av,AvFILL(av)+1,val);
79072805
LW
367}
368
369SV *
463ee0b2
LW
370av_pop(av)
371register AV *av;
79072805
LW
372{
373 SV *retval;
374
a0d0e21e
LW
375 if (!av || AvFILL(av) < 0)
376 return &sv_undef;
43fcc5d2
CS
377 if (SvREADONLY(av))
378 croak(no_modify);
463ee0b2 379 retval = AvARRAY(av)[AvFILL(av)];
a0d0e21e 380 AvARRAY(av)[AvFILL(av)--] = &sv_undef;
8990e307 381 if (SvSMAGICAL(av))
463ee0b2 382 mg_set((SV*)av);
79072805
LW
383 return retval;
384}
385
386void
463ee0b2
LW
387av_unshift(av,num)
388register AV *av;
79072805
LW
389register I32 num;
390{
391 register I32 i;
392 register SV **sstr,**dstr;
393
a0d0e21e 394 if (!av || num <= 0)
79072805 395 return;
43fcc5d2
CS
396 if (SvREADONLY(av))
397 croak(no_modify);
49beac48
CS
398 if (!AvREAL(av) && AvREIFY(av))
399 av_reify(av);
a0d0e21e
LW
400 i = AvARRAY(av) - AvALLOC(av);
401 if (i) {
402 if (i > num)
403 i = num;
404 num -= i;
405
406 AvMAX(av) += i;
407 AvFILL(av) += i;
408 SvPVX(av) = (char*)(AvARRAY(av) - i);
409 }
410 if (num) {
411 av_extend(av,AvFILL(av)+num);
412 AvFILL(av) += num;
463ee0b2 413 dstr = AvARRAY(av) + AvFILL(av);
79072805
LW
414 sstr = dstr - num;
415#ifdef BUGGY_MSC5
416 # pragma loop_opt(off) /* don't loop-optimize the following code */
417#endif /* BUGGY_MSC5 */
a0d0e21e 418 for (i = AvFILL(av) - num; i >= 0; --i) {
79072805
LW
419 *dstr-- = *sstr--;
420#ifdef BUGGY_MSC5
421 # pragma loop_opt() /* loop-optimization back to command-line setting */
422#endif /* BUGGY_MSC5 */
423 }
a0d0e21e
LW
424 while (num)
425 AvARRAY(av)[--num] = &sv_undef;
79072805
LW
426 }
427}
428
429SV *
463ee0b2
LW
430av_shift(av)
431register AV *av;
79072805
LW
432{
433 SV *retval;
434
a0d0e21e
LW
435 if (!av || AvFILL(av) < 0)
436 return &sv_undef;
43fcc5d2
CS
437 if (SvREADONLY(av))
438 croak(no_modify);
463ee0b2 439 retval = *AvARRAY(av);
a0d0e21e
LW
440 if (AvREAL(av))
441 *AvARRAY(av) = &sv_undef;
463ee0b2
LW
442 SvPVX(av) = (char*)(AvARRAY(av) + 1);
443 AvMAX(av)--;
444 AvFILL(av)--;
8990e307 445 if (SvSMAGICAL(av))
463ee0b2 446 mg_set((SV*)av);
79072805
LW
447 return retval;
448}
449
450I32
463ee0b2
LW
451av_len(av)
452register AV *av;
79072805 453{
463ee0b2 454 return AvFILL(av);
79072805
LW
455}
456
457void
463ee0b2
LW
458av_fill(av, fill)
459register AV *av;
79072805
LW
460I32 fill;
461{
a0d0e21e
LW
462 if (!av)
463 croak("panic: null array");
79072805
LW
464 if (fill < 0)
465 fill = -1;
463ee0b2 466 if (fill <= AvMAX(av)) {
a0d0e21e
LW
467 I32 key = AvFILL(av);
468 SV** ary = AvARRAY(av);
469
470 if (AvREAL(av)) {
471 while (key > fill) {
472 SvREFCNT_dec(ary[key]);
473 ary[key--] = &sv_undef;
474 }
475 }
476 else {
477 while (key < fill)
478 ary[++key] = &sv_undef;
479 }
480
463ee0b2 481 AvFILL(av) = fill;
8990e307 482 if (SvSMAGICAL(av))
463ee0b2
LW
483 mg_set((SV*)av);
484 }
a0d0e21e
LW
485 else
486 (void)av_store(av,fill,&sv_undef);
79072805 487}
c750a3ec
MB
488
489SV**
490avhv_fetch(av, key, klen, lval)
491AV *av;
492char *key;
493U32 klen;
494I32 lval;
495{
496 SV **keys, **indsvp;
497 I32 ind;
498
499 keys = av_fetch(av, 0, FALSE);
500 if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
501 croak("Can't coerce array into hash");
502 indsvp = hv_fetch((HV*)SvRV(*keys), key, klen, FALSE);
503 if (indsvp) {
504 ind = SvIV(*indsvp);
505 if (ind < 1)
506 croak("Bad index while coercing array into hash");
507 } else {
508 if (!lval)
509 return 0;
510
511 ind = AvFILL(av) + 1;
512 hv_store((HV*)SvRV(*keys), key, klen, newSViv(ind), 0);
513 }
514 return av_fetch(av, ind, lval);
515}
516
517SV**
97fcbf96
MB
518avhv_fetch_ent(av, keysv, lval, hash)
519AV *av;
520SV *keysv;
521I32 lval;
522U32 hash;
523{
524 SV **keys, **indsvp;
525 HE *he;
526 I32 ind;
527
528 keys = av_fetch(av, 0, FALSE);
529 if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
530 croak("Can't coerce array into hash");
531 he = hv_fetch_ent((HV*)SvRV(*keys), keysv, FALSE, hash);
532 if (he) {
533 ind = SvIV(HeVAL(he));
534 if (ind < 1)
535 croak("Bad index while coercing array into hash");
536 } else {
537 if (!lval)
538 return 0;
539
540 ind = AvFILL(av) + 1;
541 hv_store_ent((HV*)SvRV(*keys), keysv, newSViv(ind), 0);
542 }
543 return av_fetch(av, ind, lval);
544}
545
546SV**
c750a3ec
MB
547avhv_store(av, key, klen, val, hash)
548AV *av;
549char *key;
550U32 klen;
551SV *val;
552U32 hash;
553{
554 SV **keys, **indsvp;
555 I32 ind;
556
557 keys = av_fetch(av, 0, FALSE);
558 if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
559 croak("Can't coerce array into hash");
560 indsvp = hv_fetch((HV*)SvRV(*keys), key, klen, FALSE);
561 if (indsvp) {
562 ind = SvIV(*indsvp);
563 if (ind < 1)
564 croak("Bad index while coercing array into hash");
565 } else {
566 ind = AvFILL(av) + 1;
567 hv_store((HV*)SvRV(*keys), key, klen, newSViv(ind), hash);
568 }
569 return av_store(av, ind, val);
570}
571
5bc6513d
MB
572SV**
573avhv_store_ent(av, keysv, val, hash)
574AV *av;
575SV *keysv;
576SV *val;
577U32 hash;
578{
579 SV **keys;
580 HE *he;
581 I32 ind;
582
583 keys = av_fetch(av, 0, FALSE);
584 if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
585 croak("Can't coerce array into hash");
586 he = hv_fetch_ent((HV*)SvRV(*keys), keysv, FALSE, hash);
587 if (he) {
588 ind = SvIV(HeVAL(he));
589 if (ind < 1)
590 croak("Bad index while coercing array into hash");
591 } else {
592 ind = AvFILL(av) + 1;
593 hv_store_ent((HV*)SvRV(*keys), keysv, newSViv(ind), hash);
594 }
595 return av_store(av, ind, val);
596}
597
c750a3ec 598bool
97fcbf96
MB
599avhv_exists_ent(av, keysv, hash)
600AV *av;
601SV *keysv;
602U32 hash;
603{
604 SV **keys;
605
606 keys = av_fetch(av, 0, FALSE);
607 if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
608 croak("Can't coerce array into hash");
609 return hv_exists_ent((HV*)SvRV(*keys), keysv, hash);
610}
611
612bool
c750a3ec
MB
613avhv_exists(av, key, klen)
614AV *av;
615char *key;
616U32 klen;
617{
618 SV **keys;
619
620 keys = av_fetch(av, 0, FALSE);
621 if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
622 croak("Can't coerce array into hash");
623 return hv_exists((HV*)SvRV(*keys), key, klen);
624}
625
626/* avhv_delete leaks. Caller can re-index and compress if so desired. */
627SV *
628avhv_delete(av, key, klen, flags)
629AV *av;
630char *key;
631U32 klen;
632I32 flags;
633{
634 SV **keys;
635 SV *sv;
636 SV **svp;
637 I32 ind;
638
639 keys = av_fetch(av, 0, FALSE);
640 if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
641 croak("Can't coerce array into hash");
642 sv = hv_delete((HV*)SvRV(*keys), key, klen, 0);
643 if (!sv)
644 return Nullsv;
645 ind = SvIV(sv);
646 if (ind < 1)
647 croak("Bad index while coercing array into hash");
648 svp = av_fetch(av, ind, FALSE);
649 if (!svp)
650 return Nullsv;
651 if (flags & G_DISCARD) {
652 sv = Nullsv;
653 SvREFCNT_dec(*svp);
654 } else {
655 sv = sv_2mortal(*svp);
656 }
657 *svp = &sv_undef;
658 return sv;
659}
660
97fcbf96
MB
661/* avhv_delete_ent leaks. Caller can re-index and compress if so desired. */
662SV *
663avhv_delete_ent(av, keysv, flags, hash)
664AV *av;
665SV *keysv;
666I32 flags;
667U32 hash;
668{
669 SV **keys;
670 SV *sv;
671 SV **svp;
672 I32 ind;
673
674 keys = av_fetch(av, 0, FALSE);
675 if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
676 croak("Can't coerce array into hash");
677 sv = hv_delete_ent((HV*)SvRV(*keys), keysv, 0, hash);
678 if (!sv)
679 return Nullsv;
680 ind = SvIV(sv);
681 if (ind < 1)
682 croak("Bad index while coercing array into hash");
683 svp = av_fetch(av, ind, FALSE);
684 if (!svp)
685 return Nullsv;
686 if (flags & G_DISCARD) {
687 sv = Nullsv;
688 SvREFCNT_dec(*svp);
689 } else {
690 sv = sv_2mortal(*svp);
691 }
692 *svp = &sv_undef;
693 return sv;
694}
695
c750a3ec
MB
696I32
697avhv_iterinit(av)
698AV *av;
699{
700 SV **keys;
701
702 keys = av_fetch(av, 0, FALSE);
703 if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
704 croak("Can't coerce array into hash");
705 return hv_iterinit((HV*)SvRV(*keys));
706}
707
708HE *
709avhv_iternext(av)
710AV *av;
711{
712 SV **keys;
713
714 keys = av_fetch(av, 0, FALSE);
715 if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
716 croak("Can't coerce array into hash");
717 return hv_iternext((HV*)SvRV(*keys));
718}
719
720SV *
721avhv_iterval(av, entry)
722AV *av;
723register HE *entry;
724{
725 SV **keys;
726 SV *sv;
727 I32 ind;
728
729 keys = av_fetch(av, 0, FALSE);
730 if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
731 croak("Can't coerce array into hash");
732 sv = hv_iterval((HV*)SvRV(*keys), entry);
733 ind = SvIV(sv);
734 if (ind < 1)
735 croak("Bad index while coercing array into hash");
736 return *av_fetch(av, ind, TRUE);
737}
738
739SV *
740avhv_iternextsv(av, key, retlen)
741AV *av;
742char **key;
743I32 *retlen;
744{
745 SV **keys;
746 HE *he;
747 SV *sv;
748 I32 ind;
749
750 keys = av_fetch(av, 0, FALSE);
751 if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
752 croak("Can't coerce array into hash");
753 if ( (he = hv_iternext((HV*)SvRV(*keys))) == NULL)
754 return NULL;
755 *key = hv_iterkey(he, retlen);
756 sv = hv_iterval((HV*)SvRV(*keys), he);
757 ind = SvIV(sv);
758 if (ind < 1)
759 croak("Bad index while coercing array into hash");
760 return *av_fetch(av, ind, TRUE);
761}