Created
March 3, 2014 15:18
-
-
Save MindScriptAct/9327147 to your computer and use it in GitHub Desktop.
function to get all array member permutations.
This file contains 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
public static function permutate(elements:Array):Array { | |
var retVal:Array = new Array(); | |
var elementCount:int = elements.length; | |
var doSearch:Boolean = true; | |
var indexVector:Vector.<int> = new Vector.<int>(elementCount); | |
var headIndex:int = 0; | |
while (doSearch) { | |
// set up initial data | |
var tempCards:Vector.<CardVO> = new <CardVO>[]; | |
for (var j:int = 0; j < elementCount; j++) { | |
tempCards.push(elements[j]); | |
} | |
var currentCards:Array = []; | |
// form new sequence. | |
for (var i:int = 0; i < elementCount; i++) { | |
currentCards.push(tempCards.splice(indexVector[i], 1)[0]); | |
} | |
retVal.push(currentCards); | |
// advance index. | |
indexVector[headIndex]++; | |
if (indexVector[headIndex] >= elementCount - headIndex) { | |
headIndex++; | |
var indexDepth:int = 1; | |
while (headIndex && doSearch) { | |
if (headIndex < elementCount) { | |
indexVector[headIndex]++; | |
if (indexVector[headIndex] >= elementCount - indexDepth) { | |
headIndex++ | |
indexDepth++; | |
} else { | |
while (headIndex > 0) { | |
headIndex--; | |
indexVector[headIndex] = 0; | |
} | |
} | |
} else { | |
doSearch = false; | |
} | |
} | |
} | |
} | |
return retVal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment