/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* *NSS_CMSArray_Count-countnumberofelementsinarray
*/ int
NSS_CMSArray_Count(void **array)
{ int n = 0;
if (array == NULL) return0;
while (*array++ != NULL)
n++;
return n;
}
/* *NSS_CMSArray_Sort-sortanarrayinplace * *If"secondary"or"tertiaryarenotNULL,itmustbearrayswiththesame *numberofelementsas"primary".Thesamereorderingwillgetappliedtoit. * *"compare"isafunctionthatreturns *<0whenthefirstelementislessthanthesecond *=0whenthefirstelementisequaltothesecond *>0whenthefirstelementisgreaterthanthesecond *toacheiveascendingordering.
*/ void
NSS_CMSArray_Sort(void **primary, int (*compare)(void *, void *), void **secondary, void **tertiary)
{ int n, i, limit, lastxchg; void *tmp;
n = NSS_CMSArray_Count(primary);
PORT_Assert(secondary == NULL || NSS_CMSArray_Count(secondary) == n);
PORT_Assert(tertiary == NULL || NSS_CMSArray_Count(tertiary) == n); /* Companion arrays must have the same length as primary; if they don't,
* ignore them rather than swapping past their NULL terminator (OOB). */ if (secondary && NSS_CMSArray_Count(secondary) != n)
secondary = NULL; if (tertiary && NSS_CMSArray_Count(tertiary) != n)
tertiary = NULL;
if (n <= 1) /* ordering is fine */ return;
/* yes, ladies and gentlemen, it's BUBBLE SORT TIME! */
limit = n - 1; while (1) {
lastxchg = 0; for (i = 0; i < limit; i++) { if ((*compare)(primary[i], primary[i + 1]) > 0) { /* exchange the neighbours */
tmp = primary[i + 1];
primary[i + 1] = primary[i];
primary[i] = tmp; if (secondary) { /* secondary array? */
tmp = secondary[i + 1]; /* exchange there as well */
secondary[i + 1] = secondary[i];
secondary[i] = tmp;
} if (tertiary) { /* tertiary array? */
tmp = tertiary[i + 1]; /* exchange there as well */
tertiary[i + 1] = tertiary[i];
tertiary[i] = tmp;
}
lastxchg = i + 1; /* index of the last element bubbled up */
}
} if (lastxchg == 0) /* no exchanges, so array is sorted */ break; /* we're done */
limit = lastxchg; /* array is sorted up to [limit] */
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.