-
Notifications
You must be signed in to change notification settings - Fork 420
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
Question on iteration #28
Comments
Oops, sorry, fatfingered a response. Trying again... You appear correct in all regards :) While iterating, a move from one object to the next during an iteration call is O(1), the search for an item within the tree to start the iteration is O(logn), so a repeated (search+move) will be O(logn) * searches, or O(nlogn) assuming n searches into the btree. You're right that we currently don't have a more robust iterator that knows its position and allows increment/decrement to move forward/back within the tree. I expect your use-case is not unique, however, so it seems like the addition of such an interface would probably be useful. One note: currently we provide no guarantees about iterating during insertion... if you write to a btree (insert/delete), you invalidate all iterators currently iterating on that tree. I don't see that changing any time soon. I'm interested if you can provide more information about your use-case: it appears to me that a call to Next() followed by a call to Prev() could be avoided by just caching the original value before calling Next(), since you're guaranteed to get back the same thing you're looking for? Are you calling Next() multiple times, then Prev() once? Or calling each an arbitrary number of times? |
@jvsteiner You might want to take a look at https://godoc.org/modernc.org/b. It has O(1) |
@cznic I am indeed considering it, thanks! @gconnell - In my use case, I don't think Additionally, I have a different use case where I call I get that this data structure is not concurrent, and that it's not like my iterator gets a snapshot of the internal state or something - It would be nice, but my use case, it's not a priority. I wonder if it would be possible, though, to use |
@cznic - I ran the benchmarks for btree, and it's much faster for inserts, although, indeed, the iteration is relatively more performant.
|
I don't see here which benchmarks and their results are the base for the comparison "much faster". Note that the 1e3, 1e4 etc suffixes of the modernc.org/b benchmarks are the number of items set, get, deleted etc. So to get time per item one has to divide the time/op value by 10^3, 10^4 etc. |
I see - indeed that was not obvious from the published benchmarks, at least to me. |
@cznic may I ask, where can I obtain the code for inspection? The URL's you provided only lead to the documentation, which does not contain , AFAICT, instructions on how to obtain the actual code. The import statements there, when you try to resolve them in the usual fashion redirect to godocs. updateI |
There's no organization behind above me purchasing a domain to make packages at github.com/cznic available independently from the hosting site as I've started the process of abandoning Github after it has been bought by its current owner. |
I'm using
btree
to provide an in-memory key-value store for use as a non-persistent substitute for other key-value databases in a cacheing application. The interface that I need to implement using this package requires that I provide an iterator which exposesNext()
andPrev()
methods.I have done this using the
AscendGreaterOrEqual
andDescendLessOrEqual
methods as follows: I store a pointer to the current item, and use it as a reference to ascend or descend from, whenNext()
orPrev()
are called, respectively.While, I believe that internally, iteration within the provided method calls is O(1), in my case, I believe that the lookup of the current item is always O(logN). Since there is not another obvious, O(1) way (to me at least) to restart iteration from a given Item, and move forward or backward one position in the manner I need, and which is common in most key-value store interfaces, I was wondering if the authors thought it might be useful to expose another way to control iteration in a step-by-step manner - O(1) - which keeps track of the current item.
The text was updated successfully, but these errors were encountered: