Created
August 5, 2024 18:00
-
-
Save Crinfarr/c8989965a0b1c5293e9e44b1aba32eb2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 /* whatever you want but I use helpers as the top level and haxelib dev it */; | |
/** | |
* Intended use is with Enums but you can technically use whatever you feel like | |
*/ | |
class Bitmask<T> { | |
private final mvals:Array<T>; | |
private final maxval:Int; | |
public function new(mask:Array<T>) { | |
this.mvals = mask; | |
this.maxval = 1 << mask.length; | |
} | |
public function mask(val:Int):Array<T> { | |
if (val > maxval) | |
throw 'Value $val is larger than max value $maxval'; | |
var returns:Array<T> = []; | |
for (idx in 0...mvals.length) { | |
if (val & (idx << 1) == 1) | |
returns.push(mvals[idx]); | |
} | |
return returns; | |
} | |
public function getMask(vals:Array<T>):Null<Int> { | |
var rv = 0; | |
for (itm in vals) { | |
if (!mvals.contains(itm)) | |
return null; | |
rv |= (1<<(mvals.indexOf(itm))); | |
} | |
return rv; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment