Skip to content

Commit 739c7ef

Browse files
committed
Multi-find function bug fixes
When a terminal tree node is unambiguously found in the tree, multi-find functions do not return additional child nodes.
1 parent 1430afe commit 739c7ef

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

prefixtree.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ func (t *Tree) FindKeys(prefix string) (keys []string) {
105105
if err == ErrPrefixNotFound {
106106
return []string{}
107107
}
108+
if st.isTerminal() && err != ErrPrefixAmbiguous {
109+
return []string{st.key}
110+
}
108111
return appendDescendantKeys(st, nil)
109112
}
110113

@@ -127,6 +130,9 @@ func (t *Tree) FindKeyValues(prefix string) (values []KeyValue) {
127130
if err == ErrPrefixNotFound {
128131
return []KeyValue{}
129132
}
133+
if st.isTerminal() && err != ErrPrefixAmbiguous {
134+
return []KeyValue{{st.key, st.value}}
135+
}
130136
return appendDescendantKeyValues(st, nil)
131137
}
132138

@@ -137,6 +143,9 @@ func (t *Tree) FindValues(prefix string) (values []any) {
137143
if err == ErrPrefixNotFound {
138144
return []any{}
139145
}
146+
if st.isTerminal() && err != ErrPrefixAmbiguous {
147+
return []any{st.value}
148+
}
140149
return appendDescendantValues(st, nil)
141150
}
142151

prefixtree_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ func TestFindKeys(t *testing.T) {
199199
keys []string
200200
}{
201201
{"", []string{"a", "apple", "applepie", "arm", "bee", "bog"}},
202-
{"a", []string{"a", "apple", "applepie", "arm"}},
202+
{"a", []string{"a"}},
203203
{"ap", []string{"apple", "applepie"}},
204204
{"app", []string{"apple", "applepie"}},
205205
{"appl", []string{"apple", "applepie"}},
206-
{"apple", []string{"apple", "applepie"}},
206+
{"apple", []string{"apple"}},
207207
{"applep", []string{"applepie"}},
208208
{"applepi", []string{"applepie"}},
209209
{"applepie", []string{"applepie"}},
@@ -221,7 +221,7 @@ func TestFindKeys(t *testing.T) {
221221
{"c", []string{}},
222222
}
223223

224-
for _, c := range cases {
224+
for i, c := range cases {
225225
keys := tree.FindKeys(c.prefix)
226226
match := false
227227
if len(keys) == len(c.keys) {
@@ -235,13 +235,13 @@ func TestFindKeys(t *testing.T) {
235235
}
236236

237237
if !match {
238-
t.Errorf("FindAllValues(\"%s\") returned %v, expected %v.\n",
239-
c.prefix, keys, c.keys)
238+
t.Errorf("Case %d: FindKeys(\"%s\") returned %v, expected %v.\n",
239+
i, c.prefix, keys, c.keys)
240240
}
241241
}
242242
}
243243

244-
func TestFindAll(t *testing.T) {
244+
func TestFindValues(t *testing.T) {
245245
entries := []entry{
246246
{"apple", 1},
247247
{"applepie", 2},
@@ -260,11 +260,11 @@ func TestFindAll(t *testing.T) {
260260
values []any
261261
}{
262262
{"", []any{3, 1, 2, 4, 5}},
263-
{"a", []any{3, 1, 2, 4}},
263+
{"a", []any{3}},
264264
{"ap", []any{1, 2}},
265265
{"app", []any{1, 2}},
266266
{"appl", []any{1, 2}},
267-
{"apple", []any{1, 2}},
267+
{"apple", []any{1}},
268268
{"applep", []any{2}},
269269
{"applepi", []any{2}},
270270
{"applepie", []any{2}},
@@ -279,7 +279,7 @@ func TestFindAll(t *testing.T) {
279279
{"c", []any{}},
280280
}
281281

282-
for _, c := range cases {
282+
for i, c := range cases {
283283
values := tree.FindValues(c.key)
284284
match := false
285285
if len(values) == len(c.values) {
@@ -293,8 +293,8 @@ func TestFindAll(t *testing.T) {
293293
}
294294

295295
if !match {
296-
t.Errorf("FindAllValues(\"%s\") returned %v, expected %v.\n",
297-
c.key, values, c.values)
296+
t.Errorf("Case %d: FindValues(\"%s\") returned %v, expected %v.\n",
297+
i, c.key, values, c.values)
298298
}
299299
}
300300
}
@@ -314,11 +314,11 @@ func TestMatchingChars(t *testing.T) {
314314
{"apple", "a", 1},
315315
{"apple", "bag", 0},
316316
}
317-
for _, c := range cases {
317+
for i, c := range cases {
318318
r := matchingChars(c.s1, c.s2)
319319
if r != c.result {
320-
t.Errorf("matchingChars(\"%s\", \"%s\") returned %d, expected %d\n",
321-
c.s1, c.s2, r, c.result)
320+
t.Errorf("Case %d: matchingChars(\"%s\", \"%s\") returned %d, expected %d\n",
321+
i, c.s1, c.s2, r, c.result)
322322
}
323323
}
324324
}
@@ -351,10 +351,10 @@ func TestDictionary(t *testing.T) {
351351
"diametricall",
352352
"diametrically",
353353
}
354-
for _, key := range keys {
354+
for i, key := range keys {
355355
_, err := tree.Find(key)
356356
if err != nil {
357-
t.Errorf("Find(\"%s\") encountered error: %v\n", key, err)
357+
t.Errorf("Case %d: Find(\"%s\") encountered error: %v\n", i, key, err)
358358
}
359359
}
360360

@@ -365,10 +365,10 @@ func TestDictionary(t *testing.T) {
365365
"de",
366366
"dea",
367367
}
368-
for _, key := range keys {
368+
for i, key := range keys {
369369
_, err := tree.Find(key)
370370
if err != ErrPrefixAmbiguous {
371-
t.Errorf("Find(\"%s\") should have been ambiguous\n", key)
371+
t.Errorf("Case %d: Find(\"%s\") should have been ambiguous\n", i, key)
372372
}
373373
}
374374
}

0 commit comments

Comments
 (0)