Skip to content

Commit

Permalink
Add Item function to Stepper (#25)
Browse files Browse the repository at this point in the history
The Stepper.Item function returns a struct that contains a key and a value, or nil if there is no value at the current position. This allows the key as well as the value to be retrieved from the current position.
  • Loading branch information
gammazero authored Feb 27, 2024
1 parent 44b2734 commit 037a743
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/gammazero/radixtree

go 1.20
go 1.21
21 changes: 14 additions & 7 deletions stepper.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,23 @@ func (s *Stepper) Next(radix byte) bool {
return true
}

// Item returns an Item containing the key and value at the current Stepper
// position, or returns nil if no value is present at the position.
func (s *Stepper) Item() *Item {
// Only return item if all of this node's prefix was matched. Otherwise,
// have not fully traversed into this node (edge not completely traversed).
if s.p == len(s.node.prefix) {
return s.node.leaf
}
return nil
}

// Value returns the value at the current Stepper position, and true or false
// to indicate if a value is present at the position.
func (s *Stepper) Value() (any, bool) {
// Only return value if all of this node's prefix was matched. Otherwise,
// have not fully traversed into this node (edge not completely traversed).
if s.p != len(s.node.prefix) {
return nil, false
}
if s.node.leaf == nil {
item := s.Item()
if item == nil {
return nil, false
}
return s.node.leaf.value, true
return item.value, true
}
11 changes: 11 additions & 0 deletions stepper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func TestStepper(t *testing.T) {
if ok || val != nil {
t.Fatal("should not have value at 't'")
}
item := iter.Item()
if item != nil {
t.Fatal("should not have item at 't'")
}
if !iter.Next('o') {
t.Fatal("'o' should have advanced iterator")
}
Expand All @@ -44,6 +48,13 @@ func TestStepper(t *testing.T) {
if !ok || val != "TOM" {
t.Fatalf("expected \"TOM\" at 'm', got %q", val)
}
item = iter.Item()
if item == nil || item.Value() != "TOM" {
t.Fatalf("expected value \"TOM\" at 'm'")
}
if item.Key() != "tom" {
t.Fatalf("expected key \"tom\" at 'm'")
}
if !iter.Next('a') {
t.Fatal("'a' should have advanced iterator")
}
Expand Down
11 changes: 7 additions & 4 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type radixNode struct {
// segment used in the parent to index this child.
prefix string
edges []edge
leaf *leaf
leaf *Item
}

// WalkFunc is the type of the function called for each value visited by Walk
Expand All @@ -39,11 +39,14 @@ type WalkFunc func(key string, value any) bool
// If the function returns true Inspect stops immediately and returns.
type InspectFunc func(link, prefix, key string, depth, children int, hasValue bool, value any) bool

type leaf struct {
type Item struct {
key string
value any
}

func (kv *Item) Key() string { return kv.key }
func (kv *Item) Value() any { return kv.value }

type edge struct {
radix byte
node *radixNode
Expand Down Expand Up @@ -108,7 +111,7 @@ func (t *Tree) Put(key string, value any) bool {
// data, so add child that has a prefix of the unmatched key data and
// set its value to the new value.
newChild := &radixNode{
leaf: &leaf{
leaf: &Item{
key: key,
value: value,
},
Expand Down Expand Up @@ -139,7 +142,7 @@ func (t *Tree) Put(key string, value any) bool {
isNewValue = true
t.size++
}
node.leaf = &leaf{
node.leaf = &Item{
key: key,
value: value,
}
Expand Down

0 comments on commit 037a743

Please sign in to comment.