How to compare TMap

If I have two TMap variables

TMap<FGameplayTag, float> Test1;
TMap<FGameplayTag, float> Test2;

What is the easiest and fast way to compare them?

I see one way only to compare them (I didn’t test next code):

if (Test1.Num() != Test2.Num()){
	
	return false;
}

for (auto& Elem : Test1){
	
	if (Elem.Value != Test2.FindRef(Elem.Key)){
		
        return false;
	}
}

return true;

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 ?)

Yes, agree :slight_smile:
I have mistaken.
Please see above the edited code.

This code wiil be used for adding items to an inventory.
I have to check all unique propierties of the item to put it to the stack or not.

How can you suggest to do it faster?

Sorry, I’ve missed your question.
They can contain different sets due to some modification.

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 !

Thank you! Agree. I think it’ll work.

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;
	}
2 Likes