-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update web/docs and update tree.hh to latest version.
- Loading branch information
Kasper Peeters
committed
Nov 17, 2023
1 parent
ad85a64
commit cf053c1
Showing
3 changed files
with
65 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
|
||
// STL-like templated tree class. | ||
// | ||
// Copyright (C) 2001-2020 Kasper Peeters <[email protected]> | ||
// Copyright (C) 2001-2023 Kasper Peeters <[email protected]> | ||
// Distributed under the GNU General Public License version 3. | ||
// | ||
// Special permission to use tree.hh under the conditions of a | ||
// different license can be requested from the author. | ||
|
||
/** \mainpage tree.hh | ||
\author Kasper Peeters | ||
\version 3.17 | ||
\date 07-Nov-2020 | ||
\see http://tree.phi-sci.com/ | ||
\version 3.19 | ||
\date 2023-11-17 | ||
\see http://github.com/kpeeters/tree.hh/ | ||
The tree.hh library for C++ provides an STL-like container class | ||
|
@@ -34,7 +33,7 @@ | |
#include <queue> | ||
#include <algorithm> | ||
#include <cstddef> | ||
|
||
#include <string> | ||
|
||
/// A node in the tree, combining links to other nodes as well as the actual data. | ||
template<class T> | ||
|
@@ -86,7 +85,7 @@ class navigation_error : public std::logic_error { | |
// std::cerr << boost::stacktrace::stacktrace() << std::endl; | ||
// str << boost::stacktrace::stacktrace(); | ||
// stacktrace=str.str(); | ||
}; | ||
} | ||
|
||
// virtual const char *what() const noexcept override | ||
// { | ||
|
@@ -363,6 +362,7 @@ class tree { | |
template<typename iter> iter insert(iter position, T&& x); | ||
/// Specialisation of previous member. | ||
sibling_iterator insert(sibling_iterator position, const T& x); | ||
sibling_iterator insert(sibling_iterator position, T&& x); | ||
/// Insert node (with children) pointed to by subtree as previous sibling of node pointed to by position. | ||
/// Does not change the subtree itself (use move_in or move_in_below for that). | ||
template<typename iter> iter insert_subtree(iter position, const iterator_base& subtree); | ||
|
@@ -503,9 +503,9 @@ class tree { | |
template<class StrictWeakOrdering> | ||
class compare_nodes { | ||
public: | ||
compare_nodes(StrictWeakOrdering comp) : comp_(comp) {}; | ||
compare_nodes(StrictWeakOrdering comp) : comp_(comp) {} | ||
|
||
bool operator()(const tree_node *a, const tree_node *b) | ||
bool operator()(const tree_node *a, const tree_node *b) const | ||
{ | ||
return comp_(a->data, b->data); | ||
} | ||
|
@@ -1363,6 +1363,37 @@ typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_alloca | |
return tmp; | ||
} | ||
|
||
template <class T, class tree_node_allocator> | ||
typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::insert(sibling_iterator position, T&& x) | ||
{ | ||
tree_node *tmp=std::allocator_traits<decltype(alloc_)>::allocate(alloc_, 1, 0); | ||
std::allocator_traits<decltype(alloc_)>::construct(alloc_, tmp); | ||
std::swap(tmp->data, x); // Move semantics | ||
|
||
tmp->first_child=0; | ||
tmp->last_child=0; | ||
|
||
tmp->next_sibling=position.node; | ||
if(position.node==0) { // iterator points to end of a subtree | ||
tmp->parent=position.parent_; | ||
tmp->prev_sibling=position.range_last(); | ||
tmp->parent->last_child=tmp; | ||
} | ||
else { | ||
tmp->parent=position.node->parent; | ||
tmp->prev_sibling=position.node->prev_sibling; | ||
position.node->prev_sibling=tmp; | ||
} | ||
|
||
if(tmp->prev_sibling==0) { | ||
if(tmp->parent) // when inserting nodes at the head, there is no parent | ||
tmp->parent->first_child=tmp; | ||
} | ||
else | ||
tmp->prev_sibling->next_sibling=tmp; | ||
return tmp; | ||
} | ||
|
||
template <class T, class tree_node_allocator> | ||
template <class iter> | ||
iter tree<T, tree_node_allocator>::insert_after(iter position, const T& x) | ||
|
@@ -1829,7 +1860,6 @@ tree<T, tree_node_allocator> tree<T, tree_node_allocator>::move_out(iterator sou | |
// Move source node into the 'ret' tree. | ||
ret.head->next_sibling = source.node; | ||
ret.feet->prev_sibling = source.node; | ||
source.node->parent=0; | ||
|
||
// Close the links in the current tree. | ||
if(source.node->prev_sibling!=0) | ||
|
@@ -1838,6 +1868,22 @@ tree<T, tree_node_allocator> tree<T, tree_node_allocator>::move_out(iterator sou | |
if(source.node->next_sibling!=0) | ||
source.node->next_sibling->prev_sibling = source.node->prev_sibling; | ||
|
||
// If the moved-out node was a first or last child of | ||
// the parent, adjust those links. | ||
if(source.node->parent->first_child==source.node) { | ||
if(source.node->next_sibling!=0) | ||
source.node->parent->first_child=source.node->next_sibling; | ||
else | ||
source.node->parent->first_child=0; | ||
} | ||
if(source.node->parent->last_child==source.node) { | ||
if(source.node->prev_sibling!=0) | ||
source.node->parent->last_child=source.node->prev_sibling; | ||
else | ||
source.node->parent->last_child=0; | ||
} | ||
source.node->parent=0; | ||
|
||
// Fix source prev/next links. | ||
source.node->prev_sibling = ret.head; | ||
source.node->next_sibling = ret.feet; | ||
|
Binary file not shown.
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