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
  
1001
  
1002
  
1003
  
1004
  
1005
  
1006
  
1007
  
1008
  
1009
  
1010
  
1011
  
1012
  
1013
  
1014
  
1015
  
1016
  
1017
  
1018
  
1019
  
1020
  
1021
  
1022
  
1023
  
1024
  
1025
  
1026
  
1027
  
1028
  
1029
  
1030
  
1031
  
1032
  
1033
  
1034
  
1035
  
1036
  
1037
  
1038
  
1039
  
1040
  
1041
  
1042
  
1043
  
1044
  
1045
  
1046
  
1047
  
1048
  
1049
  
1050
  
1051
  
1052
  
1053
  
1054
  
1055
  
1056
  
1057
  
1058
  
1059
  
1060
  
1061
  
1062
  
1063
  
1064
  
1065
  
1066
  
1067
  
1068
  
1069
  
1070
  
1071
  
1072
  
1073
  
1074
  
1075
  
1076
  
1077
  
1078
  
1079
  
1080
  
1081
  
1082
  
1083
  
1084
  
1085
  
1086
  
1087
  
1088
  
1089
  
1090
  
1091
  
1092
  
1093
  
1094
  
1095
  
1096
  
1097
  
1098
  
1099
  
1100
  
1101
  
1102
  
1103
  
1104
  
1105
  
1106
  
1107
  
1108
  
1109
  
1110
  
1111
  
1112
  
1113
  
1114
  
1115
  
1116
  
1117
  
1118
  
1119
  
1120
  
1121
  
1122
  
1123
  
1124
  
1125
  
1126
  
1127
  
1128
  
1129
  
1130
  
1131
  
1132
  
1133
  
1134
  
1135
  
1136
  
1137
  
1138
  
1139
  
1140
  
1141
  
1142
  
1143
  
1144
  
1145
  
1146
  
1147
  
1148
  
1149
  
1150
  
1151
  
1152
  
1153
  
1154
  
1155
  
1156
  
1157
  
1158
  
1159
  
1160
  
1161
  
1162
  
1163
  
1164
  
1165
  
1166
  
1167
  
1168
  
1169
  
1170
  
1171
  
1172
  
1173
  
1174
  
1175
  
1176
  
1177
  
1178
  
1179
  
1180
  
1181
  
1182
  
1183
  
1184
  
1185
  
1186
  
1187
  
1188
  
1189
  
1190
  
1191
  
1192
  
1193
  
1194
  
1195
  
1196
  
1197
  
1198
  
1199
  
1200
  
1201
  
1202
  
1203
  
1204
  
1205
  
1206
  
1207
  
1208
  
1209
  
1210
  
1211
  
1212
  
1213
  
1214
  
1215
  
1216
  
1217
  
1218
  
1219
  
1220
  
1221
  
1222
  
1223
  
1224
  
1225
  
1226
  
1227
  
1228
  
1229
  
1230
  
1231
  
1232
  
1233
  
1234
  
1235
  
1236
  
1237
  
1238
  
1239
  
1240
  
1241
  
1242
  
1243
  
1244
  
1245
  
1246
  
1247
  
1248
  
1249
  
1250
  
1251
  
1252
  
1253
  
1254
  
1255
  
1256
  
1257
  
1258
  
1259
  
1260
  
1261
  
1262
  
1263
  
1264
  
1265
  
1266
  
1267
  
1268
  
1269
  
1270
  
1271
  
1272
  
1273
  
1274
  
1275
  
1276
  
1277
  
1278
  
1279
  
1280
  
1281
  
1282
  
1283
  
1284
  
1285
  
1286
  
1287
  
1288
  
1289
  
1290
  
1291
  
1292
  
1293
  
1294
  
1295
  
1296
  
1297
  
1298
  
1299
  
1300
  
1301
  
1302
  
1303
  
1304
  
1305
  
1306
  
1307
  
1308
  
1309
  
1310
  
1311
  
1312
  
1313
  
1314
  
1315
  
1316
  
1317
  
1318
  
1319
  
1320
  
1321
  
1322
  
1323
  
1324
  
1325
  
1326
  
1327
  
1328
  
1329
  
1330
  
1331
  
1332
  
1333
  
1334
  
1335
  
1336
  
1337
  
1338
  
1339
  
1340
  
1341
  
1342
  
1343
  
1344
  
1345
  
1346
  
1347
  
1348
  
1349
  
1350
  
1351
  
1352
  
1353
  
1354
  
1355
  
1356
  
1357
  
1358
  
1359
  
1360
  
1361
  
1362
  
1363
  
1364
  
1365
  
1366
  
1367
  
1368
  
1369
  
1370
  
1371
  
1372
  
1373
  
1374
  
1375
  
1376
  
1377
  
1378
  
1379
  
1380
  
1381
  
1382
  
1383
  
1384
  
1385
  
1386
  
1387
  
1388
  
1389
  
1390
  
1391
  
1392
  
1393
  
1394
  
1395
  
1396
  
1397
  
1398
  
1399
  
1400
  
1401
  
1402
  
1403
  
1404
  
1405
  
1406
  
1407
  
1408
  
1409
  
1410
  
1411
  
1412
  
1413
  
1414
  
1415
  
1416
  
1417
  
1418
  
1419
  
1420
  
1421
  
1422
  
1423
  
1424
  
1425
  
1426
  
1427
  
1428
  
1429
  
1430
  
1431
  
1432
  
1433
  
1434
  
1435
  
1436
  
1437
  
1438
  
1439
  
1440
  
1441
  
1442
  
1443
  
1444
  
1445
  
1446
  
1447
  
1448
  
1449
  
1450
  
1451
  
1452
  
1453
  
1454
  
1455
  
1456
  
1457
  
1458
  
1459
  
1460
  
1461
  
1462
  
1463
  
1464
  
1465
  
1466
  
1467
  
1468
  
1469
  
1470
  
1471
  
1472
  
1473
  
1474
  
1475
  
1476
  
1477
  
1478
  
1479
  
1480
  
1481
  
1482
  
1483
  
1484
  
1485
  
1486
  
1487
  
1488
  
1489
  
1490
  
1491
  
1492
  
1493
  
1494
  
1495
  
1496
  
1497
  
1498
  
1499
  
1500
  
1501
  
1502
  
1503
  
1504
  
1505
  
1506
  
1507
  
1508
  
1509
  
1510
  
1511
  
1512
  
1513
  
1514
  
1515
  
1516
  
1517
  
1518
  
1519
  
1520
  
1521
  
1522
  
1523
  
1524
  
1525
  
1526
  
1527
  
1528
  
1529
  
1530
  
1531
  
1532
  
1533
  
1534
  
1535
  
1536
  
1537
  
1538
  
1539
  
1540
  
1541
  
1542
  
1543
  
1544
  
1545
  
1546
  
1547
  
1548
  
1549
  
1550
  
1551
  
1552
  
1553
  
1554
  
1555
  
1556
  
1557
  
1558
  
1559
  
1560
  
1561
  
1562
  
1563
  
1564
  
1565
  
1566
  
1567
  
1568
  
1569
  
1570
  
1571
  
1572
  
1573
  
1574
  
1575
  
1576
  
1577
  
1578
  
1579
  
1580
  
1581
  
1582
  
1583
  
1584
  
1585
  
1586
  
1587
  
1588
  
1589
  
1590
  
1591
  
1592
  
1593
  
1594
  
1595
  
1596
  
1597
  
1598
  
1599
  
1600
  
1601
  
1602
  
1603
  
1604
  
1605
  
1606
  
1607
  
1608
  
1609
  
1610
  
1611
  
1612
  
1613
  
1614
  
1615
  
1616
  
1617
  
1618
  
1619
  
1620
  
1621
  
1622
  
1623
  
1624
  
1625
  
1626
  
1627
  
1628
  
1629
  
1630
  
1631
  
1632
  
1633
  
1634
  
1635
  
1636
  
1637
  
1638
  
1639
  
1640
  
1641
  
1642
  
1643
  
1644
  
1645
  
1646
  
1647
  
1648
  
1649
  
1650
  
1651
  
1652
  
1653
  
1654
  
1655
  
1656
  
1657
  
1658
  
1659
  
1660
  
1661
  
1662
  
1663
  
1664
  
1665
  
1666
  
1667
  
1668
  
1669
  
1670
  
1671
  
1672
  
1673
  
1674
  
1675
  
1676
  
1677
  
1678
  
1679
  
1680
  
1681
  
1682
  
1683
  
1684
  
1685
  
1686
  
1687
  
1688
  
1689
  
1690
  
1691
  
1692
  
1693
  
1694
  
1695
  
1696
  
1697
  
1698
  
1699
  
1700
  
1701
  
1702
  
1703
  
1704
  
1705
  
1706
  
1707
  
1708
  
1709
  
1710
  
1711
  
1712
  
1713
  
1714
  
1715
  
1716
  
1717
  
1718
  
1719
  
1720
  
1721
  
1722
  
1723
  
1724
  
1725
  
1726
  
1727
  
1728
  
1729
  
1730
  
1731
  
1732
  
1733
  
1734
  
1735
  
1736
  
1737
  
1738
  
1739
  
1740
  
1741
  
1742
  
1743
  
1744
  
1745
  
1746
  
1747
  
1748
  
1749
  
1750
  
1751
  
1752
  
1753
  
1754
  
1755
  
1756
  
1757
  
1758
  
1759
  
1760
  
1761
  
1762
  
1763
  
1764
  
1765
  
1766
  
1767
  
1768
  
1769
  
1770
  
1771
  
1772
  
1773
  
1774
  
1775
  
1776
  
1777
  
1778
  
1779
  
1780
  
1781
  
1782
  
1783
  
1784
  
1785
  
1786
  
1787
  
1788
  
1789
  
1790
  
1791
  
1792
  
1793
  
1794
  
1795
  
1796
  
1797
  
1798
  
1799
  
1800
  
1801
  
1802
  
1803
  
1804
  
1805
  
1806
  
1807
  
1808
  
1809
  
1810
  
1811
  
1812
  
1813
  
1814
  
1815
  
1816
  
1817
  
1818
  
1819
  
1820
  
1821
  
1822
  
1823
  
1824
  
1825
  
1826
  
1827
  
1828
  
1829
  
1830
  
1831
  
1832
  
1833
  
1834
  
1835
  
1836
  
1837
  
1838
  
1839
  
1840
  
1841
  
1842
  
1843
  
1844
  
1845
  
1846
  
1847
  
1848
  
1849
  
1850
  
1851
  
1852
  
1853
  
1854
  
1855
  
1856
  
1857
  
1858
  
1859
  
1860
  
1861
  
1862
  
1863
  
1864
  
1865
  
1866
  
1867
  
1868
  
1869
  
1870
  
1871
  
1872
  
1873
  
1874
  
1875
  
1876
  
1877
  
1878
  
1879
  
1880
  
1881
  
1882
  
1883
  
1884
  
1885
  
1886
  
1887
  
1888
  
1889
  
1890
  
1891
  
1892
  
1893
  
1894
  
1895
  
1896
  
1897
  
1898
  
1899
  
1900
  
1901
  
1902
  
1903
  
1904
  
1905
  
1906
  
1907
  
1908
  
1909
  
1910
  
1911
  
1912
  
1913
  
1914
  
1915
  
1916
  
1917
  
1918
  
1919
  
1920
  
1921
  
1922
  
1923
  
1924
  
1925
  
1926
  
1927
  
1928
  
1929
  
1930
  
1931
  
1932
  
1933
  
1934
  
1935
  
1936
  
1937
  
1938
  
1939
  
1940
  
1941
  
1942
  
1943
  
1944
  
1945
  
1946
  
1947
  
1948
  
1949
  
1950
  
1951
  
1952
  
1953
  
1954
  
1955
  
1956
  
1957
  
1958
  
1959
  
1960
  
1961
  
1962
  
1963
  
1964
  
1965
  
1966
  
1967
  
1968
  
1969
  
1970
  
1971
  
1972
  
1973
  
1974
  
1975
  
1976
  
1977
  
1978
  
1979
  
1980
  
1981
  
1982
  
1983
  
1984
  
1985
  
1986
  
1987
  
1988
  
1989
  
1990
  
1991
  
1992
  
1993
  
1994
  
1995
  
1996
  
1997
  
1998
  
1999
  
2000
  
2001
  
2002
  
2003
  
2004
  
2005
  
2006
  
2007
  
2008
  
2009
  
2010
  
2011
  
2012
  
2013
  
2014
  
2015
  
2016
  
2017
  
2018
  
2019
  
2020
  
2021
  
2022
  
2023
  
2024
  
2025
  
2026
  
2027
  
2028
  
2029
  
2030
  
2031
  
2032
  
2033
  
2034
  
2035
  
2036
  
2037
  
2038
  
2039
  
2040
  
2041
  
2042
  
2043
  
2044
  
2045
  
2046
  
2047
  
2048
  
2049
  
2050
  
2051
  
2052
  
2053
  
2054
  
2055
  
2056
  
2057
  
2058
  
2059
  
2060
  
2061
  
2062
  
2063
  
2064
  
2065
  
2066
  
2067
  
2068
  
2069
  
2070
  
2071
  
2072
  
2073
  
2074
  
2075
  
2076
  
2077
  
2078
  
2079
  
2080
  
2081
  
2082
  
2083
  
2084
  
2085
  
2086
  
2087
  
2088
  
2089
  
2090
  
2091
  
2092
  
2093
  
2094
  
2095
  
2096
  
2097
  
2098
  
2099
  
2100
  
2101
  
2102
  
2103
  
2104
  
2105
  
2106
  
2107
  
2108
  
2109
  
2110
  
2111
  
2112
  
2113
  
2114
  
2115
  
2116
  
2117
  
2118
  
2119
  
2120
  
2121
  
2122
  
2123
  
2124
  
2125
  
2126
  
2127
  
2128
  
2129
  
2130
  
2131
  
2132
  
2133
  
2134
  
2135
  
2136
  
2137
  
2138
  
2139
  
2140
  
2141
  
2142
  
2143
  
2144
  
2145
  
2146
  
2147
  
2148
  
2149
  
2150
  
2151
  
2152
  
2153
  
2154
  
2155
  
2156
  
2157
  
2158
  
2159
  
2160
  
2161
  
2162
  
2163
  
2164
  
2165
  
2166
  
2167
  
2168
  
2169
  
2170
  
2171
  
2172
  
2173
  
2174
  
2175
  
2176
  
2177
  
2178
  
2179
  
2180
  
2181
  
2182
  
2183
  
2184
  
2185
  
2186
  
2187
  
2188
  
2189
  
2190
  
2191
  
2192
  
2193
  
2194
  
2195
  
2196
  
2197
  
2198
  
2199
  
2200
  
2201
  
2202
  
2203
  
2204
  
2205
  
2206
  
2207
  
2208
  
2209
  
2210
  
2211
  
2212
  
2213
  
2214
  
2215
  
2216
  
2217
  
2218
  
2219
  
2220
  
2221
  
2222
  
2223
  
2224
  
2225
  
2226
  
2227
  
2228
  
2229
  
2230
  
2231
  
2232
  
2233
  
2234
  
2235
  
2236
  
2237
  
2238
  
2239
  
2240
  
2241
  
2242
  
2243
  
2244
  
2245
  
2246
  
2247
  
2248
  
2249
  
2250
  
2251
  
2252
  
2253
  
2254
  
2255
  
2256
  
2257
  
2258
  
2259
  
2260
  
2261
  
2262
  
2263
  
2264
  
2265
  
2266
  
2267
  
2268
  
2269
  
2270
  
2271
  
2272
  
2273
  
2274
  
2275
  
2276
  
2277
  
2278
  
2279
  
2280
  
2281
  
2282
  
2283
  
2284
  
2285
  
2286
  
2287
  
2288
  
2289
  
2290
  
2291
  
2292
  
2293
  
2294
  
2295
  
2296
  
2297
  
2298
  
2299
  
2300
  
2301
  
2302
  
2303
  
2304
  
2305
  
2306
  
2307
  
2308
  
2309
  
2310
  
2311
  
2312
  
2313
  
2314
  
2315
  
2316
  
2317
  
2318
  
2319
  
2320
  
2321
  
2322
  
2323
  
2324
  
2325
  
2326
  
2327
  
2328
  
2329
  
2330
  
2331
  
2332
  
2333
  
2334
  
2335
  
2336
  
2337
  
2338
  
2339
  
2340
  
2341
  
2342
  
2343
  
2344
  
2345
  
2346
  
2347
  
2348
  
2349
  
2350
  
2351
  
2352
  
2353
  
2354
  
2355
  
2356
  
2357
  
2358
  
2359
  
2360
  
2361
  
2362
  
2363
  
2364
  
2365
  
2366
  
2367
  
2368
  
2369
  
2370
  
2371
  
2372
  
2373
  
2374
  
2375
  
2376
  
2377
  
2378
  
2379
  
2380
  
2381
  
2382
  
2383
  
2384
  
2385
  
2386
  
2387
  
2388
  
2389
  
2390
  
2391
  
2392
  
2393
  
2394
  
2395
  
2396
  
2397
  
2398
  
2399
  
2400
  
2401
  
2402
  
2403
  
2404
  
2405
  
2406
  
2407
  
2408
  
2409
  
2410
  
2411
  
2412
  
2413
  
2414
  
2415
  
2416
  
2417
  
2418
  
2419
  
2420
  
2421
  
2422
  
2423
  
2424
  
2425
  
2426
  
2427
  
2428
  
2429
  
2430
  
2431
  
2432
  
2433
  
2434
  
2435
  
2436
  
2437
  
2438
  
2439
  
2440
  
2441
  
2442
  
2443
  
2444
  
2445
  
2446
  
2447
  
2448
  
2449
  
2450
  
2451
  
2452
  
2453
  
2454
  
2455
  
2456
  
2457
  
2458
  
2459
  
2460
  
2461
  
2462
  
2463
  
2464
  
2465
  
2466
  
2467
  
2468
  
2469
  
2470
  
2471
  
2472
  
2473
  
2474
  
2475
  
2476
  
2477
  
2478
  
2479
  
2480
  
2481
  
2482
  
2483
  
2484
  
2485
  
2486
  
2487
  
2488
  
2489
  
2490
  
2491
  
2492
  
2493
  
2494
  
2495
  
2496
  
2497
  
2498
  
2499
  
2500
  
2501
  
2502
  
2503
  
2504
  
2505
  
2506
  
2507
  
2508
  
2509
  
2510
  
2511
  
2512
  
2513
  
2514
  
2515
  
2516
  
2517
  
2518
  
2519
  
2520
  
2521
  
2522
  
2523
  
2524
  
2525
  
2526
  
2527
  
2528
  
2529
  
2530
  
2531
  
2532
  
2533
  
2534
  
2535
  
2536
  
2537
  
2538
  
2539
  
2540
  
2541
  
2542
  
2543
  
2544
  
2545
  
2546
  
2547
  
2548
  
2549
  
2550
  
2551
  
2552
  
2553
  
2554
  
2555
  
2556
  
2557
  
2558
  
2559
  
2560
  
2561
  
2562
  
2563
  
2564
  
2565
  
2566
  
2567
  
2568
  
2569
  
2570
  
2571
  
2572
  
2573
  
2574
  
2575
  
2576
  
2577
  
2578
  
2579
  
2580
  
2581
  
2582
  
2583
  
2584
  
2585
  
2586
  
2587
  
2588
  
2589
  
2590
  
2591
  
2592
  
2593
  
2594
  
2595
  
2596
  
2597
  
2598
  
2599
  
2600
  
2601
  
2602
  
2603
  
2604
  
2605
  
2606
  
2607
  
2608
  
2609
  
2610
  
2611
  
2612
  
2613
  
2614
  
2615
  
2616
  
2617
  
2618
  
2619
  
2620
  
2621
  
2622
  
2623
  
2624
  
2625
  
2626
  
2627
  
2628
  
2629
  
2630
  
2631
  
2632
  
2633
  
2634
  
2635
  
2636
  
2637
  
2638
  
2639
  
2640
  
2641
  
2642
  
2643
  
2644
  
2645
  
2646
  
2647
  
2648
  
2649
  
2650
  
2651
  
2652
  
2653
  
2654
  
2655
  
2656
  
2657
  
2658
  
2659
  
2660
  
2661
  
2662
  
2663
  
2664
  
2665
  
2666
  
2667
  
2668
  
2669
  
2670
  
2671
  
2672
  
2673
  
2674
  
2675
  
2676
  
2677
  
2678
  
2679
  
2680
  
2681
  
2682
  
2683
  
2684
  
2685
  
2686
  
2687
  
2688
  
2689
  
2690
  
2691
  
2692
  
2693
  
2694
  
2695
  
2696
  
2697
  
2698
  
2699
  
2700
  
2701
  
2702
  
2703
  
2704
  
2705
  
2706
  
2707
  
2708
  
2709
  
2710
  
2711
  
2712
  
2713
  
2714
  
2715
  
2716
  
2717
  
2718
  
2719
  
2720
  
2721
  
2722
  
2723
  
2724
  
2725
  
2726
  
2727
  
2728
  
2729
  
2730
  
2731
  
2732
  
2733
  
2734
  
2735
  
2736
  
2737
  
2738
  
2739
  
2740
  
2741
  
2742
  
2743
  
2744
  
2745
  
2746
  
2747
  
2748
  
2749
  
2750
  
2751
  
2752
  
2753
  
2754
  
2755
  
2756
  
2757
  
2758
  
2759
  
2760
  
2761
  
2762
  
2763
  
2764
  
2765
  
2766
  
2767
  
2768
  
2769
  
2770
  
2771
  
2772
  
2773
  
2774
  
2775
  
2776
  
2777
  
2778
  
2779
  
2780
  
2781
  
2782
  
2783
  
2784
  
2785
  
2786
  
2787
  
2788
  
2789
  
2790
  
2791
  
2792
  
2793
  
2794
  
2795
  
2796
  
2797
  
2798
  
2799
  
2800
  
2801
  
2802
  
2803
  
2804
  
2805
  
2806
  
2807
  
2808
  
2809
  
2810
  
2811
  
2812
  
2813
  
2814
  
2815
  
2816
  
2817
  
2818
  
2819
  
2820
  
2821
  
2822
  
2823
  
2824
  
2825
  
2826
  
2827
  
2828
  
2829
  
2830
  
2831
  
2832
  
2833
  
2834
  
2835
  
2836
  
2837
  
2838
  
2839
  
2840
  
2841
  
2842
  
2843
  
2844
  
2845
  
2846
  
2847
  
2848
  
2849
  
2850
  
2851
  
2852
  
2853
  
2854
  
2855
  
2856
  
2857
  
2858
  
2859
  
2860
  
2861
  
2862
  
2863
  
2864
  
2865
  
2866
  
2867
  
2868
  
2869
  
2870
  
2871
  
2872
  
2873
  
2874
  
2875
  
2876
  
2877
  
2878
  
2879
  
2880
  
2881
  
2882
  
2883
  
2884
  
2885
  
2886
  
2887
  
2888
  
2889
  
2890
  
2891
  
2892
  
2893
  
2894
  
2895
  
2896
  
2897
  
2898
  
2899
  
2900
  
2901
  
2902
  
2903
  
2904
  
2905
  
2906
  
2907
  
2908
  
2909
  
2910
  
2911
  
2912
  
2913
  
2914
  
2915
  
2916
  
2917
  
2918
  
2919
  
2920
  
2921
  
2922
  
2923
  
2924
  
2925
  
2926
  
2927
  
2928
  
2929
  
2930
  
2931
  
2932
  
2933
  
2934
  
2935
  
2936
  
2937
  
2938
  
2939
  
2940
  
2941
  
2942
  
2943
  
2944
  
2945
  
2946
  
2947
  
2948
  
2949
  
2950
  
2951
  
2952
  
2953
  
2954
  
2955
  
2956
  
2957
  
2958
  
2959
  
2960
  
2961
  
2962
  
2963
  
2964
  
2965
  
2966
  
2967
  
2968
  
2969
  
2970
  
2971
  
2972
  
2973
  
2974
  
2975
  
2976
  
2977
  
2978
  
2979
  
2980
  
2981
  
2982
  
2983
  
2984
  
2985
  
2986
  
2987
  
2988
  
2989
  
2990
  
2991
  
2992
  
2993
  
2994
  
2995
  
2996
  
2997
  
2998
  
2999
  
3000
  
3001
  
3002
  
3003
  
3004
  
3005
  
3006
  
3007
  
3008
  
3009
  
3010
  
3011
  
3012
  
3013
  
3014
  
3015
  
3016
  
3017
  
3018
  
3019
  
3020
  
3021
  
3022
  
3023
  
3024
  
3025
  
3026
  
3027
  
3028
  
3029
  
3030
  
3031
  
3032
  
3033
  
3034
  
3035
  
3036
  
3037
  
3038
  
3039
  
3040
  
3041
  
3042
  
3043
  
3044
  
3045
  
3046
  
3047
  
3048
  
3049
  
3050
  
3051
  
3052
  
3053
  
3054
  
3055
  
3056
  
3057
  
3058
  
3059
  
3060
  
3061
  
3062
  
3063
  
3064
  
3065
  
3066
  
3067
  
3068
  
3069
  
3070
  
3071
  
3072
  
3073
  
3074
  
3075
  
3076
  
3077
  
3078
  
3079
  
3080
  
3081
  
3082
  
3083
  
3084
  
3085
  
3086
  
3087
  
3088
  
3089
  
3090
  
3091
  
3092
  
3093
  
3094
  
3095
  
3096
  
3097
  
3098
  
3099
  
3100
  
3101
  
3102
  
3103
  
3104
  
3105
  
3106
  
3107
  
3108
  
3109
  
3110
  
3111
  
3112
  
3113
  
3114
  
3115
  
3116
  
3117
  
3118
  
3119
  
3120
  
3121
  
3122
  
3123
  
3124
  
3125
  
3126
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
#include "global.h" 
#include "svalue.h" 
#include "array.h" 
#include "object.h" 
#include "las.h" 
#include "stralloc.h" 
#include "interpret.h" 
#include "opcodes.h" 
#include "pike_error.h" 
#include "pike_types.h" 
#include "fsort.h" 
#include "builtin_functions.h" 
#include "pike_memory.h" 
#include "gc.h" 
#include "main.h" 
#include "cyclic.h" 
#include "multiset.h" 
#include "mapping.h" 
#include "bignum.h" 
#include "pike_search.h" 
 
/** The empty array. */ 
PMOD_EXPORT struct array empty_array= 
{ 
  PIKE_CONSTANT_MEMOBJ_INIT(1, PIKE_T_ARRAY), /* Never free */ 
  0,                     /* Size = 0 */ 
  0,                     /* malloced Size = 0 */ 
  0,                     /* no types */ 
  0,                     /* no flags */ 
  &weak_empty_array,     /* Next */ 
  0,                     /* previous */ 
  empty_array.real_item, /* Initialize the item pointer. */ 
  {SVALUE_INIT_FREE}, 
}; 
 
/** The empty weak array. */ 
PMOD_EXPORT struct array weak_empty_array= 
{ 
  PIKE_CONSTANT_MEMOBJ_INIT(1, PIKE_T_ARRAY), /* Never free */ 
  0,                     /* Size = 0 */ 
  0,                     /* malloced Size = 0 */ 
  0,                     /* no types */ 
  ARRAY_WEAK_FLAG,       /* weak */ 
  0,                     /* next */ 
  &empty_array,          /* previous */ 
  weak_empty_array.real_item, /* Initialize the item pointer. */ 
  {SVALUE_INIT_FREE}, 
}; 
 
struct array *first_array = &empty_array; 
struct array *gc_internal_array = 0; 
static struct array *gc_mark_array_pos; 
 
#ifdef TRACE_UNFINISHED_TYPE_FIELDS 
PMOD_EXPORT int accept_unfinished_type_fields = 0; 
PMOD_EXPORT void dont_accept_unfinished_type_fields (void *orig) 
{ 
  accept_unfinished_type_fields = (int) orig; 
} 
#endif 
 
 
/** 
 * Allocate an array. This might be changed in the future to allocate 
 * linked lists or something. The new array has zero references. 
 * 
 * When building arrays, it is recommended that you push the values on 
 * the stack and call aggregate_array or f_aggregate instead of 
 * allocating and filling in the values 'by hand'. 
 * 
 * @param size The size of the new array, in elements. 
 * @param extra_space The number of extra elements space 
 * should be reserved for. 
 * @return A pointer to the allocated array struct. 
 */ 
PMOD_EXPORT struct array *real_allocate_array(ptrdiff_t size, 
                                              ptrdiff_t extra_space) 
{ 
  struct array *v; 
  size_t length = size; 
 
  if (DO_SIZE_T_ADD_OVERFLOW(length, (size_t)extra_space, &length)) goto TOO_BIG; 
 
  if(length == 0) 
  { 
    add_ref(&empty_array); 
    return &empty_array; 
  } 
 
  /* 
   * Do we really need this limit? 
   *    - arne 
   */ 
  if (length > 1U<<29) goto TOO_BIG; 
 
  /* struct array contains one svalue already */ 
  length --; 
 
  if (DO_SIZE_T_MUL_OVERFLOW(length, sizeof(struct svalue), &length) || 
      DO_SIZE_T_ADD_OVERFLOW(length, sizeof(struct array), &length)) goto TOO_BIG; 
 
  v=xcalloc(length, 1); 
 
  GC_ALLOC(v); 
  gc_init_marker(v); 
 
  /* for now, we don't know what will go in here */ 
  v->type_field = BIT_MIXED | BIT_UNFINISHED; 
 
  v->malloced_size = (INT32)(size + extra_space); 
  v->item=v->real_item; 
  v->size = (INT32)size; 
  INIT_PIKE_MEMOBJ(v, T_ARRAY); 
  DOUBLELINK (first_array, v); 
 
  return v; 
TOO_BIG: 
  Pike_error("Too large array (size %ld is too big).\n", length); 
} 
 
/** 
 * Free an array without freeing the values inside it. 
 * Any values inside of the array will be kept. 
 * @param v The array to be freed. 
 */ 
static void array_free_no_free(struct array *v) 
{ 
  DOUBLEUNLINK (first_array, v); 
 
  free(v); 
 
  GC_FREE(v); 
} 
 
/** 
 * Free an array. Call this when the array has zero references. 
 * @param v The array to free. 
 */ 
PMOD_EXPORT void really_free_array(struct array *v) 
{ 
#ifdef PIKE_DEBUG 
  if(v == & empty_array || v == &weak_empty_array) 
    Pike_fatal("Tried to free some *_empty_array.\n"); 
  if (v->refs) { 
#ifdef DEBUG_MALLOC 
    describe_something(v, T_ARRAY, 0,2,0, NULL); 
#endif 
    Pike_fatal("Freeing array with %d refs.\n", v->refs); 
  } 
#endif 
 
#ifdef PIKE_DEBUG 
  if(d_flag > 1)  array_check_type_field(v); 
#endif 
 
  add_ref(v); 
  EXIT_PIKE_MEMOBJ(v); 
  free_svalues(ITEM(v), v->size, v->type_field); 
  sub_ref(v); 
  array_free_no_free(v); 
} 
 
/** 
 *  Decrement the references (and free if unused) an array if it is not null. 
 */ 
PMOD_EXPORT void do_free_array(struct array *a) 
{ 
  if (a) 
    free_array(a); 
} 
 
/** 
 *  Free all elements in an array and set them to zero. 
 */ 
PMOD_EXPORT void clear_array(struct array *a) 
{ 
  if (!a->size) return; 
  free_svalues(ITEM(a), a->size, a->type_field); 
  /* NB: We know that INT_T == 0. */ 
  memset(ITEM(a), 0, a->size * sizeof(struct svalue)); 
  a->type_field = BIT_INT; 
} 
 
/** 
 *  Set the flags on an array. If the array is empty then only the 
 *  weak flag is significant. 
 */ 
PMOD_EXPORT struct array *array_set_flags(struct array *a, int flags) 
{ 
  if (a->size) 
    a->flags = flags; 
  else { 
    free_array(a); 
    if (flags & ARRAY_WEAK_FLAG) 
      add_ref(a = &weak_empty_array); 
    else 
      add_ref(a = &empty_array); 
  } 
  return a; 
} 
 
 
/** 
 * Extract an svalue from an array. This function frees the contents of 
 * of the svalue 's' and replaces it with a copy of the 
 * contents from index 'index' in the array 'v'. 
 * 
 * @param index The index of the array to be extracted. 
 * @param s The recipient of the extracted array element. 
 * @param v The array to extract the element from. 
 * 
 * This function is similar to 
 *    assign_svalue(s, v->item + n); 
 * except that it adds debug and safety measures. Usually, this function 
 * is not needed. 
 * 
 * @note If n is out of bounds, Pike will dump core. If Pike was compiled 
 * with DEBUG, a message will be written first stating what the problem was. 
 */ 
PMOD_EXPORT void array_index(struct svalue *s,struct array *v,INT32 index) 
{ 
#ifdef PIKE_DEBUG 
  if(index<0 || index>=v->size) 
    Pike_fatal("Illegal index in low level index routine.\n"); 
#endif 
 
  add_ref(v); 
  assign_svalue(s, ITEM(v) + index); 
  free_array(v); 
} 
 
/** Is destructive on data if destructive is set and it only has one ref. */ 
PMOD_EXPORT struct array *array_column (struct array *data, struct svalue *index, 
                                        int destructive) 
{ 
  int e; 
  struct array *a; 
  TYPE_FIELD types = 0; 
 
  DECLARE_CYCLIC(); 
 
  /* Optimization */ 
  if(data->refs == 1 && destructive) 
  { 
    /* An array with one ref cannot possibly be cyclic */ 
    struct svalue sval; 
    data->type_field = BIT_MIXED | BIT_UNFINISHED; 
    for(e=0;e<data->size;e++) 
    { 
      index_no_free(&sval, ITEM(data)+e, index); 
      types |= 1 << TYPEOF(sval); 
      free_svalue(ITEM(data)+e); 
      move_svalue (ITEM(data) + e, &sval); 
    } 
    data->type_field = types; 
    add_ref (data); 
    return data; 
  } 
 
  if((a=(struct array *)BEGIN_CYCLIC(data,0))) 
  { 
    add_ref(a); 
  }else{ 
    push_array(a=allocate_array(data->size)); 
    SET_CYCLIC_RET(a); 
 
    for(e=0;e<a->size;e++) { 
      index_no_free(ITEM(a)+e, ITEM(data)+e, index); 
      types |= 1 << TYPEOF(ITEM(a)[e]); 
    } 
    a->type_field = types; 
 
    dmalloc_touch_svalue(Pike_sp-1); 
    Pike_sp--; 
  } 
  END_CYCLIC(); 
 
  return a; 
} 
 
PMOD_EXPORT void simple_array_index_no_free(struct svalue *s, 
                                struct array *a,struct svalue *ind) 
{ 
  switch(TYPEOF(*ind)) 
  { 
    case T_INT: { 
      INT_TYPE p = ind->u.integer; 
      INT_TYPE i = p < 0 ? p + a->size : p; 
      if(i<0 || i>=a->size) { 
        struct svalue tmp; 
        SET_SVAL(tmp, T_ARRAY, 0, array, a); 
        if (a->size) { 
          index_error(0,&tmp,ind, 
                      "Index %"PRINTPIKEINT"d is out of array range " 
                      "%d..%d.\n", p, -a->size, a->size-1); 
        } else { 
          index_error(0,&tmp,ind, 
                      "Attempt to index the empty array with %"PRINTPIKEINT"d.\n", p); 
        } 
      } 
      array_index_no_free(s,a,i); 
      break; 
    } 
 
    case T_STRING: 
    { 
      SET_SVAL(*s, T_ARRAY, 0, array, array_column(a, ind, 0)); 
      break; 
    } 
 
    default: 
      { 
        struct svalue tmp; 
        SET_SVAL(tmp, T_ARRAY, 0, array, a); 
        index_error(0,&tmp,ind,"Array index is neither int nor string.\n"); 
      } 
  } 
} 
 
/** 
 * Extract an svalue from an array. 
 */ 
PMOD_EXPORT void array_free_index(struct array *v,INT32 index) 
{ 
#ifdef PIKE_DEBUG 
  if(index<0 || index>=v->size) 
    Pike_fatal("Illegal index in low level free index routine.\n"); 
#endif 
 
  free_svalue(ITEM(v) + index); 
} 
 
/** set an element in an array to a value. 
 * 
 *  @param a the array whose element is to be set 
 *  @param ind an int or string containing the index to set 
 *  @param s the value to set 
 */ 
PMOD_EXPORT void simple_set_index(struct array *a,struct svalue *ind,struct svalue *s) 
{ 
  switch (TYPEOF(*ind)) { 
    case T_INT: { 
      INT_TYPE p = ind->u.integer; 
      INT_TYPE i = p < 0 ? p + a->size : p; 
      if(i<0 || i>=a->size) { 
        if (a->size) { 
          Pike_error("Index %"PRINTPIKEINT"d is out of array range " 
                     "%d..%d.\n", p, -a->size, a->size-1); 
        } else { 
          Pike_error("Attempt to index the empty array with %"PRINTPIKEINT"d.\n", p); 
        } 
      } 
      array_set_index(a,i,s); 
      break; 
    } 
 
    case T_STRING: 
    { 
      INT32 i, n; 
      check_stack(2); 
      mark_free_svalue (Pike_sp++); 
      push_svalue(ind); 
      for (i = 0, n = a->size; i < n; i++) { 
        assign_svalue(Pike_sp-2, &a->item[i]); 
        assign_lvalue(Pike_sp-2, s); 
      } 
      pop_n_elems(2); 
      break; 
    } 
 
    default: 
    { 
      struct svalue tmp; 
      SET_SVAL(tmp, T_ARRAY, 0, array, a); 
      index_error(0,&tmp,ind,"Array index is neither int nor string.\n"); 
    } 
  } 
} 
 
/** 
 * Insert an svalue into an array and grow the array if necessary. 
 */ 
PMOD_EXPORT struct array *array_insert(struct array *v,struct svalue *s,INT32 index) 
{ 
#ifdef PIKE_DEBUG 
  if(index<0 || index>v->size) 
    Pike_fatal("Illegal index in low level insert routine.\n"); 
#endif 
 
  /* Can we fit it into the existing block? */ 
  if(v->refs<=1 && (v->malloced_size > v->size)) 
  { 
    if ((v->item != v->real_item) && 
        (((index<<1) < v->size) || 
         ((v->item + v->size) == (v->real_item + v->malloced_size)))) { 
      memmove(ITEM(v)-1, ITEM(v), index * sizeof(struct svalue)); 
      v->item--; 
    } else { 
      memmove(ITEM(v)+index+1, ITEM(v)+index, 
              (v->size-index) * sizeof(struct svalue)); 
    } 
    assert_free_svalue (ITEM(v) + index); 
    v->size++; 
  }else{ 
    struct array *ret; 
 
    ret = array_set_flags(allocate_array_no_init(v->size+1, v->size + 1), 
                          v->flags); 
    ret->type_field = v->type_field; 
 
    memcpy(ITEM(ret), ITEM(v), sizeof(struct svalue) * index); 
    memcpy(ITEM(ret)+index+1, ITEM(v)+index, 
           sizeof(struct svalue) * (v->size-index)); 
    assert_free_svalue (ITEM(ret) + index); 
    if (v->refs == 1) { 
      /* Optimization: Steal the references. */ 
      v->size = 0; 
    } else if (v->type_field & BIT_REF_TYPES) { 
      /* Adjust the references. */ 
      int e = v->size; 
      struct svalue *s = ITEM(ret); 
      while (e--) { 
        if (REFCOUNTED_TYPE(TYPEOF(*s))) add_ref(s->u.dummy); 
        s++; 
      } 
    } 
    free_array(v); 
    v=ret; 
  } 
 
  array_set_index_no_free (v,index,s); 
 
  return v; 
} 
 
/* 
 * lval += ({ @args }); 
 * 
 * Stack is lvalue followed by a zero placeholder followed by arguments. 
 */ 
void o_append_array(INT32 args) 
{ 
  struct svalue *lval = Pike_sp - args; 
  struct svalue *val = lval + 2; 
  int lval_type; 
#ifdef PIKE_DEBUG 
  if (args < 3) { 
    Pike_fatal("Too few arguments to o_append_array(): %d\n", args); 
  } 
#endif 
  args -= 3; 
  /* Note: val should always be a zero here! */ 
  lval_type = lvalue_to_svalue_no_free(val, lval); 
 
  if (TYPEOF(*val) == T_ARRAY) { 
    struct svalue tmp; 
    struct array *v = val->u.array; 
    /* simple case: if refs == 2 and there is space, just add the 
       element and do not do the assign.  This can be done because the 
       lvalue already has the array as it's value. 
    */ 
    if( (v->refs == 2) && (lval_type != PIKE_T_GET_SET) ) { 
      if ((TYPEOF(*lval) == T_OBJECT) && 
          lval->u.object->prog && 
          ((FIND_LFUN(lval->u.object->prog, LFUN_ASSIGN_INDEX) >= 0) || 
           (FIND_LFUN(lval->u.object->prog, LFUN_ASSIGN_ARROW) >= 0))) { 
        /* There's a function controlling assignments in this object, 
         * so we can't alter the array in place. 
         */ 
      } else if( v->real_item+v->malloced_size >= v->item+v->size+args ) { 
        struct svalue *from = val+1; 
        int i; 
        for( i = 0; i<args; i++,from++ ) 
        { 
          v->item[v->size++] = *from; 
          v->type_field |= 1<<TYPEOF(*from); 
        } 
        Pike_sp -= args; 
        stack_pop_2_elems_keep_top(); 
        return; 
      } 
    } 
    /* This is so that we can minimize the number of references 
     * to the array, and be able to use destructive operations. 
     * It's done by freeing the old reference to foo after it has been 
     * pushed on the stack. That way foo can have only 1 reference if we 
     * are lucky, and then the low array manipulation routines can 
     * be destructive if they like. 
     */ 
    SET_SVAL(tmp, PIKE_T_INT, NUMBER_NUMBER, integer, 0); 
    assign_lvalue(lval, &tmp); 
 
    if (args == 1) { 
      val->u.array = array_insert(v, Pike_sp - 1, v->size); 
      pop_stack(); 
    } else if (!args) { 
      /* FIXME: Weak? */ 
      if ((v->refs > 1) && (v->size)) { 
        val->u.array = copy_array(v); 
        free_array(v); 
      } 
    } else { 
      int i; 
      for (i = 0; i < args; i++) { 
        v = array_insert(v, val + 1 + i, v->size); 
      } 
      val->u.array = v; 
      pop_n_elems(args); 
    } 
    assign_lvalue(lval, val); 
  } else { 
    int i; 
    struct object *o; 
    struct program *p; 
    /* Fall back to aggregate(). */ 
    f_aggregate(args); 
    if ((TYPEOF(*val) == T_OBJECT) && 
        /* One ref in the lvalue, and one on the stack. */ 
        ((o = val->u.object)->refs <= 2) && 
        (p = o->prog) && 
        (i = FIND_LFUN(p->inherits[SUBTYPEOF(Pike_sp[-2])].prog, 
                       LFUN_ADD_EQ)) != -1) { 
      apply_low(o, i + p->inherits[SUBTYPEOF(Pike_sp[-2])].identifier_level, 1); 
      /* NB: The lvalue already contains the object, so 
       *     no need to reassign it. 
       */ 
      pop_stack(); 
    } else { 
      f_add(2); 
      assign_lvalue(lval, val); 
    } 
  } 
  stack_pop_2_elems_keep_top(); 
} 
 
/** 
 * Shrink an array destructively 
 */ 
PMOD_EXPORT struct array *array_shrink(struct array *v, ptrdiff_t size) 
{ 
  struct array *a; 
 
#ifdef PIKE_DEBUG 
  if(v->refs>2) /* Odd, but has to be two */ 
    Pike_fatal("Array shrink on array with many references.\n"); 
 
  if(size > v->size) 
    Pike_fatal("Illegal argument to array_shrink.\n"); 
#endif 
 
  /* Ensure that one of the empty arrays are returned if size is zero. */ 
  if( !size ) 
  { 
    struct array *e = (v->flags & ARRAY_WEAK_FLAG ? 
                       &weak_empty_array : &empty_array); 
    if (e != v) { 
      free_array (v); 
      add_ref (e); 
    } 
    return e; 
  } 
 
  if (size == v->size) return v; 
 
  /* Free items outside the new array. */ 
  free_svalues(ITEM(v) + size, v->size - size, v->type_field); 
  v->size=size; 
 
  if(size*4 < v->malloced_size + 4) /* Should we realloc it? */ 
  { 
    a = array_set_flags(allocate_array_no_init(size, 0), v->flags); 
    if (a->size) { 
      a->type_field = v->type_field; 
    } 
 
    memcpy(ITEM(a), ITEM(v), size*sizeof(struct svalue)); 
    v->size=0; 
    free_array(v); 
    return a; 
  }else{ 
    return v; 
  } 
} 
 
/** 
 * Resize an array destructively, with the exception that a may be one 
 * of the static empty arrays. 
 */ 
PMOD_EXPORT struct array *resize_array(struct array *a, INT32 size) 
{ 
#ifdef PIKE_DEBUG 
  if(d_flag > 1)  array_check_type_field(a); 
#endif 
 
  /* Ensure that one of the empty arrays are returned if size is zero. */ 
  if (!size && a->malloced_size) return array_shrink (a, size); 
 
  if(a->size == size) return a; 
  if(size > a->size) 
  { 
    /* We should grow the array */ 
 
    if((a->malloced_size >= size) && 
       ((a->item + size) <= (a->real_item + a->malloced_size))) 
    { 
      for(;a->size < size; a->size++) 
      { 
        SET_SVAL(ITEM(a)[a->size], T_INT, NUMBER_NUMBER, integer, 0); 
      } 
      a->type_field |= BIT_INT; 
      return a; 
    } else { 
      struct array *ret; 
      ret = array_set_flags(low_allocate_array(size, size + 1), a->flags); 
      memcpy(ITEM(ret), ITEM(a), sizeof(struct svalue)*a->size); 
      ret->type_field = (TYPE_FIELD)(a->type_field | BIT_INT); 
      a->size=0; 
      free_array(a); 
      return ret; 
    } 
  } else { 
    return array_shrink(a, size); 
  } 
} 
 
/** 
 * Remove an index from an array and shrink the array destructively. 
 * Because this function is destructive, and might free the region for 'v', 
 * do not use this function on arrays that might have been sent to a 
 * Pike function. 
 * 
 * @param v The array to operate on. 
 * @param index The index of the element to remove 
 * @return a new array with the contents of the input minus the removed index. 
 */ 
PMOD_EXPORT struct array *array_remove(struct array *v,INT32 index) 
{ 
  struct array *a; 
 
#ifdef PIKE_DEBUG 
  if(index<0 || index >= v->size) 
    Pike_fatal("Illegal argument to array_remove.\n"); 
#endif 
 
  array_free_index(v, index); 
  if (v->size == 1) { 
    v->size = 0; 
    /* NOTE: The following uses the fact that array_set_flags() 
     *       will reallocate the array if it has zero size! 
     */ 
    return array_set_flags(v, v->flags); 
  } else if(v->size*4 + 4 < v->malloced_size ) /* Should we realloc it? */ 
  { 
    a = array_set_flags(allocate_array_no_init(v->size-1, 0), v->flags); 
    a->type_field = v->type_field; 
 
    if(index>0) 
      memcpy(ITEM(a), ITEM(v), index*sizeof(struct svalue)); 
    if(v->size-index>1) 
      memcpy(ITEM(a)+index, 
             ITEM(v)+index+1, 
             (v->size-index-1)*sizeof(struct svalue)); 
    v->size=0; 
    free_array(v); 
    return a; 
  } else { 
    if(v->size-index>1) 
    { 
      memmove(ITEM(v)+index, ITEM(v)+index+1, 
              (v->size-index-1)*sizeof(struct svalue)); 
    } 
    v->size--; 
    return v; 
  } 
} 
 
static ptrdiff_t fast_array_search( struct array *v, struct svalue *s, ptrdiff_t start ) 
{ 
  ptrdiff_t e; 
  struct svalue *ip = ITEM(v); 
  for(e=start;e<v->size;e++) 
    if(is_eq(ip+e,s)) 
      return e; 
  return -1; 
} 
 
/** 
 * Search for in svalue in an array. 
 * @param v the array to search 
 * @param s the value to search for 
 * @param start the index to start search at 
 * @return the index if found, -1 otherwise 
 */ 
PMOD_EXPORT ptrdiff_t array_search(struct array *v, struct svalue *s, 
                                   ptrdiff_t start) 
{ 
#ifdef PIKE_DEBUG 
  if(start<0) 
    Pike_fatal("Start of find_index is less than zero.\n"); 
#endif 
#ifdef PIKE_DEBUG 
  if(d_flag > 1)  array_check_type_field(v); 
#endif 
  check_destructed(s); 
 
  /* Why search for something that is not there? 
   * however, we must explicitly check for searches 
   * for destructed objects/functions 
   */ 
  if((v->type_field & (1 << TYPEOF(*s)))  || 
     (UNSAFE_IS_ZERO(s) && (v->type_field & (BIT_FUNCTION|BIT_OBJECT))) || 
     ( (v->type_field | (1<<TYPEOF(*s)))  & BIT_OBJECT )) /* for overloading */ 
    return fast_array_search( v, s, start ); 
  return -1; 
} 
 
/** 
 * Slice a piece of an array (conditionally destructively) 
 * @param v the array to slice 
 * @param start the beginning element to be included 
 * @param end the element beyond the end of the slice 
 * @return an array consisting of v[start..end-1] 
 */ 
PMOD_EXPORT struct array *slice_array(struct array *v, ptrdiff_t start, 
                                      ptrdiff_t end) 
{ 
  struct array *a; 
 
#ifdef PIKE_DEBUG 
  if(start > end || end>v->size || start<0) 
    Pike_fatal("Illegal arguments to slice_array()\n"); 
 
  if(d_flag > 1)  array_check_type_field(v); 
#endif 
 
  if(v->refs==1)    /* Can we use the same array? */ 
  { 
    if((end-start)*4 > v->malloced_size) /* don't waste too much memory */ 
    { 
      add_ref(v); 
      free_svalues(ITEM(v) + end, v->size - end, v->type_field); 
      free_svalues(ITEM(v), start, v->type_field); 
      v->item+=start; 
      v->size=end-start; 
#ifdef PIKE_DEBUG 
      if(d_flag>1) 
        check_array(v); 
#endif 
      return v; 
    } 
  } 
 
  a=allocate_array_no_init(end-start,0); 
  if (end-start) { 
    a->type_field = v->type_field; 
 
    assign_svalues_no_free(ITEM(a), ITEM(v)+start, end-start, v->type_field); 
  } 
 
  return a; 
} 
 
/** 
 * Slice a piece of an array (nondestructively). 
 * @return an array consisting of v[start..end-1] 
 */ 
PMOD_EXPORT struct array *friendly_slice_array(struct array *v, 
                                               ptrdiff_t start, 
                                               ptrdiff_t end) 
{ 
  struct array *a; 
 
#ifdef PIKE_DEBUG 
  if(start > end || end>v->size || start<0) 
    Pike_fatal("Illegal arguments to slice_array()\n"); 
 
  if(d_flag > 1)  array_check_type_field(v); 
#endif 
 
  a=allocate_array_no_init(end-start,0); 
  a->type_field = v->type_field; 
 
  assign_svalues_no_free(ITEM(a), ITEM(v)+start, end-start, v->type_field); 
 
  return a; 
} 
 
/** 
 * Copy an array. 
 * @param v the array to be copied. 
 * @returns the copy of the input array. 
 */ 
PMOD_EXPORT struct array *copy_array(struct array *v) 
{ 
  struct array *a; 
 
  if (!v->size) { 
    /* Empty array. */ 
    add_ref(&empty_array); 
    return &empty_array; 
  } 
 
  a=allocate_array_no_init(v->size, 0); 
  a->type_field = v->type_field; 
 
  assign_svalues_no_free(ITEM(a), ITEM(v), v->size, v->type_field); 
 
  return a; 
} 
 
/** 
 * Clean an array from destructed objects. 
 */ 
PMOD_EXPORT void check_array_for_destruct(struct array *v) 
{ 
  int e; 
  INT16 types; 
 
  types = 0; 
#ifdef PIKE_DEBUG 
  if(d_flag > 1)  array_check_type_field(v); 
#endif 
  if(v->type_field & (BIT_OBJECT | BIT_FUNCTION)) 
  { 
    for(e=0; e<v->size; e++) 
    { 
      if((TYPEOF(ITEM(v)[e]) == T_OBJECT || 
          (TYPEOF(ITEM(v)[e]) == T_FUNCTION && 
           SUBTYPEOF(ITEM(v)[e]) != FUNCTION_BUILTIN)) && 
         (!ITEM(v)[e].u.object->prog)) 
      { 
        free_svalue(ITEM(v)+e); 
        SET_SVAL(ITEM(v)[e], T_INT, NUMBER_DESTRUCTED, integer, 0); 
 
        types |= BIT_INT; 
      }else{ 
        types |= 1<<TYPEOF(ITEM(v)[e]); 
      } 
    } 
    v->type_field = types; 
  } 
} 
 
/** 
 * This function finds the index of any destructed object in a set. 
 * It could be optimized to search out the object part with a binary 
 * search lookup if the array is mixed. 
 */ 
PMOD_EXPORT INT32 array_find_destructed_object(struct array *v) 
{ 
  INT32 e; 
  TYPE_FIELD types; 
#ifdef PIKE_DEBUG 
  if(d_flag > 1)  array_check_type_field(v); 
#endif 
  if(v->type_field & (BIT_OBJECT | BIT_FUNCTION)) 
  { 
    types=0; 
    for(e=0; e<v->size; e++) 
    { 
      if((TYPEOF(ITEM(v)[e]) == T_OBJECT || 
          (TYPEOF(ITEM(v)[e]) == T_FUNCTION && 
           SUBTYPEOF(ITEM(v)[e]) != FUNCTION_BUILTIN)) && 
         (!ITEM(v)[e].u.object->prog)) 
        return e; 
      types |= 1<<TYPEOF(ITEM(v)[e]); 
    } 
    v->type_field = types; 
  } 
#ifdef PIKE_DEBUG 
  if(d_flag > 1)  array_check_type_field(v); 
#endif 
  return -1; 
} 
 
static int internal_cmpfun(INT32 *a, 
                           INT32 *b, 
                           cmpfun current_cmpfun, 
                           struct svalue *current_array_p) 
{ 
  int res = current_cmpfun(current_array_p + *a, current_array_p + *b); 
  /* If the comparison considers the elements equal we compare their 
   * positions. Thus we get a stable sort function. */ 
  return res ? res : *a - *b; 
} 
 
#define CMP(X,Y) internal_cmpfun((X),(Y),current_cmpfun, current_array_p) 
#define TYPE INT32 
#define ID get_order_fsort 
#define EXTRA_ARGS ,cmpfun current_cmpfun, struct svalue *current_array_p 
#define XARGS ,current_cmpfun, current_array_p 
#include "fsort_template.h" 
#undef CMP 
#undef TYPE 
#undef ID 
#undef EXTRA_ARGS 
#undef XARGS 
 
/* The sort is stable. */ 
INT32 *get_order(struct array *v, cmpfun fun) 
{ 
  INT32 e, *current_order; 
  ONERROR tmp; 
 
  if(!v->size) return 0; 
 
  /* Overlow safe: ((1<<29)-4)*4 < ULONG_MAX */ 
  current_order=xalloc(v->size * sizeof(INT32)); 
  SET_ONERROR(tmp, free, current_order); 
  for(e=0; e<v->size; e++) current_order[e]=e; 
 
  get_order_fsort(current_order, 
                  current_order+v->size-1, 
                  fun, 
                  ITEM(v)); 
 
  UNSET_ONERROR(tmp); 
  return current_order; 
} 
 
/* Returns CMPFUN_UNORDERED if no relation is established through lfun 
 * calls, or -CMPFUN_UNORDERED if no order defining lfuns (i.e. `< or 
 * `>) were found. */ 
static int lfun_cmp (const struct svalue *a, const struct svalue *b) 
{ 
  struct program *p; 
  int default_res = -CMPFUN_UNORDERED, fun; 
 
  if (TYPEOF(*a) == T_OBJECT && (p = a->u.object->prog)) { 
    if ((fun = FIND_LFUN(p->inherits[SUBTYPEOF(*a)].prog, LFUN_LT)) != -1) { 
      push_svalue(b); 
      apply_low(a->u.object, 
                fun + p->inherits[SUBTYPEOF(*a)].identifier_level, 1); 
      if(!UNSAFE_IS_ZERO(Pike_sp-1)) 
      { 
        pop_stack(); 
        return -1; 
      } 
      pop_stack(); 
      default_res = CMPFUN_UNORDERED; 
    } 
 
    if ((fun = FIND_LFUN(p->inherits[SUBTYPEOF(*a)].prog, LFUN_GT)) != -1) { 
      push_svalue(b); 
      apply_low(a->u.object, 
                fun + p->inherits[SUBTYPEOF(*a)].identifier_level, 1); 
      if(!UNSAFE_IS_ZERO(Pike_sp-1)) 
      { 
        pop_stack(); 
        return 1; 
      } 
      pop_stack(); 
      default_res = CMPFUN_UNORDERED; 
    } 
 
    /* NB: It's not a good idea to use LFUN_EQ here if 
     *     there is neither LFUN_LT nor LFUN_GT, since 
     *     the sorting order may get confused, which 
     *     will cause merge_array_with_order() to fail. 
     */ 
    if ((default_res == CMPFUN_UNORDERED) && 
        (fun = FIND_LFUN(p->inherits[SUBTYPEOF(*a)].prog, LFUN_EQ)) != -1) { 
      push_svalue(b); 
      apply_low(a->u.object, 
                fun + p->inherits[SUBTYPEOF(*a)].identifier_level, 1); 
      if (!UNSAFE_IS_ZERO(Pike_sp-1)) { 
        pop_stack(); 
        return 0; 
      } 
      pop_stack(); 
    } 
  } 
 
  if(TYPEOF(*b) == T_OBJECT && (p = b->u.object->prog)) { 
    if ((fun = FIND_LFUN(p->inherits[SUBTYPEOF(*b)].prog, LFUN_LT)) != -1) { 
      push_svalue(a); 
      apply_low(b->u.object, 
                fun + p->inherits[SUBTYPEOF(*b)].identifier_level, 1); 
      if(!UNSAFE_IS_ZERO(Pike_sp-1)) 
      { 
        pop_stack(); 
        return 1; 
      } 
      pop_stack(); 
      default_res = CMPFUN_UNORDERED; 
    } 
 
    if ((fun = FIND_LFUN(p->inherits[SUBTYPEOF(*b)].prog, LFUN_GT)) != -1) { 
      push_svalue(a); 
      apply_low(b->u.object, 
                fun + p->inherits[SUBTYPEOF(*b)].identifier_level, 1); 
      if(!UNSAFE_IS_ZERO(Pike_sp-1)) 
      { 
        pop_stack(); 
        return -1; 
      } 
      pop_stack(); 
      default_res = CMPFUN_UNORDERED; 
    } 
 
    /* NB: It's not a good idea to use LFUN_EQ here if 
     *     there is neither LFUN_LT nor LFUN_GT, since 
     *     the sorting order may get confused, which 
     *     will cause merge_array_with_order() to fail. 
     */ 
    if ((default_res == CMPFUN_UNORDERED) && 
        (fun = FIND_LFUN(p->inherits[SUBTYPEOF(*b)].prog, LFUN_EQ)) != -1) { 
      push_svalue(a); 
      apply_low(b->u.object, 
                fun + p->inherits[SUBTYPEOF(*b)].identifier_level, 1); 
      if (!UNSAFE_IS_ZERO(Pike_sp-1)) { 
        pop_stack(); 
        return 0; 
      } 
      pop_stack(); 
    } 
  } 
 
  return default_res; 
} 
 
static int obj_or_func_cmp (const struct svalue *a, const struct svalue *b) 
/* Call with either two T_OBJECT or two T_FUNCTION. */ 
{ 
  int a_subtype, b_subtype, res; 
  struct svalue tmp_a, tmp_b; 
 
  assert ((TYPEOF(*a) == T_OBJECT && TYPEOF(*b) == T_OBJECT) || 
          (TYPEOF(*a) == T_FUNCTION && TYPEOF(*b) == T_FUNCTION)); 
 
  if (a->u.object == b->u.object) 
    return SUBTYPEOF(*a) - SUBTYPEOF(*b); 
 
  /* Destructed objects are considered equal to each other, and 
   * greater than others. That makes them sort close to real zeroes, 
   * which are sorted after objects without compare functions. */ 
  if (!a->u.object->prog) 
    return !b->u.object->prog ? 0 : 1; 
  else if (!b->u.object->prog) 
    return -1; 
 
  if (TYPEOF(*a) == T_FUNCTION) { 
    /* Sort pike functions before builtins. */ 
    if (SUBTYPEOF(*a) == FUNCTION_BUILTIN) { 
      if (SUBTYPEOF(*b) == FUNCTION_BUILTIN) 
        return a->u.efun < b->u.efun ? -1 : (a->u.efun == b->u.efun ? 0 : 1); 
      else 
        return 1; 
    } 
    else 
      if (SUBTYPEOF(*b) == FUNCTION_BUILTIN) 
        return -1; 
 
    if (a->u.object->prog != b->u.object->prog) 
      return a->u.object->prog < b->u.object->prog ? -1 : 1; 
    if (SUBTYPEOF(*a) != SUBTYPEOF(*b)) 
      return SUBTYPEOF(*a) - SUBTYPEOF(*b); 
 
    /* We have the same function but in different objects. Compare the 
     * objects themselves. */ 
    /* FIXME: Should we try to convert the subtypes to the ones for 
     * the closest inherits? That'd make some sense if the functions 
     * are private, but otherwise it's doubtful. */ 
    a_subtype = b_subtype = SUBTYPEOF(*a); 
    SET_SVAL(tmp_a, T_OBJECT, 0, object, a->u.object); 
    a = &tmp_a; 
    SET_SVAL(tmp_b, T_OBJECT, 0, object, b->u.object); 
    b = &tmp_b; 
  } 
 
  else { 
    a_subtype = SUBTYPEOF(*a); 
    b_subtype = SUBTYPEOF(*b); 
  } 
 
  res = lfun_cmp (a, b); 
 
  if (res == -CMPFUN_UNORDERED) { 
    /* If the objects had no inequality comparison lfuns to call, use 
     * their pointers to get a well defined internal sort order. Let's 
     * also group objects cloned from the same program. */ 
    if (a->u.object->prog == b->u.object->prog) 
      return a->u.object < b->u.object ? -1 : 1; 
    else 
      return a->u.object->prog < b->u.object->prog ? -1 : 1; 
  } 
  else if (!res) 
    return a_subtype - b_subtype; 
 
  return res; 
} 
 
int set_svalue_cmpfun(const struct svalue *a, const struct svalue *b) 
{ 
  int typediff = TYPEOF(*a) - TYPEOF(*b); 
  if (typediff) { 
    if (TYPEOF(*a) == T_OBJECT || TYPEOF(*b) == T_OBJECT) { 
      int res = lfun_cmp (a, b); 
      if (res != -CMPFUN_UNORDERED) return res; 
    } 
    return typediff; 
  } 
 
  switch(TYPEOF(*a)) 
  { 
    case T_FLOAT: 
      if(a->u.float_number < b->u.float_number) return -1; 
      if(a->u.float_number > b->u.float_number) return 1; 
      return 0; 
 
    case T_INT: 
      if(a->u.integer < b->u.integer) return -1; 
      if(a->u.integer > b->u.integer) return 1; 
      return 0; 
 
    case T_OBJECT: 
    case T_FUNCTION: 
      return obj_or_func_cmp (a, b); 
 
    default: 
      if(a->u.refs < b->u.refs) return -1; 
      if(a->u.refs > b->u.refs) return 1; 
      return 0; 
  } 
} 
 
