Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvement to Trie's implementation (#2777)
* Move trie implementation to new file * Add benchmarks for trie impl * Remove return val `error` from `trieNode.insert()` * Add unit tests for trie impl * Switch to iterative trie impl * Remove lowercase op in `trieNode.contains()`
- Loading branch information
Showing
4 changed files
with
201 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package types | ||
|
||
import "strings" | ||
|
||
type trieNode struct { | ||
isLeaf bool | ||
children map[rune]*trieNode | ||
} | ||
|
||
func (t *trieNode) insert(s string) { | ||
runes := []rune(s) | ||
|
||
if t.children == nil { | ||
t.children = map[rune]*trieNode{} | ||
} | ||
|
||
ptr := t | ||
for i := len(runes) - 1; i >= 0; i-- { | ||
c, ok := ptr.children[runes[i]] | ||
|
||
if !ok { | ||
ptr.children[runes[i]] = &trieNode{children: map[rune]*trieNode{}} | ||
c = ptr.children[runes[i]] | ||
} | ||
|
||
ptr = c | ||
} | ||
|
||
ptr.isLeaf = true | ||
} | ||
|
||
func (t *trieNode) contains(s string) (string, bool) { | ||
rs := []rune(s) | ||
|
||
builder, wMatch := strings.Builder{}, "" | ||
found := true | ||
|
||
ptr := t | ||
for i := len(rs) - 1; i >= 0; i-- { | ||
child, ok := ptr.children[rs[i]] | ||
|
||
if _, wOk := ptr.children['*']; wOk { | ||
wMatch = builder.String() + string('*') | ||
} | ||
|
||
if !ok { | ||
found = false | ||
break | ||
} | ||
|
||
builder.WriteRune(rs[i]) | ||
ptr = child | ||
} | ||
|
||
if found && ptr.isLeaf { | ||
return reverseString(builder.String()), true | ||
} | ||
|
||
if _, ok := ptr.children['*']; ok { | ||
builder.WriteRune('*') | ||
return reverseString(builder.String()), true | ||
} | ||
|
||
return reverseString(wMatch), wMatch != "" | ||
} | ||
|
||
func reverseString(s string) string { | ||
rs := []rune(s) | ||
for i, j := 0, len(rs)-1; i < len(rs)/2; i, j = i+1, j-1 { | ||
rs[i], rs[j] = rs[j], rs[i] | ||
} | ||
|
||
return string(rs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTrieInsert(t *testing.T) { | ||
t.Parallel() | ||
|
||
root := trieNode{} | ||
|
||
const val = "k6.io" | ||
root.insert(val) | ||
|
||
ptr := &root | ||
for i, rs := len(val)-1, []rune(val); i >= 0; i-- { | ||
next, ok := ptr.children[rs[i]] | ||
require.True(t, ok) | ||
ptr = next | ||
} | ||
|
||
require.True(t, ptr.isLeaf) | ||
} | ||
|
||
func TestTrieContains(t *testing.T) { | ||
t.Parallel() | ||
|
||
root := trieNode{} | ||
root.insert("k6.io") | ||
root.insert("specific.k6.io") | ||
root.insert("*.k6.io") | ||
|
||
tcs := []struct { | ||
query, expVal string | ||
found bool | ||
}{ | ||
// Trie functionality | ||
{query: "k6.io", expVal: "k6.io", found: true}, | ||
{query: "io", expVal: "", found: false}, | ||
{query: "no.k6.no.io", expVal: "", found: false}, | ||
{query: "specific.k6.io", expVal: "specific.k6.io", found: true}, | ||
{query: "", expVal: "", found: false}, | ||
{query: "long.long.long.long.long.long.long.long.no.match", expVal: "", found: false}, | ||
{query: "pre.matching.long.long.long.long.test.k6.noio", expVal: "", found: false}, | ||
|
||
// Wildcard | ||
{query: "foo.k6.io", expVal: "*.k6.io", found: true}, | ||
{query: "specific.k6.io", expVal: "specific.k6.io", found: true}, | ||
{query: "not.specific.k6.io", expVal: "*.k6.io", found: true}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
tc := tc | ||
t.Run(tc.query, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
val, ok := root.contains(tc.query) | ||
|
||
require.Equal(t, tc.found, ok) | ||
require.Equal(t, tc.expVal, val) | ||
}) | ||
} | ||
} | ||
|
||
func TestReverseString(t *testing.T) { | ||
t.Parallel() | ||
|
||
tcs := []struct{ str, rev string }{ | ||
{str: "even", rev: "neve"}, | ||
{str: "odd", rev: "ddo"}, | ||
{str: "", rev: ""}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
tc := tc | ||
|
||
t.Run(tc.str, func(t *testing.T) { | ||
t.Parallel() | ||
val := reverseString(tc.str) | ||
|
||
require.Equal(t, tc.rev, val) | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkTrieInsert(b *testing.B) { | ||
arr := []string{ | ||
"k6.io", "*.sub.k6.io", "specific.sub.k6.io", | ||
"grafana.com", "*.sub.sub.grafana.com", "test.sub.sub.grafana.com", | ||
} | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
root := trieNode{} | ||
for _, v := range arr { | ||
root.insert(v) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkTrieContains(b *testing.B) { | ||
root := trieNode{} | ||
arr := []string{ | ||
"k6.io", "*.sub.k6.io", "specific.sub.k6.io", | ||
"grafana.com", "*.sub.sub.grafana.com", "test.sub.sub.grafana.com", | ||
} | ||
|
||
for _, v := range arr { | ||
root.insert(v) | ||
} | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
for _, v := range arr { | ||
root.contains(v) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters