Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make composite key for a hash map in go

First, my definition of composite key - two ore more values combine to make the key. Not to confuse with composite keys in databases.

My goal is to save computed values of pow(x, y) in a hash table, where x and y are integers. This is where I need ideas on how to make a key, so that given x and y, I can look it up in the hash table, to find pow(x,y).

For example:

pow(2, 3) => {key(2,3):8}

What I want to figure out is how to get the map key for the pair (2,3), i.e. the best way to generate a key which is a combination of multiple values, and use it in hash table.

like image 590
SeattleOrBayArea Avatar asked Sep 15 '18 20:09

SeattleOrBayArea


People also ask

How do you get the map key in Golang?

You can retrieve the value assigned to a key in a map using the syntax m[key] . If the key exists in the map, you'll get the assigned value. Otherwise, you'll get the zero value of the map's value type.

Is Golang map a hash table?

Golang Maps is a collection of unordered pairs of key-value. It is widely used because it provides fast lookups and values that can retrieve, update or delete with the help of keys. It is a reference to a hash table.

Is map in Golang Hashmap?

Maps are associative containers mapping keys to values. Almost all the programming languages have these data structures. They are called hashmap in java, dictionary in python, associative array in PHP etc.

How do I create a Golang map?

Go by Example: Maps Maps are Go's built-in associative data type (sometimes called hashes or dicts in other languages). To create an empty map, use the builtin make : make(map[key-type]val-type) . Set key/value pairs using typical name[key] = val syntax. Printing a map with e.g. fmt.


1 Answers

The easiest and most flexible way is to use a struct as the key type, including all the data you want to be part of the key, so in your case:

type Key struct {
    X, Y int
}

And that's all. Using it:

m := map[Key]int{}
m[Key{2, 2}] = 4
m[Key{2, 3}] = 8

fmt.Println("2^2 = ", m[Key{2, 2}])
fmt.Println("2^3 = ", m[Key{2, 3}])

Output (try it on the Go Playground):

2^2 =  4
2^3 =  8

Spec: Map types: You may use any types as the key where the comparison operators == and != are fully defined, and the above Key struct type fulfills this.

Spec: Comparison operators: Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.

One important thing: you should not use a pointer as the key type (e.g. *Key), because comparing pointers only compares the memory address, and not the pointed values.

Also note that you could also use arrays (not slices) as key type, but arrays are not as flexible as structs. You can read more about this here: Why have arrays in Go?

This is how it would look like with arrays:

type Key [2]int

m := map[Key]int{}
m[Key{2, 2}] = 4
m[Key{2, 3}] = 8

fmt.Println("2^2 = ", m[Key{2, 2}])
fmt.Println("2^3 = ", m[Key{2, 3}])

Output is the same. Try it on the Go Playground.

like image 184
icza Avatar answered Sep 28 '22 03:09

icza