Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. import haxe.ds.Either;
  2. class Main {
  3. static function main() {
  4. trace("HI");
  5. var t : StringIntMap = ["a" => 1, "b"=> 2];
  6. trace(t.intmap + " is the value for t.intmap");
  7. }
  8. }
  9.  
  10. abstract StringIntMap (Either<Map<String,Int>,Map<Int,String>>) {
  11.  
  12. public inline function new(e : Either<Map<String,Int>,Map<Int,String>>) this = e;
  13. public var intmap(get,never):Map<Int,String>;
  14. public var stringmap(get,never): Map<String,Int>;
  15. inline function get_intmap() switch this {
  16. case Left(v) : {
  17. var result = new Map<Int,String>();
  18. for (i in v.keys()){
  19. result.set(v.get(i), i);
  20. }
  21. return result;
  22. }
  23. case Right (v) : return v;
  24. }
  25.  
  26. @:to inline function get_stringmap() : Map<String,Int> switch this {
  27. case Left(v) : return v;
  28. case Right(v) : {
  29. var result = new Map<String,Int>();
  30. for (i in v.keys()){
  31. result.set(v.get(i), i);
  32. }
  33. return result;
  34. }
  35. }
  36.  
  37. @:from static function fromStringMap( v:Map<Int,String> ):StringIntMap return new StringIntMap( Right(v) );
  38. @:from static function fromIntMap( v:Map<String,Int> ):StringIntMap return new StringIntMap(Left(v));
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement