-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathXS.xs
2875 lines (2481 loc) · 67.6 KB
/
XS.xs
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
/**
* List::MoreUtils::XS
* Copyright 2004 - 2010 by by Tassilo von Parseval
* Copyright 2013 - 2017 by Jens Rehsack
*
* All code added with 0.417 or later is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* All code until 0.416 is licensed under the same terms as Perl itself,
* either Perl version 5.8.4 or, at your option, any later version of
* Perl 5 you may have available.
*/
#include "LMUconfig.h"
#ifdef HAVE_TIME_H
# include <time.h>
#endif
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "multicall.h"
#define NEED_gv_fetchpvn_flags
#include "ppport.h"
#ifndef MAX
# define MAX(a,b) ((a)>(b)?(a):(b))
#endif
#ifndef MIN
# define MIN(a,b) (((a)<(b))?(a):(b))
#endif
#ifndef aTHX
# define aTHX
# define pTHX
#endif
#ifndef croak_xs_usage
# ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
# define PERL_ARGS_ASSERT_CROAK_XS_USAGE assert(cv); assert(params)
# endif
static void
S_croak_xs_usage(pTHX_ const CV *const cv, const char *const params)
{
const GV *const gv = CvGV(cv);
PERL_ARGS_ASSERT_CROAK_XS_USAGE;
if (gv) {
const char *const gvname = GvNAME(gv);
const HV *const stash = GvSTASH(gv);
const char *const hvname = stash ? HvNAME(stash) : NULL;
if (hvname)
Perl_croak_nocontext("Usage: %s::%s(%s)", hvname, gvname, params);
else
Perl_croak_nocontext("Usage: %s(%s)", gvname, params);
} else {
/* Pants. I don't think that it should be possible to get here. */
Perl_croak_nocontext("Usage: CODE(0x%"UVxf")(%s)", PTR2UV(cv), params);
}
}
# define croak_xs_usage(a,b) S_croak_xs_usage(aTHX_ a,b)
#endif
#ifdef SVf_IVisUV
# define slu_sv_value(sv) (SvIOK(sv)) ? (SvIOK_UV(sv)) ? (NV)(SvUVX(sv)) : (NV)(SvIVX(sv)) : (SvNV(sv))
#else
# define slu_sv_value(sv) (SvIOK(sv)) ? (NV)(SvIVX(sv)) : (SvNV(sv))
#endif
#ifndef SvTEMP_off
# define SvTEMP_off(a) (a)
#endif
/*
* Perl < 5.18 had some kind of different SvIV_please_nomg
*/
#if PERL_VERSION_LE(5,18,0)
#undef SvIV_please_nomg
# define SvIV_please_nomg(sv) \
(!SvIOKp(sv) && (SvNOK(sv) || SvPOK(sv)) \
? (SvIV_nomg(sv), SvIOK(sv)) \
: SvIOK(sv))
#endif
#ifndef MUTABLE_GV
# define MUTABLE_GV(a) (GV *)(a)
#endif
#if !defined(HAS_BUILTIN_EXPECT) && defined(HAVE_BUILTIN_EXPECT)
# ifdef LIKELY
# undef LIKELY
# endif
# ifdef UNLIKELY
# undef UNLIKELY
# endif
# define LIKELY(x) __builtin_expect(!!(x), 1)
# define UNLIKELY(x) __builtin_expect(!!(x), 0)
#endif
#ifndef LIKELY
# define LIKELY(x) (x)
#endif
#ifndef UNLIKELY
# define UNLIKELY(x) (x)
#endif
#ifndef GV_NOTQUAL
# define GV_NOTQUAL 0
#endif
#ifdef _MSC_VER
# define inline __inline
#endif
#ifndef HAVE_SIZE_T
# if SIZEOF_PTR == SIZEOF_LONG_LONG
typedef unsigned long long size_t;
# elif SIZEOF_PTR == SIZEOF_LONG
typedef unsigned long size_t;
# elif SIZEOF_PTR == SIZEOF_INT
typedef unsigned int size_t;
# else
# error "Can't determine type for size_t"
# endif
#endif
#ifndef HAVE_SSIZE_T
# if SIZEOF_PTR == SIZEOF_LONG_LONG
typedef signed long long ssize_t;
# elif SIZEOF_PTR == SIZEOF_LONG
typedef signed long ssize_t;
# elif SIZEOF_PTR == SIZEOF_INT
typedef signed int ssize_t;
# else
# error "Can't determine type for ssize_t"
# endif
#endif
/* compare left and right SVs. Returns:
* -1: <
* 0: ==
* 1: >
* 2: left or right was a NaN
*/
static I32
LMUncmp(pTHX_ SV* left, SV * right)
{
/* Fortunately it seems NaN isn't IOK */
if(SvAMAGIC(left) || SvAMAGIC(right))
return SvIVX(amagic_call(left, right, ncmp_amg, 0));
if (SvIV_please_nomg(right) && SvIV_please_nomg(left))
{
if (!SvUOK(left))
{
const IV leftiv = SvIVX(left);
if (!SvUOK(right))
{
/* ## IV <=> IV ## */
const IV rightiv = SvIVX(right);
return (leftiv > rightiv) - (leftiv < rightiv);
}
/* ## IV <=> UV ## */
if (leftiv < 0)
/* As (b) is a UV, it's >=0, so it must be < */
return -1;
return ((UV)leftiv > SvUVX(right)) - ((UV)leftiv < SvUVX(right));
}
if (SvUOK(right))
{
/* ## UV <=> UV ## */
const UV leftuv = SvUVX(left);
const UV rightuv = SvUVX(right);
return (leftuv > rightuv) - (leftuv < rightuv);
}
/* ## UV <=> IV ## */
if (SvIVX(right) < 0)
/* As (a) is a UV, it's >=0, so it cannot be < */
return 1;
return (SvUVX(left) > SvUVX(right)) - (SvUVX(left) < SvUVX(right));
}
else
{
#ifdef SvNV_nomg
NV const rnv = SvNV_nomg(right);
NV const lnv = SvNV_nomg(left);
#else
NV const rnv = slu_sv_value(right);
NV const lnv = slu_sv_value(left);
#endif
#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
if (Perl_isnan(lnv) || Perl_isnan(rnv))
return 2;
return (lnv > rnv) - (lnv < rnv);
#else
if (lnv < rnv)
return -1;
if (lnv > rnv)
return 1;
if (lnv == rnv)
return 0;
return 2;
#endif
}
}
#define ncmp(left,right) LMUncmp(aTHX_ left,right)
#define FUNC_NAME GvNAME(GvEGV(ST(items)))
/* shameless stolen from PadWalker */
#ifndef PadARRAY
typedef AV PADNAMELIST;
typedef SV PADNAME;
# if PERL_VERSION_LE(5,8,0)
typedef AV PADLIST;
typedef AV PAD;
# endif
# define PadlistARRAY(pl) ((PAD **)AvARRAY(pl))
# define PadlistMAX(pl) av_len(pl)
# define PadlistNAMES(pl) (*PadlistARRAY(pl))
# define PadnamelistARRAY(pnl) ((PADNAME **)AvARRAY(pnl))
# define PadnamelistMAX(pnl) av_len(pnl)
# define PadARRAY AvARRAY
# define PadnameIsOUR(pn) !!(SvFLAGS(pn) & SVpad_OUR)
# define PadnameOURSTASH(pn) SvOURSTASH(pn)
# define PadnameOUTER(pn) !!SvFAKE(pn)
# define PadnamePV(pn) (SvPOKp(pn) ? SvPVX(pn) : NULL)
#endif
static int
in_pad (pTHX_ SV *code)
{
GV *gv;
HV *stash;
CV *cv = sv_2cv(code, &stash, &gv, 0);
PADLIST *pad_list = (CvPADLIST(cv));
PADNAMELIST *pad_namelist = PadlistNAMES(pad_list);
int i;
for (i=PadnamelistMAX(pad_namelist); i>=0; --i)
{
PADNAME* name_sv = PadnamelistARRAY(pad_namelist)[i];
if (name_sv)
{
char *name_str = PadnamePV(name_sv);
if (name_str) {
/* perl < 5.6.0 does not yet have our */
# ifdef SVpad_OUR
if(PadnameIsOUR(name_sv))
continue;
# endif
#if PERL_VERSION_LT(5,21,7)
if (!SvOK(name_sv))
continue;
#endif
if (strEQ(name_str, "$a") || strEQ(name_str, "$b"))
return 1;
}
}
}
return 0;
}
#define ASSERT_PL_defgv \
if(UNLIKELY(!GvSV(PL_defgv))) \
croak("panic: *_ disappeared");
#define WARN_OFF \
SV *oldwarn = PL_curcop->cop_warnings; \
PL_curcop->cop_warnings = pWARN_NONE;
#define WARN_ON \
PL_curcop->cop_warnings = oldwarn;
#define EACH_ARRAY_BODY \
int i; \
arrayeach_args * args; \
HV *stash = gv_stashpv("List::MoreUtils::XS_ea", TRUE); \
CV *closure = newXS(NULL, XS_List__MoreUtils__XS__array_iterator, __FILE__); \
\
/* prototype */ \
sv_setpv((SV*)closure, ";$"); \
\
New(0, args, 1, arrayeach_args); \
New(0, args->avs, items, AV*); \
args->navs = items; \
args->curidx = 0; \
\
for (i = 0; i < items; i++) { \
if(UNLIKELY(!arraylike(ST(i)))) \
croak_xs_usage(cv, "\\@;\\@\\@..."); \
args->avs[i] = (AV*)SvRV(ST(i)); \
SvREFCNT_inc(args->avs[i]); \
} \
\
CvXSUBANY(closure).any_ptr = args; \
RETVAL = newRV_noinc((SV*)closure); \
\
/* in order to allow proper cleanup in DESTROY-handler */ \
sv_bless(RETVAL, stash)
#define dMULTICALLSVCV \
HV *stash; \
GV *gv; \
I32 gimme = G_SCALAR; \
CV *mc_cv = sv_2cv(code, &stash, &gv, 0)
#define FOR_EACH(on_item) \
if(!codelike(code)) \
croak_xs_usage(cv, "code, ..."); \
\
if (items > 1) { \
dMULTICALL; \
dMULTICALLSVCV; \
int i; \
SV **args = &PL_stack_base[ax]; \
PUSH_MULTICALL(mc_cv); \
SAVESPTR(GvSV(PL_defgv)); \
\
for(i = 1 ; i < items ; ++i) { \
SV *def_sv; \
ASSERT_PL_defgv \
def_sv = GvSV(PL_defgv) = args[i]; \
SvTEMP_off(def_sv); \
MULTICALL; \
on_item; \
} \
POP_MULTICALL; \
}
#define TRUE_JUNCTION \
FOR_EACH(if (SvTRUE(*PL_stack_sp)) ON_TRUE) \
else ON_EMPTY;
#define FALSE_JUNCTION \
FOR_EACH(if (!SvTRUE(*PL_stack_sp)) ON_FALSE) \
else ON_EMPTY;
#define ROF_EACH(on_item) \
if(!codelike(code)) \
croak_xs_usage(cv, "code, ..."); \
\
if (items > 1) { \
dMULTICALL; \
dMULTICALLSVCV; \
int i; \
SV **args = &PL_stack_base[ax]; \
PUSH_MULTICALL(mc_cv); \
SAVESPTR(GvSV(PL_defgv)); \
\
for(i = items-1; i > 0; --i) { \
SV *def_sv; \
ASSERT_PL_defgv \
def_sv = GvSV(PL_defgv) = args[i]; \
SvTEMP_off(def_sv); \
MULTICALL; \
on_item; \
} \
POP_MULTICALL; \
}
#define REDUCE_WITH(init) \
dMULTICALL; \
dMULTICALLSVCV; \
SV *rc, **args = &PL_stack_base[ax]; \
IV i; \
\
if(!codelike(code)) \
croak_xs_usage(cv, "code, list, list"); \
\
if (in_pad(aTHX_ code)) { \
croak("Can't use lexical $a or $b in pairwise code block"); \
} \
\
rc = (init); \
sv_2mortal(newRV_noinc(rc)); \
\
PUSH_MULTICALL(mc_cv); \
SAVESPTR(GvSV(PL_defgv)); \
\
/* Following code is stolen on request of */ \
/* Zefram from pp_sort.c of perl core 16ada23 */ \
/* I have no idea why it's necessary and there */\
/* is no reasonable documentation regarding */ \
/* deal with localized $a/$b/$_ */ \
SAVEGENERICSV(PL_firstgv); \
SAVEGENERICSV(PL_secondgv); \
PL_firstgv = MUTABLE_GV(SvREFCNT_inc( \
gv_fetchpvs("a", GV_ADD|GV_NOTQUAL, SVt_PV) \
)); \
PL_secondgv = MUTABLE_GV(SvREFCNT_inc( \
gv_fetchpvs("b", GV_ADD|GV_NOTQUAL, SVt_PV) \
)); \
save_gp(PL_firstgv, 0); save_gp(PL_secondgv, 0); \
GvINTRO_off(PL_firstgv); \
GvINTRO_off(PL_secondgv); \
SAVEGENERICSV(GvSV(PL_firstgv)); \
SvREFCNT_inc(GvSV(PL_firstgv)); \
SAVEGENERICSV(GvSV(PL_secondgv)); \
SvREFCNT_inc(GvSV(PL_secondgv)); \
\
for (i = 1; i < items; ++i) \
{ \
SV *olda, *oldb; \
sv_setiv(GvSV(PL_defgv), i-1); \
\
olda = GvSV(PL_firstgv); \
oldb = GvSV(PL_secondgv); \
GvSV(PL_firstgv) = SvREFCNT_inc_simple_NN(rc); \
GvSV(PL_secondgv) = SvREFCNT_inc_simple_NN(args[i]); \
SvREFCNT_dec(olda); \
SvREFCNT_dec(oldb); \
MULTICALL; \
\
SvSetMagicSV(rc, *PL_stack_sp); \
} \
\
POP_MULTICALL; \
\
EXTEND(SP, 1); \
ST(0) = sv_2mortal(newSVsv(rc)); \
XSRETURN(1)
#define COUNT_ARGS \
for (i = 0; i < items; i++) { \
SvGETMAGIC(args[i]); \
if(SvOK(args[i])) { \
HE *he; \
SvSetSV_nosteal(tmp, args[i]); \
he = hv_fetch_ent(hv, tmp, 0, 0); \
if (NULL == he) { \
args[count++] = args[i]; \
hv_store_ent(hv, tmp, newSViv(1), 0); \
} \
else { \
SV *v = HeVAL(he); \
IV how_many = SvIVX(v); \
sv_setiv(v, ++how_many); \
} \
} \
else if(0 == seen_undef++) { \
args[count++] = args[i]; \
} \
}
#define COUNT_ARGS_MAX \
do { \
for (i = 0; i < items; i++) { \
SvGETMAGIC(args[i]); \
if(SvOK(args[i])) { \
HE *he; \
SvSetSV_nosteal(tmp, args[i]); \
he = hv_fetch_ent(hv, tmp, 0, 0); \
if (NULL == he) { \
args[count++] = args[i]; \
hv_store_ent(hv, tmp, newSViv(1), 0); \
} \
else { \
SV *v = HeVAL(he); \
IV how_many = SvIVX(v); \
if(UNLIKELY(max < ++how_many)) \
max = how_many; \
sv_setiv(v, how_many); \
} \
} \
else if(0 == seen_undef++) { \
args[count++] = args[i]; \
} \
} \
if(UNLIKELY(max < seen_undef)) max = seen_undef; \
} while(0)
/* need this one for array_each() */
typedef struct
{
AV **avs; /* arrays over which to iterate in parallel */
int navs; /* number of arrays */
int curidx; /* the current index of the iterator */
} arrayeach_args;
/* used for natatime and slideatatime_args */
typedef struct
{
SV **svs;
int nsvs;
int curidx;
int window;
int move;
} slideatatime_args;
static void
insert_after (pTHX_ int idx, SV *what, AV *av)
{
int i, len;
av_extend(av, (len = av_len(av) + 1));
for (i = len; i > idx+1; i--)
{
SV **sv = av_fetch(av, i-1, FALSE);
SvREFCNT_inc(*sv);
av_store(av, i, *sv);
}
if (!av_store(av, idx+1, what))
SvREFCNT_dec(what);
}
static int
is_like(pTHX_ SV *sv, const char *like)
{
int likely = 0;
if( sv_isobject( sv ) )
{
dSP;
int count;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs( sv_2mortal( newSVsv( sv ) ) );
XPUSHs( sv_2mortal( newSVpv( like, strlen(like) ) ) );
PUTBACK;
if( ( count = call_pv("overload::Method", G_SCALAR) ) )
{
I32 ax;
SPAGAIN;
SP -= count;
ax = (SP - PL_stack_base) + 1;
if( SvTRUE(ST(0)) )
++likely;
}
FREETMPS;
LEAVE;
}
return likely;
}
static int
is_array(SV *sv)
{
return SvROK(sv) && ( SVt_PVAV == SvTYPE(SvRV(sv) ) );
}
static int
LMUcodelike(pTHX_ SV *code)
{
SvGETMAGIC(code);
return SvROK(code) && ( ( SVt_PVCV == SvTYPE(SvRV(code)) ) || ( is_like(aTHX_ code, "&{}" ) ) );
}
#define codelike(code) LMUcodelike(aTHX_ code)
static int
LMUarraylike(pTHX_ SV *array)
{
SvGETMAGIC(array);
return is_array(array) || is_like(aTHX_ array, "@{}" );
}
#define arraylike(array) LMUarraylike(aTHX_ array)
static void
LMUav2flat(pTHX_ AV *tgt, AV *args)
{
I32 k = 0, j = av_len(args) + 1;
av_extend(tgt, AvFILLp(tgt) + j);
while( --j >= 0 )
{
SV *sv = *av_fetch(args, k++, FALSE);
if(arraylike(sv))
{
AV *av = (AV *)SvRV(sv);
LMUav2flat(aTHX_ tgt, av);
}
else
{
// av_push(tgt, newSVsv(sv));
av_push(tgt, SvREFCNT_inc(sv));
}
}
}
/*-
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* FreeBSD's Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
* Modified for using Perl Sub (no XSUB) via MULTICALL and all values are SV **
*/
static inline void
swapfunc(SV **a, SV **b, size_t n)
{
SV **pa = a;
SV **pb = b;
while(n-- > 0)
{
SV *t = *pa;
*pa++ = *pb;
*pb++ = t;
}
}
#define swap(a, b) \
do { \
SV *t = *(a); \
*(a) = *(b); \
*(b) = t; \
} while(0)
#define vecswap(a, b, n) \
if ((n) > 0) swapfunc(a, b, n)
#if HAVE_FEATURE_STATEMENT_EXPRESSION
# define CMP(x, y) ({ \
SV *olda, *oldb; \
olda = GvSV(PL_firstgv); \
oldb = GvSV(PL_secondgv); \
GvSV(PL_firstgv) = SvREFCNT_inc_simple_NN(*(x)); \
GvSV(PL_secondgv) = SvREFCNT_inc_simple_NN(*(y)); \
SvREFCNT_dec(olda); \
SvREFCNT_dec(oldb); \
\
MULTICALL; \
SvIV(*PL_stack_sp); \
})
#else
static inline int _cmpsvs(pTHX_ SV *x, SV *y, OP *multicall_cop )
{
SV *olda, *oldb;
olda = GvSV(PL_firstgv);
oldb = GvSV(PL_secondgv);
GvSV(PL_firstgv) = SvREFCNT_inc_simple_NN(x);
GvSV(PL_secondgv) = SvREFCNT_inc_simple_NN(y);
SvREFCNT_dec(olda);
SvREFCNT_dec(oldb);
MULTICALL;
return SvIV(*PL_stack_sp);
}
# define CMP(x, y) _cmpsvs(aTHX_ *(x), *(y), multicall_cop)
#endif
#define MED3(a, b, c) ( \
CMP(a, b) < 0 ? \
(CMP(b, c) < 0 ? b : (CMP(a, c) < 0 ? c : a )) \
:(CMP(b, c) > 0 ? b : (CMP(a, c) < 0 ? a : c )) \
)
static void
bsd_qsort_r(pTHX_ SV **ary, size_t nelem, OP *multicall_cop)
{
SV **pa, **pb, **pc, **pd, **pl, **pm, **pn;
size_t d1, d2;
int cmp_result, swap_cnt = 0;
loop:
if (nelem < 7)
{
for (pm = ary + 1; pm < ary + nelem; ++pm)
for (pl = pm;
pl > ary && CMP(pl - 1, pl) > 0;
pl -= 1)
swap(pl, pl - 1);
return;
}
pm = ary + (nelem / 2);
if (nelem > 7)
{
pl = ary;
pn = ary + (nelem - 1);
if (nelem > 40)
{
size_t d = (nelem / 8);
pl = MED3(pl, pl + d, pl + 2 * d);
pm = MED3(pm - d, pm, pm + d);
pn = MED3(pn - 2 * d, pn - d, pn);
}
pm = MED3(pl, pm, pn);
}
swap(ary, pm);
pa = pb = ary + 1;
pc = pd = ary + (nelem - 1);
for (;;)
{
while (pb <= pc && (cmp_result = CMP(pb, ary)) <= 0)
{
if (cmp_result == 0)
{
swap_cnt = 1;
swap(pa, pb);
pa += 1;
}
pb += 1;
}
while (pb <= pc && (cmp_result = CMP(pc, ary)) >= 0)
{
if (cmp_result == 0)
{
swap_cnt = 1;
swap(pc, pd);
pd -= 1;
}
pc -= 1;
}
if (pb > pc)
break;
swap(pb, pc);
swap_cnt = 1;
pb += 1;
pc -= 1;
}
if (swap_cnt == 0)
{ /* Switch to insertion sort */
for (pm = ary + 1; pm < ary + nelem; pm += 1)
for (pl = pm;
pl > ary && CMP(pl - 1, pl) > 0;
pl -= 1)
swap(pl, pl - 1);
return;
}
pn = ary + nelem;
d1 = MIN(pa - ary, pb - pa);
vecswap(ary, pb - d1, d1);
d1 = MIN(pd - pc, pn - pd - 1);
vecswap(pb, pn - d1, d1);
d1 = pb - pa;
d2 = pd - pc;
if (d1 <= d2)
{
/* Recurse on left partition, then iterate on right partition */
if (d1 > 1)
bsd_qsort_r(aTHX_ ary, d1, multicall_cop);
if (d2 > 1)
{
/* Iterate rather than recurse to save stack space */
/* qsort(pn - d2, d2, multicall_cop); */
ary = pn - d2;
nelem = d2;
goto loop;
}
}
else
{
/* Recurse on right partition, then iterate on left partition */
if (d2 > 1)
bsd_qsort_r(aTHX_ pn - d2, d2, multicall_cop);
if (d1 > 1)
{
/* Iterate rather than recurse to save stack space */
/* qsort(ary, d1, multicall_cop); */
nelem = d1;
goto loop;
}
}
}
/* lower_bound algorithm from STL - see http://en.cppreference.com/w/cpp/algorithm/lower_bound */
#define LOWER_BOUND(at) \
while (count > 0) { \
ssize_t step = count / 2; \
ssize_t it = first + step; \
\
ASSERT_PL_defgv \
GvSV(PL_defgv) = at; \
MULTICALL; \
cmprc = SvIV(*PL_stack_sp); \
if (cmprc < 0) { \
first = ++it; \
count -= step + 1; \
} \
else \
count = step; \
}
#define LOWER_BOUND_QUICK(at) \
while (count > 0) { \
ssize_t step = count / 2; \
ssize_t it = first + step; \
\
ASSERT_PL_defgv \
GvSV(PL_defgv) = at; \
MULTICALL; \
cmprc = SvIV(*PL_stack_sp); \
if(UNLIKELY(0 == cmprc)) { \
first = it; \
break; \
} \
if (cmprc < 0) { \
first = ++it; \
count -= step + 1; \
} \
else \
count = step; \
}
/* upper_bound algorithm from STL - see http://en.cppreference.com/w/cpp/algorithm/upper_bound */
#define UPPER_BOUND(at) \
while (count > 0) { \
ssize_t step = count / 2; \
ssize_t it = first + step; \
\
ASSERT_PL_defgv \
GvSV(PL_defgv) = at; \
MULTICALL; \
cmprc = SvIV(*PL_stack_sp); \
if (cmprc <= 0) { \
first = ++it; \
count -= step + 1; \
} \
else \
count = step; \
}
MODULE = List::MoreUtils::XS_ea PACKAGE = List::MoreUtils::XS_ea
void
DESTROY(sv)
SV *sv;
CODE:
{
int i;
CV *code = (CV*)SvRV(sv);
arrayeach_args *args = (arrayeach_args *)(CvXSUBANY(code).any_ptr);
if (args)
{
for (i = 0; i < args->navs; ++i)
SvREFCNT_dec(args->avs[i]);
Safefree(args->avs);
Safefree(args);
CvXSUBANY(code).any_ptr = NULL;
}
}
MODULE = List::MoreUtils::XS_sa PACKAGE = List::MoreUtils::XS_sa
void
DESTROY(sv)
SV *sv;
CODE:
{
int i;
CV *code = (CV*)SvRV(sv);
slideatatime_args *args = (slideatatime_args *)(CvXSUBANY(code).any_ptr);
if (args)
{
for (i = 0; i < args->nsvs; ++i)
SvREFCNT_dec(args->svs[i]);
Safefree(args->svs);
Safefree(args);
CvXSUBANY(code).any_ptr = NULL;
}
}
MODULE = List::MoreUtils::XS PACKAGE = List::MoreUtils::XS
void
any (code,...)
SV *code;
PROTOTYPE: &@
CODE:
{
#define ON_TRUE { POP_MULTICALL; XSRETURN_YES; }
#define ON_EMPTY XSRETURN_NO
TRUE_JUNCTION;
XSRETURN_NO;
#undef ON_EMPTY
#undef ON_TRUE
}
void
all (code, ...)
SV *code;
PROTOTYPE: &@
CODE:
{
#define ON_FALSE { POP_MULTICALL; XSRETURN_NO; }
#define ON_EMPTY XSRETURN_YES
FALSE_JUNCTION;
XSRETURN_YES;
#undef ON_EMPTY
#undef ON_FALSE
}
void
none (code, ...)
SV *code;
PROTOTYPE: &@
CODE:
{
#define ON_TRUE { POP_MULTICALL; XSRETURN_NO; }
#define ON_EMPTY XSRETURN_YES
TRUE_JUNCTION;
XSRETURN_YES;
#undef ON_EMPTY
#undef ON_TRUE
}
void
notall (code, ...)
SV *code;
PROTOTYPE: &@
CODE:
{
#define ON_FALSE { POP_MULTICALL; XSRETURN_YES; }
#define ON_EMPTY XSRETURN_NO
FALSE_JUNCTION;
XSRETURN_NO;
#undef ON_EMPTY
#undef ON_FALSE
}
void
one (code, ...)
SV *code;
PROTOTYPE: &@
CODE:
{
int found = 0;
#define ON_TRUE { if (found++) { POP_MULTICALL; XSRETURN_NO; }; }
#define ON_EMPTY XSRETURN_NO