-
Notifications
You must be signed in to change notification settings - Fork 37
/
bimap.go
35 lines (28 loc) · 785 Bytes
/
bimap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import "fmt"
type Bimap struct {
forward, reverse map[string]string
}
func NewBimap() *Bimap {
return &Bimap{
forward: make(map[string]string),
reverse: make(map[string]string),
}
}
func (b *Bimap) Add(key, value string, aliases ...string) error {
if v, ok := b.forward[key]; ok && v != value {
return fmt.Errorf("key %s maps to multiple values (%s,%s)", key, v, value)
}
b.forward[key] = value
if k, ok := b.reverse[value]; ok && k != key {
return fmt.Errorf("multiple keys (%s,%s) map to the same value %s", k, key, value)
}
b.reverse[value] = key
for _, a := range aliases {
if k, ok := b.reverse[a]; ok && k != key {
return fmt.Errorf("multiple keys (%s,%s) map to the same alias %s", k, key, a)
}
b.reverse[a] = key
}
return nil
}