From 1bfe42b0ce02ab63cdc53e012cb6c553a261861e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 17 Dec 2017 19:09:04 +0100 Subject: [PATCH] Simplify sort order handling for first child item Test whether this is the first child before testing whether the branch is open as this allows to avoid the special case of inserting the first child under the root node and simplifies the assert condition to a simple check that the sort order is already what we expect. --- src/generic/datavgen.cpp | 42 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index 9c8736cb78..3750116e64 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -1694,33 +1694,33 @@ void wxDataViewTreeNode::InsertChild(wxDataViewTreeNode *node, unsigned index) // child list to lose the current sort order, if any. m_branchData->sortOrder = SortOrder(); } - else if ( m_branchData->open ) + else if ( m_branchData->children.empty() ) { - // For open branches, children should be already sorted, with one - // possible exception that we check for here: - if ( m_branchData->sortOrder != sortOrder ) + if ( m_branchData->open ) { - // This can happen for the root node, on the first child addition, - // in which case the children are nevertheless sorted (because - // there is only one of them), but we need to set the correct sort - // order. - wxASSERT_MSG( !m_parent && m_branchData->children.empty(), - "Logic error in wxDVC sorting code" ); - + // We don't need to search for the right place to insert the first + // item (there is only one), but we do need to remember the sort + // order to use for the subsequent ones. m_branchData->sortOrder = sortOrder; } + else + { + // We're inserting the first child of a closed node. We can choose + // whether to consider this empty child list sorted or unsorted. + // By choosing unsorted, we postpone comparisons until the parent + // node is opened in the view, which may be never. + m_branchData->sortOrder = SortOrder(); + } + } + else if ( m_branchData->open ) + { + // For open branches, children should be already sorted. + wxASSERT_MSG( m_branchData->sortOrder == sortOrder, + wxS("Logic error in wxDVC sorting code") ); // We can use fast insertion. insertSorted = true; } - else if ( m_branchData->children.empty() ) - { - // We're inserting the first child of a closed node. We can choose - // whether to consider this empty child list sorted or unsorted. - // By choosing unsorted, we postpone comparisons until the parent - // node is opened in the view, which may be never. - m_branchData->sortOrder = SortOrder(); - } else if ( m_branchData->sortOrder == sortOrder ) { // The children are already sorted by the correct criteria (because @@ -1731,8 +1731,8 @@ void wxDataViewTreeNode::InsertChild(wxDataViewTreeNode *node, unsigned index) } else { - // The children aren't sorted by the correct criteria, so we just - // insert unsorted. + // The children of this closed node aren't sorted by the correct + // criteria, so we just insert unsorted. m_branchData->sortOrder = SortOrder(); }