1 /* $RCSfile: array.c,v $$Revision: 4.1 $$Date: 92/08/07 17:18:22 $
3 * Copyright (c) 1991, Larry Wall
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.
9 * Revision 4.1 92/08/07 17:18:22 lwall
12 * Revision 4.0.1.3 92/06/08 11:45:05 lwall
13 * patch20: Perl now distinguishes overlapped copies from non-overlapped
15 * Revision 4.0.1.2 91/11/05 16:00:14 lwall
16 * patch11: random cleanup
17 * patch11: passing non-existend array elements to subrouting caused core dump
19 * Revision 4.0.1.1 91/06/07 10:19:08 lwall
20 * patch4: new copyright notice
22 * Revision 4.0 91/03/20 01:03:32 lwall
39 if (mg_find((SV*)av,'P')) {
43 mg_copy((SV*)av, sv, 0, key);
53 if (key < 0 || key > AvFILL(av)) {
55 key += AvFILL(av) + 1;
66 return av_store(av,key,sv);
69 if (!AvARRAY(av)[key]) {
72 return av_store(av,key,sv);
76 return &AvARRAY(av)[key];
89 key += AvFILL(av) + 1;
95 if (mg_find((SV*)av,'P')) {
96 mg_copy((SV*)av, val, 0, key);
101 if (key > AvMAX(av)) {
104 if (AvALLOC(av) != AvARRAY(av)) {
105 tmp = AvARRAY(av) - AvALLOC(av);
106 Move(AvARRAY(av), AvALLOC(av), AvMAX(av)+1, SV*);
107 Zero(AvALLOC(av)+AvMAX(av)+1, tmp, SV*);
109 SvPVX(av) = (char*)(AvARRAY(av) - tmp);
110 if (key > AvMAX(av) - 10) {
111 newmax = key + AvMAX(av);
117 newmax = key + AvMAX(av) / 5;
119 Renew(AvALLOC(av),newmax+1, SV*);
120 Zero(&AvALLOC(av)[AvMAX(av)+1], newmax - AvMAX(av), SV*);
123 newmax = key < 4 ? 4 : key;
124 Newz(2,AvALLOC(av), newmax+1, SV*);
126 SvPVX(av) = (char*)AvALLOC(av);
132 if (AvFILL(av) < key) {
133 while (++AvFILL(av) < key) {
134 if (ary[AvFILL(av)] != Nullsv) {
135 SvREFCNT_dec(ary[AvFILL(av)]);
136 ary[AvFILL(av)] = Nullsv;
141 SvREFCNT_dec(ary[key]);
144 if (SvSMAGICAL(av)) {
145 MAGIC* mg = SvMAGIC(av);
146 sv_magic(val, (SV*)av, tolower(mg->mg_type), 0, key);
159 sv_upgrade(av,SVt_PVAV);
163 AvMAX(av) = AvFILL(av) = -1;
177 sv_upgrade(av,SVt_PVAV);
178 New(4,ary,size+1,SV*);
182 SvPVX(av) = (char*)ary;
183 AvFILL(av) = size - 1;
184 AvMAX(av) = size - 1;
185 for (i = 0; i < size; i++) {
188 sv_setsv(ary[i], *strp);
206 sv_upgrade(av,SVt_PVAV);
207 New(4,ary,size+1,SV*);
209 Copy(strp,ary,size,SV*);
211 SvPVX(av) = (char*)ary;
212 AvFILL(av) = size - 1;
213 AvMAX(av) = size - 1;
229 if (!av || !AvREAL(av) || AvMAX(av) < 0)
232 if (key = AvARRAY(av) - AvALLOC(av)) {
234 SvPVX(av) = (char*)(AvARRAY(av) - key);
236 for (key = 0; key <= AvMAX(av); key++)
237 SvREFCNT_dec(AvARRAY(av)[key]);
239 Zero(AvARRAY(av), AvMAX(av)+1, SV*);
251 if (key = AvARRAY(av) - AvALLOC(av)) {
253 SvPVX(av) = (char*)(AvARRAY(av) - key);
256 for (key = 0; key <= AvMAX(av); key++)
257 SvREFCNT_dec(AvARRAY(av)[key]);
259 Safefree(AvALLOC(av));
262 AvMAX(av) = AvFILL(av) = -1;
270 return av_store(av,++(AvFILL(av)),val) != 0;
281 retval = AvARRAY(av)[AvFILL(av)];
282 AvARRAY(av)[AvFILL(av)--] = Nullsv;
292 register I32 fill = AvFILL(av);
294 while (fill >= 0 && !AvARRAY(av)[fill])
305 register SV **sstr,**dstr;
309 if (AvARRAY(av) - AvALLOC(av) >= num) {
313 SvPVX(av) = (char*)(AvARRAY(av) - 1);
314 *AvARRAY(av) = Nullsv;
318 (void)av_store(av,AvFILL(av)+num,(SV*)0); /* maybe extend array */
319 dstr = AvARRAY(av) + AvFILL(av);
322 # pragma loop_opt(off) /* don't loop-optimize the following code */
323 #endif /* BUGGY_MSC5 */
324 for (i = AvFILL(av) - num; i >= 0; i--) {
327 # pragma loop_opt() /* loop-optimization back to command-line setting */
328 #endif /* BUGGY_MSC5 */
330 Zero(AvARRAY(av), num, SV*);
342 retval = *AvARRAY(av);
343 *AvARRAY(av) = Nullsv;
344 SvPVX(av) = (char*)(AvARRAY(av) + 1);
366 if (fill <= AvMAX(av)) {
372 AvFILL(av) = fill - 1; /* don't clobber in-between values */
373 (void)av_store(av,fill,Nullsv);