I implemented a custom comparator for a map, but it seems not to work for inserting elements and finding keys.
Addition: I wonder if it is related to the implementation of STL. Since map::key_comp returns false reflexively for the two keys, which means they are equal, but map::find can find only one of them. This does not meet the specification of STL.
I tried to compare whether two function types are the same since I used function types as map's keys. The key point is to identify identical structures with different suffixes.
Therefore, I wrote this custom comparator. isTypeEqual
is a static function.
struct cmp_func_type {
bool operator()(FunctionType* a, FunctionType* b) {
if (a->getNumParams() == b->getNumParams()) {
if (isTypeEqual(a->getReturnType(), b->getReturnType())) {
for (unsigned i = 0; i < a->getNumParams(); ++i) {
if (!isTypeEqual(a->getParamType(i), b->getParamType(i))) {
return a < b;
}
}
return false; // They are equal
}
}
return a < b;
}
};
In results, I found there are two types that should be equal according to my algorithm, but they seem not to share the same key. One is i32 (%struct.nvkm_bios.100694*, i8*, i32, i16)
, the other one is i32 (%struct.nvkm_bios*, i8*, i32, i16)
.
I used cmp_func_type()(a, b)
and cmp_func_type()(b, a)
to test the result, they all returned false.
Then I assigned a value to type a
then used map.find(a)
and map.find(b)
to check whether they mapped to the same value. But only a
could be found.