TMap using int and object
BlitzMax Forums/BlitzMax Programming/TMap using int and object
| ||
I thought I would be handy to use a TMap to map object ID's to the actual object. However TMap uses 2x objects, is there a way around I haven't thought about? I need to compare the actual value, so if I create an object with an value the comparison won't act on the values, but on the object references. If I loop the TMap to get the value, then I can just as well use an List. Is what I'm trying to do impossible? Can I override compare? Ex: Type Number Field value:Int EndType Local One:Number = New Number One.Value = 1 Local map:TMap = CreateMap() map.Insert(One,"tank") Local AnotherOne:Number = New Number AnotherOne.Value = 1 If map.Contains(AnotherOne) Then Print "Got an 1" Else Print "Fail |
| ||
oki, found a solution :P If anyone has a nicer one, please share though^^ Local One:Int = 1 Local map:TMap = CreateMap() map.Insert(""+One,"tank") Local AnotherOne:Int = 1 If map.Contains(""+AnotherOne) Then Print "Got an 1" Else Print "" |
| ||
You could use value field as the key for the TMap like this...Local map:TMap = CreateMap() Type Number Field value:Int EndType Local One:Number = New Number One.Value = 1 map.Insert(string(One.Value),One) But every type Number instance should have diferent value so that the resulting key is also different. If you store objects in TMap with the same key, then each new added object just replace the previous one. |
| ||
Type TInteger Field value:Int Method Compare:Int(o:Object) local i:TInteger=TInteger(o) if i.value>value return 1 if i.value<value return -1 return 0 EndMethod EndType |
| ||
So you want a unique ID?Field id:String Method New() Global next_id:Int = 0 id = String(next_id) next_id :+ 1 End Method .. map.Insert(o.id, o) |
| ||
For wrapping an Integer: |
| ||
For wrapping any number: Number Class Cluster |