Created
December 1, 2013 22:18
-
-
Save vodik/7741580 to your computer and use it in GitHub Desktop.
Hash to pallet lookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
static unsigned colour_table[] = { | |
0x24, 0x25, 0x26, 0x27, 0x29, 0x30, 0x31, 0x32, | |
0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40, | |
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, | |
0x49, 0x50, 0x51, 0x59, 0x60, 0x61, 0x62, 0x63, | |
0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, | |
0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, | |
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, | |
0x96, 0x97, 0x98, 0x99, 0x100, 0x101, 0x102, 0x103, | |
0x104, 0x105, 0x107, 0x108, 0x109, 0x110, 0x111, 0x112, | |
0x113, 0x114, 0x115, 0x116, 0x117, 0x118, 0x119, 0x120, | |
0x121, 0x130, 0x131, 0x132, 0x133, 0x134, 0x135, 0x137, | |
0x138, 0x139, 0x140, 0x141, 0x143, 0x144, 0x145, 0x146, | |
0x147, 0x148, 0x149, 0x150, 0x151, 0x154, 0x155, 0x156, | |
0x157, 0x166, 0x167, 0x168, 0x169, 0x170, 0x171, 0x172, | |
0x173, 0x174, 0x175, 0x176, 0x177, 0x178, 0x179, 0x180, | |
0x181, 0x182, 0x183, 0x184, 0x185, 0x186, 0x190, 0x191, | |
0x192, 0x193, 0x197, 0x198, 0x199, 0x200, 0x215, 0x228 | |
}; | |
static uint64_t hash_sdbm(const char *str) | |
{ | |
uint64_t hash = 0; | |
const char *c; | |
for (c = str; c && *c; ++c) { | |
hash *= 65599; | |
hash += *c; | |
} | |
return hash; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int i; | |
static const size_t table_length = sizeof(colour_table) / sizeof(colour_table[0]); | |
for (i = 1; i < argc; ++i) { | |
uint64_t index = hash_sdbm(argv[i]) % table_length; | |
unsigned colour = colour_table[index]; | |
printf("\033[38;5;%um%s\033[0m\n", colour, argv[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment