As TMap doesn’t have a equal operator you have to do all equalities
also your code is wrong, it will return true at the fist item from Test 1 contained in Test2 ( i don’t know if that’s what you were looking but from your explanation i don’t think )
also, contain + findRef is doing 2 time the hashMap search,might be better to do a ‘Find’ and compare value
does it have to contains all the same keys ? or do they also need to be in the same order ? ( faster to fail so it’s better)
anyway, if it’s a code that is only called sometimes it’s ok to have some not so optimised code, if it’s call very often for many objects, you might want to think to and other way to do it (i can think of an enum based array ?)
new code should work fine yes, and should be good enough as long as you don’t have too much (tags/objects)*numberOf object in inventory
maybe replace “if (Test1.FindRef(Elem.Key) != Test2.FindRef(Elem.Key))” with “if (Elem.Value != Test2.FindRef(Elem.Key))” ( i’m not in front of my computer right now, but i thing a variable like that should exist here )
optimise further might be not necessary right now, for the purpose !
From 4.21, Test1.OrderIndependentCompareEqual(Test2) might be used as the same purpose, but would be slow. Code attached as below, PEACE.
/**
* Compare this map with another for equality. Does not make any assumptions about Key order.
* NOTE: this might be a candidate for operator== but it was decided to make it an explicit function
* since it can potentially be quite slow.
*
* @param Other The other map to compare against
* @returns True if both this and Other contain the same keys with values that compare ==
*/
bool OrderIndependentCompareEqual(const TMapBase& Other) const
{
// first check counts (they should be the same obviously)
if (Num() != Other.Num())
{
return false;
}
// since we know the counts are the same, we can just iterate one map and check for existence in the other
for (typename ElementSetType::TConstIterator It(Pairs); It; ++It)
{
const ValueType* BVal = Other.Find(It->Key);
if (BVal == nullptr)
{
return false;
}
if (!(*BVal == It->Value))
{
return false;
}
}
// all fields in A match B and A and B's counts are the same (so there can be no fields in B not in A)
return true;
}