Skip to content

Commit

Permalink
Move trieNode to separate file
Browse files Browse the repository at this point in the history
Since we use the `trieNode` in multiple files IMO this is clear
  • Loading branch information
eugercek committed Oct 27, 2022
1 parent e03e11b commit 6ef3225
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 99 deletions.
50 changes: 0 additions & 50 deletions lib/types/hostnametrie.go
Expand Up @@ -135,53 +135,3 @@ func (t *HostnameTrie) insert(s string) error {
func (t *HostnameTrie) Contains(s string) (matchedPattern string, matchFound bool) {
return t.trieNode.contains(s)
}

type trieNode struct {
isLeaf bool
children map[rune]*trieNode
}

func (t *trieNode) insert(s string) {
if len(s) == 0 {
t.isLeaf = true
return
}

// mask creation of the trie by initializing the root here
if t.children == nil {
t.children = make(map[rune]*trieNode)
}

rStr := []rune(s) // need to iterate by runes for intl' names
last := len(rStr) - 1
if c, ok := t.children[rStr[last]]; ok {
c.insert(string(rStr[:last]))
return
}

t.children[rStr[last]] = &trieNode{children: make(map[rune]*trieNode)}
t.children[rStr[last]].insert(string(rStr[:last]))
}

func (t *trieNode) contains(s string) (matchedPattern string, matchFound bool) {
s = strings.ToLower(s)
if len(s) == 0 {
if t.isLeaf {
return "", true
}
} else {
rStr := []rune(s)
last := len(rStr) - 1
if c, ok := t.children[rStr[last]]; ok {
if match, matched := c.contains(string(rStr[:last])); matched {
return match + string(rStr[last]), true
}
}
}

if _, wild := t.children['*']; wild {
return "*", true
}

return "", false
}
79 changes: 33 additions & 46 deletions lib/types/trie.go
Expand Up @@ -4,65 +4,52 @@ import (
"strings"
)

type itTrieNode struct {
type trieNode struct {
isLeaf bool
children map[rune]*itTrieNode
children map[rune]*trieNode
}

func newIterativeTrieNode() *itTrieNode {
return &itTrieNode{
children: map[rune]*itTrieNode{},
func (t *trieNode) insert(s string) {
if len(s) == 0 {
t.isLeaf = true
return
}
}

func (m *itTrieNode) insert(s string) {
runes := []rune(s)

ptr := m
for i := len(runes) - 1; i >= 0; i-- {
r := runes[i]
c, ok := ptr.children[r]

if !ok {
ptr.children[r] = &itTrieNode{children: map[rune]*itTrieNode{}}
c = ptr.children[r]
}
// mask creation of the trie by initializing the root here
if t.children == nil {
t.children = make(map[rune]*trieNode)
}

ptr = c
rStr := []rune(s) // need to iterate by runes for intl' names
last := len(rStr) - 1
if c, ok := t.children[rStr[last]]; ok {
c.insert(string(rStr[:last]))
return
}

ptr.isLeaf = true
t.children[rStr[last]] = &trieNode{children: make(map[rune]*trieNode)}
t.children[rStr[last]].insert(string(rStr[:last]))
}

func (m *itTrieNode) contains(s string) (string, bool) {
runes := []rune(s)
ptr := m

var sb strings.Builder
var wcMatch string
for i := len(runes) - 1; i >= 0 && ptr != nil; i-- {
c, ok := ptr.children[runes[i]]

if _, wOk := ptr.children['*']; wOk {
wcMatch = sb.String() + "*"
func (t *trieNode) contains(s string) (matchedPattern string, matchFound bool) {
s = strings.ToLower(s)
if len(s) == 0 {
if t.isLeaf {
return "", true
}

if !ok {
return reverse(wcMatch), wcMatch != ""
} else {
rStr := []rune(s)
last := len(rStr) - 1
if c, ok := t.children[rStr[last]]; ok {
if match, matched := c.contains(string(rStr[:last])); matched {
return match + string(rStr[last]), true
}
}

sb.WriteRune(runes[i])

ptr = c
}

return reverse(sb.String()), true
}

func reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
if _, wild := t.children['*']; wild {
return "*", true
}
return string(runes)

return "", false
}
6 changes: 3 additions & 3 deletions lib/types/trie_test.go
Expand Up @@ -11,7 +11,7 @@ func TestTrie(t *testing.T) {

t.Run("Basic insert contains", func(t *testing.T) {
t.Parallel()
root := newIterativeTrieNode()
root := trieNode{}

root.insert("foo")

Expand All @@ -21,7 +21,7 @@ func TestTrie(t *testing.T) {
})
t.Run("Trie functionality", func(t *testing.T) {
t.Parallel()
root := newIterativeTrieNode()
root := trieNode{}

root.insert("k6.io")
root.insert("test.k6.io")
Expand All @@ -46,7 +46,7 @@ func TestTrie(t *testing.T) {
})
t.Run("Wildcard", func(t *testing.T) {
t.Parallel()
root := newIterativeTrieNode()
root := trieNode{}

root.insert("*.k6.io")
root.insert("specific.k6.io")
Expand Down

0 comments on commit 6ef3225

Please sign in to comment.