Sat, 08 Nov 2025 23:06:11 +0100
update ucx
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
4 | * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved. |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | /** |
| 852 | 29 | * @file tree.h |
| 30 | * @brief Interface for tree implementations. | |
| 31 | * @author Mike Becker | |
| 32 | * @author Olaf Wintermann | |
| 33 | * @copyright 2-Clause BSD License | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | */ |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | #ifndef UCX_TREE_H |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | #define UCX_TREE_H |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | #include "common.h" |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
40 | |
| 852 | 41 | #include "collection.h" |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43 | #ifdef __cplusplus |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | extern "C" { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | #endif |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47 | /** |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
48 | * A depth-first tree iterator. |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49 | * |
| 852 | 50 | * This iterator is not position-aware in a strict sense, as it does not assume |
| 51 | * a particular order of elements in the tree. However, the iterator keeps track | |
| 889 | 52 | * of the number of nodes it has passed in a counter-variable. |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
53 | * Each node, regardless of the number of passes, is counted only once. |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
54 | * |
| 852 | 55 | * @note Objects that are pointed to by an iterator are mutable through that |
| 56 | * iterator. However, if the | |
| 889 | 57 | * underlying data structure is mutated by other means than this iterator (e.g., |
| 852 | 58 | * elements added or removed), the iterator becomes invalid (regardless of what |
| 59 | * cxIteratorValid() returns). | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60 | * |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
61 | * @see CxIterator |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
62 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
63 | typedef struct cx_tree_iterator_s { |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
64 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
65 | * Base members. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
66 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
67 | CX_ITERATOR_BASE; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
68 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
69 | * Indicates whether the subtree below the current node shall be skipped. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
70 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
71 | bool skip; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
72 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
73 | * Set to true, when the iterator shall visit a node again |
| 889 | 74 | * when all its children have been processed. |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
75 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
76 | bool visit_on_exit; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
77 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
78 | * True, if this iterator is currently leaving the node. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
79 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
80 | bool exiting; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
81 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
82 | * Offset in the node struct for the children linked list. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
83 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
84 | ptrdiff_t loc_children; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
85 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
86 | * Offset in the node struct for the next pointer. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
87 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
88 | ptrdiff_t loc_next; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
89 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
90 | * The total number of distinct nodes that have been passed so far. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
91 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
92 | size_t counter; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
93 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
94 | * The currently observed node. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
95 | * |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
96 | * This is the same what cxIteratorCurrent() would return. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
97 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
98 | void *node; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
99 | /** |
| 889 | 100 | * Stores a copy of the pointer to the successor of the visited node. |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
101 | * Allows freeing a node on exit without corrupting the iteration. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
102 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
103 | void *node_next; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
104 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
105 | * Internal stack. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
106 | * Will be automatically freed once the iterator becomes invalid. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
107 | * |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
108 | * If you want to discard the iterator before, you need to manually |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
109 | * call cxTreeIteratorDispose(). |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
110 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
111 | void **stack; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
112 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
113 | * Internal capacity of the stack. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
114 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
115 | size_t stack_capacity; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
116 | union { |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
117 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
118 | * Internal stack size. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
119 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
120 | size_t stack_size; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
121 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
122 | * The current depth in the tree. |
| 886 | 123 | * The node with which the iteration starts has depth 1. |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
124 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
125 | size_t depth; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
126 | }; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
127 | } CxTreeIterator; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
128 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
129 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
130 | * An element in a visitor queue. |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
131 | */ |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
132 | struct cx_tree_visitor_queue_s { |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
133 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
134 | * The tree node to visit. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
135 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
136 | void *node; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
137 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
138 | * The depth of the node. |
| 886 | 139 | * The first visited node has depth 1. |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
140 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
141 | size_t depth; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
142 | /** |
| 852 | 143 | * The next element in the queue or @c NULL. |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
144 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
145 | struct cx_tree_visitor_queue_s *next; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
146 | }; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
147 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
148 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
149 | * A breadth-first tree iterator. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
150 | * |
| 852 | 151 | * This iterator needs to maintain a visitor queue that will be automatically |
| 152 | * freed once the iterator becomes invalid. | |
| 153 | * If you want to discard the iterator before, you MUST manually call | |
| 154 | * cxTreeVisitorDispose(). | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
155 | * |
| 852 | 156 | * This iterator is not position-aware in a strict sense, as it does not assume |
| 157 | * a particular order of elements in the tree. However, the iterator keeps track | |
| 889 | 158 | * of the number of nodes it has passed in a counter-variable. |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
159 | * Each node, regardless of the number of passes, is counted only once. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
160 | * |
| 852 | 161 | * @note Objects that are pointed to by an iterator are mutable through that |
| 162 | * iterator. However, if the | |
| 889 | 163 | * underlying data structure is mutated by other means than this iterator (e.g., |
| 852 | 164 | * elements added or removed), the iterator becomes invalid (regardless of what |
| 165 | * cxIteratorValid() returns). | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
166 | * |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
167 | * @see CxIterator |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
168 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
169 | typedef struct cx_tree_visitor_s { |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
170 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
171 | * Base members. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
172 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
173 | CX_ITERATOR_BASE; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
174 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
175 | * Indicates whether the subtree below the current node shall be skipped. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
176 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
177 | bool skip; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
178 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
179 | * Offset in the node struct for the children linked list. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
180 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
181 | ptrdiff_t loc_children; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
182 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
183 | * Offset in the node struct for the next pointer. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
184 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
185 | ptrdiff_t loc_next; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
186 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
187 | * The total number of distinct nodes that have been passed so far. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
188 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
189 | size_t counter; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
190 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
191 | * The currently observed node. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
192 | * |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
193 | * This is the same what cxIteratorCurrent() would return. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
194 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
195 | void *node; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
196 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
197 | * The current depth in the tree. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
198 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
199 | size_t depth; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
200 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
201 | * The next element in the visitor queue. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
202 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
203 | struct cx_tree_visitor_queue_s *queue_next; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
204 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
205 | * The last element in the visitor queue. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
206 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
207 | struct cx_tree_visitor_queue_s *queue_last; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
208 | } CxTreeVisitor; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
209 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
210 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
211 | * Releases internal memory of the given tree iterator. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
212 | * @param iter the iterator |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
213 | */ |
| 852 | 214 | cx_attr_nonnull |
| 889 | 215 | CX_EXPORT void cxTreeIteratorDispose(CxTreeIterator *iter); |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
216 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
217 | /** |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
218 | * Releases internal memory of the given tree visitor. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
219 | * @param visitor the visitor |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
220 | */ |
| 852 | 221 | cx_attr_nonnull |
| 889 | 222 | CX_EXPORT void cxTreeVisitorDispose(CxTreeVisitor *visitor); |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
223 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
224 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
225 | * Advises the iterator to skip the subtree below the current node and |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
226 | * also continues the current loop. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
227 | * |
| 852 | 228 | * @param iterator (@c CxTreeIterator) the iterator |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
229 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
230 | #define cxTreeIteratorContinue(iterator) (iterator).skip = true; continue |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
231 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
232 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
233 | * Advises the visitor to skip the subtree below the current node and |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
234 | * also continues the current loop. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
235 | * |
| 852 | 236 | * @param visitor (@c CxTreeVisitor) the visitor |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
237 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
238 | #define cxTreeVisitorContinue(visitor) cxTreeIteratorContinue(visitor) |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
239 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
240 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
241 | * Links a node to a (new) parent. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
242 | * |
| 889 | 243 | * If the node already has a parent, it is unlinked, first. |
| 852 | 244 | * If the parent has children already, the node is @em appended to the list |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
245 | * of all currently existing children. |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
246 | * |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
247 | * @param parent the parent node |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
248 | * @param node the node that shall be linked |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
249 | * @param loc_parent offset in the node struct for the parent pointer |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
250 | * @param loc_children offset in the node struct for the children linked list |
| 852 | 251 | * @param loc_last_child optional offset in the node struct for the pointer to |
| 252 | * the last child in the linked list (negative if there is no such pointer) | |
| 253 | * @param loc_prev optional offset in the node struct for the prev pointer | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
254 | * @param loc_next offset in the node struct for the next pointer |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
255 | * @see cx_tree_unlink() |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
256 | */ |
| 852 | 257 | cx_attr_nonnull |
| 889 | 258 | CX_EXPORT void cx_tree_link(void *parent, void *node, |
| 259 | ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child, | |
| 260 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
261 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
262 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
263 | * Unlinks a node from its parent. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
264 | * |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
265 | * If the node has no parent, this function does nothing. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
266 | * |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
267 | * @param node the node that shall be unlinked from its parent |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
268 | * @param loc_parent offset in the node struct for the parent pointer |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
269 | * @param loc_children offset in the node struct for the children linked list |
| 852 | 270 | * @param loc_last_child optional offset in the node struct for the pointer to |
| 271 | * the last child in the linked list (negative if there is no such pointer) | |
| 272 | * @param loc_prev optional offset in the node struct for the prev pointer | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
273 | * @param loc_next offset in the node struct for the next pointer |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
274 | * @see cx_tree_link() |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
275 | */ |
| 852 | 276 | cx_attr_nonnull |
| 889 | 277 | CX_EXPORT void cx_tree_unlink(void *node, |
| 278 | ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child, | |
| 279 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
280 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
281 | /** |
| 852 | 282 | * Macro that can be used instead of the magic value for infinite search depth. |
| 283 | */ | |
| 284 | #define CX_TREE_SEARCH_INFINITE_DEPTH 0 | |
| 285 | ||
| 286 | /** | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
287 | * Function pointer for a search function. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
288 | * |
| 852 | 289 | * A function of this kind shall check if the specified @p node |
| 290 | * contains the given @p data or if one of the children might contain | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
291 | * the data. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
292 | * |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
293 | * The function should use the returned integer to indicate how close the |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
294 | * match is, where a negative number means that it does not match at all. |
| 852 | 295 | * Zero means exact match and a positive number is an implementation defined |
| 296 | * measure for the distance to an exact match. | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
297 | * |
| 889 | 298 | * For example, consider a tree that stores file path information. |
| 299 | * A node which is describing a parent directory of a searched file shall | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
300 | * return a positive number to indicate that a child node might contain the |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
301 | * searched item. On the other hand, if the node denotes a path that is not a |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
302 | * prefix of the searched filename, the function would return -1 to indicate |
| 852 | 303 | * that the search does not need to be continued in that branch. |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
304 | * |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
305 | * @param node the node that is currently investigated |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
306 | * @param data the data that is searched for |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
307 | * |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
308 | * @return 0 if the node contains the data, |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
309 | * positive if one of the children might contain the data, |
| 889 | 310 | * negative if neither the node nor the children contains the data |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
311 | */ |
| 852 | 312 | typedef int (*cx_tree_search_data_func)(const void *node, const void *data); |
| 313 | ||
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
314 | |
| 852 | 315 | /** |
| 316 | * Function pointer for a search function. | |
| 317 | * | |
| 318 | * A function of this kind shall check if the specified @p node | |
| 319 | * contains the same @p data as @p new_node or if one of the children might | |
| 320 | * contain the data. | |
| 321 | * | |
| 322 | * The function should use the returned integer to indicate how close the | |
| 323 | * match is, where a negative number means that it does not match at all. | |
| 324 | * Zero means exact match and a positive number is an implementation defined | |
| 325 | * measure for the distance to an exact match. | |
| 326 | * | |
| 889 | 327 | * For example, consider a tree that stores file path information. |
| 328 | * A node which is describing a parent directory of a searched file shall | |
| 852 | 329 | * return a positive number to indicate that a child node might contain the |
| 330 | * searched item. On the other hand, if the node denotes a path that is not a | |
| 331 | * prefix of the searched filename, the function would return -1 to indicate | |
| 332 | * that the search does not need to be continued in that branch. | |
| 333 | * | |
| 334 | * @param node the node that is currently investigated | |
| 335 | * @param new_node a new node with the information which is searched | |
| 336 | * | |
| 337 | * @return 0 if @p node contains the same data as @p new_node, | |
| 338 | * positive if one of the children might contain the data, | |
| 889 | 339 | * negative if neither the node nor the children contains the data |
| 852 | 340 | */ |
| 341 | typedef int (*cx_tree_search_func)(const void *node, const void *new_node); | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
342 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
343 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
344 | * Searches for data in a tree. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
345 | * |
| 889 | 346 | * When the data cannot be found exactly, the search function might return the |
| 347 | * closest result, which might be a good starting point for adding a new node | |
| 852 | 348 | * to the tree (see also #cx_tree_add()). |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
349 | * |
| 889 | 350 | * Depending on the tree structure, it is not necessarily guaranteed that the |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
351 | * "closest" match is uniquely defined. This function will search for a node |
| 852 | 352 | * with the best match according to the @p sfunc (meaning: the return value of |
| 353 | * @p sfunc which is closest to zero). If that is also ambiguous, an arbitrary | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
354 | * node matching the criteria is returned. |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
355 | * |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
356 | * @param root the root node |
| 852 | 357 | * @param depth the maximum depth (zero=indefinite, one=just root) |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
358 | * @param data the data to search for |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
359 | * @param sfunc the search function |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
360 | * @param result where the result shall be stored |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
361 | * @param loc_children offset in the node struct for the children linked list |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
362 | * @param loc_next offset in the node struct for the next pointer |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
363 | * @return zero if the node was found exactly, positive if a node was found that |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
364 | * could contain the node (but doesn't right now), negative if the tree does not |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
365 | * contain any node that might be related to the searched data |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
366 | */ |
| 889 | 367 | cx_attr_nonnull cx_attr_access_w(5) |
| 368 | CX_EXPORT int cx_tree_search_data(const void *root, size_t depth, | |
| 369 | const void *data, cx_tree_search_data_func sfunc, | |
| 370 | void **result, ptrdiff_t loc_children, ptrdiff_t loc_next); | |
| 852 | 371 | |
| 372 | /** | |
| 373 | * Searches for a node in a tree. | |
| 374 | * | |
| 375 | * When no node with the same data can be found, the search function might | |
| 889 | 376 | * return the closest result, which might be a good starting point for adding the |
| 852 | 377 | * new node to the tree (see also #cx_tree_add()). |
| 378 | * | |
| 889 | 379 | * Depending on the tree structure, it is not necessarily guaranteed that the |
| 852 | 380 | * "closest" match is uniquely defined. This function will search for a node |
| 381 | * with the best match according to the @p sfunc (meaning: the return value of | |
| 382 | * @p sfunc which is closest to zero). If that is also ambiguous, an arbitrary | |
| 383 | * node matching the criteria is returned. | |
| 384 | * | |
| 385 | * @param root the root node | |
| 386 | * @param depth the maximum depth (zero=indefinite, one=just root) | |
| 387 | * @param node the node to search for | |
| 388 | * @param sfunc the search function | |
| 389 | * @param result where the result shall be stored | |
| 390 | * @param loc_children offset in the node struct for the children linked list | |
| 391 | * @param loc_next offset in the node struct for the next pointer | |
| 392 | * @return zero if the node was found exactly, positive if a node was found that | |
| 393 | * could contain the node (but doesn't right now), negative if the tree does not | |
| 394 | * contain any node that might be related to the searched data | |
| 395 | */ | |
| 889 | 396 | cx_attr_nonnull cx_attr_access_w(5) |
| 397 | CX_EXPORT int cx_tree_search(const void *root, size_t depth, | |
| 398 | const void *node, cx_tree_search_func sfunc, | |
| 399 | void **result, ptrdiff_t loc_children, ptrdiff_t loc_next); | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
400 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
401 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
402 | * Creates a depth-first iterator for a tree with the specified root node. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
403 | * |
| 852 | 404 | * @note A tree iterator needs to maintain a stack of visited nodes, which is |
| 886 | 405 | * allocated using the cxDefaultAllocator. |
| 852 | 406 | * When the iterator becomes invalid, this memory is automatically released. |
| 407 | * However, if you wish to cancel the iteration before the iterator becomes | |
| 408 | * invalid by itself, you MUST call cxTreeIteratorDispose() manually to release | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
409 | * the memory. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
410 | * |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
411 | * @remark The returned iterator does not support cxIteratorFlagRemoval(). |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
412 | * |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
413 | * @param root the root node |
| 852 | 414 | * @param visit_on_exit set to true, when the iterator shall visit a node again |
| 415 | * after processing all children | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
416 | * @param loc_children offset in the node struct for the children linked list |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
417 | * @param loc_next offset in the node struct for the next pointer |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
418 | * @return the new tree iterator |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
419 | * @see cxTreeIteratorDispose() |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
420 | */ |
| 852 | 421 | cx_attr_nodiscard |
| 889 | 422 | CX_EXPORT CxTreeIterator cx_tree_iterator(void *root, bool visit_on_exit, |
| 423 | ptrdiff_t loc_children, ptrdiff_t loc_next); | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
424 | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
425 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
426 | * Creates a breadth-first iterator for a tree with the specified root node. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
427 | * |
| 886 | 428 | * @note A tree visitor needs to maintain a queue of to-be visited nodes, which |
| 429 | * is allocated using the cxDefaultAllocator. | |
| 852 | 430 | * When the visitor becomes invalid, this memory is automatically released. |
| 431 | * However, if you wish to cancel the iteration before the visitor becomes | |
| 432 | * invalid by itself, you MUST call cxTreeVisitorDispose() manually to release | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
433 | * the memory. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
434 | * |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
435 | * @remark The returned iterator does not support cxIteratorFlagRemoval(). |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
436 | * |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
437 | * @param root the root node |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
438 | * @param loc_children offset in the node struct for the children linked list |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
439 | * @param loc_next offset in the node struct for the next pointer |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
440 | * @return the new tree visitor |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
441 | * @see cxTreeVisitorDispose() |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
442 | */ |
| 852 | 443 | cx_attr_nodiscard |
| 889 | 444 | CX_EXPORT CxTreeVisitor cx_tree_visitor(void *root, |
| 445 | ptrdiff_t loc_children, ptrdiff_t loc_next); | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
446 | |
| 852 | 447 | /** |
| 448 | * Describes a function that creates a tree node from the specified data. | |
| 889 | 449 | * The first argument points to the data the node shall contain, and |
| 450 | * the second argument may be used for additional data (e.g., an allocator). | |
| 852 | 451 | * Functions of this type shall either return a new pointer to a newly |
| 452 | * created node or @c NULL when allocation fails. | |
| 453 | * | |
| 454 | * @note the function may leave the node pointers in the struct uninitialized. | |
| 455 | * The caller is responsible to set them according to the intended use case. | |
| 456 | */ | |
| 457 | typedef void *(*cx_tree_node_create_func)(const void *, void *); | |
| 458 | ||
| 459 | /** | |
| 460 | * The local search depth for a new subtree when adding multiple elements. | |
| 461 | * The default value is 3. | |
| 462 | * This variable is used by #cx_tree_add_array() and #cx_tree_add_iter() to | |
| 463 | * implement optimized insertion of multiple elements into a tree. | |
| 464 | */ | |
| 889 | 465 | CX_EXPORT extern unsigned int cx_tree_add_look_around_depth; |
| 852 | 466 | |
| 467 | /** | |
| 468 | * Adds multiple elements efficiently to a tree. | |
| 469 | * | |
| 470 | * Once an element cannot be added to the tree, this function returns, leaving | |
| 471 | * the iterator in a valid state pointing to the element that could not be | |
| 472 | * added. | |
| 473 | * Also, the pointer of the created node will be stored to @p failed. | |
| 474 | * The integer returned by this function denotes the number of elements obtained | |
| 475 | * from the @p iter that have been successfully processed. | |
| 476 | * When all elements could be processed, a @c NULL pointer will be written to | |
| 477 | * @p failed. | |
| 478 | * | |
| 479 | * The advantage of this function compared to multiple invocations of | |
| 480 | * #cx_tree_add() is that the search for the insert locations is not always | |
| 481 | * started from the root node. | |
| 482 | * Instead, the function checks #cx_tree_add_look_around_depth many parent nodes | |
| 483 | * of the current insert location before starting from the root node again. | |
| 484 | * When the variable is set to zero, only the last found location is checked | |
| 485 | * again. | |
| 486 | * | |
| 487 | * Refer to the documentation of #cx_tree_add() for more details. | |
| 488 | * | |
| 489 | * @param iter a pointer to an arbitrary iterator | |
| 490 | * @param num the maximum number of elements to obtain from the iterator | |
| 491 | * @param sfunc a search function | |
| 492 | * @param cfunc a node creation function | |
| 493 | * @param cdata optional additional data | |
| 494 | * @param root the root node of the tree | |
| 495 | * @param failed location where the pointer to a failed node shall be stored | |
| 496 | * @param loc_parent offset in the node struct for the parent pointer | |
| 497 | * @param loc_children offset in the node struct for the children linked list | |
| 498 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 499 | * the last child in the linked list (negative if there is no such pointer) | |
| 500 | * @param loc_prev optional offset in the node struct for the prev pointer | |
| 501 | * @param loc_next offset in the node struct for the next pointer | |
| 502 | * @return the number of nodes created and added | |
| 503 | * @see cx_tree_add() | |
| 504 | */ | |
| 889 | 505 | cx_attr_nonnull_arg(1, 3, 4, 6, 7) cx_attr_access_w(6) |
| 506 | CX_EXPORT size_t cx_tree_add_iter(struct cx_iterator_base_s *iter, size_t num, | |
| 507 | cx_tree_search_func sfunc, cx_tree_node_create_func cfunc, | |
| 508 | void *cdata, void **failed, void *root, | |
| 509 | ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child, | |
| 510 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | |
| 852 | 511 | |
| 512 | /** | |
| 513 | * Adds multiple elements efficiently to a tree. | |
| 514 | * | |
| 515 | * Once an element cannot be added to the tree, this function returns, storing | |
| 516 | * the pointer of the created node to @p failed. | |
| 517 | * The integer returned by this function denotes the number of elements from | |
| 518 | * the @p src array that have been successfully processed. | |
| 519 | * When all elements could be processed, a @c NULL pointer will be written to | |
| 520 | * @p failed. | |
| 521 | * | |
| 522 | * The advantage of this function compared to multiple invocations of | |
| 523 | * #cx_tree_add() is that the search for the insert locations is not always | |
| 524 | * started from the root node. | |
| 525 | * Instead, the function checks #cx_tree_add_look_around_depth many parent nodes | |
| 526 | * of the current insert location before starting from the root node again. | |
| 527 | * When the variable is set to zero, only the last found location is checked | |
| 528 | * again. | |
| 529 | * | |
| 530 | * Refer to the documentation of #cx_tree_add() for more details. | |
| 531 | * | |
| 532 | * @param src a pointer to the source data array | |
| 533 | * @param num the number of elements in the @p src array | |
| 534 | * @param elem_size the size of each element in the @p src array | |
| 535 | * @param sfunc a search function | |
| 536 | * @param cfunc a node creation function | |
| 537 | * @param cdata optional additional data | |
| 538 | * @param failed location where the pointer to a failed node shall be stored | |
| 539 | * @param root the root node of the tree | |
| 540 | * @param loc_parent offset in the node struct for the parent pointer | |
| 541 | * @param loc_children offset in the node struct for the children linked list | |
| 542 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 543 | * the last child in the linked list (negative if there is no such pointer) | |
| 544 | * @param loc_prev optional offset in the node struct for the prev pointer | |
| 545 | * @param loc_next offset in the node struct for the next pointer | |
| 546 | * @return the number of array elements successfully processed | |
| 547 | * @see cx_tree_add() | |
| 548 | */ | |
| 889 | 549 | cx_attr_nonnull_arg(1, 4, 5, 7, 8) cx_attr_access_w(7) |
| 550 | CX_EXPORT size_t cx_tree_add_array(const void *src, size_t num, size_t elem_size, | |
| 551 | cx_tree_search_func sfunc, cx_tree_node_create_func cfunc, | |
| 552 | void *cdata, void **failed, void *root, | |
| 553 | ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child, | |
| 554 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | |
| 852 | 555 | |
| 556 | /** | |
| 557 | * Adds data to a tree. | |
| 558 | * | |
| 559 | * An adequate location where to add the new tree node is searched with the | |
| 560 | * specified @p sfunc. | |
| 561 | * | |
| 562 | * When a location is found, the @p cfunc will be invoked with @p cdata. | |
| 563 | * | |
| 564 | * The node returned by @p cfunc will be linked into the tree. | |
| 889 | 565 | * When @p sfunc returns a positive integer, the new node will be linked as a |
| 852 | 566 | * child. The other children (now siblings of the new node) are then checked |
| 567 | * with @p sfunc, whether they could be children of the new node and re-linked | |
| 568 | * accordingly. | |
| 569 | * | |
| 889 | 570 | * When @p sfunc returns zero and the found node has a parent, the new |
| 571 | * node will be added as a sibling - otherwise, the new node will be added | |
| 852 | 572 | * as a child. |
| 573 | * | |
| 889 | 574 | * When @p sfunc returns a negative value, the new node will not be added to |
| 575 | * the tree, and this function returns a non-zero value. | |
| 852 | 576 | * The caller should check if @p cnode contains a node pointer and deal with the |
| 577 | * node that could not be added. | |
| 578 | * | |
| 579 | * This function also returns a non-zero value when @p cfunc tries to allocate | |
| 580 | * a new node but fails to do so. In that case, the pointer stored to @p cnode | |
| 581 | * will be @c NULL. | |
| 582 | * | |
| 583 | * Multiple elements can be added more efficiently with | |
| 584 | * #cx_tree_add_array() or #cx_tree_add_iter(). | |
| 585 | * | |
| 586 | * @param src a pointer to the data | |
| 587 | * @param sfunc a search function | |
| 588 | * @param cfunc a node creation function | |
| 589 | * @param cdata optional additional data | |
| 590 | * @param cnode the location where a pointer to the new node is stored | |
| 591 | * @param root the root node of the tree | |
| 592 | * @param loc_parent offset in the node struct for the parent pointer | |
| 593 | * @param loc_children offset in the node struct for the children linked list | |
| 594 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 595 | * the last child in the linked list (negative if there is no such pointer) | |
| 596 | * @param loc_prev optional offset in the node struct for the prev pointer | |
| 597 | * @param loc_next offset in the node struct for the next pointer | |
| 598 | * @return zero when a new node was created and added to the tree, | |
| 599 | * non-zero otherwise | |
| 600 | */ | |
| 889 | 601 | cx_attr_nonnull_arg(1, 2, 3, 5, 6) cx_attr_access_w(5) |
| 602 | CX_EXPORT int cx_tree_add(const void *src, | |
| 603 | cx_tree_search_func sfunc, cx_tree_node_create_func cfunc, | |
| 604 | void *cdata, void **cnode, void *root, | |
| 605 | ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child, | |
| 606 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | |
| 852 | 607 | |
| 608 | ||
| 609 | /** | |
| 610 | * Tree class type. | |
| 611 | */ | |
| 612 | typedef struct cx_tree_class_s cx_tree_class; | |
| 613 | ||
| 614 | /** | |
| 615 | * Base structure that can be used for tree nodes in a #CxTree. | |
| 616 | */ | |
| 617 | struct cx_tree_node_base_s { | |
| 618 | /** | |
| 619 | * Pointer to the parent. | |
| 620 | */ | |
| 621 | struct cx_tree_node_base_s *parent; | |
| 622 | /** | |
| 623 | * Pointer to the first child. | |
| 624 | */ | |
| 625 | struct cx_tree_node_base_s *children; | |
| 626 | /** | |
| 627 | * Pointer to the last child. | |
| 628 | */ | |
| 629 | struct cx_tree_node_base_s *last_child; | |
| 630 | /** | |
| 631 | * Pointer to the previous sibling. | |
| 632 | */ | |
| 633 | struct cx_tree_node_base_s *prev; | |
| 634 | /** | |
| 635 | * Pointer to the next sibling. | |
| 636 | */ | |
| 637 | struct cx_tree_node_base_s *next; | |
| 638 | }; | |
| 639 | ||
| 640 | /** | |
| 641 | * Structure for holding the base data of a tree. | |
| 642 | */ | |
| 643 | struct cx_tree_s { | |
| 644 | /** | |
| 645 | * The tree class definition. | |
| 646 | */ | |
| 647 | const cx_tree_class *cl; | |
| 648 | ||
| 649 | /** | |
| 650 | * Allocator to allocate new nodes. | |
| 651 | */ | |
| 652 | const CxAllocator *allocator; | |
| 653 | ||
| 654 | /** | |
| 655 | * A pointer to the root node. | |
| 656 | * | |
| 657 | * Will be @c NULL when @c size is 0. | |
| 658 | */ | |
| 659 | void *root; | |
| 660 | ||
| 661 | /** | |
| 662 | * A function to create new nodes. | |
| 663 | * | |
| 664 | * Invocations to this function will receive a pointer to this tree | |
| 889 | 665 | * structure as the second argument. |
| 852 | 666 | * |
| 889 | 667 | * Nodes MAY use #cx_tree_node_base_s as the base layout, but do not need to. |
| 852 | 668 | */ |
| 669 | cx_tree_node_create_func node_create; | |
| 670 | ||
| 671 | /** | |
| 672 | * An optional simple destructor for the tree nodes. | |
| 673 | */ | |
| 674 | cx_destructor_func simple_destructor; | |
| 675 | ||
| 676 | /** | |
| 677 | * An optional advanced destructor for the tree nodes. | |
| 678 | */ | |
| 679 | cx_destructor_func2 advanced_destructor; | |
| 680 | ||
| 681 | /** | |
| 682 | * The pointer to additional data that is passed to the advanced destructor. | |
| 683 | */ | |
| 684 | void *destructor_data; | |
| 685 | ||
| 686 | /** | |
| 687 | * A function to compare two nodes. | |
| 688 | */ | |
| 689 | cx_tree_search_func search; | |
| 690 | ||
| 691 | /** | |
| 692 | * A function to compare a node with data. | |
| 693 | */ | |
| 694 | cx_tree_search_data_func search_data; | |
| 695 | ||
| 696 | /** | |
| 697 | * The number of currently stored elements. | |
| 698 | */ | |
| 699 | size_t size; | |
| 700 | ||
| 701 | /** | |
| 702 | * Offset in the node struct for the parent pointer. | |
| 703 | */ | |
| 704 | ptrdiff_t loc_parent; | |
| 705 | ||
| 706 | /** | |
| 707 | * Offset in the node struct for the children linked list. | |
| 708 | */ | |
| 709 | ptrdiff_t loc_children; | |
| 710 | ||
| 711 | /** | |
| 712 | * Optional offset in the node struct for the pointer to the last child | |
| 713 | * in the linked list (negative if there is no such pointer). | |
| 714 | */ | |
| 715 | ptrdiff_t loc_last_child; | |
| 716 | ||
| 717 | /** | |
| 718 | * Offset in the node struct for the previous sibling pointer. | |
| 719 | */ | |
| 720 | ptrdiff_t loc_prev; | |
| 721 | ||
| 722 | /** | |
| 723 | * Offset in the node struct for the next sibling pointer. | |
| 724 | */ | |
| 725 | ptrdiff_t loc_next; | |
| 726 | }; | |
| 727 | ||
| 728 | /** | |
| 729 | * Macro to roll out the #cx_tree_node_base_s structure with a custom | |
| 730 | * node type. | |
| 731 | * | |
| 889 | 732 | * Must be used as the first member in your custom tree struct. |
| 852 | 733 | * |
| 734 | * @param type the data type for the nodes | |
| 735 | */ | |
| 736 | #define CX_TREE_NODE_BASE(type) \ | |
| 737 | type *parent; \ | |
| 738 | type *children;\ | |
| 739 | type *last_child;\ | |
| 740 | type *prev;\ | |
| 741 | type *next | |
| 742 | ||
| 743 | /** | |
| 744 | * Macro for specifying the layout of a base node tree. | |
| 745 | * | |
| 746 | * When your tree uses #CX_TREE_NODE_BASE, you can use this | |
| 747 | * macro in all tree functions that expect the layout parameters | |
| 748 | * @c loc_parent, @c loc_children, @c loc_last_child, @c loc_prev, | |
| 749 | * and @c loc_next. | |
| 750 | */ | |
| 751 | #define cx_tree_node_base_layout \ | |
| 752 | offsetof(struct cx_tree_node_base_s, parent),\ | |
| 753 | offsetof(struct cx_tree_node_base_s, children),\ | |
| 754 | offsetof(struct cx_tree_node_base_s, last_child),\ | |
| 755 | offsetof(struct cx_tree_node_base_s, prev), \ | |
| 756 | offsetof(struct cx_tree_node_base_s, next) | |
| 757 | ||
| 758 | /** | |
| 759 | * The class definition for arbitrary trees. | |
| 760 | */ | |
| 761 | struct cx_tree_class_s { | |
| 762 | /** | |
| 763 | * Member function for inserting a single element. | |
| 764 | * | |
| 765 | * Implementations SHALL NOT simply invoke @p insert_many as this comes | |
| 766 | * with too much overhead. | |
| 767 | */ | |
| 889 | 768 | int (*insert_element)(struct cx_tree_s *tree, const void *data); |
| 852 | 769 | |
| 770 | /** | |
| 771 | * Member function for inserting multiple elements. | |
| 772 | * | |
| 889 | 773 | * Implementations SHALL avoid performing a full search in the tree for |
| 852 | 774 | * every element even though the source data MAY be unsorted. |
| 775 | */ | |
| 889 | 776 | size_t (*insert_many)(struct cx_tree_s *tree, struct cx_iterator_base_s *iter, size_t n); |
| 852 | 777 | |
| 778 | /** | |
| 779 | * Member function for finding a node. | |
| 780 | */ | |
| 889 | 781 | void *(*find)(struct cx_tree_s *tree, const void *subtree, const void *data, size_t depth); |
| 852 | 782 | }; |
| 783 | ||
| 784 | /** | |
| 785 | * Common type for all tree implementations. | |
| 786 | */ | |
| 787 | typedef struct cx_tree_s CxTree; | |
| 788 | ||
| 789 | ||
| 790 | /** | |
| 889 | 791 | * Destroys a node and its subtree. |
| 852 | 792 | * |
| 793 | * It is guaranteed that the simple destructor is invoked before | |
| 794 | * the advanced destructor, starting with the leaf nodes of the subtree. | |
| 795 | * | |
| 796 | * When this function is invoked on the root node of the tree, it destroys the | |
| 797 | * tree contents, but - in contrast to #cxTreeFree() - not the tree | |
| 798 | * structure, leaving an empty tree behind. | |
| 799 | * | |
| 800 | * @note The destructor function, if any, will @em not be invoked. That means | |
| 801 | * you will need to free the removed subtree by yourself, eventually. | |
| 802 | * | |
| 803 | * @attention This function will not free the memory of the nodes with the | |
| 804 | * tree's allocator, because that is usually done by the advanced destructor | |
| 805 | * and would therefore result in a double-free. | |
| 806 | * | |
| 807 | * @param tree the tree | |
| 808 | * @param node the node to remove | |
| 809 | * @see cxTreeFree() | |
| 810 | */ | |
| 811 | cx_attr_nonnull | |
| 889 | 812 | CX_EXPORT void cxTreeDestroySubtree(CxTree *tree, void *node); |
| 852 | 813 | |
| 814 | ||
| 815 | /** | |
| 816 | * Destroys the tree contents. | |
| 817 | * | |
| 818 | * It is guaranteed that the simple destructor is invoked before | |
| 819 | * the advanced destructor, starting with the leaf nodes of the subtree. | |
| 820 | * | |
| 821 | * This is a convenience macro for invoking #cxTreeDestroySubtree() on the | |
| 822 | * root node of the tree. | |
| 823 | * | |
| 824 | * @attention Be careful when calling this function when no destructor function | |
| 825 | * is registered that actually frees the memory of nodes. In that case you will | |
| 889 | 826 | * need a reference to the (former) root node of the tree somewhere, or |
| 852 | 827 | * otherwise you will be leaking memory. |
| 828 | * | |
| 829 | * @param tree the tree | |
| 830 | * @see cxTreeDestroySubtree() | |
| 831 | */ | |
| 832 | #define cxTreeClear(tree) cxTreeDestroySubtree(tree, tree->root) | |
| 833 | ||
| 834 | /** | |
| 835 | * Deallocates the tree structure. | |
| 836 | * | |
| 837 | * The destructor functions are invoked for each node, starting with the leaf | |
| 838 | * nodes. | |
| 839 | * It is guaranteed that for each node the simple destructor is invoked before | |
| 840 | * the advanced destructor. | |
| 841 | * | |
| 842 | * @attention This function will only invoke the destructor functions | |
| 843 | * on the nodes. | |
| 844 | * It will NOT additionally free the nodes with the tree's allocator, because | |
| 845 | * that would cause a double-free in most scenarios where the advanced | |
| 846 | * destructor is already freeing the memory. | |
| 847 | * | |
| 848 | * @param tree the tree to free | |
| 849 | */ | |
| 889 | 850 | CX_EXPORT void cxTreeFree(CxTree *tree); |
| 852 | 851 | |
| 852 | /** | |
| 853 | * Creates a new tree structure based on the specified layout. | |
| 854 | * | |
| 855 | * The specified @p allocator will be used for creating the tree struct | |
| 856 | * and SHALL be used by @p create_func to allocate memory for the nodes. | |
| 857 | * | |
| 858 | * @note This function will also register an advanced destructor which | |
| 859 | * will free the nodes with the allocator's free() method. | |
| 860 | * | |
| 861 | * @param allocator the allocator that shall be used | |
| 886 | 862 | * (if @c NULL, the cxDefaultAllocator will be used) |
| 852 | 863 | * @param create_func a function that creates new nodes |
| 864 | * @param search_func a function that compares two nodes | |
| 865 | * @param search_data_func a function that compares a node with data | |
| 866 | * @param loc_parent offset in the node struct for the parent pointer | |
| 867 | * @param loc_children offset in the node struct for the children linked list | |
| 868 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 869 | * the last child in the linked list (negative if there is no such pointer) | |
| 870 | * @param loc_prev optional offset in the node struct for the prev pointer | |
| 871 | * @param loc_next offset in the node struct for the next pointer | |
| 872 | * @return the new tree | |
| 873 | * @see cxTreeCreateSimple() | |
| 874 | * @see cxTreeCreateWrapped() | |
| 875 | */ | |
| 889 | 876 | cx_attr_nonnull_arg(2, 3, 4) cx_attr_nodiscard cx_attr_malloc cx_attr_dealloc(cxTreeFree, 1) |
| 877 | CX_EXPORT CxTree *cxTreeCreate(const CxAllocator *allocator, cx_tree_node_create_func create_func, | |
| 878 | cx_tree_search_func search_func, cx_tree_search_data_func search_data_func, | |
| 879 | ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child, | |
| 880 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | |
| 852 | 881 | |
| 882 | /** | |
| 883 | * Creates a new tree structure based on a default layout. | |
| 884 | * | |
| 889 | 885 | * Nodes created by @p create_func MUST contain #cx_tree_node_base_s as the first |
| 852 | 886 | * member (or at least respect the default offsets specified in the tree |
| 889 | 887 | * struct), and they MUST be allocated with the specified allocator. |
| 852 | 888 | * |
| 889 | * @note This function will also register an advanced destructor which | |
| 890 | * will free the nodes with the allocator's free() method. | |
| 891 | * | |
| 892 | * @param allocator (@c CxAllocator*) the allocator that shall be used | |
| 893 | * @param create_func (@c cx_tree_node_create_func) a function that creates new nodes | |
| 894 | * @param search_func (@c cx_tree_search_func) a function that compares two nodes | |
| 895 | * @param search_data_func (@c cx_tree_search_data_func) a function that compares a node with data | |
| 896 | * @return (@c CxTree*) the new tree | |
| 897 | * @see cxTreeCreate() | |
| 898 | */ | |
| 889 | 899 | #define cxTreeCreateSimple(allocator, create_func, search_func, search_data_func) \ |
| 900 | cxTreeCreate(allocator, create_func, search_func, search_data_func, cx_tree_node_base_layout) | |
| 852 | 901 | |
| 902 | /** | |
| 903 | * Creates a new tree structure based on an existing tree. | |
| 904 | * | |
| 905 | * The specified @p allocator will be used for creating the tree struct. | |
| 906 | * | |
| 907 | * @attention This function will create an incompletely defined tree structure | |
| 908 | * where neither the create function, the search function, nor a destructor | |
| 909 | * will be set. If you wish to use any of this functionality for the wrapped | |
| 889 | 910 | * tree, you need to specify those functions afterward. |
| 852 | 911 | * |
| 912 | * @param allocator the allocator that was used for nodes of the wrapped tree | |
| 886 | 913 | * (if @c NULL, the cxDefaultAllocator is assumed) |
| 852 | 914 | * @param root the root node of the tree that shall be wrapped |
| 915 | * @param loc_parent offset in the node struct for the parent pointer | |
| 916 | * @param loc_children offset in the node struct for the children linked list | |
| 917 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 918 | * the last child in the linked list (negative if there is no such pointer) | |
| 919 | * @param loc_prev optional offset in the node struct for the prev pointer | |
| 920 | * @param loc_next offset in the node struct for the next pointer | |
| 921 | * @return the new tree | |
| 922 | * @see cxTreeCreate() | |
| 923 | */ | |
| 889 | 924 | cx_attr_nonnull_arg(2) cx_attr_nodiscard cx_attr_malloc cx_attr_dealloc(cxTreeFree, 1) |
| 925 | CX_EXPORT CxTree *cxTreeCreateWrapped(const CxAllocator *allocator, void *root, | |
| 926 | ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child, | |
| 927 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | |
| 852 | 928 | |
| 929 | /** | |
| 930 | * Inserts data into the tree. | |
| 931 | * | |
| 932 | * @remark For this function to work, the tree needs specified search and | |
| 933 | * create functions, which might not be available for wrapped trees | |
| 934 | * (see #cxTreeCreateWrapped()). | |
| 935 | * | |
| 936 | * @param tree the tree | |
| 937 | * @param data the data to insert | |
| 938 | * @retval zero success | |
| 939 | * @retval non-zero failure | |
| 940 | */ | |
| 941 | cx_attr_nonnull | |
| 889 | 942 | CX_EXPORT int cxTreeInsert(CxTree *tree, const void *data); |
| 852 | 943 | |
| 944 | /** | |
| 945 | * Inserts elements provided by an iterator efficiently into the tree. | |
| 946 | * | |
| 947 | * @remark For this function to work, the tree needs specified search and | |
| 948 | * create functions, which might not be available for wrapped trees | |
| 949 | * (see #cxTreeCreateWrapped()). | |
| 950 | * | |
| 951 | * @param tree the tree | |
| 952 | * @param iter the iterator providing the elements | |
| 953 | * @param n the maximum number of elements to insert | |
| 954 | * @return the number of elements that could be successfully inserted | |
| 955 | */ | |
| 956 | cx_attr_nonnull | |
| 889 | 957 | CX_EXPORT size_t cxTreeInsertIter(CxTree *tree, CxIteratorBase *iter, size_t n); |
| 852 | 958 | |
| 959 | /** | |
| 960 | * Inserts an array of data efficiently into the tree. | |
| 961 | * | |
| 962 | * @remark For this function to work, the tree needs specified search and | |
| 963 | * create functions, which might not be available for wrapped trees | |
| 964 | * (see #cxTreeCreateWrapped()). | |
| 965 | * | |
| 966 | * @param tree the tree | |
| 967 | * @param data the array of data to insert | |
| 968 | * @param elem_size the size of each element in the array | |
| 969 | * @param n the number of elements in the array | |
| 970 | * @return the number of elements that could be successfully inserted | |
| 971 | */ | |
| 972 | cx_attr_nonnull | |
| 889 | 973 | CX_EXPORT size_t cxTreeInsertArray(CxTree *tree, const void *data, size_t elem_size, size_t n); |
| 852 | 974 | |
| 975 | /** | |
| 976 | * Searches the data in the specified tree. | |
| 977 | * | |
| 978 | * @remark For this function to work, the tree needs a specified @c search_data | |
| 979 | * function, which might not be available wrapped trees | |
| 980 | * (see #cxTreeCreateWrapped()). | |
| 981 | * | |
| 982 | * @param tree the tree | |
| 983 | * @param data the data to search for | |
| 984 | * @return the first matching node, or @c NULL when the data cannot be found | |
| 985 | */ | |
| 889 | 986 | cx_attr_nonnull cx_attr_nodiscard |
| 987 | CX_EXPORT void *cxTreeFind(CxTree *tree, const void *data); | |
| 852 | 988 | |
| 989 | /** | |
| 990 | * Searches the data in the specified subtree. | |
| 991 | * | |
| 992 | * When @p max_depth is zero, the depth is not limited. | |
| 993 | * The @p subtree_root itself is on depth 1 and its children have depth 2. | |
| 994 | * | |
| 995 | * @note When @p subtree_root is not part of the @p tree, the behavior is | |
| 996 | * undefined. | |
| 997 | * | |
| 998 | * @remark For this function to work, the tree needs a specified @c search_data | |
| 999 | * function, which might not be the case for wrapped trees | |
| 1000 | * (see #cxTreeCreateWrapped()). | |
| 1001 | * | |
| 1002 | * @param tree the tree | |
| 1003 | * @param data the data to search for | |
| 1004 | * @param subtree_root the node where to start | |
| 1005 | * @param max_depth the maximum search depth | |
| 1006 | * @return the first matching node, or @c NULL when the data cannot be found | |
| 1007 | */ | |
| 889 | 1008 | cx_attr_nonnull cx_attr_nodiscard |
| 1009 | CX_EXPORT void *cxTreeFindInSubtree(CxTree *tree, const void *data, void *subtree_root, size_t max_depth); | |
| 852 | 1010 | |
| 1011 | /** | |
| 1012 | * Determines the size of the specified subtree. | |
| 1013 | * | |
| 1014 | * @param tree the tree | |
| 1015 | * @param subtree_root the root node of the subtree | |
| 1016 | * @return the number of nodes in the specified subtree | |
| 1017 | */ | |
| 889 | 1018 | cx_attr_nonnull cx_attr_nodiscard |
| 1019 | CX_EXPORT size_t cxTreeSubtreeSize(CxTree *tree, void *subtree_root); | |
| 852 | 1020 | |
| 1021 | /** | |
| 1022 | * Determines the depth of the specified subtree. | |
| 1023 | * | |
| 1024 | * @param tree the tree | |
| 1025 | * @param subtree_root the root node of the subtree | |
| 1026 | * @return the tree depth including the @p subtree_root | |
| 1027 | */ | |
| 889 | 1028 | cx_attr_nonnull cx_attr_nodiscard |
| 1029 | CX_EXPORT size_t cxTreeSubtreeDepth(CxTree *tree, void *subtree_root); | |
| 852 | 1030 | |
| 1031 | /** | |
| 886 | 1032 | * Determines the size of the entire tree. |
| 1033 | * | |
| 1034 | * @param tree the tree | |
| 1035 | * @return the tree size, counting the root as one | |
| 1036 | */ | |
| 889 | 1037 | cx_attr_nonnull cx_attr_nodiscard |
| 1038 | CX_EXPORT size_t cxTreeSize(CxTree *tree); | |
| 886 | 1039 | |
| 1040 | /** | |
| 852 | 1041 | * Determines the depth of the entire tree. |
| 1042 | * | |
| 1043 | * @param tree the tree | |
| 1044 | * @return the tree depth, counting the root as one | |
| 1045 | */ | |
| 889 | 1046 | cx_attr_nonnull cx_attr_nodiscard |
| 1047 | CX_EXPORT size_t cxTreeDepth(CxTree *tree); | |
| 852 | 1048 | |
| 1049 | /** | |
| 1050 | * Creates a depth-first iterator for the specified tree starting in @p node. | |
| 1051 | * | |
| 1052 | * If the node is not part of the tree, the behavior is undefined. | |
| 1053 | * | |
| 1054 | * @param tree the tree to iterate | |
| 1055 | * @param node the node where to start | |
| 1056 | * @param visit_on_exit true, if the iterator shall visit a node again when | |
| 1057 | * leaving the subtree | |
| 1058 | * @return a tree iterator (depth-first) | |
| 1059 | * @see cxTreeVisit() | |
| 1060 | */ | |
| 889 | 1061 | cx_attr_nonnull cx_attr_nodiscard |
| 1062 | CX_EXPORT CxTreeIterator cxTreeIterateSubtree(CxTree *tree, void *node, bool visit_on_exit); | |
| 852 | 1063 | |
| 1064 | /** | |
| 1065 | * Creates a breadth-first iterator for the specified tree starting in @p node. | |
| 1066 | * | |
| 1067 | * If the node is not part of the tree, the behavior is undefined. | |
| 1068 | * | |
| 1069 | * @param tree the tree to iterate | |
| 1070 | * @param node the node where to start | |
| 1071 | * @return a tree visitor (a.k.a. breadth-first iterator) | |
| 1072 | * @see cxTreeIterate() | |
| 1073 | */ | |
| 889 | 1074 | cx_attr_nonnull cx_attr_nodiscard |
| 1075 | CX_EXPORT CxTreeVisitor cxTreeVisitSubtree(CxTree *tree, void *node); | |
| 852 | 1076 | |
| 1077 | /** | |
| 1078 | * Creates a depth-first iterator for the specified tree. | |
| 1079 | * | |
| 1080 | * @param tree the tree to iterate | |
| 1081 | * @param visit_on_exit true, if the iterator shall visit a node again when | |
| 1082 | * leaving the subtree | |
| 1083 | * @return a tree iterator (depth-first) | |
| 1084 | * @see cxTreeVisit() | |
| 1085 | */ | |
| 889 | 1086 | cx_attr_nonnull cx_attr_nodiscard |
| 1087 | CX_EXPORT CxTreeIterator cxTreeIterate(CxTree *tree, bool visit_on_exit); | |
| 852 | 1088 | |
| 1089 | /** | |
| 1090 | * Creates a breadth-first iterator for the specified tree. | |
| 1091 | * | |
| 1092 | * @param tree the tree to iterate | |
| 1093 | * @return a tree visitor (a.k.a. breadth-first iterator) | |
| 1094 | * @see cxTreeIterate() | |
| 1095 | */ | |
| 889 | 1096 | cx_attr_nonnull cx_attr_nodiscard |
| 1097 | CxTreeVisitor cxTreeVisit(CxTree *tree); | |
| 852 | 1098 | |
| 1099 | /** | |
| 1100 | * Sets the (new) parent of the specified child. | |
| 1101 | * | |
| 889 | 1102 | * If the @p child is not already a member of the tree, this function behaves |
| 852 | 1103 | * as #cxTreeAddChildNode(). |
| 1104 | * | |
| 1105 | * @param tree the tree | |
| 1106 | * @param parent the (new) parent of the child | |
| 1107 | * @param child the node to add | |
| 1108 | * @see cxTreeAddChildNode() | |
| 1109 | */ | |
| 1110 | cx_attr_nonnull | |
| 889 | 1111 | CX_EXPORT void cxTreeSetParent(CxTree *tree, void *parent, void *child); |
| 852 | 1112 | |
| 1113 | /** | |
| 1114 | * Adds a new node to the tree. | |
| 1115 | * | |
| 889 | 1116 | * If the @p child is already a member of the tree, the behavior is undefined. |
| 852 | 1117 | * Use #cxTreeSetParent() if you want to move a subtree to another location. |
| 1118 | * | |
| 1119 | * @attention The node may be externally created, but MUST obey the same rules | |
| 889 | 1120 | * as if it was created by the tree itself with #cxTreeAddChild() (e.g., use |
| 852 | 1121 | * the same allocator). |
| 1122 | * | |
| 1123 | * @param tree the tree | |
| 1124 | * @param parent the parent of the node to add | |
| 1125 | * @param child the node to add | |
| 1126 | * @see cxTreeSetParent() | |
| 1127 | */ | |
| 1128 | cx_attr_nonnull | |
| 889 | 1129 | CX_EXPORT void cxTreeAddChildNode(CxTree *tree, void *parent, void *child); |
| 852 | 1130 | |
| 1131 | /** | |
| 1132 | * Creates a new node and adds it to the tree. | |
| 1133 | * | |
| 1134 | * With this function you can decide where exactly the new node shall be added. | |
| 1135 | * If you specified an appropriate search function, you may want to consider | |
| 1136 | * leaving this task to the tree by using #cxTreeInsert(). | |
| 1137 | * | |
| 1138 | * Be aware that adding nodes at arbitrary locations in the tree might cause | |
| 889 | 1139 | * wrong or undesired results when subsequently invoking #cxTreeInsert(), and |
| 852 | 1140 | * the invariant imposed by the search function does not hold any longer. |
| 1141 | * | |
| 1142 | * @param tree the tree | |
| 1143 | * @param parent the parent node of the new node | |
| 1144 | * @param data the data that will be submitted to the create function | |
| 1145 | * @return zero when the new node was created, non-zero on allocation failure | |
| 1146 | * @see cxTreeInsert() | |
| 1147 | */ | |
| 1148 | cx_attr_nonnull | |
| 889 | 1149 | CX_EXPORT int cxTreeAddChild(CxTree *tree, void *parent, const void *data); |
| 852 | 1150 | |
| 1151 | /** | |
| 1152 | * A function that is invoked when a node needs to be re-linked to a new parent. | |
| 1153 | * | |
| 1154 | * When a node is re-linked, sometimes the contents need to be updated. | |
| 1155 | * This callback is invoked by #cxTreeRemoveNode() and #cxTreeDestroyNode() | |
| 1156 | * so that those updates can be applied when re-linking the children of the | |
| 1157 | * removed node. | |
| 1158 | * | |
| 1159 | * @param node the affected node | |
| 1160 | * @param old_parent the old parent of the node | |
| 1161 | * @param new_parent the new parent of the node | |
| 1162 | */ | |
| 1163 | typedef void (*cx_tree_relink_func)( | |
| 1164 | void *node, | |
| 1165 | const void *old_parent, | |
| 1166 | const void *new_parent | |
| 1167 | ); | |
| 1168 | ||
| 1169 | /** | |
| 1170 | * Removes a node and re-links its children to its former parent. | |
| 1171 | * | |
| 1172 | * If the node is not part of the tree, the behavior is undefined. | |
| 1173 | * | |
| 1174 | * @note The destructor function, if any, will @em not be invoked. That means | |
| 1175 | * you will need to free the removed node by yourself, eventually. | |
| 1176 | * | |
| 1177 | * @param tree the tree | |
| 1178 | * @param node the node to remove (must not be the root node) | |
| 1179 | * @param relink_func optional callback to update the content of each re-linked | |
| 1180 | * node | |
| 1181 | * @return zero on success, non-zero if @p node is the root node of the tree | |
| 1182 | */ | |
| 1183 | cx_attr_nonnull_arg(1, 2) | |
| 889 | 1184 | CX_EXPORT int cxTreeRemoveNode(CxTree *tree, void *node, cx_tree_relink_func relink_func); |
| 852 | 1185 | |
| 1186 | /** | |
| 889 | 1187 | * Removes a node and its subtree from the tree. |
| 852 | 1188 | * |
| 1189 | * If the node is not part of the tree, the behavior is undefined. | |
| 1190 | * | |
| 1191 | * @note The destructor function, if any, will @em not be invoked. That means | |
| 1192 | * you will need to free the removed subtree by yourself, eventually. | |
| 1193 | * | |
| 1194 | * @param tree the tree | |
| 1195 | * @param node the node to remove | |
| 1196 | */ | |
| 1197 | cx_attr_nonnull | |
| 889 | 1198 | CX_EXPORT void cxTreeRemoveSubtree(CxTree *tree, void *node); |
| 852 | 1199 | |
| 1200 | /** | |
| 1201 | * Destroys a node and re-links its children to its former parent. | |
| 1202 | * | |
| 1203 | * If the node is not part of the tree, the behavior is undefined. | |
| 1204 | * | |
| 1205 | * It is guaranteed that the simple destructor is invoked before | |
| 1206 | * the advanced destructor. | |
| 1207 | * | |
| 1208 | * @attention This function will not free the memory of the node with the | |
| 1209 | * tree's allocator, because that is usually done by the advanced destructor | |
| 1210 | * and would therefore result in a double-free. | |
| 1211 | * | |
| 1212 | * @param tree the tree | |
| 1213 | * @param node the node to destroy (must not be the root node) | |
| 1214 | * @param relink_func optional callback to update the content of each re-linked | |
| 1215 | * node | |
| 1216 | * @return zero on success, non-zero if @p node is the root node of the tree | |
| 1217 | */ | |
| 1218 | cx_attr_nonnull_arg(1, 2) | |
| 889 | 1219 | CX_EXPORT int cxTreeDestroyNode(CxTree *tree, void *node, cx_tree_relink_func relink_func); |
| 852 | 1220 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1221 | #ifdef __cplusplus |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1222 | } // extern "C" |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1223 | #endif |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1224 | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
1225 | #endif //UCX_TREE_H |