Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sarrow104 opened this issue Jan 14, 2022 · 4 comments
Open

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

sarrow104 opened this issue Jan 14, 2022 · 4 comments

Comments

@sarrow104
Copy link

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)
@tidwall
Copy link
Owner

tidwall commented Jan 14, 2022

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.

@sarrow104
Copy link
Author

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)

@tidwall
Copy link
Owner

tidwall commented Mar 22, 2022

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

@jason-ni
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants