I have a list of integers, for example 1,2,2,3,4,1. I need to be able to check equivalence (==) between different lists.
However, I do not mean a simple quantitative comparison. Each of these lists actually denotes a given section, where the position in the list denotes the index of the element, and the number denotes the index of the group. For example, in the first element 0 and element 5 are in the same group, elements 1 and 2 are in the same group, and elements 3 and 4 are in their own separate groups. The actual group index is not important, but only the grouping.
I need to be able to check equivalence in this sense, therefore, for example, the previous list would be equivalent 5,3,3,2,9,5,, since they have the same grouping.
The way I do this reduces the array to some normal form. I find all the numbers that have the same value as the first number, and set them all to 0. Then I continue on the list until I find a new number, find all the numbers of the same value, and they will all be equal to 1. I continue in this way.
In my example, both numbers decrease to decrease to 0,1,1,2,3,0, and of course, I can just use a simple comparison to make sure they are equivalent.
However, this is rather slow, since I have to make a few linear passes over the list. So, to abort the chase, is there a more efficient way to reduce these numbers to this normal form?
Be that as it may, more generally, I can avoid this reduction together and compare arrays in a different and possibly more efficient way
Implementation details
- These arrays are actually implemented as bits to save space, so I really need to iterate over the entire list every time that rb_tree esque hashing does not occur.
- Large numbers of these arrays will be stored in stl unordered_set, therefore it is necessary to consider the hash requirement.