Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
"sort"
"strings"
"github.com/sensiblecodeio/faststringmap"
)
func main() {
m := exampleSource{
"key1": 42,
"key2": 27644437,
"l": 2,
}
fm := faststringmap.NewUint32Store(m)
// add an entry that is not in the fast map
m["m"] = 4
// sort the keys so output is the same for each test run
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
// lookup every key in the fast map and print the corresponding value
for _, k := range keys {
v, ok := fm.LookupString(k)
fmt.Printf("%q: %d, %v\n", k, v, ok)
}
// Dump out the store to aid in understanding the implementation
fmt.Println()
dump := fmt.Sprintf("%+v", fm)
dump = strings.ReplaceAll(dump, "}", "}\n")
dump = strings.ReplaceAll(dump, "[", "[\n ")
fmt.Println(dump)
}
type exampleSource map[string]uint32
func (s exampleSource) AppendKeys(a []string) []string {
for k := range s {
a = append(a, k)
}
return a
}
func (s exampleSource) Get(k string) uint32 {
return s[k]
}
Output: "key1": 42, true "key2": 27644437, true "l": 2, true "m": 0, false {store:[ {nextLo:1 nextLen:2 nextOffset:107 valid:false value:0} {nextLo:3 nextLen:1 nextOffset:101 valid:false value:0} {nextLo:0 nextLen:0 nextOffset:0 valid:true value:2} {nextLo:4 nextLen:1 nextOffset:121 valid:false value:0} {nextLo:5 nextLen:2 nextOffset:49 valid:false value:0} {nextLo:0 nextLen:0 nextOffset:0 valid:true value:42} {nextLo:0 nextLen:0 nextOffset:0 valid:true value:27644437} ]}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Uint32Source ¶
type Uint32Source interface {
// AppendKeys should append the keys of the maps to the supplied slice and return the resulting slice
AppendKeys([]string) []string
// Get should return the value for the supplied key
Get(string) uint32
}
Uint32Source is for supplying data to initialise Uint32Store
type Uint32Store ¶
type Uint32Store struct {
// contains filtered or unexported fields
}
Uint32Store is a fast read only map from string to uint32 Lookups are about 5x faster than the built-in Go map type
func NewUint32Store ¶
func NewUint32Store(src Uint32Source) Uint32Store
NewUint32Store creates from the data supplied in src
func (*Uint32Store) LookupBytes ¶
func (m *Uint32Store) LookupBytes(s []byte) (uint32, bool)
LookupBytes looks up the supplied byte slice in the map
func (*Uint32Store) LookupString ¶
func (m *Uint32Store) LookupString(s string) (uint32, bool)
LookupString looks up the supplied string in the map
Click to show internal directories.
Click to hide internal directories.