I have a structure called Col:
struct Col
{
Object* o1;
Object* o2;
Object* o3;
bool operator==(const Col &other) const
{
return o1 == o1 && o2 == o2 && o3 == o3;
}
};
I define a hash on this:
template <>
struct std::hash<Col>
{
std::size_t operator()(const Col& k) const
{
using std::size_t;
std::hash<void> void_hash;
std::size_t res = void_hash(k.o1) + void_hash(k.o2) + void_hash(k.o3;
return res;
}
};
I then have a long std::vector
of Col
objects which contains duplicates (per my definition of equality), and I wish to "process" each unique element only once.
This is what I do:
std::unordered_map<Col, bool> done_list;
for (Col c : col_list)
{
if (done_list.find(c) == done_list.end())
{
process(c);
done_list[c] = true;
}
else
{
continue;
}
}
Is this a reasonable way to check for whether the same collection of objects has been processed already? Would a sparse matrix be better?