btree icon indicating copy to clipboard operation
btree copied to clipboard

The Map[K, V].find() API design discussion

Open sarrow104 opened this issue 3 years ago • 4 comments

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)

sarrow104 avatar Jan 14 '22 12:01 sarrow104

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.

tidwall avatar Jan 14 '22 14:01 tidwall

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)

sarrow104 avatar Jan 15 '22 03:01 sarrow104

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
})

tidwall avatar Mar 22 '22 16:03 tidwall

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.

jason-ni avatar Jul 15 '22 09:07 jason-ni