Skip to content

Commit 056aa6f

Browse files
committed
Implement Size
1 parent 50786d1 commit 056aa6f

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

trie/ctrie/ctrie.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,15 @@ func (c *Ctrie) Iterator() <-chan *Entry {
350350
return ch
351351
}
352352

353+
// Size returns the number of keys in the Ctrie.
354+
func (c *Ctrie) Size() uint {
355+
size := uint(0)
356+
for _ = range c.Iterator() {
357+
size++
358+
}
359+
return size
360+
}
361+
353362
func traverse(i *iNode, ch chan<- *Entry) {
354363
switch {
355364
case i.main.cNode != nil:

trie/ctrie/ctrie_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,14 @@ func TestIterator(t *testing.T) {
283283
assert.Equal(len(expected), count)
284284
}
285285

286+
func TestSize(t *testing.T) {
287+
ctrie := New(nil)
288+
for i := 0; i < 10; i++ {
289+
ctrie.Insert([]byte(strconv.Itoa(i)), i)
290+
}
291+
assert.Equal(t, uint(10), ctrie.Size())
292+
}
293+
286294
func BenchmarkInsert(b *testing.B) {
287295
ctrie := New(nil)
288296
b.ResetTimer()

0 commit comments

Comments
 (0)