btree
btree copied to clipboard
The Map[K, V].find() API design discussion
can you export
func (tr *Map[K, V]) find(n *mapNode[K, V], key K) (index int, found bool)
to public domain, like:
func (tr *Map[K, V]) Find(n *mapNode[K, V], key K) (index int, found bool)
for method to use:
func (tr *Map[K, V]) GetAt(index int) (K, V, bool)
The return value of find
is not the inverse of the input to GetAt
. The find
method does not return the index of a key in the entire Map
, it only returns the index of a key in the a single mapNode
.
Rather, we would need to create a new inverse function to GetAt
, such as:
func (tr *Map[K, V]) IndexOf(key K) (int, bool)
Which is certainly a possibility.
Oh, I see, I've made a mistake.
Actually, I want to do a range search; e.g. :
func (m *Map[K, V]) RangeDo(keyLow, keyHigh K, fn func(key K, value Value)) {...}
then, used like this:
m := NewMap[int, string](...)
// ... some init actions
m.RangeDo(10, 100, func(key K, value Value) {... })
so, I think, I need Find(key K) (int, bool)
For an inclusive range operation you can use the Ascend
function.
min, max := 10, 100
m.Ascend(min, func(key int, value string) bool {
if key > max {
return false
}
// TODO: something with the key/value pair
return true
})
For an inclusive range operation you can use the
Ascend
function.min, max := 10, 100 m.Ascend(min, func(key int, value string) bool { if key > max { return false } // TODO: something with the key/value pair return true })
For an inclusive range operation you can use the
Ascend
function.min, max := 10, 100 m.Ascend(min, func(key int, value string) bool { if key > max { return false } // TODO: something with the key/value pair return true })
Does this mean we are doing a full scan on the dataset? A range query on a btree would be best to leverage the btree structure and achieve a O(log n) complexity.