3

For any $v$ many $b$-bits vectors $(\mathbf{x}_0, \mathbf{x}_1, \ldots, \mathbf{x}_{v-1}) \in \{\{0, 1\}^b\}^v$, what's the fastest way to combine $\mathbf{x}_0, \mathbf{x}_1, \ldots, \mathbf{x}_{v-1}$ into a single number, such that the operation is order-sensitive?

E.g. say that $\hat+$ is some method of combining numbers (not necessarily addition, but we can define it however we want). The goal is to have $\mathbf{x}_0 \hat+ \mathbf{x}_1 \hat+ \ldots \hat+ \mathbf{x}_{v-1}$ result in a unique number different than any other order, such as, $\mathbf{x}_{v-1} \hat+ \mathbf{x}_{v-2} \hat+ \ldots \hat+ \mathbf{x}_0$.

As for fastest, speed is measured on general purpose CPUs. E.g. x86-64.


My thought so far is:

b_bits_variable xor = 0;
for (i = 0; i < v; i++) {
  xor = xor ^ (xor + x_i);
}

where:

  • b_bits_variable is a variable that has exactly $b$ many bits. E.g. if $b=16$, then we may use uint16_t in the C programming language.
  • v is $v$ (quantity of vectors as in question above).
  • x_i is $\mathbf{x}_i$ (a vector among the $v$ many ones as in question above).
  • i++ $= i+1$.
  • ^ is bit-wise XOR.
  • + is addition as commonly used in programming languages, which overflows if number is larger than $2^b - 1$. I think such overflow is basically modulo $2^b$ addition. I.e. xor + x_i $=\text{xor} + \mathbf{x}_i \mod 2^b$.
caveman
  • 721
  • 3
  • 15

1 Answers1

1

This is one simple idea.

I will use $x_i$ for the vector in $d$ dimensions and use $a_i$ for the corresponding integer in $\{0,1,\ldots, 2^d-1\}$.

Sort the $a_i$ from small to large. Let $r_i$ be the rank of $a_i$ use ranks from $\{0,1,\ldots,v-1\}$. Break ties arbitrarily during sorting.

Example: $(a_1,a_2,a_3)=(1,7,2)$ has rank vector $(r_1,r_2,r_3)=(0,2,1).$ There are three entries numbered from 0 to 2 and the middle entry 7 is the largest with rank 2.

Now consider the list of permutations of 3 objects in standard lexicographic order and their index

$$ 012~~0\\ 021~~1\\ 102~~2\\ 120~~3\\ 201~~4\\ 210~~5\\ $$ and note that this permutation is 021 so has position 1 in the list of permutations. Encoding the position as a binary vector takes $r=\lceil \log_2 v\rceil$ bits.

If $r\leq d,$ we can define $$ x_1+x_2+\cdots+x_v=x_1\oplus x_2\oplus \cdots\oplus x_v \oplus index(permutation(x_1,\ldots,x_v)), $$ so at the end we xor the index of the permutation which changes if the $x_i$ are reordered.

kodlu
  • 25,146
  • 2
  • 30
  • 63