Skip to content

Instantly share code, notes, and snippets.

@odoe
Created November 18, 2010 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odoe/705345 to your computer and use it in GitHub Desktop.
Save odoe/705345 to your computer and use it in GitHub Desktop.
Actionscript 3 implementation of Duff's Device. Seems to work well.
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