-
Notifications
You must be signed in to change notification settings - Fork 55
/
api.go
139 lines (112 loc) · 5.02 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package art
import "errors"
// Node types.
const (
Leaf Kind = 0
Node4 Kind = 1
Node16 Kind = 2
Node48 Kind = 3
Node256 Kind = 4
)
// Traverse Options.
const (
// Iterate only over leaf nodes.
TraverseLeaf = 1
// Iterate only over non-leaf nodes.
TraverseNode = 2
// Iterate over all nodes in the tree.
TraverseAll = TraverseLeaf | TraverseNode
// Iterate in reverse order.
TraverseReverse = 4
)
// These errors can be returned when iteration over the tree.
var (
ErrConcurrentModification = errors.New("concurrent modification has been detected")
ErrNoMoreNodes = errors.New("there are no more nodes in the tree")
)
// Kind is a node type.
type Kind int
// String returns string representation of the Kind value.
func (k Kind) String() string {
return []string{"Leaf", "Node4", "Node16", "Node48", "Node256"}[k]
}
// Key represents the type used for keys in the Adaptive Radix Tree.
// It can consist of any byte sequence, including Unicode characters and null bytes.
type Key []byte
// Value is an interface representing the value type stored in the tree.
// Any type of data can be stored as a Value.
type Value interface{}
// Callback defines the function type used during tree traversal.
// It is invoked for each node visited in the traversal.
// If the callback function returns false, the iteration is terminated early.
type Callback func(node Node) (cont bool)
// Node represents a node within the Adaptive Radix Tree.
type Node interface {
// Kind returns the type of the node, distinguishing between leaf and internal nodes.
Kind() Kind
// Key returns the key associated with a leaf node.
// This method should only be called on leaf nodes.
// Calling this on a non-leaf node will return nil.
Key() Key
// Value returns the value stored in a leaf node.
// This method should only be called on leaf nodes.
// Calling this on a non-leaf node will return nil.
Value() Value
}
// Iterator provides a mechanism to traverse nodes in key order within the tree.
type Iterator interface {
// HasNext returns true if there are more nodes to visit during the iteration.
// Use this method to check for remaining nodes before calling Next.
HasNext() bool
// Next returns the next node in the iteration and advances the iterator's position.
// If the iteration has no more nodes, it returns ErrNoMoreNodes error.
// Ensure you call HasNext before invoking Next to avoid errors.
// If the tree has been structurally modified since the iterator was created,
// it returns an ErrConcurrentModification error.
Next() (Node, error)
}
// Tree is an Adaptive Radix Tree interface.
type Tree interface {
// Insert adds a new key-value pair into the tree.
// If the key already exists in the tree, it updates its value and returns the old value along with true.
// If the key is new, it returns nil and false.
Insert(key Key, value Value) (oldValue Value, updated bool)
// Delete removes the specified key and its associated value from the tree.
// If the key is found and deleted, it returns the removed value and true.
// If the key does not exist, it returns nil and false.
Delete(key Key) (value Value, deleted bool)
// Search retrieves the value associated with the specified key in the tree.
// If the key exists, it returns the value and true.
// If the key does not exist, it returns nil and false.
Search(key Key) (value Value, found bool)
// ForEach iterates over all the nodes in the tree, invoking a provided callback function for each node.
// By default, it processes leaf nodes in ascending order.
// The iteration can be customized using options:
// - Pass TraverseReverse to iterate over nodes in descending order.
// The iteration stops if the callback function returns false, allowing for early termination.
ForEach(callback Callback, options ...int)
// ForEachPrefix iterates over all leaf nodes whose keys start with the specified keyPrefix,
// invoking a provided callback function for each matching node.
// By default, the iteration processes nodes in ascending order.
// Use the TraverseReverse option to iterate over nodes in descending order.
// Iteration stops if the callback function returns false, allowing for early termination.
ForEachPrefix(keyPrefix Key, callback Callback, options ...int)
// Iterator returns an iterator for traversing leaf nodes in the tree.
// By default, the iteration occurs in ascending order.
// To traverse nodes in reverse (descending) order, pass the TraverseReverse option.
Iterator(options ...int) Iterator
// Minimum retrieves the leaf node with the smallest key in the tree.
// If such a leaf is found, it returns its value and true.
// If the tree is empty, it returns nil and false.
Minimum() (Value, bool)
// Maximum retrieves the leaf node with the largest key in the tree.
// If such a leaf is found, it returns its value and true.
// If the tree is empty, it returns nil and false.
Maximum() (Value, bool)
// Size returns the number of key-value pairs stored in the tree.
Size() int
}
// New creates a new adaptive radix tree.
func New() Tree {
return newTree()
}