static int switch_svalue_cmpfun(const struct svalue *a, const struct svalue *b) 
{ 
  int typediff = TYPEOF(*a) - TYPEOF(*b); 
  if (typediff) 
    return typediff; 
 
  switch(TYPEOF(*a)) 
  { 
    case T_INT: 
      if(a->u.integer < b->u.integer) return -1; 
      if(a->u.integer > b->u.integer) return 1; 
      return 0; 
 
    case T_FLOAT: 
      if(a->u.float_number < b->u.float_number) return -1; 
      if(a->u.float_number > b->u.float_number) return 1; 
      return 0; 
 
    case T_STRING: 
      return (int)my_quick_strcmp(a->u.string, b->u.string); 
 
    case T_OBJECT: 
    case T_FUNCTION: 
      return obj_or_func_cmp (a, b); 
 
    default: 
      if(a->u.refs < b->u.refs) return -1; 
      if(a->u.refs > b->u.refs) return 1; 
      return 0; 
  } 
} 
 
int alpha_svalue_cmpfun(const struct svalue *a, const struct svalue *b) 
{ 
  int typediff = TYPEOF(*a) - TYPEOF(*b); 
  if (typediff) { 
    if (TYPEOF(*a) == T_OBJECT || TYPEOF(*b) == T_OBJECT) { 
      int res = lfun_cmp (a, b); 
      if (res != -CMPFUN_UNORDERED) return res; 
    } 
    return typediff; 
  } 
 
  switch(TYPEOF(*a)) 
  { 
    case T_INT: 
      if(a->u.integer < b->u.integer) return -1; 
      if(a->u.integer > b->u.integer) return  1; 
      return 0; 
 
    case T_FLOAT: 
      if(a->u.float_number < b->u.float_number) return -1; 
      if(a->u.float_number > b->u.float_number) return  1; 
      return 0; 
 
    case T_STRING: 
      return (int)my_quick_strcmp(a->u.string, b->u.string); 
 
    case T_ARRAY: 
      if(a==b) return 0; 
      if (!a->u.array->size) 
        if (!b->u.array->size) /* There are several different empty arrays. */ 
          return 0; 
        else 
          return -1; 
      else 
        if (!b->u.array->size) 
          return 1; 
      return alpha_svalue_cmpfun(ITEM(a->u.array), ITEM(b->u.array)); 
 
    case T_MULTISET: 
      if (a == b) return 0; 
      { 
        ptrdiff_t a_pos = multiset_first (a->u.multiset); 
        ptrdiff_t b_pos = multiset_first (b->u.multiset); 
        int res; 
        struct svalue ind_a, ind_b; 
        if (a_pos < 0) 
          if (b_pos < 0) 
            return 0; 
          else 
            return -1; 
        else 
          if (b_pos < 0) 
            return 1; 
        res = alpha_svalue_cmpfun ( 
          use_multiset_index (a->u.multiset, a_pos, ind_a), 
          use_multiset_index (b->u.multiset, b_pos, ind_b)); 
        sub_msnode_ref (a->u.multiset); 
        sub_msnode_ref (b->u.multiset); 
        return res; 
      } 
 
    case T_OBJECT: 
    case T_FUNCTION: 
      return obj_or_func_cmp (a, b); 
 
    default: 
      if(a->u.ptr < b->u.ptr) return -1; 
      if(a->u.ptr > b->u.ptr) return 1; 
      return 0; 
  } 
} 
 
#define CMP(X,Y) alpha_svalue_cmpfun(X,Y) 
#define TYPE struct svalue 
#define ID low_sort_svalues 
#include "fsort_template.h" 
#undef CMP 
#undef TYPE 
#undef ID 
 
/* Same, but only integers. */ 
static int alpha_int_svalue_cmpfun(const struct svalue *a, const struct svalue *b) 
{ 
#ifdef PIKE_DEBUG 
  if ((TYPEOF(*a) != T_INT) || (TYPEOF(*b) != T_INT)) { 
    Pike_fatal("Invalid elements in supposedly integer array.\n"); 
  } 
#endif /* PIKE_DEBUG */ 
  if(a->u.integer < b->u.integer) return -1; 
  if(a->u.integer > b->u.integer) return  1; 
  return 0; 
} 
 
#define CMP(X,Y) alpha_int_svalue_cmpfun(X,Y) 
#define TYPE struct svalue 
#define ID low_sort_int_svalues 
#include "fsort_template.h" 
#undef CMP 
#undef TYPE 
#undef ID 
 
/** This sort is unstable. */ 
PMOD_EXPORT void sort_array_destructively(struct array *v) 
{ 
  if(!v->size) return; 
  if (v->type_field == BIT_INT) { 
    low_sort_int_svalues(ITEM(v), ITEM(v)+v->size-1); 
  } else { 
    low_sort_svalues(ITEM(v), ITEM(v)+v->size-1); 
  } 
} 
 
#define SORT_BY_INDEX 
#define EXTRA_LOCALS int cmpfun_res; 
#define CMP(X,Y) ((cmpfun_res =                                               \ 
                     (alpha_svalue_cmpfun(svals + X, svals + Y) &       \ 
                      ~CMPFUN_UNORDERED)) ?                             \ 
                  cmpfun_res : pos[X] - pos[Y]) 
#define SWAP(X,Y) {                                                   \ 
  {struct svalue tmp = svals[X]; svals[X] = svals[Y]; svals[Y] = tmp;}  \ 
  {int tmp = pos[X]; pos[X] = pos[Y]; pos[Y] = tmp;}                    \ 
} 
#define TYPE struct svalue 
#define ID low_stable_sort_svalues 
#define EXTRA_ARGS , struct svalue *svals, INT32 *pos, int size 
#define XARGS , svals, pos, size 
#include "fsort_template.h" 
#undef SORT_BY_INDEX 
#undef EXTRA_LOCALS 
#undef CMP 
#undef SWAP 
#undef TYPE 
#undef ID 
#undef EXTRA_ARGS 
#undef XARGS 
 
/** This sort is stable. The return value is like the one from 
 * get_alpha_order. */ 
PMOD_EXPORT INT32 *stable_sort_array_destructively(struct array *v) 
{ 
  INT32 *current_order; 
  ONERROR tmp; 
  int e; 
 
  if(!v->size) return NULL; 
 
  /* Overflow safe: ((1<<29)-4)*4 < ULONG_MAX */ 
  current_order=xalloc(v->size * sizeof(INT32)); 
  SET_ONERROR(tmp, free, current_order); 
  for(e=0; e<v->size; e++) current_order[e]=e; 
 
  low_stable_sort_svalues (0, v->size - 1, ITEM (v), current_order, v->size); 
 
  UNSET_ONERROR (tmp); 
  return current_order; 
} 
 
 
/** 
 * Return an 'order' suitable for making mappings and multisets. 
 */ 
PMOD_EXPORT INT32 *get_set_order(struct array *a) 
{ 
  return get_order(a, set_svalue_cmpfun); 
} 
 
/** 
 * Return an 'order' suitable for switches. 
 * 
 * Note: This is used by encode_value_canonic(). It must keep the 
 * sorting rules for all the types that function allows in multiset 
 * and mapping indices. 
 */ 
PMOD_EXPORT INT32 *get_switch_order(struct array *a) 
{ 
  return get_order(a, switch_svalue_cmpfun); 
} 
 
 
/** 
 * Return an 'order' suitable for sorting. 
 */ 
PMOD_EXPORT INT32 *get_alpha_order(struct array *a) 
{ 
  return get_order(a, alpha_svalue_cmpfun); 
} 
 
 
static INT32 low_lookup(struct array *v, 
                        struct svalue *s, 
                        cmpfun fun) 
{ 
  INT32 a,b,c; 
  int q; 
 
  a=0; 
  b=v->size; 
  while(b > a) 
  { 
    c=(a+b)/2; 
    q=fun(ITEM(v)+c,s); 
 
    if(q < 0) 
      a=c+1; 
    else if(q > 0) 
      b=c; 
    else 
      return c; 
  } 
  if(a<v->size && fun(ITEM(v)+a,s)<0) a++; 
  return ~a; 
} 
 
INT32 set_lookup(struct array *a, struct svalue *s) 
{ 
#ifdef PIKE_DEBUG 
  if(d_flag > 1)  array_check_type_field(a); 
#endif 
 
  /* objects may have `< `> operators, evil stuff! */ 
  if(TYPEOF(*s) != T_OBJECT && !(a->type_field & BIT_OBJECT)) 
  { 
    /* face it, it's not there */ 
    if( (((2 << TYPEOF(*s)) -1) & a->type_field) == 0) 
      return -1; 
 
  /* face it, it's not there */ 
    if( ((BIT_MIXED << TYPEOF(*s)) & BIT_MIXED & a->type_field) == 0) 
      return ~a->size; 
  } 
 
  return low_lookup(a,s,set_svalue_cmpfun); 
} 
 
INT32 switch_lookup(struct array *a, struct svalue *s) 
{ 
  /* face it, it's not there */ 
#ifdef PIKE_DEBUG 
  if(d_flag > 1)  array_check_type_field(a); 
#endif 
  /* objects may have `< `> operators, evil stuff! */ 
  if(TYPEOF(*s) != T_OBJECT && !(a->type_field & BIT_OBJECT)) 
  { 
    if( (((2 << TYPEOF(*s)) -1) & a->type_field) == 0) 
      return -1; 
 
    /* face it, it's not there */ 
    if( ((BIT_MIXED << TYPEOF(*s)) & BIT_MIXED & a->type_field) == 0) 
      return ~a->size; 
  } 
 
  return low_lookup(a,s,switch_svalue_cmpfun); 
} 
 
 
/** 
 * Reorganize an array in the order specified by 'order'. 
 */ 
PMOD_EXPORT struct array *order_array(struct array *v, const INT32 *order) 
{ 
  reorder((char *)ITEM(v),v->size,sizeof(struct svalue),order); 
  return v; 
} 
 
 
/** 
 * Copy and reorganize an array. 
 */ 
PMOD_EXPORT struct array *reorder_and_copy_array(const struct array *v, const INT32 *order) 
{ 
  INT32 e; 
  struct array *ret; 
  ret=allocate_array_no_init(v->size, 0); 
  ret->type_field = v->type_field; 
 
  for(e=0;e<v->size;e++) 
    assign_svalue_no_free(ITEM(ret)+e, ITEM(v)+order[e]); 
 
  return ret; 
} 
 
/* Maybe I should have a 'clean' flag for this computation */ 
PMOD_EXPORT TYPE_FIELD array_fix_type_field(struct array *v) 
{ 
  int e; 
  TYPE_FIELD t; 
 
  if(v->flags & ARRAY_LVALUE) 
  { 
    v->type_field=BIT_MIXED|BIT_UNFINISHED; 
    return BIT_MIXED|BIT_UNFINISHED; 
  } 
 
  t=0; 
 
  for(e=0; e<v->size; e++) { 
    check_svalue (ITEM(v) + e); 
    t |= BITOF(ITEM(v)[e]); 
  } 
 
#ifdef PIKE_DEBUG 
  if(t & ~(v->type_field)) 
  { 
    describe(v); 
    Pike_fatal("Type field out of order (old:0x%04x new:0x%04x)!\n", 
               v->type_field, t); 
  } 
#endif 
  v->type_field = t; 
  return t; 
} 
 
#ifdef PIKE_DEBUG 
/* Maybe I should have a 'clean' flag for this computation */ 
PMOD_EXPORT void array_check_type_field(struct array *v) 
{ 
  int e; 
  TYPE_FIELD t; 
 
  t=0; 
 
  if(v->flags & ARRAY_LVALUE) 
    return; 
 
#ifdef TRACE_UNFINISHED_TYPE_FIELDS 
  if (v->type_field & BIT_UNFINISHED && !accept_unfinished_type_fields) { 
    fputs ("Array got an unfinished type field.\n", stderr); 
    describe_something (v, T_ARRAY, 2, 2, 0, NULL); 
  } 
#endif 
 
  for(e=0; e<v->size; e++) 
  { 
    if(TYPEOF(ITEM(v)[e]) > MAX_TYPE) 
      Pike_fatal("Type is out of range.\n"); 
 
    t |= 1 << TYPEOF(ITEM(v)[e]); 
  } 
 
  if(t & ~(v->type_field)) 
  { 
    describe(v); 
    Pike_fatal("Type field out of order!\n"); 
  } 
} 
#endif /* PIKE_DEBUG */ 
 
/** 
 * Get a pointer to the 'union anything' specified if it is of the specified 
 * type. The 'union anything' may be changed, but not the type. 
 */ 
PMOD_EXPORT union anything *low_array_get_item_ptr(struct array *a, 
                                                   INT32 ind, 
                                                   TYPE_T t) 
{ 
  if(TYPEOF(ITEM(a)[ind]) == t) return & (ITEM(a)[ind].u); 
  return 0; 
} 
 
/** 
 * Get a pointer to the 'union anything' specified if it is of the specified 
 * type. The 'union anything' may be changed, but not the type. 
 * The differance between this routine and the one above is that this takes 
 * the index as an svalue. 
 */ 
PMOD_EXPORT union anything *array_get_item_ptr(struct array *a, 
                                               struct svalue *ind, 
                                               TYPE_T t) 
{ 
  INT_TYPE i, p; 
  if(TYPEOF(*ind) != T_INT) 
    Pike_error("Expected integer as array index, got %s.\n", 
               get_name_of_type (TYPEOF(*ind))); 
  p = ind->u.integer; 
  i = p < 0 ? p + a->size : p; 
  if(i<0 || i>=a->size) { 
    if (a->size) { 
      Pike_error("Index %"PRINTPIKEINT"d is out of array range " 
                 "%d..%d.\n", p, -a->size, a->size-1); 
    } else { 
      Pike_error("Attempt to index the empty array with %"PRINTPIKEINT"d.\n", p); 
    } 
  } 
  return low_array_get_item_ptr(a,i,t); 
} 
 
/** 
 * Organize an array of INT32 to specify how to zip two arrays together 
 * to maintain the order. 
 * The first item in this array is the size of the result 
 * the rest is n >= 0 for a[ n ] 
 * or n < 0 for b[ ~n ]. 
 */ 
INT32 * merge(struct array *a,struct array *b,INT32 opcode) 
{ 
  ONERROR r; 
  INT32 ap,bp,i,*ret,*ptr; 
 
  ap=bp=0; 
#ifdef PIKE_DEBUG 
  if(d_flag > 1) 
  { 
    array_check_type_field(a); 
    array_check_type_field(b); 
  } 
#endif 
  if(!(a->type_field & b->type_field) && 
     !((a->type_field | b->type_field) & BIT_OBJECT)) 
  { 
    /* do smart optimizations */ 
    switch(opcode) 
    { 
    case PIKE_ARRAY_OP_AND: 
      /* Trivially overflow safe */ 
      ret=xalloc(sizeof(INT32)); 
      *ret=0; 
      return ret; 
 
    case PIKE_ARRAY_OP_SUB: 
      /* Overlow safe: ((1<<29)-4+1)*4 < ULONG_MAX */ 
      ptr=ret=xalloc(sizeof(INT32)*(a->size+1)); 
      *(ptr++)=a->size; 
      for(i=0;i<a->size;i++) *(ptr++)=i; 
      return ret; 
    } 
  } 
 
  /* Note: The following is integer overflow safe as long as 
   *       sizeof(struct svalue) >= 2*sizeof(INT32). 
   */ 
  ptr=ret=xalloc(sizeof(INT32)*(a->size + b->size + 1)); 
  SET_ONERROR(r, free,ret); 
  ptr++; 
 
  while(ap < a->size && bp < b->size) 
  { 
    i=set_svalue_cmpfun(ITEM(a)+ap,ITEM(b)+bp); 
    if(i < 0) 
      i=opcode >> 8; 
    else if(i > 0) 
      i=opcode; 
    else 
      i=opcode >> 4; 
 
    if(i & PIKE_ARRAY_OP_A) *(ptr++)=ap; 
    if(i & PIKE_ARRAY_OP_B) *(ptr++)=~bp; 
    if(i & PIKE_ARRAY_OP_SKIP_A) ap++; 
    if(i & PIKE_ARRAY_OP_SKIP_B) bp++; 
  } 
 
  if((opcode >> 8) & PIKE_ARRAY_OP_A) while(ap<a->size) *(ptr++)=ap++; 
  if(opcode & PIKE_ARRAY_OP_B) while(bp<b->size) *(ptr++)=~(bp++); 
 
  *ret = (INT32)(ptr-ret-1); 
 
  UNSET_ONERROR(r); 
 
  return ret; 
} 
 
/** 
 * This routine merges two arrays in the order specified by 'zipper' 
 * zipper normally produced by merge() above. 
 */ 
PMOD_EXPORT struct array *array_zip(struct array *a, struct array *b,INT32 *zipper) 
{ 
  INT32 size,e; 
  struct array *ret; 
  size=zipper[0]; 
  zipper++; 
 
  ret=allocate_array_no_init(size,0); 
  for(e=0; e<size; e++) 
  { 
    if(*zipper >= 0) 
      assign_svalue_no_free(ITEM(ret)+e, ITEM(a)+*zipper); 
    else 
      assign_svalue_no_free(ITEM(ret)+e, ITEM(b)+~*zipper); 
    zipper++; 
  } 
  ret->type_field = a->type_field | b->type_field; 
  return ret; 
} 
 
/** Add an arbitrary number of arrays together (destructively). 
* @param argp An array of svalues containing the arrays to be concatenated 
*             Note that the svalues may get modified by this function. 
* @param args The number of elements in argp 
* @returns The resulting struct array. 
*/ 
PMOD_EXPORT struct array *add_arrays(struct svalue *argp, INT32 args) 
{ 
  INT32 e, size; 
  struct array *v; 
  struct array *v2 = NULL; 
 
  for(size=e=0;e<args;e++) 
    size+=argp[e].u.array->size; 
 
#if 1 
  { 
    INT32 tmp=0;    /* Svalues needed so far. */ 
    INT32 tmp2 = 0; 
    INT32 e2 = -1; 
 
    for(e=0;e<args;e++) 
    { 
      v=argp[e].u.array; 
      if(v->refs == 1 && v->malloced_size >= size) 
      { 
        if (((v->item - v->real_item) >= tmp) && 
            ((v->item + size - tmp) <= (v->real_item + v->malloced_size))) { 
          /* There's enough space before and after. */ 
          debug_malloc_touch(v); 
          mark_free_svalue(argp + e); 
          for(tmp=e-1;tmp>=0;tmp--) 
          { 
            v2 = argp[tmp].u.array; 
            debug_malloc_touch(v2); 
            v->type_field |= v2->type_field; 
            assign_svalues_no_free(ITEM(v) - v2->size, ITEM(v2), 
                                   v2->size, v2->type_field); 
            v->item -= v2->size; 
            v->size += v2->size; 
          } 
 
          for(tmp=e+1;tmp<args;tmp++) 
          { 
            v2 = argp[tmp].u.array; 
            debug_malloc_touch(v2); 
            v->type_field |= v2->type_field; 
            assign_svalues_no_free(ITEM(v) + v->size, ITEM(v2), 
                                   v2->size, v2->type_field); 
            v->size += v2->size; 
          } 
#ifdef PIKE_DEBUG 
          if(d_flag>1) 
            check_array(v); 
#endif 
          return v; 
        } 
        if (!v2 || (v->size > v2->size)) { 
          /* Got a potential candidate. 
           * 
           * Optimize for maximum memmove() 
           * (ie minimum assign_svalues_no_free()). 
           */ 
          tmp2 = tmp; 
          v2 = v; 
          e2 = e; 
        } 
      } 
      tmp+=v->size; 
    } 
    if (v2) { 
      debug_malloc_touch(v2); 
      mark_free_svalue(argp + e2); 
      memmove(v2->real_item + tmp2, ITEM(v2), 
              v2->size * sizeof(struct svalue)); 
      v2->item = v2->real_item + tmp2; 
      for(tmp=e2-1;tmp>=0;tmp--) 
      { 
        v = argp[tmp].u.array; 
        debug_malloc_touch(v); 
        v2->type_field |= v->type_field; 
        assign_svalues_no_free(ITEM(v2) - v->size, ITEM(v), 
                               v->size, v->type_field); 
        v2->item -= v->size; 
        v2->size += v->size; 
      } 
      for(tmp=e2+1;tmp<args;tmp++) 
      { 
        v = argp[tmp].u.array; 
        debug_malloc_touch(v); 
        v2->type_field |= v->type_field; 
        assign_svalues_no_free(ITEM(v2) + v2->size, ITEM(v), 
                               v->size, v->type_field); 
        v2->size += v->size; 
      } 
#ifdef PIKE_DEBUG 
      if(d_flag>1) 
        check_array(v2); 
#endif 
      return v2; 
    } 
  } 
#endif 
 
  if(args && (v2 = argp[0].u.array)->refs==1) 
  { 
    e = v2->size; 
    v = resize_array(v2, size); 
    mark_free_svalue(argp); 
    size=e; 
    e=1; 
  }else{ 
    v=allocate_array_no_init(size, 0); 
    v->type_field=0; 
    e=size=0; 
  } 
 
  for(; e<args; e++) 
  { 
    v2 = argp[e].u.array; 
    v->type_field |= v2->type_field; 
    assign_svalues_no_free(ITEM(v)+size, ITEM(v2), v2->size, v2->type_field); 
    size += v2->size; 
  } 
 
  return v; 
} 
 
PMOD_EXPORT int array_equal_p(struct array *a, struct array *b, struct processing *p) 
{ 
  struct processing curr; 
  INT32 e; 
 
  if(a == b) return 1; 
  if(a->size != b->size) return 0; 
  if(!a->size) return 1; 
 
#ifdef PIKE_DEBUG 
  if(d_flag > 1) 
  { 
    array_check_type_field(a); 
    array_check_type_field(b); 
  } 
#endif 
 
  /* This could be done much better if I KNEW that 
   * the type fields didn't contain types that 
   * really aren't in the array 
   */ 
  if(!(a->type_field & b->type_field) && 
     !( (a->type_field | b->type_field) & (BIT_OBJECT|BIT_FUNCTION) )) 
    return 0; 
 
  curr.pointer_a = a; 
  curr.pointer_b = b; 
  curr.next = p; 
 
  for( ;p ;p=p->next) 
    if(p->pointer_a == (void *)a && p->pointer_b == (void *)b) 
      return 1; 
 
  for(e=0; e<a->size; e++) 
    if(!low_is_equal(ITEM(a)+e, ITEM(b)+e, &curr)) 
      return 0; 
 
  return 1; 
} 
 
typedef int(*mycmpfun)(INT32*,INT32*,INT32*,INT32*); 
#define ID fsort_with_order 
#define CMP(X,Y) ((*cmpfun)((X),(Y),oa,ob)) 
#define EXTRA_ARGS ,mycmpfun cmpfun,INT32 *oa,INT32 *ob 
#define XARGS ,cmpfun,oa,ob 
#define TYPE INT32 
#include "fsort_template.h" 
#undef ID 
#undef TYPE 
#undef XARGS 
#undef EXTRA_ARGS 
#undef CMP 
 
 
 
/** 
 * This is used to rearrange the zipper so that the order is retained 
 * as it was before (check merge_array_with_order below). 
 */ 
static int array_merge_fun(INT32 *a, INT32 *b, 
                           INT32 *ordera, INT32 *orderb) 
{ 
  if(*a<0) 
  { 
    if(*b<0) 
    { 
      return orderb[~*a] - orderb[~*b]; 
    }else{ 
      return 1; 
    } 
  }else{ 
    if(*b<0) 
    { 
      return -1; 
    }else{ 
      return ordera[*a] - ordera[*b]; 
    } 
  } 
} 
 
/** 
 * Merge two arrays and retain their order. This is done by arranging them 
 * into ordered sets, merging them as sets and then rearranging the zipper 
 * before zipping the sets together. 
 */ 
PMOD_EXPORT struct array *merge_array_with_order(struct array *a, 
                                                 struct array *b, INT32 op) 
{ 
  ONERROR r1,r2,r3,r4,r5; 
  INT32 *zipper; 
  struct array *tmpa,*tmpb,*ret; 
  INT32 *ordera, *orderb; 
 
  ordera=get_set_order(a); 
  SET_ONERROR(r4,free,ordera); 
 
  orderb=get_set_order(b); 
  SET_ONERROR(r5,free,orderb); 
 
  tmpa=reorder_and_copy_array(a,ordera); 
  SET_ONERROR(r1,do_free_array,tmpa); 
 
  tmpb=reorder_and_copy_array(b,orderb); 
  SET_ONERROR(r2,do_free_array,tmpb); 
 
  zipper=merge(tmpa,tmpb,op); 
  SET_ONERROR(r3,free,zipper); 
 
#if 0 
  { 
    int i; 
 
    simple_describe_array (a); 
    simple_describe_array (b); 
 
    fprintf (stderr, "order a: "); 
    for (i = 0; i < a->size; i++) 
      fprintf (stderr, "%d ", ordera[i]); 
    fprintf (stderr, "\n"); 
 
    fprintf (stderr, "order b: "); 
    for (i = 0; i < b->size; i++) 
      fprintf (stderr, "%d ", orderb[i]); 
    fprintf (stderr, "\n"); 
 
    simple_describe_array (tmpa); 
    simple_describe_array (tmpb); 
 
    fprintf (stderr, "zipper: "); 
    for (i = 1; i < *zipper + 1; i++) 
      fprintf (stderr, "%d ", zipper[i]); 
    fprintf (stderr, "\n"); 
  } 
#endif 
 
  fsort_with_order( (zipper+1), zipper+*zipper, array_merge_fun, 
                    ordera, orderb ); 
 
  ret=array_zip(tmpa,tmpb,zipper); 
  UNSET_ONERROR(r3);  free(zipper); 
  UNSET_ONERROR(r2);  free_array(tmpb); 
  UNSET_ONERROR(r1);  free_array(tmpa); 
  UNSET_ONERROR(r5);  free(orderb); 
  UNSET_ONERROR(r4);  free(ordera); 
  return ret; 
} 
 
 
#define CMP(X,Y) set_svalue_cmpfun(X,Y) 
#define TYPE struct svalue 
#define ID set_sort_svalues 
#include "fsort_template.h" 
#undef CMP 
#undef TYPE 
#undef ID 
 
/** Remove all instances of an svalue from an array 
*/ 
static struct array *subtract_array_svalue(struct array *a, struct svalue *b) 
{ 
  size_t size = a->size; 
  size_t from=0, to=0; 
  TYPE_FIELD to_type = 1<<TYPEOF(*b); 
  TYPE_FIELD type_field = 0; 
  ONERROR ouch; 
  struct svalue *ip=ITEM(a), *dp=ip; 
  int destructive = 1; 
 
  if( size == 0 ) 
    return copy_array(a); 
 
  if( a->refs > 1 ) 
  { 
    /* We only need to do anything if the value exists in the array. */ 
    ptrdiff_t off  = fast_array_search( a, b, 0 ); 
    TYPE_FIELD tmp; 
 
    if( off == -1 ) 
      /* We still need to return a new array. */ 
      return copy_array(a); 
 
    /* In this case we generate a new array and modify that one. */ 
    destructive = 0; 
    from = (size_t)off; 
    tmp = a->type_field; 
    a = allocate_array_no_init(size-1,0); 
    a->type_field = tmp; 
    SET_ONERROR( ouch, do_free_array, a ); 
    dp = ITEM(a); 
 
    /* Copy the part of the array that is not modified first.. */ 
    for( to=0; to<from; to++, ip++, dp++) 
    { 
      assign_svalue_no_free(dp, ip); 
      type_field |= 1<<TYPEOF(*dp); 
    } 
    a->size = from; 
  } 
 
#define MATCH_COPY(X)  do {                                                 \ 
    if( X )                                                                 \ 
    {  /* include entry */                                                  \ 
      type_field|=1<<TYPEOF(*ip);                                           \ 
      if(!destructive)                                                      \ 
        assign_svalue_no_free(dp,ip);                                       \ 
      else if(ip!=dp)                                                       \ 
        *dp=*ip;                                                            \ 
      dp++;                                                                 \ 
      if( !destructive ) a->size++;                                         \ 
    }                                                                       \ 
    else if( destructive )                                                  \ 
      free_svalue( ip );                                                    \ 
  } while(0) 
 
 
  if( UNSAFE_IS_ZERO( b ) ) 
  { 
    /* Remove 0-valued elements. 
       Rather common, so a special case is motivated. 
 
       This saves time becase there is no need to check if 'b' is zero 
       for each loop. 
    */ 
    for( ;from<size; from++, ip++ ) 
      MATCH_COPY( !UNSAFE_IS_ZERO(ip) ); 
  } 
  else if((a->type_field & to_type) || ((a->type_field | to_type) & BIT_OBJECT)) 
  { 
    for( ; from<size; from++, ip++ ) 
      MATCH_COPY( !is_eq(ip,b) ); 
  } 
  else /* b does not exist in the array. */ 
  { 
    add_ref(a); 
    return a; 
  } 
#undef MATCH_COPY 
 
  if( dp != ip ) 
  { 
    a->type_field = type_field; 
    a->size = dp-ITEM(a); 
  } 
 
  if( !destructive ) 
    UNSET_ONERROR( ouch ); 
  else 
    add_ref(a); 
 
  if( a->size ) 
    return a; 
 
  free_array(a); 
  add_ref(&empty_array); 
  return &empty_array; 
} 
 
/** Subtract an array from another. 
*/ 
PMOD_EXPORT struct array *subtract_arrays(struct array *a, struct array *b) 
{ 
#ifdef PIKE_DEBUG 
  if(d_flag > 1) 
  { 
    array_check_type_field(b); 
  } 
#endif 
  if( b->size == 1 ) 
    return subtract_array_svalue( a, ITEM(b) ); 
 
  if(b->size && 
     ((a->type_field & b->type_field) || 
      ((a->type_field | b->type_field) & BIT_OBJECT))) 
  { 
    return merge_array_with_order(a, b, PIKE_ARRAY_OP_SUB); 
  }else{ 
    if(a->refs == 1) 
    { 
      add_ref(a); 
      return a; 
    } 
    return copy_array(a); 
  } 
} 
 
 
/** And two arrays together. 
 */ 
PMOD_EXPORT struct array *and_arrays(struct array *a, struct array *b) 
{ 
#ifdef PIKE_DEBUG 
  if(d_flag > 1) 
    array_check_type_field(b); 
#endif 
  check_array_for_destruct(a); 
 
  if((a->type_field & b->type_field) || 
     ((a->type_field | b->type_field) & BIT_OBJECT)) 
    return merge_array_with_order(a, b, PIKE_ARRAY_OP_AND_LEFT); 
  else 
    return allocate_array_no_init(0,0); 
} 
 
int array_is_constant(struct array *a, 
                      struct processing *p) 
{ 
  return svalues_are_constant(ITEM(a), 
                              a->size, 
                              array_fix_type_field(a), 
                              p); 
} 
 
/* Return true for integers with more than one bit set */ 
static inline int is_more_than_one_bit(unsigned INT32 x) 
{ 
  return !!(x & (x-1)); 
} 
 
node *make_node_from_array(struct array *a) 
{ 
  struct svalue s; 
  INT32 e; 
 
  if(!a->size) 
    return mkefuncallnode("aggregate",0); 
  if (a->size == 1) 
    return mkefuncallnode("aggregate", mksvaluenode(ITEM(a))); 
 
  if(array_fix_type_field(a) == BIT_INT) 
  { 
    debug_malloc_touch(a); 
    for(e=0; e<a->size; e++) 
      if(ITEM(a)[e].u.integer != 0) 
        break; 
    if(e == a->size) 
    { 
      return mkefuncallnode("allocate",mkintnode(a->size)); 
    } 
  } 
  debug_malloc_touch(a); 
  if(!is_more_than_one_bit(a->type_field)) 
  { 
    e=0; 
    debug_malloc_touch(a); 
    switch(a->type_field) 
    { 
      case BIT_INT: 
        for(e=1; e<a->size; e++) 
          if(ITEM(a)[e].u.integer != ITEM(a)[0].u.integer) 
            break; 
        if(e==a->size && ITEM(a)[0].u.integer==0) 
          return mkefuncallnode("allocate",mkintnode(a->size)); 
        break; 
 
      case BIT_STRING: 
      case BIT_PROGRAM: 
        for(e=1; e<a->size; e++) 
          if(ITEM(a)[e].u.refs != ITEM(a)[0].u.refs) 
            break; 
        break; 
 
      case BIT_OBJECT: 
      case BIT_FUNCTION: 
        for(e=1; e<a->size; e++) 
          if(ITEM(a)[e].u.object != ITEM(a)[0].u.object || 
             SUBTYPEOF(ITEM(a)[e]) != SUBTYPEOF(ITEM(a)[0])) 
            break; 
        break; 
    } 
    debug_malloc_touch(a); 
    if(e == a->size) 
      return mkefuncallnode("allocate",mknode(F_ARG_LIST, 
                                              mkintnode(a->size), 
                                              mksvaluenode(ITEM(a)))); 
  } 
 
  if(array_is_constant(a,0)) 
  { 
    debug_malloc_touch(a); 
    SET_SVAL(s, T_ARRAY, 0, array, a); 
    return mkconstantsvaluenode(&s); 
  }else{ 
    node *ret=0; 
    debug_malloc_touch(a); 
    for(e = a->size; e--;) { 
      if (ret) { 
        ret = mknode(F_ARG_LIST, mksvaluenode(ITEM(a)+e), ret); 
      } else { 
        ret = mksvaluenode(ITEM(a)+e); 
      } 
    } 
    return mkefuncallnode("aggregate",ret); 
  } 
} 
 
/** Push elements of an array onto the stack. The array will be freed. 
 */ 
PMOD_EXPORT void push_array_items(struct array *a) 
{ 
  check_stack(a->size); 
  check_array_for_destruct(a); 
  if(a->refs == 1) 
  { 
    memcpy(Pike_sp,ITEM(a),sizeof(struct svalue)*a->size); 
    Pike_sp += a->size; 
    a->size=0; 
    free_array(a); 
  }else{ 
    assign_svalues_no_free(Pike_sp, ITEM(a), a->size, a->type_field); 
    Pike_sp += a->size; 
    free_array(a); 
  } 
} 
 
void describe_array_low(struct byte_buffer *buf, struct array *a, struct processing *p, int indent) 
{ 
  INT32 e,d; 
  indent += 2; 
 
  for(e=0; e<a->size; e++) 
  { 
    buffer_ensure_space(buf, indent + 2); 
    if(e) buffer_add_str_unsafe(buf, ",\n"); 
    for(d=0; d<indent; d++) buffer_add_char_unsafe(buf, ' '); 
    describe_svalue(buf, ITEM(a)+e,indent,p); 
  } 
} 
 
 
#ifdef PIKE_DEBUG 
void simple_describe_array(struct array *a) 
{ 
  char *s; 
  if (a->size) { 
    struct byte_buffer buf = BUFFER_INIT(); 
    describe_array_low(&buf,a,0,0); 
    fprintf(stderr,"({\n%s\n})\n",buffer_get_string(&buf)); 
    buffer_free(&buf); 
  } 
  else 
    fputs ("({ })\n", stderr); 
} 
#endif 
 
 
void describe_array(struct byte_buffer *buffer,struct array *a,struct processing *p,int indent) 
{ 
  struct processing doing; 
  INT32 e; 
  char buf[60]; 
  if(! a->size) 
  { 
    buffer_add_str(buffer, "({ })"); 
    return; 
  } 
 
  doing.next=p; 
  doing.pointer_a=(void *)a; 
  for(e=0;p;e++,p=p->next) 
  { 
    if(p->pointer_a == (void *)a) 
    { 
      sprintf(buf,"@%ld",(long)e); 
      buffer_add_str(buffer, buf); 
      return; 
    } 
  } 
 
  if (a->size == 1) { 
    sprintf(buf, "({ /* 1 element */\n"); 
  } else { 
    sprintf(buf, "({ /* %ld elements */\n", (long)a->size); 
  } 
  buffer_add_str(buffer, buf); 
  describe_array_low(buffer,a,&doing,indent); 
  buffer_add_char(buffer, '\n'); 
  for(e=2; e<indent; e++) buffer_add_char(buffer, ' '); 
  buffer_add_str(buffer, "})"); 
} 
 
/** 
 * Pops a number of arguments off of the stack an puts them in an array. 
 * The 'top' of the stack will be the last element in the array. 
 * @param args The number of arguments to aggregate. 
 */ 
PMOD_EXPORT struct array *aggregate_array(INT32 args) 
{ 
  struct array *a; 
 
  a=allocate_array_no_init(args,0); 
  if (args) { 
    memcpy(ITEM(a),Pike_sp-args,args*sizeof(struct svalue)); 
    array_fix_type_field (a); 
    Pike_sp-=args; 
    DO_IF_DMALLOC(while(args--) dmalloc_touch_svalue(Pike_sp + args)); 
  } 
  return a; 
} 
 
/** Add an element to the end of an array by resizing the array. 
 * 
 * @param a the array to be appended 
 * @param s the value to be added to the new element in the array 
 */ 
PMOD_EXPORT struct array *append_array(struct array *a, struct svalue *s) 
{ 
  INT32 size = a->size; 
  a=resize_array(a, size+1); 
  array_set_index(a, size, s); 
  return a; 
} 
 
/** Automap assignments 
 * This implements X[*] = ...[*].. 
 * Assign elements in a at @level to elements from b at the same @level. 
 * This will not actually modify any of the arrays, only change the 
 * values in them. 
 */ 
void assign_array_level( struct array *a, struct array *b, int level ) 
{ 
    if( a->size != b->size ) 
      /* this should not really happen. */ 
        Pike_error("Source and destination differs in size in automap?!\n"); 
 
    if( level > 1 ) 
    { 
        /* recurse. */ 
        INT32 i; 
        for( i=0; i<a->size; i++ ) 
        { 
            if( TYPEOF(a->item[i]) != PIKE_T_ARRAY ) 
                Pike_error("Too many automap levels.\n"); 
            if( TYPEOF(b->item[i]) != PIKE_T_ARRAY ) /* obscure messages much? */ 
                Pike_error("Not enough levels of mapping in RHS\n"); 
            assign_array_level( a->item[i].u.array, b->item[i].u.array, level-1 ); 
        } 
    } 
    else { 
      assign_svalues( a->item, b->item, a->size, 
                      a->type_field|b->type_field ); 
      a->type_field = b->type_field; 
    } 
} 
 
/* Assign all elemnts in a at level to b. 
 * This implements X[*] = expression without automap. 
 */ 
void assign_array_level_value( struct array *a, struct svalue *b, int level ) 
{ 
    INT32 i; 
    if( level > 1 ) 
    { 
        /* recurse. */ 
        for( i=0; i<a->size; i++ ) 
        { 
            if( TYPEOF(a->item[i]) != PIKE_T_ARRAY ) 
                Pike_error("Too many automap levels.\n"); 
            assign_array_level_value( a->item[i].u.array, b, level-1 ); 
        } 
    } 
    else 
    { 
        if( a->type_field & BIT_REF_TYPES )  free_mixed_svalues( a->item, a->size ); 
        if( REFCOUNTED_TYPE(TYPEOF(*b)) )     *b->u.refs+=a->size; 
        for( i=0; i<a->size; i++) 
            a->item[i] = *b; 
        a->type_field = 1 << TYPEOF(*b); 
    } 
} 
 
typedef char *(* explode_searchfunc)(void *,void *,size_t); 
 
/** Explode a string into an array by a delimiter. 
 * 
 * @param str the string to be split 
 * @param del the string to split str by 
 * @returns an array containing the elements of the split string 
 */ 
PMOD_EXPORT struct array *explode(struct pike_string *str, 
                       struct pike_string *del) 
{ 
  INT32 e; 
  struct array *ret; 
  char *s, *end, *tmp; 
 
#if 0 
  if(!str->len) 
  { 
    return allocate_array_no_init(0,0); 
  } 
#endif 
  if(!del->len) 
  { 
    ret=allocate_array_no_init(str->len,0); 
    for(e=0;e<str->len;e++) 
    { 
      SET_SVAL(ITEM(ret)[e], T_STRING, 0, string, string_slice(str,e,1)); 
    } 
  }else{ 
    SearchMojt mojt; 
    ONERROR uwp; 
    explode_searchfunc f = (explode_searchfunc)0; 
 
    s=str->str; 
    end=s+(str->len << str->size_shift); 
 
    ret=allocate_array(2); 
    ret->size=0; 
 
    mojt=compile_memsearcher(MKPCHARP_STR(del), 
                             del->len, 
                             str->len, 
                             del); 
    SET_ONERROR (uwp, do_free_object, mojt.container); 
 
    switch(str->size_shift) 
    { 
      case eightbit:     f=(explode_searchfunc)mojt.vtab->func0; break; 
      case sixteenbit:   f=(explode_searchfunc)mojt.vtab->func1; break; 
      case thirtytwobit: f=(explode_searchfunc)mojt.vtab->func2; break; 
      default: Pike_fatal("Invalid size_shift: %d.\n", str->size_shift); 
    } 
 
    while((tmp = f(mojt.data, s, (end-s)>> str->size_shift))) 
    { 
      if(ret->size == ret->malloced_size) 
      { 
        e=ret->size; 
        ACCEPT_UNFINISHED_TYPE_FIELDS { 
          ret=resize_array(ret, e * 2); 
        } END_ACCEPT_UNFINISHED_TYPE_FIELDS; 
        ret->size=e; 
      } 
 
      SET_SVAL(ITEM(ret)[ret->size], T_STRING, 0, string, 
               string_slice(str, 
                            (s-str->str)>>str->size_shift, 
                            (tmp-s)>>str->size_shift)); 
      ret->size++; 
 
      s=tmp+(del->len << str->size_shift); 
    } 
 
    if(ret->size == ret->malloced_size) 
    { 
      e=ret->size; 
      ACCEPT_UNFINISHED_TYPE_FIELDS { 
        ret=resize_array(ret, e * 2); 
      } END_ACCEPT_UNFINISHED_TYPE_FIELDS; 
      ret->size=e; 
    } 
 
    SET_SVAL(ITEM(ret)[ret->size], T_STRING, 0, string, 
             string_slice(str, 
                          (s-str->str)>>str->size_shift, 
                          (end-s)>>str->size_shift)); 
    ret->size++; 
 
    CALL_AND_UNSET_ONERROR (uwp); 
  } 
  ret->type_field=BIT_STRING; 
  return ret; 
} 
 
/** Implode an array by creating a string with all of the array's 
 *  elements separated by a delimiter. 
 * 
 * @param a The array containing elements to be imploded 
 * @param del The delimiter used to separate the array's elements in the resulting string 
 * @return The imploded string 
 * 
 */ 
PMOD_EXPORT struct pike_string *implode(struct array *a, 
                                        struct pike_string *del) 
{ 
  INT32 len, e, delims; 
  PCHARP r; 
  struct pike_string *ret; 
  struct svalue *ae; 
  int max_shift = del->size_shift; 
 
  len=0; 
  delims = 0; 
 
 
 
  for(e=a->size, ae=a->item; e--; ae++) 
    switch(TYPEOF(*ae)) 
    { 
      case T_INT: 
         if(!ae->u.integer) 
           continue;                    /* skip zero (strings) */ 
         /* FALLTHROUGH */ 
      default: 
        Pike_error("Array element %d is not a string\n", ae-a->item); 
        break; 
      case T_STRING: 
        delims++; 
        len+=ae->u.string->len + del->len; 
        if(ae->u.string->size_shift > max_shift) 
          max_shift=ae->u.string->size_shift; 
        break; 
    } 
 
  if(delims) 
  { 
    len-=del->len; 
    delims--; 
  } 
 
  if( a->size == 1 && TYPEOF(*ITEM(a)) == PIKE_T_STRING ) 
  { 
      struct pike_string * res = ITEM(a)->u.string; 
      res->refs++; 
      return res; 
  } 
 
  ret=begin_wide_shared_string(len,max_shift); 
  r=MKPCHARP_STR(ret); 
  len = del->len; 
  if((e = a->size)) 
    for(ae=a->item;e--;ae++) 
    { 
      if (TYPEOF(*ae) == T_STRING) 
      { 
        struct pike_string *tmp = ae->u.string; 
        pike_string_cpy(r,tmp); 
        INC_PCHARP(r,tmp->len); 
        if(len && delims) 
        { 
          delims--; 
          pike_string_cpy(r,del); 
          INC_PCHARP(r,len); 
        } 
      } 
    } 
 
  return low_end_shared_string(ret); 
} 
 
/** Deeply copy an array. The mapping is used for temporary storage. 
 */ 
PMOD_EXPORT struct array *copy_array_recursively(struct array *a, 
                                                 struct mapping *m) 
{ 
  struct array *ret; 
  struct svalue aa, bb; 
 
#ifdef PIKE_DEBUG 
  if(d_flag > 1)  array_check_type_field(a); 
#endif 
 
  if (!a->size) { 
    ret = (a->flags & ARRAY_WEAK_FLAG) ? &weak_empty_array : &empty_array; 
    add_ref(ret); 
    return ret; 
  } 
 
  ret=allocate_array_no_init(a->size,0); 
 
  if (m) { 
    SET_SVAL(aa, T_ARRAY, 0, array, a); 
    SET_SVAL(bb, T_ARRAY, 0, array, ret); 
    low_mapping_insert(m, &aa, &bb, 1); 
  } 
 
  ret->flags = a->flags & ~ARRAY_LVALUE; 
 
  copy_svalues_recursively_no_free(ITEM(ret),ITEM(a),a->size,m); 
 
  ret->type_field=a->type_field; 
  return ret; 
} 
 
/** Apply the elements of an array. Arguments the array should be 
 *  applied with should be on the stack before the call and the 
 *  resulting array will be on the stack after the call. 
 * 
 *  Note that the array a may be modified destructively if it has 
 *  only a single reference. 
 */ 
PMOD_EXPORT void apply_array(struct array *a, INT32 args, int flags) 
{ 
  INT32 e, hash = 0; 
  struct svalue *argp = Pike_sp-args; 
  struct array *cycl; 
  DECLARE_CYCLIC(); 
 
  check_stack(args); 
  check_array_for_destruct(a); 
  for (e=0; e<args; e++) 
    hash = hash * 33 + (INT32) PTR_TO_INT (Pike_sp[-e-1].u.ptr); 
 
  if (!(cycl = (struct array *)BEGIN_CYCLIC(a, (ptrdiff_t)hash))) { 
    TYPE_FIELD new_types = 0; 
    struct array *aa; 
    if ((flags & 1) && (a->refs == 1)) { 
      /* Destructive operation possible. */ 
      add_ref(aa = a); 
      aa->type_field |= BIT_UNFINISHED; 
    } else { 
      aa = allocate_array(a->size); 
    } 
    SET_CYCLIC_RET(aa); 
    push_array(aa); 
    for (e=0; e < a->size; e++) 
    { 
      assign_svalues_no_free(Pike_sp, argp, args, BIT_MIXED); 
      Pike_sp+=args; 
      /* FIXME: Don't throw apply errors from apply_svalue here. */ 
      apply_svalue(ITEM(a)+e, args); 
      new_types |= 1 << TYPEOF(Pike_sp[-1]); 
      assign_svalue(ITEM(aa)+e, &Pike_sp[-1]); 
      pop_stack(); 
    } 
    aa->type_field = new_types; 
#ifdef PIKE_DEBUG 
    array_check_type_field(aa); 
#endif 
    stack_pop_n_elems_keep_top(args); 
  } 
  else { 
    pop_n_elems(args); 
    ref_push_array(cycl); 
  } 
 
  END_CYCLIC(); 
} 
 
/** Reverse the elements in an array. If the array has more than one 
 *  reference, the array will be reversed into a new array. Otherwise 
 *  the array will be destructively reversed in place. 
 */ 
PMOD_EXPORT struct array *reverse_array(struct array *a, int start, int end) 
{ 
  INT32 e; 
  struct array *ret; 
 
  if ((end <= start) || (start >= a->size)) { 
    add_ref(a); 
    return a; 
  } 
  if (end >= a->size) { 
    end = a->size; 
  } else { 
    end++; 
  } 
 
  if(a->refs == 1) 
    /* Reverse in-place. */ 
  { 
    struct svalue *tmp0, *tmp1, swap; 
 
    tmp0 = ITEM(a) + start; 
    tmp1 = ITEM(a) + end; 
    while (tmp0 < tmp1) { 
      swap = *tmp0; 
      *(tmp0++) = *(--tmp1); 
      *tmp1 = swap; 
    } 
 
    /* FIXME: What about the flags field? */ 
 
    add_ref(a); 
    return a; 
  } 
 
  ret=allocate_array_no_init(a->size,0); 
  for(e=0;e<start;e++) 
    assign_svalue_no_free(ITEM(ret)+e,ITEM(a)+e); 
  for(;e<end;e++) 
    assign_svalue_no_free(ITEM(ret)+e,ITEM(a)+end+~e-start); 
  for(;e<a->size;e++) 
    assign_svalue_no_free(ITEM(ret)+e,ITEM(a)+e); 
  ret->type_field = a->type_field; 
  return ret; 
} 
 
/** Replaces all from elements in array a with to elements. Called 
 *  from replaces when first argument is an array. The replace is applied 
 *  desctructivly. 
 */ 
void array_replace(struct array *a, 
                   struct svalue *from, 
                   struct svalue *to) 
{ 
  ptrdiff_t i = -1; 
  check_array_for_destruct(a); 
  while((i=fast_array_search(a,from,i+1)) >= 0) array_set_index(a,i,to); 
} 
 
/** 
 * Perform a quick gc of the specified weak array. 
 * 
 * @param a The weak array to be garbage collected. 
 * @return The number of freed elements. 
 * 
 * @see do_gc 
 */ 
ptrdiff_t do_gc_weak_array(struct array *a) 
{ 
  INT32 e; 
  ptrdiff_t res = 0; 
 
  if (!(a->flags & ARRAY_WEAK_FLAG)) { 
    return 0; 
  } 
 
  for (e = 0; e < a->size; e++) { 
    struct svalue *s = ITEM(a) + e; 
    if (!REFCOUNTED_TYPE(TYPEOF(*s)) || (*s->u.refs > 1)) { 
      continue; 
    } 
    /* NB: cf svalue.c:ZAP_SVALUE(). */ 
    free_svalue(s); 
    SET_SVAL(*s, T_INT, NUMBER_DESTRUCTED, integer, 0); 
    res++; 
  } 
  return res; 
} 
 
#ifdef PIKE_DEBUG 
PMOD_EXPORT void check_array(struct array *a) 
{ 
  INT32 e; 
 
  if(a->next && a->next->prev != a) 
    Pike_fatal("array->next->prev != array.\n"); 
 
  if(a->prev) 
  { 
    if(a->prev->next != a) 
      Pike_fatal("array->prev->next != array.\n"); 
  }else{ 
    if(first_array != a) 
      Pike_fatal("array->prev == 0 but first_array != array.\n"); 
  } 
 
  if(a->size > a->malloced_size) 
    Pike_fatal("Array is larger than malloced block!\n"); 
 
  if(a->size < 0) 
    Pike_fatal("Array size is negative!\n"); 
 
  if(a->malloced_size < 0) 
    Pike_fatal("Array malloced size is negative!\n"); 
 
  if((a->item + a->size) > (a->real_item + a->malloced_size)) 
    Pike_fatal("Array uses memory outside of the malloced block!\n"); 
 
  if(a->item < a->real_item) 
  { 
#ifdef DEBUG_MALLOC 
    describe(a); 
#endif 
    Pike_fatal("Array item pointer is too small!\n"); 
  } 
 
  if(a->refs <=0 ) 
    Pike_fatal("Array has zero refs.\n"); 
 
  for(e=0;e<a->size;e++) 
  { 
    if(! ( (1 << TYPEOF(ITEM(a)[e])) & (a->type_field) ) && 
       TYPEOF(ITEM(a)[e])<16) 
      Pike_fatal("Type field lies.\n"); 
 
    check_svalue(ITEM(a)+e); 
  } 
} 
 
void check_all_arrays(void) 
{ 
  struct array *a; 
  for (a = first_array; a; a = a->next) 
    check_array(a); 
} 
#endif /* PIKE_DEBUG */ 
 
 
PMOD_EXPORT void visit_array (struct array *a, int action, void *extra) 
{ 
  visit_enter(a, T_ARRAY, extra); 
  switch (action & VISIT_MODE_MASK) { 
#ifdef PIKE_DEBUG 
    default: 
      Pike_fatal ("Unknown visit action %d.\n", action); 
    case VISIT_NORMAL: 
    case VISIT_COMPLEX_ONLY: 
      break; 
#endif 
    case VISIT_COUNT_BYTES: 
      mc_counted_bytes += sizeof (struct array) + 
        (a->malloced_size - 1) * sizeof (struct svalue); 
      break; 
  } 
 
  if (!(action & VISIT_NO_REFS) && 
      a->type_field & 
      (action & VISIT_COMPLEX_ONLY ? BIT_COMPLEX : BIT_REF_TYPES)) { 
    size_t e, s = a->size; 
    int ref_type = a->flags & ARRAY_WEAK_FLAG ? REF_TYPE_WEAK : REF_TYPE_NORMAL; 
    for (e = 0; e < s; e++) 
      visit_svalue (ITEM (a) + e, ref_type, extra); 
  } 
  visit_leave(a, T_ARRAY, extra); 
} 
 
static void gc_check_array(struct array *a) 
{ 
  GC_ENTER (a, T_ARRAY) { 
    if(a->type_field & BIT_COMPLEX) 
    { 
      if (a->flags & ARRAY_WEAK_FLAG) { 
        gc_check_weak_svalues(ITEM(a), a->size); 
        gc_checked_as_weak(a); 
      } 
      else 
        gc_check_svalues(ITEM(a), a->size); 
    } 
  } GC_LEAVE; 
} 
 
PMOD_EXPORT void gc_mark_array_as_referenced(struct array *a) 
{ 
  if(gc_mark(a, T_ARRAY)) 
    GC_ENTER (a, T_ARRAY) { 
      if (a == gc_mark_array_pos) 
        gc_mark_array_pos = a->next; 
      if (a == gc_internal_array) 
        gc_internal_array = a->next; 
      else { 
        DOUBLEUNLINK (first_array, a); 
        DOUBLELINK (first_array, a); /* Linked in first. */ 
      } 
 
      if (a->type_field & BIT_COMPLEX) 
      { 
        if (a->flags & ARRAY_WEAK_FLAG) { 
          TYPE_FIELD t; 
          if (!(t = gc_mark_weak_svalues(a->item, a->size))) 
            t = a->type_field; 
 
          /* Ugly, but we are not allowed to change type_field 
           * at the same time as the array is being built... 
           * Actually we just need better primitives for building arrays. 
           */ 
          if(!(a->type_field & BIT_UNFINISHED) || a->refs!=1) 
            a->type_field = t; 
          else 
            a->type_field |= t; /* There might be an additional BIT_INT. */ 
 
          gc_assert_checked_as_weak(a); 
        } 
        else { 
          TYPE_FIELD t; 
          if ((t = gc_mark_svalues(ITEM(a), a->size))) { 
            if(!(a->type_field & BIT_UNFINISHED) || a->refs!=1) 
              a->type_field = t; 
            else 
              a->type_field |= t; 
          } 
          gc_assert_checked_as_nonweak(a); 
        } 
      } 
    } GC_LEAVE; 
} 
 
PMOD_EXPORT void real_gc_cycle_check_array(struct array *a, int weak) 
{ 
  GC_CYCLE_ENTER(a, T_ARRAY, weak) { 
#ifdef PIKE_DEBUG 
    if (!gc_destruct_everything && 
        (a == &empty_array || a == &weak_empty_array)) 
      Pike_fatal("Trying to gc cycle check some *_empty_array.\n"); 
#endif 
 
    if (a->type_field & BIT_COMPLEX) 
    { 
      TYPE_FIELD t = a->flags & ARRAY_WEAK_FLAG ? 
        gc_cycle_check_weak_svalues(ITEM(a), a->size) : 
        gc_cycle_check_svalues(ITEM(a), a->size); 
      if (t) { 
        /* In the weak case we should only get here if references to 
         * destructed objects are removed. */ 
        if(!(a->type_field & BIT_UNFINISHED) || a->refs!=1) 
          a->type_field = t; 
        else 
          a->type_field |= t; 
      } 
#ifdef PIKE_DEBUG 
      if (a->flags & ARRAY_WEAK_FLAG) 
        gc_assert_checked_as_weak(a); 
      else 
        gc_assert_checked_as_nonweak(a); 
#endif 
    } 
  } GC_CYCLE_LEAVE; 
} 
 
unsigned gc_touch_all_arrays(void) 
{ 
  unsigned n = 0; 
  struct array *a; 
  if (!first_array || first_array->prev) 
    Pike_fatal ("error in array link list.\n"); 
  for (a = first_array; a; a = a->next) { 
    debug_gc_touch(a); 
    n++; 
    if (a->next && a->next->prev != a) 
      Pike_fatal("Error in array link list.\n"); 
  } 
  return n; 
} 
 
void gc_check_all_arrays(void) 
{ 
  struct array *a; 
  for (a = first_array; a; a = a->next) { 
#ifdef PIKE_DEBUG 
    if(d_flag > 1)  array_check_type_field(a); 
#endif 
    gc_check_array(a); 
  } 
} 
 
 
void gc_mark_all_arrays(void) 
{ 
  gc_mark_array_pos = gc_internal_array; 
  while (gc_mark_array_pos) { 
    struct array *a = gc_mark_array_pos; 
    gc_mark_array_pos = a->next; 
    if(gc_is_referenced(a)) 
      gc_mark_array_as_referenced(a); 
  } 
} 
 
void gc_cycle_check_all_arrays(void) 
{ 
  struct array *a; 
  for (a = gc_internal_array; a; a = a->next) { 
    real_gc_cycle_check_array(a, 0); 
    gc_cycle_run_queue(); 
  } 
} 
 
void gc_zap_ext_weak_refs_in_arrays(void) 
{ 
  gc_mark_array_pos = first_array; 
  while (gc_mark_array_pos != gc_internal_array && gc_ext_weak_refs) { 
    struct array *a = gc_mark_array_pos; 
    gc_mark_array_pos = a->next; 
    gc_mark_array_as_referenced(a); 
  } 
  gc_mark_discard_queue(); 
} 
 
size_t gc_free_all_unreferenced_arrays(void) 
{ 
  struct array *a,*next; 
  size_t unreferenced = 0; 
 
  for (a = gc_internal_array; a; a = next) 
  { 
#ifdef PIKE_DEBUG 
    if (!a) 
      Pike_fatal("Null pointer in array list.\n"); 
#endif 
    if(gc_do_free(a)) 
    { 
      /* Got an extra ref from gc_cycle_pop(). */ 
      free_svalues(ITEM(a), a->size, a->type_field); 
      a->size=0; 
 
      gc_free_extra_ref(a); 
      SET_NEXT_AND_FREE(a, free_array); 
    } 
    else 
    { 
      next=a->next; 
    } 
    unreferenced++; 
  } 
 
  return unreferenced; 
} 
 
 
#ifdef PIKE_DEBUG 
 
void debug_dump_type_field(TYPE_FIELD t) 
{ 
  int e; 
  for(e=0;e<=MAX_TYPE;e++) 
    if(t & (1<<e)) 
      fprintf(stderr," %s",get_name_of_type(e)); 
 
  for(;e<16;e++) 
    if(t & (1<<e)) 
      fprintf(stderr," <%d>",e); 
} 
 
void debug_dump_array(struct array *a) 
{ 
  fprintf(stderr,"Location=%p Refs=%d, next=%p, prev=%p, " 
          "flags=0x%x, size=%d, malloced_size=%d%s\n", 
          a, 
          a->refs, 
          a->next, 
          a->prev, 
          a->flags, 
          a->size, 
          a->malloced_size, 
          a == &empty_array ? " (the empty_array)" : 
          a == &weak_empty_array ? " (the weak_empty_array)" : 
          ""); 
  fprintf(stderr,"Type field ="); 
  debug_dump_type_field(a->type_field); 
  fprintf(stderr,"\n"); 
  simple_describe_array(a); 
} 
#endif 
 
 
/** Returns (by argument) the number of arrays and the total amount of 
 *  memory allocated for arrays (array structs + svalues). Called from 
 *  _memory_usage, which is exposed through Debug.memory_usage(). 
 */ 
void count_memory_in_arrays(size_t *num_, size_t *size_) 
{ 
  size_t num=0, size=0; 
  struct array *m; 
  for(m=first_array;m;m=m->next) 
  { 
    num++; 
    size+=sizeof(struct array)+ 
      sizeof(struct svalue) *  (m->malloced_size - 1); 
  } 
  *num_=num; 
  *size_=size; 
} 
 
/** Segments an array into several elements in an array based on the 
 *  sequence in the second array argument. This function is called 
 *  when an array is divided by another array. Pike level example, ({ 
 *  "hello", " ", "world", "!" }) / ({ " " }) -> ({ ({ "hello" }), ({ 
 *  "world", "!" }) }) 
 */ 
PMOD_EXPORT struct array *explode_array(struct array *a, struct array *b) 
{ 
  INT32 e,d,start; 
  struct array *tmp; 
 
  start=0; 
#if 0 
  if(!a->size) 
  { 
    return allocate_array_no_init(0,0); 
  } 
#endif 
  if(b->size) 
  { 
    BEGIN_AGGREGATE_ARRAY(1) { 
      for(e=0;e<=a->size - b->size;e++) 
      { 
        for(d=0;d<b->size;d++) 
        { 
          if(!is_eq(ITEM(a)+(e+d),ITEM(b)+d)) 
            break; 
        } 
        if(d==b->size) 
        { 
          check_stack(1); 
          push_array(friendly_slice_array(a, start, e)); 
          DO_AGGREGATE_ARRAY(120); 
          e+=b->size-1; 
          start=e+1; 
        } 
      } 
      check_stack(1); 
      push_array(friendly_slice_array(a, start, a->size)); 
    } END_AGGREGATE_ARRAY; 
  }else{ 
    check_stack(120); 
    BEGIN_AGGREGATE_ARRAY(a->size) { 
      for(e=0;e<a->size;e++) { 
        push_array(friendly_slice_array(a, e, e+1)); 
        DO_AGGREGATE_ARRAY(120); 
      } 
    } END_AGGREGATE_ARRAY; 
  } 
  tmp=(--Pike_sp)->u.array; 
  debug_malloc_touch(tmp); 
  if(tmp->size) tmp->type_field=BIT_ARRAY; 
  return tmp; 
} 
 
/** Joins array elements of an array into a new array with the 
 *  elements of the second array as joiners. Performs the opposite 
 *  action from explode_array and is called when an array is 
 *  multiplied by another array. 
 */ 
PMOD_EXPORT struct array *implode_array(struct array *a, struct array *b) 
{ 
  INT32 e, size; 
  struct array *ret; 
 
  if (!a->size) { 
    add_ref(a); 
    return a; 
  } 
 
  size=0; 
  for(e=0;e<a->size;e++) 
  { 
    if(TYPEOF(ITEM(a)[e]) != T_ARRAY) 
      Pike_error("Implode array contains non-arrays.\n"); 
    size+=ITEM(a)[e].u.array->size; 
  } 
 
  ret=allocate_array((a->size -1) * b->size + size); 
  size=0; 
  ret->type_field=0; 
  for(e=0;e<a->size;e++) 
  { 
    if(e) 
    { 
      ret->type_field|=b->type_field; 
      assign_svalues_no_free(ITEM(ret)+size, 
                             ITEM(b), 
                             b->size, 
                             b->type_field); 
      size+=b->size; 
    } 
    ret->type_field|=ITEM(a)[e].u.array->type_field; 
    assign_svalues_no_free(ITEM(ret)+size, 
                           ITEM(ITEM(a)[e].u.array), 
                           ITEM(a)[e].u.array->size, 
                           ITEM(a)[e].u.array->type_field); 
    size+=ITEM(a)[e].u.array->size; 
  } 
#ifdef PIKE_DEBUG 
  if(size != ret->size) 
    Pike_fatal("Implode_array failed miserably (%d != %d)\n", size, ret->size); 
#endif 
  return ret; 
}