Created
May 10, 2011 15:18
-
-
Save Pewpewarrows/964673 to your computer and use it in GitHub Desktop.
WebKit Array.sort() algorithm: You lazy fucks...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://svn.webkit.org/repository/webkit/trunk/Source/JavaScriptCore/runtime/ArrayPrototype.cpp | |
// "Min" sort. Not the fastest, but definitely less code than heapsort | |
// or quicksort, and much less swapping than bubblesort/insertionsort. | |
for (unsigned i = 0; i < length - 1; ++i) { | |
JSValue iObj = thisObj->get(exec, i); | |
if (exec->hadException()) | |
return JSValue::encode(jsUndefined()); | |
unsigned themin = i; | |
JSValue minObj = iObj; | |
for (unsigned j = i + 1; j < length; ++j) { | |
JSValue jObj = thisObj->get(exec, j); | |
if (exec->hadException()) | |
return JSValue::encode(jsUndefined()); | |
double compareResult; | |
if (jObj.isUndefined()) | |
compareResult = 1; // don't check minObj because there's no need to differentiate == (0) from > (1) | |
else if (minObj.isUndefined()) | |
compareResult = -1; | |
else if (callType != CallTypeNone) { | |
MarkedArgumentBuffer l; | |
l.append(jObj); | |
l.append(minObj); | |
compareResult = call(exec, function, callType, callData, exec->globalThisValue(), l).toNumber(exec); | |
} else | |
compareResult = (jObj.toString(exec) < minObj.toString(exec)) ? -1 : 1; | |
if (compareResult < 0) { | |
themin = j; | |
minObj = jObj; | |
} | |
} | |
// Swap themin and i | |
if (themin > i) { | |
thisObj->put(exec, i, minObj); | |
thisObj->put(exec, themin, iObj); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment