Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import haxe.ds.Either;
- class Main {
- static function main() {
- trace("HI");
- var t : StringIntMap = ["a" => 1, "b"=> 2];
- trace(t.intmap + " is the value for t.intmap");
- }
- }
- abstract StringIntMap (Either<Map<String,Int>,Map<Int,String>>) {
- public inline function new(e : Either<Map<String,Int>,Map<Int,String>>) this = e;
- public var intmap(get,never):Map<Int,String>;
- public var stringmap(get,never): Map<String,Int>;
- inline function get_intmap() switch this {
- case Left(v) : {
- var result = new Map<Int,String>();
- for (i in v.keys()){
- result.set(v.get(i), i);
- }
- return result;
- }
- case Right (v) : return v;
- }
- @:to inline function get_stringmap() : Map<String,Int> switch this {
- case Left(v) : return v;
- case Right(v) : {
- var result = new Map<String,Int>();
- for (i in v.keys()){
- result.set(v.get(i), i);
- }
- return result;
- }
- }
- @:from static function fromStringMap( v:Map<Int,String> ):StringIntMap return new StringIntMap( Right(v) );
- @:from static function fromIntMap( v:Map<String,Int> ):StringIntMap return new StringIntMap(Left(v));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement