Created
November 18, 2010 17:56
-
-
Save odoe/705345 to your computer and use it in GitHub Desktop.
Actionscript 3 implementation of Duff's Device. Seems to work well.
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
package { | |
public class IteratorUtil { | |
public static function duffDeviceOptimized(ar:Array, process:Function):void { | |
var x:int = ar.length; | |
if (x > 0) { | |
var t:int = 0; | |
var n:int = x % 8; | |
while (n--) { | |
process(ar[t++]); | |
} | |
n = Math.floor((x + 7) / 8); | |
while (n--) { | |
process(ar[t++]); | |
process(ar[t++]); | |
process(ar[t++]); | |
process(ar[t++]); | |
process(ar[t++]); | |
process(ar[t++]); | |
process(ar[t++]); | |
process(ar[t++]); | |
} | |
} | |
} | |
public static function duffDeviceSwitch(ar:Array, process:Function):void { | |
var x:int = ar.length; | |
if (x > 0) { | |
var t:int = 0; | |
var n:int = Math.floor((x + 7) / 8); | |
var startAt:int = x % 8 | |
do { | |
switch (startAt) { | |
case 0: | |
process(ar[t++]); | |
case 7: | |
process(ar[t++]); | |
case 6: | |
process(ar[t++]); | |
case 5: | |
process(ar[t++]); | |
case 4: | |
process(ar[t++]); | |
case 3: | |
process(ar[t++]); | |
case 2: | |
process(ar[t++]); | |
case 1: | |
process(ar[t++]); | |
} | |
startAt = 0; | |
} | |
while (--n > 0); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment