-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
61 lines (53 loc) · 1.5 KB
/
example_test.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Modifications copyright (c) Arista Networks, Inc. 2022
// Underlying
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gomap_test
import (
"bytes"
"fmt"
"hash/maphash"
"github.com/aristanetworks/gomap"
)
func ExampleMap_Iter() {
m := gomap.New(
func(a, b string) bool { return a == b },
maphash.String,
gomap.KeyElem[string, string]{"Avenue", "AVE"},
gomap.KeyElem[string, string]{"Street", "ST"},
gomap.KeyElem[string, string]{"Court", "CT"},
)
for i := m.Iter(); i.Next(); {
fmt.Printf("The abbreviation for %q is %q", i.Key(), i.Elem())
}
}
func ExampleNew() {
// Some examples of different maps:
// Map that uses []byte as the key type
byteSliceMap := gomap.New[[]byte, int](bytes.Equal, maphash.Bytes)
byteSliceMap.Set([]byte("hello"), 42)
// Map that uses map[string]struct{} as the key type
stringSetEqual := func(a, b map[string]struct{}) bool {
if len(a) != len(b) {
return false
}
for k := range a {
_, ok := b[k]
if !ok {
return false
}
}
return true
}
stringSetHash := func(seed maphash.Seed, ss map[string]struct{}) uint64 {
var sum uint64
for s := range ss {
// combine hashes with addition so that iteration order does not matter
sum += maphash.String(seed, s)
}
return sum
}
stringSetMap := gomap.New[map[string]struct{}, int](stringSetEqual, stringSetHash)
stringSetMap.Set(map[string]struct{}{"foo": {}, "bar": {}}, 42)
}