MCPcopy Create free account
hub / github.com/cantabular/faststringmap / Example

Function Example

uint32_store_example_test.go:11–59  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

9)
10
11func Example() {
12 m := exampleSource{
13 "key1": 42,
14 "key2": 27644437,
15 "l": 2,
16 }
17
18 fm := faststringmap.NewUint32Store(m)
19
20 // add an entry that is not in the fast map
21 m["m"] = 4
22
23 // sort the keys so output is the same for each test run
24 keys := make([]string, 0, len(m))
25 for k := range m {
26 keys = append(keys, k)
27 }
28 sort.Strings(keys)
29
30 // lookup every key in the fast map and print the corresponding value
31 for _, k := range keys {
32 v, ok := fm.LookupString(k)
33 fmt.Printf("%q: %d, %v\n", k, v, ok)
34 }
35
36 // Dump out the store to aid in understanding the implementation
37 fmt.Println()
38 dump := fmt.Sprintf("%+v", fm)
39 dump = strings.ReplaceAll(dump, "}", "}\n")
40 dump = strings.ReplaceAll(dump, "[", "[\n ")
41 fmt.Println(dump)
42
43 // Output:
44 //
45 // "key1": 42, true
46 // "key2": 27644437, true
47 // "l": 2, true
48 // "m": 0, false
49 //
50 // {store:[
51 // {nextLo:1 nextLen:2 nextOffset:107 valid:false value:0}
52 // {nextLo:3 nextLen:1 nextOffset:101 valid:false value:0}
53 // {nextLo:0 nextLen:0 nextOffset:0 valid:true value:2}
54 // {nextLo:4 nextLen:1 nextOffset:121 valid:false value:0}
55 // {nextLo:5 nextLen:2 nextOffset:49 valid:false value:0}
56 // {nextLo:0 nextLen:0 nextOffset:0 valid:true value:42}
57 // {nextLo:0 nextLen:0 nextOffset:0 valid:true value:27644437}
58 // ]}
59}
60
61type exampleSource map[string]uint32
62

Callers

nothing calls this directly

Calls 2

LookupStringMethod · 0.95
NewUint32StoreFunction · 0.92

Tested by

no test coverage detected