3

I'm trying to make a Map with an object as a key. The problem is, that when I try to get elements from this map, I always get null. It's because I'm not providing the exact same reference as the key was. I'm providing an object with the same values, so the reference is different.

Is there any way to solve that? Can I make it use some kind of equals() function?

class PointInt
{
    public var x:Int;
    public var y:Int;

    ...
}
var map = new Map<PointInt, Hex>();

var a = new PointInt(1, 1);
var b = new PointInt(1, 1);

var hex_a = new Hex();

map[a] = hex_a;
var hex_b = map[b];

/// hex_b == null now because reference(a) == reference(b)
Gama11
  • 31,714
  • 9
  • 78
  • 100
Krzycho
  • 99
  • 1
  • 11

1 Answers1

9

As explained here and here, Map in Haxe works using the reference of the object as the key.

What you want to use instead is a HashMap like this (try.haxe link):

import haxe.ds.HashMap;

class Test {
    static function main() {

        var map = new HashMap();
        map.set(new PointInt(1, 1), 1);

        trace(map.get(new PointInt(1,1)));
    }
}

class PointInt
{
    public var x:Int;
    public var y:Int;

    public function new(x:Int, y:Int)
    {
        this.x = x;
        this.y = y;
    }

    public function hashCode():Int
    {
        return x + 1000*y; //of course don't use this, but a real hashing function
    }

    public function toString()
    {
        return '($x,$y)';
    }
}

What you need to change in your code, besides using haxe.ds.HashMap instead of Map is to implement a hashCode : Void->Int function in your key object

Since you're using an object that has 2 ints, and the hash map is just 1 int, it will happen that 2 PointInt will have the same hash code. To solve this you could create a hash map that uses strings as hashcode but if you can write (or google) a good hash function you will get better performance.

Gama11
  • 31,714
  • 9
  • 78
  • 100
npretto
  • 1,098
  • 7
  • 18
  • So what I did by now, is changing ObjectMap to ObjectMap. I've made some simple hashing function to string in PointInt class. It's slow but it does what it has to. Maybe there will be other options one day. =] – Krzycho Dec 09 '14 at 03:16
  • Is it possible that the hashCode function is required to yield unique results for different objects? http://try.haxe.org/#B753E To my understanding, a HashMap should use the hashCode function for bucketing, but not for differentiating objects. – brdlph May 20 '15 at 10:11