X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pp_sort.c;h=8d7f48f752c9b2f16aa7503c3fcc56e98d0977ae;hb=f1c8c9362c4d1029d2c52ffe1e972f0f0b3d5771;hp=18ccfa1852bebda9422ebcb1abc3d3156dcee398;hpb=eb160463266f58ba83ae9bb9ae8bbdc8f0c3027b;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pp_sort.c b/pp_sort.c index 18ccfa1..8d7f48f 100644 --- a/pp_sort.c +++ b/pp_sort.c @@ -1,6 +1,7 @@ /* pp_sort.c * - * Copyright (c) 1991-2002, Larry Wall + * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, + * 2000, 2001, 2002, 2003, 2004, by Larry Wall and others * * You may distribute under the terms of either the GNU General Public * License or the Artistic License, as specified in the README file. @@ -16,6 +17,11 @@ #define PERL_IN_PP_SORT_C #include "perl.h" +#if defined(UNDER_CE) +/* looks like 'small' is reserved word for WINCE (or somesuch)*/ +#define small xsmall +#endif + static I32 sortcv(pTHX_ SV *a, SV *b); static I32 sortcv_stacked(pTHX_ SV *a, SV *b); static I32 sortcv_xsub(pTHX_ SV *a, SV *b); @@ -29,10 +35,9 @@ static I32 amagic_cmp_locale(pTHX_ SV *a, SV *b); #define sv_cmp_static Perl_sv_cmp #define sv_cmp_locale_static Perl_sv_cmp_locale -#define SORTHINTS(hintsvp) \ - ((PL_hintgv && \ - (hintsvp = hv_fetch(GvHV(PL_hintgv), "SORT", 4, FALSE))) ? \ - (I32)SvIV(*hintsvp) : 0) +#define SORTHINTS(hintsv) \ + (((hintsv) = GvSV(gv_fetchpv("sort::hints", GV_ADDMULTI, SVt_IV))), \ + (SvIOK(hintsv) ? ((I32)SvIV(hintsv)) : 0)) #ifndef SMALLSORT #define SMALLSORT (200) @@ -1282,7 +1287,6 @@ S_qsortsvu(pTHX_ SV ** array, size_t num_elts, SVCOMPARE_t compare) * dictated by the indirect array. */ -static SVCOMPARE_t RealCmp; static I32 cmpindir(pTHX_ gptr a, gptr b) @@ -1291,7 +1295,7 @@ cmpindir(pTHX_ gptr a, gptr b) gptr *ap = (gptr *)a; gptr *bp = (gptr *)b; - if ((sense = RealCmp(aTHX_ *ap, *bp)) == 0) + if ((sense = PL_sort_RealCmp(aTHX_ *ap, *bp)) == 0) sense = (ap > bp) ? 1 : ((ap < bp) ? -1 : 0); return sense; } @@ -1299,9 +1303,9 @@ cmpindir(pTHX_ gptr a, gptr b) STATIC void S_qsortsv(pTHX_ gptr *list1, size_t nmemb, SVCOMPARE_t cmp) { - SV **hintsvp; + SV *hintsv; - if (SORTHINTS(hintsvp) & HINT_SORT_STABLE) { + if (SORTHINTS(hintsv) & HINT_SORT_STABLE) { register gptr **pp, *q; register size_t n, j, i; gptr *small[SMALLSORT], **indir, tmp; @@ -1315,8 +1319,8 @@ S_qsortsv(pTHX_ gptr *list1, size_t nmemb, SVCOMPARE_t cmp) /* Copy pointers to original array elements into indirect array */ for (n = nmemb, pp = indir, q = list1; n--; ) *pp++ = q++; - savecmp = RealCmp; /* Save current comparison routine, if any */ - RealCmp = cmp; /* Put comparison routine where cmpindir can find it */ + savecmp = PL_sort_RealCmp; /* Save current comparison routine, if any */ + PL_sort_RealCmp = cmp; /* Put comparison routine where cmpindir can find it */ /* sort, with indirection */ S_qsortsvu(aTHX_ (gptr *)indir, nmemb, cmpindir); @@ -1361,7 +1365,7 @@ S_qsortsv(pTHX_ gptr *list1, size_t nmemb, SVCOMPARE_t cmp) /* free iff allocated */ if (indir != small) { Safefree(indir); } /* restore prevailing comparison routine */ - RealCmp = savecmp; + PL_sort_RealCmp = savecmp; } else { S_qsortsvu(aTHX_ list1, nmemb, cmp); } @@ -1386,7 +1390,7 @@ Perl_sortsv(pTHX_ SV **array, size_t nmemb, SVCOMPARE_t cmp) { void (*sortsvp)(pTHX_ SV **array, size_t nmemb, SVCOMPARE_t cmp) = S_mergesortsv; - SV **hintsvp; + SV *hintsv; I32 hints; /* Sun's Compiler (cc: WorkShop Compilers 4.2 30 Oct 1996 C 4.2) used @@ -1394,7 +1398,7 @@ Perl_sortsv(pTHX_ SV **array, size_t nmemb, SVCOMPARE_t cmp) errors related to picking the correct sort() function, try recompiling this file without optimiziation. -- A.D. 4/2002. */ - hints = SORTHINTS(hintsvp); + hints = SORTHINTS(hintsv); if (hints & HINT_SORT_QUICKSORT) { sortsvp = S_qsortsv; } @@ -1409,9 +1413,9 @@ Perl_sortsv(pTHX_ SV **array, size_t nmemb, SVCOMPARE_t cmp) PP(pp_sort) { dSP; dMARK; dORIGMARK; - register SV **up; - SV **myorigmark = ORIGMARK; - register I32 max; + register SV **p1 = ORIGMARK+1, **p2; + register I32 max, i; + AV* av = Nullav; HV *stash; GV *gv; CV *cv = 0; @@ -1420,6 +1424,7 @@ PP(pp_sort) I32 overloading = 0; bool hasargs = FALSE; I32 is_xsub = 0; + I32 sorting_av = 0; if (gimme != G_ARRAY) { SP = MARK; @@ -1452,8 +1457,8 @@ PP(pp_sort) else if (gv) { SV *tmpstr = sv_newmortal(); gv_efullname3(tmpstr, gv, Nullch); - DIE(aTHX_ "Undefined sort subroutine \"%s\" called", - SvPVX(tmpstr)); + DIE(aTHX_ "Undefined sort subroutine \"%"SVf"\" called", + tmpstr); } else { DIE(aTHX_ "Undefined subroutine in sort"); @@ -1467,8 +1472,7 @@ PP(pp_sort) SAVEVPTR(CvROOT(cv)->op_ppaddr); CvROOT(cv)->op_ppaddr = PL_ppaddr[OP_NULL]; - SAVEVPTR(PL_curpad); - PL_curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]); + PAD_SET_CUR(CvPADLIST(cv), 1); } } } @@ -1477,24 +1481,54 @@ PP(pp_sort) stash = CopSTASH(PL_curcop); } - up = myorigmark + 1; - while (MARK < SP) { /* This may or may not shift down one here. */ - /*SUPPRESS 560*/ - if ((*up = *++MARK)) { /* Weed out nulls. */ - SvTEMP_off(*up); - if (!PL_sortcop && !SvPOK(*up)) { + /* optimiser converts "@a = sort @a" to "sort \@a"; + * in case of tied @a, pessimise: push (@a) onto stack, then assign + * result back to @a at the end of this function */ + if (PL_op->op_private & OPpSORT_INPLACE) { + assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV); + (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */ + av = (AV*)(*SP); + max = AvFILL(av) + 1; + if (SvMAGICAL(av)) { + MEXTEND(SP, max); + p2 = SP; + for (i=0; i < (U32)max; i++) { + SV **svp = av_fetch(av, i, FALSE); + *SP++ = (svp) ? *svp : Nullsv; + } + } + else { + p1 = p2 = AvARRAY(av); + sorting_av = 1; + } + } + else { + p2 = MARK+1; + max = SP - MARK; + } + + /* shuffle stack down, removing optional initial cv (p1!=p2), plus any + * nulls; also stringify any args */ + for (i=max; i > 0 ; i--) { + if ((*p1 = *p2++)) { /* Weed out nulls. */ + SvTEMP_off(*p1); + if (!PL_sortcop && !SvPOK(*p1)) { STRLEN n_a; - if (SvAMAGIC(*up)) + if (SvAMAGIC(*p1)) overloading = 1; else - (void)sv_2pv(*up, &n_a); + (void)sv_2pv(*p1, &n_a); } - up++; + p1++; } + else + max--; } - max = --up - myorigmark; - if (PL_sortcop) { - if (max > 1) { + if (sorting_av) + AvFILLp(av) = max-1; + + if (max > 1) { + if (PL_sortcop) { PERL_CONTEXT *cx; SV** newsp; bool oldcatch = CATCH_GET; @@ -1512,10 +1546,6 @@ PP(pp_sort) PL_secondgv = gv_fetchpv("b", TRUE, SVt_PV); PL_sortstash = stash; } -#ifdef USE_5005THREADS - sv_lock((SV *)PL_firstgv); - sv_lock((SV *)PL_secondgv); -#endif SAVESPTR(GvSV(PL_firstgv)); SAVESPTR(GvSV(PL_secondgv)); } @@ -1525,23 +1555,19 @@ PP(pp_sort) cx->cx_type = CXt_SUB; cx->blk_gimme = G_SCALAR; PUSHSUB(cx); - if (!CvDEPTH(cv)) - (void)SvREFCNT_inc(cv); /* in preparation for POPSUB */ } PL_sortcxix = cxstack_ix; if (hasargs && !is_xsub) { /* This is mostly copied from pp_entersub */ - AV *av = (AV*)PL_curpad[0]; + AV *av = (AV*)PAD_SVl(0); -#ifndef USE_5005THREADS cx->blk_sub.savearray = GvAV(PL_defgv); GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av); -#endif /* USE_5005THREADS */ - cx->blk_sub.oldcurpad = PL_curpad; + CX_CURPAD_SAVE(cx->blk_sub); cx->blk_sub.argarray = av; } - sortsv((myorigmark+1), max, + sortsv(p1-max, max, is_xsub ? sortcv_xsub : hasargs ? sortcv_stacked : sortcv); POPBLOCK(cx,PL_curpm); @@ -1549,11 +1575,9 @@ PP(pp_sort) POPSTACK; CATCH_SET(oldcatch); } - } - else { - if (max > 1) { + else { MEXTEND(SP, 20); /* Can't afford stack realloc on signal. */ - sortsv(ORIGMARK+1, max, + sortsv(sorting_av ? AvARRAY(av) : ORIGMARK+1, max, (PL_op->op_private & OPpSORT_NUMERIC) ? ( (PL_op->op_private & OPpSORT_INTEGER) ? ( overloading ? amagic_i_ncmp : sv_i_ncmp) @@ -1564,8 +1588,8 @@ PP(pp_sort) : sv_cmp_locale_static) : ( overloading ? amagic_cmp : sv_cmp_static))); if (PL_op->op_private & OPpSORT_REVERSE) { - SV **p = ORIGMARK+1; - SV **q = ORIGMARK+max; + SV **p = sorting_av ? AvARRAY(av) : ORIGMARK+1; + SV **q = p+max-1; while (p < q) { SV *tmp = *p; *p++ = *q; @@ -1574,8 +1598,28 @@ PP(pp_sort) } } } + if (av && !sorting_av) { + /* simulate pp_aassign of tied AV */ + SV *sv; + SV** base, **didstore; + for (base = ORIGMARK+1, i=0; i < max; i++) { + sv = NEWSV(28,0); + sv_setsv(sv, base[i]); + base[i] = sv; + } + av_clear(av); + av_extend(av, max); + for (i=0; i < max; i++) { + sv = base[i]; + didstore = av_store(av, i, sv); + if (SvSMAGICAL(sv)) + mg_set(sv); + if (!didstore) + sv_2mortal(sv); + } + } LEAVE; - PL_stack_sp = ORIGMARK + max; + PL_stack_sp = ORIGMARK + (sorting_av ? 0 : max); return nextop; } @@ -1610,11 +1654,7 @@ sortcv_stacked(pTHX_ SV *a, SV *b) I32 result; AV *av; -#ifdef USE_5005THREADS - av = (AV*)PL_curpad[0]; -#else av = GvAV(PL_defgv); -#endif if (AvMAX(av) < 1) { SV** ary = AvALLOC(av);