I have an array of unsigned integers, each of which corresponds to a line with 12 characters, which can contain 4 different characters, namely "A", "B", "C", "D". Thus, the array will contain 4 ^ 12 = 16777216 elements. The order of the elements in the array is arbitrary; I can choose which one matches each row. So far, I have implemented it just like this:
unsigned int my_array[16777216]; char my_string[12]; int index = string_to_index(my_string); my_array[index] = ...;
string_to_index() simply assigns 2 bits per character as follows: A β 00, B β 01, C β 10, D β 11 For example, ABCDABCDABCD corresponds to the index (000110110001101100011011) 2 = (1776411) 10
However, I know that each line that is used to access the array represents the previous line, shifted once to the left with the new last character. For example, after accessing ABCDABCDABCD, the next access will use BCDABCDABCDA or BCDABCDABCDB, BCDABCDABCDC, BCDABCDABCDD.
So my question is: Is there a better way to implement the string_to_index function to take this last fact into account, so that elements that are sequentially available are closer in the array? I hope to improve my caching performance by doing this.
edit: Perhaps I was not very clear: I am looking for a completely different line for the index matching scheme, so the ABCDABCDABCD and BCDABCDABCDA indices are closer.
source share