Last active
August 29, 2015 14:16
-
-
Save JohannesMP/393cf1612317355a1c8b 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
// A proof of concept of letting you iterate over a HashMap. | |
class Map[String, Real3] | |
{ | |
// Private Data holders that shold not be touched | |
var PRIVATE_Hash : HashMap[String, Real3] = new HashMap[String, Real3](); // holds our data | |
var PRIVATE_Array : Array[String] = new Array[String](); // used to iterate | |
// Used by ForEach | |
var All : MapRange[String, Real3] { get { return new MapRange[String, Real3](this); } } | |
// The total number of elements in the map | |
var Count : Integer { get { return this.PRIVATE_Array.Count; } } | |
constructor() { } | |
// If the map contains a value for the given key | |
function Contains(key : String) : Boolean | |
{ | |
return this.PRIVATE_Hash.Contains(key); | |
} | |
// Setting the value corresponding to a given key | |
function Set(key : String, val : Real3) | |
{ | |
if(!this.Contains(key)) | |
{ | |
this.PRIVATE_Array.Add(key); | |
} | |
this.PRIVATE_Hash.SetOrOverwrite(key, val); | |
} | |
// Getting the value corresponding to a given key | |
function Get(key : String) : Real3 | |
{ | |
return this.PRIVATE_Hash.GetOrError(key); | |
} | |
// remove a key (and associated value) from the map. | |
function Remove(key : String) : Boolean | |
{ | |
if(this.PRIVATE_Array.RemoveAll(key) > 0) | |
{ | |
this.PRIVATE_Hash.RemoveOrError(key); | |
return true; | |
} | |
return false; | |
} | |
// Get the key corresponding to a given index | |
function GetKey(index : Integer) : String | |
{ | |
return this.PRIVATE_Array.Get(index); | |
} | |
} | |
// The corresponding Range class for the Map class | |
class MapRange[String, Real3] | |
{ | |
// Private Properties that shold not be touched | |
var PRIVATE_Map : Map[String, Real3]; | |
var PRIVATE_Range : ArrayRange[String]; | |
constructor(map : Map[String, Real3]) | |
{ | |
this.PRIVATE_Map = map; | |
this.PRIVATE_Range = map.PRIVATE_Array.All; | |
this.Reset(); | |
} | |
// Properties and Functions used by foreach | |
var All : MapRange[String, Real3] { get { return this; } } | |
var Current : Real3 { get { return this.PRIVATE_Map.Get(this.PRIVATE_Range.Current); } } | |
var IsEmpty : Boolean { get { return this.PRIVATE_Range.IsEmpty; } } | |
var IsNotEmpty : Boolean { get { return this.PRIVATE_Range.IsNotEmpty; } } | |
function MoveNext() { this.PRIVATE_Range.MoveNext(); } | |
function Reset() { this.PRIVATE_Range.Reset(); } | |
} | |
class MapTest : ZilchComponent | |
{ | |
function Initialize(init : CogInitializer) | |
{ | |
// the map to iterate over | |
var map = new Map[String, Real3](); | |
// Adding elements to the map | |
map.Set("foo" , Real3(1,1,1)); | |
map.Set("derp", Real3(3,3,3)); | |
map.Set("bar" , Real3(2,2,2)); | |
map.Set("glork", Real3(4,4,4)); | |
Console.WriteLine("--- Map with `map.Count` elements:"); | |
var i = 0; // note: proper zilch integration shold have both key and val exposed in foreach. like php's foreach: http://php.net/manual/en/control-structures.foreach.php | |
foreach(var el in map) | |
{ | |
Console.WriteLine("`map.GetKey(i)`: `el`"); | |
++i; | |
} | |
// removing some elements | |
map.Remove("derp"); | |
map.Remove("glork"); | |
Console.WriteLine("--- Map with `map.Count` elements:"); | |
i = 0; | |
foreach(var el in map) | |
{ | |
Console.WriteLine("`map.GetKey(i)`: `el`"); | |
++i; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment