@@ -5,64 +5,77 @@ import (
5
5
)
6
6
7
7
func New [T any ](sep string ) * TreeMap [T ] {
8
- return & TreeMap [T ]{sep : sep , children : make (map [string ]* TreeMap [T ])}
8
+ return & TreeMap [T ]{
9
+ sep : sep ,
10
+ root : & Element [T ]{children : make (map [string ]* Element [T ])},
11
+ }
9
12
}
10
13
11
- type TreeMap [T any ] struct {
12
- children map [string ]* TreeMap [T ]
13
- sep string
14
- key string
15
- value T
16
- }
14
+ type (
15
+ TreeMap [T any ] struct {
16
+ sep string
17
+ root * Element [T ]
18
+ }
17
19
18
- func ( c * TreeMap [ T ]) Put ( key string , val T ) {
19
- if ! strings . Contains ( key , c . sep ) {
20
- c . children [ key ] = & TreeMap [T ]{ key : key , value : val }
21
- } else {
22
- c . doPut ( c , & TreeMap [ T ]{ key : key , value : val }, key )
20
+ Element [ T any ] struct {
21
+ ok bool
22
+ children map [ string ] * Element [T ]
23
+ key string
24
+ value T
23
25
}
26
+ )
27
+
28
+ func (c * TreeMap [T ]) Put (key string , val T ) {
29
+ node := & Element [T ]{key : key , value : val , ok : true }
30
+ c .doPut (c .root , node , key )
24
31
}
25
32
26
- func (c * TreeMap [T ]) doPut (far , son * TreeMap [T ], key string ) {
33
+ func (c * TreeMap [T ]) doPut (far , son * Element [T ], key string ) {
27
34
if far .children == nil {
28
- far .children = make (map [string ]* TreeMap [T ])
35
+ far .children = make (map [string ]* Element [T ])
29
36
}
30
37
31
38
index := strings .Index (key , c .sep )
32
39
if index < 0 {
33
- far .children [key ] = son
40
+ if node := far .children [key ]; node == nil {
41
+ far .children [key ] = son
42
+ } else {
43
+ node .value , node .ok = son .value , son .ok
44
+ }
34
45
return
35
46
}
36
47
37
48
s0 , s1 := key [:index ], key [index + 1 :]
38
49
next := far .children [s0 ]
39
50
if far .children [s0 ] == nil {
40
- next = & TreeMap [T ]{}
51
+ next = & Element [T ]{}
41
52
far .children [s0 ] = next
42
53
}
43
54
c .doPut (next , son , s1 )
44
55
}
45
56
46
57
func (c * TreeMap [T ]) Get (key string ) (v T , exist bool ) {
47
- if result , ok := c .children [key ]; ok {
58
+ if result , ok := c .root . children [key ]; ok && result . ok {
48
59
v , exist = result .value , true
49
60
}
50
61
return
51
62
}
52
63
53
64
func (c * TreeMap [T ]) Match (key string , cb func (k string , v T )) {
54
- c .doMatch (c , key , cb )
65
+ c .doMatch (c . root , key , cb )
55
66
}
56
67
57
- func (c * TreeMap [T ]) doMatch (cur * TreeMap [T ], key string , cb func (string , T )) {
68
+ func (c * TreeMap [T ]) doMatch (cur * Element [T ], key string , cb func (string , T )) {
58
69
index := strings .Index (key , c .sep )
59
70
if index < 0 {
60
71
if key == "*" {
61
72
for _ , node := range cur .children {
62
- cb (node .key , node .value )
73
+ if node .ok {
74
+ cb (node .key , node .value )
75
+ }
63
76
}
64
77
} else {
65
- if result , ok := cur .children [key ]; ok {
78
+ if result , ok := cur .children [key ]; ok && result . ok {
66
79
cb (result .key , result .value )
67
80
}
68
81
}
0 commit comments