I'm looking for a data structure that supports this use case - there must be one out there!
A set of objects of type T can be stored in a Group, parameterised by the type T. A grouping has a GroupIdentity that uniquely identifies the group. Any T can only exist in one Group.
A management object, let's call it GroupStore, is capable of the following operations:
group(Set[T]) : GroupIdentity // Add or update a group and return its identity
remove(Set[T]) // Remove the specified Ts from any existing groups
replaceOrMove(T1, T2) // Update any group that has T1 and replace it with T2. If T2 is already in a group the effect is that T1 is removed from its source group. Otherwise, T2 is created in T1's existing group.
getIdentity(T) : GroupIdentity
Some example test cases:
group(a, b, c)
= GroupIdentity(f98eh3)
getIdentity(b)
= GroupIdentity(f98eh3)
remove(b)
getIdentity(b)
= <empty>
group(a, b, c)
= GroupIdentity(f98eh3) // a and c already exist, so add b to the group and return the existing identity
group(a, d)
= GroupIdentity(f98eh3) // a already exists, so add d to the group
move(b, e)
getIdentity(b)
= <empty>
getIdentity(e)
= GroupIdentity(f98eh3)
move(e, a) // in effect this is a removal of e and adding a to the group that e existed in
getIdentity(e)
= <empty>
getIdentity(a)
= GroupIdentity(f98eh3)
group(x, y, z)
= GroupIdentity(e9g86z)
move(a, z)
getIdentity(a)
= <empty>
getIdentity(z)
= GroupIdentity(e9g86z)
I'm guessing this already exists...
Tcan be stored in at most one group? If so, the comment ofmovedoesn't make sense. Otherwise, I don't get that sentence, since "a Group, parameterised by the type T."movedoesn't make sense?T1. It doesn't make sense to talk about "all" when referring to a single group. Seems like "replace" would be a better name for the function.