tidwall / btree

B-tree implementation for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What is the purpose of path hints?

sandeeprapido opened this issue · comments

I am trying to understand how exactly I can use path hint methods. Could you please give some examples in the Readme. That would be helpful.

Ok. I'll try to put together some documentation on the README.

I added a PATH_HINT markdown file.

The benchmark code helped me understand the API, it wasn't clear to me prior to reading that that the functions using path hints mutated the hint

I wonder if this API would be helpful:

type PathContext struct {
	bt   *BTree
	hint PathHint
}

func (c *PathContext) Delete(key interface{}) interface{} {
	return c.bt.DeleteHint(key, &c.hint)
}

func (c *PathContext) Get(key interface{}) interface{} {
	return c.bt.GetHint(key, &c.hint)
}

func (c *PathContext) Set(key interface{}) interface{} {
	return c.bt.SetHint(key, &c.hint)
}

func main() {
	bt := New(func(a, b interface{}) bool {
		return a.(string) < b.(string)
	})

	c := bt.PathContext()
	c.Set("abc")
	fmt.Println(c.Get("abc"))
	c.Delete("abc")
}

https://play.golang.org/p/wWcBVE-YwfY

@tommie Your PathContext idea is slick. I'm not sure that it's best suited for adding directly to this library but I can see how it others might find it convenient.