I have an unique identifier (uid) x, and given an integer j, I need a function f such that f(x, j) is another uid (in the same "domain" as x) such that f(x, j1) = f(x, j2) if and only if j1 = j2 and f(x, j) != x for any j.
A naive way to do this is to maintain x as a tuple (i1, i2, ..., ik) and define f(x, j) = (j, i1, i2, ..., ik). Is there a better, more efficient way to do this? I am specifically looking for python solutions.
Edit: I also need f(x1, j1) = f(x2, j2) if and only if x1 = x2 and j1 = j2
If you can guarantee that
jis never zero, then the function you're looking for is simply called exclusive-or.Assume the type of unique identifiers
xisint. The exclusive-or operator forms a group on Python integers, with0as the identity. Hence, as long asj != 0, we havex ^ j != x. And since exclusive-or forms a group, we can cancel off thex. Supposex ^ j1 == x ^ j2. Thenx ^ x ^ j1 = x ^ x ^ j2, and the exclusive-or of a value with itself is zero, so0 ^ j1 = 0 ^ j2, hencej1 = j2.You need
j != 0, since zero is the identity of this operation.Edit: In response to your further condition that
f(x1, j1) = f(x2, j2)impliesx1 = x2andj1 = j2, I think you're stuck with what you already found (appending to the end of a tuple). That's a very powerful condition, and I think at that point you've basically got a free monoid. So I don't see a way to do better than simple linear concatenation.