In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Setting one and resetting the others

Get Adobe Flash player
by Fumio 30 Jul 2010
// forked from Fumio's Adding elements to an Array
package {
	import flash.display.Sprite;
	import flash.utils.getTimer;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	[SWF(width = "240",height = "180")]
	public class SettingOneOutofAll extends Sprite {
		private const MAX_NUMBER:int = 10000;
		private var i:uint;
		private var j:uint;
		private var started:int;
		private var my_txt:TextField = new TextField();
		private var label_txt:TextField = new TextField();
		public function SettingOneOutofAll() {
			// Creating a TextField for display
			createTextField();
			// Creating a Array for testing
			var _array:Array = createArray(MAX_NUMBER);
			// Starting test
			findOneAndSet(_array);
			resetAllAndSetOne(_array);
		}
		private function findOneAndSet(_array:Array):void {
			started = getTimer();
			var nLength:uint = _array.length;
			var my_array:Array = new Array(nLength);
			for (i = 0; i < nLength; i++) {
				var n:uint = _array[i];
				for (j = 0; j < nLength; j++) {
					if (_array[j] == n) {
						my_array[j] = 1;
					} else {
						my_array[j] = 0;
					}
				}
			}
			xTrace(getTimer() - started);
		}
		private function resetAllAndSetOne(_array:Array):void {
			started = getTimer();
			var nLength:uint = _array.length;
			var my_array:Array = new Array(nLength);
			for (i = 0; i < nLength; i++) {
				for (j = 0; j < nLength; j++) {
					my_array[j] = 0;
				}
				my_array[i] = 1;
			}
			xTrace(getTimer() - started);
		}
		private function createTextField():void {
			addChild(my_txt);
			addChild(label_txt);
			my_txt.autoSize = TextFieldAutoSize.LEFT;
			my_txt.x = 100;
			label_txt.autoSize = TextFieldAutoSize.LEFT;
			label_txt.text = "find one and set it:\nreset all and set one:";
		}
		public function createArray(nCount:uint):Array {
			var i:uint = 0;
			var _array:Array = [];
			while (_array.push(i++) < nCount);
			return _array;
		}
		private function xTrace(n:int):void {
			my_txt.appendText(String(n) + "\n");
		}
	}
}