BandoWare / GameplayTags

GameplayTags implementation for Unity
Other
53 stars 3 forks source link

HasAnyExact return true instead of false #2

Closed Akananas closed 1 month ago

Akananas commented 2 months ago

Hello!

I've noticed HasAnyExact returned the wrong result in this scenario:

If I do ContainerA.HasAnyExact(ContainerB), it returns true instead of false. I peeked a little at the code and it seems the only issue is that HasAnyInternal returns true by default instead of false. In the example above, i value is superior to end value therefore the loop is skipped and it will return true even if no match was found. As I didn't at the whole codebase, I might be overlooking something.

(Here is the function I'm talking about)

      private static bool HasAnyInternal(List<int> tagIndexes, List<int> otherTagIndexes)
      {
         if (otherTagIndexes == null || otherTagIndexes.Count == 0 || tagIndexes == null || tagIndexes.Count == 0)
         {
            return false;
         }

         int start = BinarySearchUtility.Search(tagIndexes, otherTagIndexes[0], 0, tagIndexes.Count - 1);
         if (start >= 0)
         {
            return true;
         }

         start = ~start;

         int end = BinarySearchUtility.Search(tagIndexes, otherTagIndexes[^1], start, tagIndexes.Count - 1);
         if (end >= 0)
         {
            return true;
         }

         end = ~end;

         int j = 1;
         int i = start + 1;
         while (i < end && j < otherTagIndexes.Count)
         {
            if (otherTagIndexes[j] == tagIndexes[i])
            {
               return true;
            }

            if (tagIndexes[i] > otherTagIndexes[j])
            {
               i++;
               continue;
            }

            j++;
            while (otherTagIndexes[j] < tagIndexes[i])
            {
               j++;
               if (j == end)
               {
                  return false;
               }
            }
         }

         // I believe this should return false by default
         return true;
      }

Have a nice day!

felipemcoliveira commented 2 months ago

I’m traveling, so I’ll fix it when I return home. Thanks for opening the issue!