Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GUI2/Tree View Node: store child nodes in a vector of unique_ptrs ins…
…tead of boost::ptr_vector I don't know if it would be better design to
- Loading branch information
Showing
6 changed files
with
56 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cba7493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the commit message I meant to say I don't know if it would be better to just store the nodes directly in the vector instead of as pointers.
cba7493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to store them directly in the vector. A vector of
unique_ptr
s already owns the nodes. Storing the nodes in the vector wouldn't change the ownership situation, but it would remove a level of indirection. Accessing a node wouldn't require reading a pointer from the vector and then dereferencing it.cba7493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason why we store then as pointers is that other code might hold references/pointers to the tree_view_node objects (for example
node::parent_node_
) and if we stored them directly in the vector those pointers might be corupted if nodes are added/removed.cba7493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gfgtdf Can't we use
std::list
, then?cba7493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what would be the advantage of that? vector is the default way to go simply necause it's faster.
cba7493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Vector of
unique_ptr
is slower than list because of double indirection, and also slightly harder to use.And if you're asking about the difference to vector, a list doesn't move its elements around in memory. A reference to an object stored in a list remains valid as long as the object isn't removed from the list.
cba7493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depending on the requirements,
std::deque
may also be an option. As long as you only usepush_back
,pop_back
,push_front
, andpop_front
, anstd::deque
does not move its elements in memory, so pointers to elements remain stable until they are removed. Anstd::deque
may also be slightly faster thanstd::list
since it doesn't have indirection between every element.