PrefixMap is a prefix-enhanced map that eases the retrieval of values based on key prefixes.
// creates the map object
prefixMap := prefixmap.New()
// inserts values 1, "value 2" and false for key 'someKey'
prefixMap.Insert("someKey", 1, "value 2", false)
// map now contains
//
// 'someKey' => [1, "value 2", false]
prefixMap.Insert("key", "hello")
// map contents:
//
// 'key' => ["hello"]
prefixMap.Insert("key", "world")
// map contents:
//
// 'key' => ["hello", "world"]
// now replacing the contents for key
prefixMap.Replace("key", "new value")
// map contents:
//
// 'key' => ["new value"]
prefixMap.Insert("key", "hello")
prefixMap.Contains("k") // #=> false
prefixMap.Contains("key") // #=> true
prefixMap.ContainsPrefix("k") // #=> true
prefixMap.Insert("foo", "bar", "baz", "quz")
data := prefixMap.Get("foo") // #=> [bar, baz, quz]
prefixMap.Insert("prefix1", "prefix1")
prefixMap.Insert("prefix2", "prefix2")
prefixMap.Insert("prefix3", "prefix3")
data := prefixMap.GetByPrefix("prefix") // #=> [prefix1, prefix2, prefix3]
PrefixMap exposes an EachPrefix method that executes a callback function against every prefix in the map. The prefixes are iterated over using a Depth First Search algorithm. At each iteration the given callback is invoked. The callback allows you to skip a branch iteration altogether if you're not satisfied with what you're looking for. Check out PrefixCallback documentation for more information.
prefixMap.EachPrefix(func(prefix Prefix) (bool, bool) {
// do something with the current prefix
doSomething(prefix.Key)
// keep iterating
return false, false
})
The code contained in this repository is provided as is under the terms of the MIT license as specified here.