I have already working, good and fast algorithm in java that uses Hashmap.
Hashmap<Point, Float>map; //point object as a key. no nulls objects in key and values
Point class has a following structure
static public class Point {
private int x;
private int y;
int hashCode() {
int hash = 5;
hash = 59 * hash + this->x;
hash = 59 * hash + this->y;
return hash;
}
boolean equals(Point p){
return x==p.x&&y==p.y;
}
}
I should reimplement my algorithm in c++. I decided to use c++ stl hashmap.
I just copied Point class with small changes according to c++ syntax, and wrote map in following way:
std::map<long, float>mymap;//key-hashcode of the point
mymap[point.hashcode()]=45;//example of using
But I have found, that sometimes c++ version of algorithm returns wrong result. After a lot of debugging, I found, that some points returns equal hashcodes:
Point(8,89).hashcode()==17966; //x=8,y=89
Point(9,30).hashcode()==17966; //x=9,y=30
java Hashmap resolves this conflict with equals function inside hashmap as a hash conflict resolution, but I do not know, how to implement this in c++... May be I should write better hash function, but I think this will not fully resolve my problem, only remove some collision, but will create new. Can you help me?
UPD:
added to Point class the following code:
bool operator <(const Point& rhs) const
{
return getX()<rhs.getX()|| (!(rhs.getX()<getX()) && getY()<rhs.getY());
}
and declared map as following:
std::map<Point, float>mymap;
and all started to work as intended