|
|
|
|
|
by omaranto
4887 days ago
|
|
Just to be clear: you are not explaining how car54whereareu's code solves the problem of using arbitrary length sequences as keys, but rather explaining a different solution, right? car54whereareu's code, which uses structs as keys doesn't seem to me as if it would work for variable length sequences given Go's types. Your solution is what I had in mind when I said above you can concert your keys to strings. The way I see it there are two cases for m[f(a)]: 1. f is injective: this pretty much forces f to return a string, since it must return a data type that is allowed as a key and that can represent arbitrary length sequences without loss of information. 2. f is not injective: then we're using f as a hash function and we need to store the full key inside the value (i.e., m[f(a)]=pair(a,b)) so look ups can make equality checks for the key. You also need to handle f-collisions somehow (I just described disallowing keys with the same f, but that may not work for certain applications). You are basically writing your own hash table at this point. |
|
If you want to tune for performance, you can always replace the convert-to-string code with something custom later. I've seen a few HashTable implementations in Go floating around on the internet.