Each node contains a collection, which I suppose is implemented as a balanced binary search tree. Each node set must remain fixed after this node creation before using it when creating this node children.
This is a rather unique case. I would recommend using std :: vector instead. (No!) The code that creates the node can still use set
and switches to vector
at the last second. However, vector
smaller and takes up only a small number of memory allocations (one if you use reserve
), which makes the algorithm much faster.
typedef unsigned int treekeytype; typedef std::vector<unsigned int> minortreetype; typedef std::pair<treekeytype, minortreetype> majornode typedef std::set<treekeytype, minortreetype> majortype; majortype majortree; void func(majortype::iterator perform) { std::set<unsigned int> results; results.assign(perform->second.begin(), perform->second.end()); majortree[perform->first+1].assign(results.begin(), results.end());
You can even use std::lower_bound
and std::upper_bound
to still receive O (log (n)) memory images, since they are still sorted the same as the set, so you won’t lose efficiency. This is a net profit if you do not need to insert / delete often.
I am afraid, however, that copying each set is overly expensive.
If this fear is caused by the fact that each set contains basically the same nodes as its parents, and the data is expensive (for copying or in memory, depending on what has changed), only with the change of several nodes, so that instead of the subtree contained std::shared_pointers
the data itself. This means that the data itself will not be copied, only pointers.
I understand that this is not what you were striving for, but, as James Kanjie said, I do not know such a container. Besides the possibly weird and dangerous use of the STL rope
class. Note what I said and meant STL, not the standard C ++ library. They are different.