Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
repr-sorting.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
/*
5 * Authors: see git history
6 *
7 * Copyright (C) 2018 Authors
8 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
9 */
10
11#include "repr-sorting.h"
12
13#include "util/forward-pointer-iterator.h" // for ForwardPointerIterator
14#include "util/longest-common-suffix.h" // for nearest_common_ancestor
15#include "xml/node-iterators.h" // for NodeParentIteratorStrategy
16#include "xml/node.h" // for Node, NodeType
17
19{
20 auto ancestor = Inkscape::Algorithms::nearest_common_ancestor<Inkscape::XML::NodeConstParentIterator>(a, b, nullptr);
21
22 if (ancestor && ancestor->type() != Inkscape::XML::NodeType::DOCUMENT_NODE) {
23 return ancestor;
24 }
25
26 return nullptr;
27}
28
36
37bool is_descendant_of(Inkscape::XML::Node const *descendant, Inkscape::XML::Node const *ancestor)
38{
39 while (true) {
40 if (!descendant) {
41 return false;
42 } else if (descendant == ancestor) {
43 return true;
44 } else {
45 descendant = descendant->parent();
46 }
47 }
48}
49
51{
52 while (true) {
53 if (!descendant) {
54 return nullptr;
55 }
56 auto parent = descendant->parent();
57 if (parent == ancestor) {
58 return descendant;
59 }
60 descendant = parent;
61 }
62}
63
65{
66 return const_cast<Inkscape::XML::Node*>(
68 const_cast<Inkscape::XML::Node const*>(descendant),
69 const_cast<Inkscape::XML::Node const*>(ancestor))
70 );
71}
72
73/*
74 Local Variables:
75 mode:c++
76 c-file-style:"stroustrup"
77 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
78 indent-tabs-mode:nil
79 fill-column:99
80 End:
81*/
82// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Interface for refcounted XML nodes.
Definition node.h:80
virtual Node * parent()=0
Get the parent of this node.
static char const *const parent
Definition dir-util.cpp:70
@ DOCUMENT_NODE
Top-level document node. Do not confuse with the root node.
bool is_descendant_of(Inkscape::XML::Node const *descendant, Inkscape::XML::Node const *ancestor)
Inkscape::XML::Node const * lowest_common_ancestor(Inkscape::XML::Node const *a, Inkscape::XML::Node const *b)
Inkscape::XML::Node const * find_containing_child(Inkscape::XML::Node const *descendant, Inkscape::XML::Node const *ancestor)
TODO: insert short description here.
Interface for XML nodes.