/* Test hash function for hex.c Currently the colors assigned using a table of random numbers that is looked up using coordinates, but we ought to be able to just hash those coordinate values into random values. This file tests what kind of color distributions we would get out of using hash functions. */ #include #include #include #include #include #define INPUT_RANGE 20 #define OUTPUT_BUCKETS 4 static int use_rand = 0; /* https://en.wikipedia.org/wiki/Jenkins_hash_function */ static int OneAtATimeHash(int key, int r, int q) { int i, h; key ^= (r & 0xff) ^ ((q & 0xff) << 8); for(i = h = 0; i < 4; i++) { h += (key >> (i * 8)) & 0xff; h += h << 10; h ^= h >> 6; } h += h << 3; h ^= h >> 11; h += h << 15; return h & 3; } int main(void) { int count[OUTPUT_BUCKETS], r, q, h; int key = time(NULL); srand(key); memset(count, 0, sizeof(count)); for(r = -INPUT_RANGE; r <= INPUT_RANGE; r++) { for(q = -INPUT_RANGE; q <= INPUT_RANGE; q++) { if( use_rand ) { h = (int)((double)rand() / RAND_MAX * 4.0); } else { h = OneAtATimeHash(key, r, q); } printf("\x1b[30;%dm%d", h + 101, h); assert(h >= 0); assert(h < OUTPUT_BUCKETS); count[h]++; } puts("\x1b[0m"); } for(h = 0; h < OUTPUT_BUCKETS; h++) printf("h[%d] = %d\n", h, count[h]); return 0; }