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

Add a GetIterator method to treeMap etc #59

Closed
InsZVA opened this issue Jul 7, 2017 · 3 comments
Closed

Add a GetIterator method to treeMap etc #59

InsZVA opened this issue Jul 7, 2017 · 3 comments

Comments

@InsZVA
Copy link

InsZVA commented Jul 7, 2017

I think sometimes we use treemap to search continusly data, e.g.:
"search 20 items large than 50",
I think some code like below can solve it:

// Get and return iterator
func (m *Map) GetIterator(key interface{}) (iter Iterator, found bool) {
	_iter, err := m.tree.GetIterator(key)
	return Iterator{_iter}, err
}

// Get and return iterator
func (tree *Tree) GetIterator(key interface{}) (iter Iterator, found bool) {
	node := tree.lookup(key)
	if node != nil {
		return Iterator{
			tree,
			node,
			between,
		}, true
	}
	return
}
@InsZVA
Copy link
Author

InsZVA commented Jul 7, 2017

And a function GetOrNext/GetOrPrev which try to get the key or the next key in finding may be useful for union-indexing in database design.

@StarpTech
Copy link

Similiar API like https://github.com/google/btree would be great.

@emirpasic
Copy link
Owner

"search 20 items large than 50",

There are two ways now of answering such questions with GoDS (efficient and inefficient way) after #189 using enumerable (creates a new list) or using the efficient iterator NextTo() function. Working example:

package main

import (
	"fmt"
	"github.com/emirpasic/gods/lists/arraylist"
	"math/rand"
)

// "search 20 items larger than 50"
func main() {
	// Generate some random data
	list := arraylist.New()
	for i := 0; i < 1000; i++ {
		list.Add(rand.Intn(100))
	}

	// Inefficient way of selecting all elements (enumerable select function)
	newList := list.Select(func(index int, value interface{}) bool {
		return value.(int) > 50
	}).Values()[:20]
	fmt.Println(len(newList), newList)

	// Efficient iterator way
	it := list.Iterator()
	seek := func(index int, value interface{}) bool {
		return value.(int) > 50
	}
	for count := 0; count < 20 && it.NextTo(seek); count = count + 1 {
		fmt.Println(it.Value())
	}
}

Let me know if the above solves the needed cases, otherwise I'll reopen the issue

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