[intrusive] More features for treap?
Dear All, I've been looking at Boost.Intrusive's treap, and finding that it doesn't quite do what I need. Here's an example: say I have data about earthquakes in the form (date, magnitude, description). I can store that in a treap with date as the key and magnitude as the priority. Now I'd like to be able to do operations like: - Find the strongest earthquake between dates A and B. - Iterate over all the earthquakes with magnitude > X in date order. - For each year (or month, or decade etc) find the strongest quake. The first two are straightforward in a treap, but the current Boost.Intrusive API doesn't (AFAICT) let me do it. (The third one is less obvious.) I have been wondering how the API could most easily be extended to support this sort of thing, and I'm coming to the conclusion that it is probably best to expose the actual tree structure so that the algorithms for these operations can be implemented explicitly by the user code. I have had similar thoughts in the past about other trees i.e. augmented trees to implement interval trees; the choice is either (a) implement these things inside Boost.Intrusive; (b) don't use Boost.Intrusive at all; (c) extend the intrusive API to expose the actual tree structure. In cases like these extensions to treap, the new operations are simple compared to the complexities of inserting and erasing elements; being able to use the existing implementations of those operations while adding other (non-mutating?) features would save a lot of work. Any thoughts anyone? (Is anyone else using treap?) Regards, Phil.
El 20/10/2014 15:16, Phil Endecott escribió:
Dear All,
I've been looking at Boost.Intrusive's treap, and finding that it doesn't quite do what I need.
Here's an example: say I have data about earthquakes in the form (date, magnitude, description). I can store that in a treap with date as the key and magnitude as the priority. Now I'd like to be able to do operations like:
- Find the strongest earthquake between dates A and B. - Iterate over all the earthquakes with magnitude > X in date order. - For each year (or month, or decade etc) find the strongest quake.
The first two are straightforward in a treap, but the current Boost.Intrusive API doesn't (AFAICT) let me do it. (The third one is less obvious.)
I have been wondering how the API could most easily be extended to support this sort of thing, and I'm coming to the conclusion that it is probably best to expose the actual tree structure so that the algorithms for these operations can be implemented explicitly by the user code. I have had similar thoughts in the past about other trees i.e. augmented trees to implement interval trees; the choice is either (a) implement these things inside Boost.Intrusive; (b) don't use Boost.Intrusive at all; (c) extend the intrusive API to expose the actual tree structure. In cases like these extensions to treap, the new operations are simple compared to the complexities of inserting and erasing elements; being able to use the existing implementations of those operations while adding other (non-mutating?) features would save a lot of work.
Any thoughts anyone?
There are several proposals to implement augmented trees in Boost.Intrusive. I haven't reviewed them because I don't know much about augmented data structures and because of lack of time. Boost.Intrusive tree-like containers reuse binary search tree algorithms and implementing augmented data structures for all containers could be feasible. Matei David did the last effort: https://github.com/mateidavid/intrusive/tree/augmented-bstree/include/boost/... I haven't reviewed the implementation because of lack of time but it sounds promising. In any case, accessing tree internals might be useful in newer use cases. When you talk about "exposing the tree structure", what do you have in mind? Which kind of information do you need? Best, Ion
Ion Gazta?aga
wrote: El 20/10/2014 15:16, Phil Endecott escribi?: Dear All,
I've been looking at Boost.Intrusive's treap, and finding that it doesn't quite do what I need.
Here's an example: say I have data about earthquakes in the form (date, magnitude, description). I can store that in a treap with date as the key and magnitude as the priority. Now I'd like to be able to do operations like:
- Find the strongest earthquake between dates A and B. - Iterate over all the earthquakes with magnitude > X in date order. - For each year (or month, or decade etc) find the strongest quake.
The first two are straightforward in a treap, but the current Boost.Intrusive API doesn't (AFAICT) let me do it. (The third one is less obvious.)
I have been wondering how the API could most easily be extended to support this sort of thing, and I'm coming to the conclusion that it is probably best to expose the actual tree structure so that the algorithms for these operations can be implemented explicitly by the user code. I have had similar thoughts in the past about other trees i.e. augmented trees to implement interval trees; the choice is either (a) implement these things inside Boost.Intrusive; (b) don't use Boost.Intrusive at all; (c) extend the intrusive API to expose the actual tree structure. In cases like these extensions to treap, the new operations are simple compared to the complexities of inserting and erasing elements; being able to use the existing implementations of those operations while adding other (non-mutating?) features would save a lot of work.
Any thoughts anyone?
There are several proposals to implement augmented trees in Boost.Intrusive. I haven't reviewed them because I don't know much about augmented data structures and because of lack of time. Boost.Intrusive tree-like containers reuse binary search tree algorithms and implementing augmented data structures for all containers could be feasible. Matei David did the last effort:
https://github.com/mateidavid/intrusive/tree/augmented-bstree/include/boost/...
Looking at that, I find it really hard to see what has been added. I think my git skills are lacking.
I haven't reviewed the implementation because of lack of time but it sounds promising.
In any case, accessing tree internals might be useful in newer use cases. When you talk about "exposing the tree structure", what do you have in mind? Which kind of information do you need?
I was imagining left(), right() and parent() on either the nodes or the iterators. Regards, Phil.
Looking at that, I find it really hard to see what has been added. I think my git skills are lacking.
Sorry, I pasted the wrong url. This is a bit more detailed: https://github.com/boostorg/intrusive/pull/3
I haven't reviewed the implementation because of lack of time but it sounds promising.
In any case, accessing tree internals might be useful in newer use cases. When you talk about "exposing the tree structure", what do you have in mind? Which kind of information do you need?
I was imagining left(), right() and parent() on either the nodes or the iterators.
Ok. Right now you could obtain "node_ptr" using iterator.pointed_node() and use tree::node_traits::get_left/right/parent to obtain access to other nodes. We are missing something to convert a node into an iterator, though. You have: explicit tree_iterator(const node_ptr & nodeptr, const const_value_traits_ptr &traits_ptr) where you could use a null const_value_traits_ptr (non-null values are used when value traits are stateful). A lot of hacks, but at least you can try to go ahead. In any case, we could add new members to the tree iterator so that we can move to parent, left or right. In case there is no left/right node we can choose to obtain a "null" iterator (returning "end" would be extremely expensive as iterators store a pointer to a node and we would need to climb the tree just to get the header node. If we agree on some common operations, we can open a ticket and implement some basic operations for trees. Best, Ion
participants (2)
-
Ion Gaztañaga
-
Phil Endecott