Hi everyone,
I was wondering if anyone could point me towards the best way to get the intersection of two unordered sets in JUCE? Should I use the cpp code below, or is there something built into JUCE?
The example code uses sorted sets, and a call from #include :
i thought you said your sets were unordered. if they’re not too big, you could just take each element of the first and see if it’s a member of the second and, if so, add it to your intersection set.
if they’re large, you could sort them first, then go through them both at once taking matching elements and bumping each iterator whilst it’s less than the other ordered set.
They data was unordered, but it turns out the algorithm works fine/faster if I collapse the data down to ordered sets before I store them. I also modified the code to return the difference between two ordered sets. always curious to see if there are faster JUCE calls to do stuff though.
void S2MP::differenceOfSets (Array <int> &set1, Array <int> &set2)
{
int i = 0, j = 0;
int m = set1.size();
int n = set2.size();
while (i < m && j < n)
{
if (set1.getUnchecked(i) < set2.getUnchecked(j))
{
i++;
}
else if (set2.getUnchecked(j) < set1.getUnchecked(i))
{
j++;
}
else /* if set1[i] == set2[j] */
{
set1.remove(i);
i++;
j++;
}
}
}