Eric’s Arduous BitField Challenge Strikes Back.

Hello again, Ladies and Gentlemen. Thank you for returning for what will prove to be a most spectacular event of epic proportions—even better than what we promised you the first time because… this time we'll make it work!

Actually, I lied. I didn't make it work. It bugs the crap out of me why it doesn't work the way I believe it should. I tried a few more things, double checked the code, but still no dice. So… there is really only one thing to do without the help of someone who should know better...

So, since I actually undertook this for reasons other than writing a blog entry, I decided to do the easy thing, keep my interface the way it is (because then I can swap in the more ideal BitField class when WE figure out what the problem is).

Introducing the Array-based BitField.
Now with field-wide modifying functions!

package com.orgchart.utils {

public class BitField {

[ArrayElementType("Boolean")]
private var data:Array;

public static const DEFAULT_SIZE:int = 32;

// constructor
public function BitField(theValue:Array = null) {
data = new Array();

if (theValue != null) {
for each (var theFlag:Boolean in theValue)
data.push(theFlag);
} else {
for (var i:int = 0; i < DEFAULT_SIZE; i++)
data.push(0);
}
}

public function isSet(theIndex:int):Boolean {
if (theIndex >= data.length)
return (false);
return (data[theIndex]);
}

public function setFlag(theIndex:int):void {
if (theIndex == data.length)
data.push(true);
else if (theIndex < data.length)
data[theIndex] = true;
}

public function unSetFlag(theIndex:int):void {
if (theIndex == data.length)
data.push(false);
else if (theIndex < data.length)
data[theIndex] = false;
}

public function toggle(theIndex:int):void {
if (theIndex < data.length - 1)
data[theIndex] = !data[theIndex];
}
public function clear():void {
for each (var theFlag:Boolean in data)
theFlag = false;
}

public function setAll():void {
for each (var theFlag:Boolean in data)
theFlag = true;
}

public function get bitField():Array {
return (data);
}

// toString
public function toString():String {
var theResult:String = "BitField: ";
for each (var theFlag:Boolean in data) {
if (theFlag)
theResult += "1 ";
else
theResult += "0 ";
}
return (theResult);
}

}
}

But ya know, it really tans my hide. I hate doing things in a manner just good enough to get by. I want to do them the right way.

Anybody got an idea? Anyone? Beuhler?


about this entry