ufe 7.0
Universal Front End is a DCC-agnostic component that will allow a DCC to browse and edit data in multiple data models
trie.h
Go to the documentation of this file.
1#line 1 "D:/Jenkins/workspace/EMS/ECG/ufe/full/ufe-full-python3.13-windows/ufe/include/trie.h"
2#ifndef UFE_TRIE_H
3#define UFE_TRIE_H
4
5// ===========================================================================
6// Copyright 2025 Autodesk, Inc. All rights reserved.
7//
8// The use of this software is subject to the Autodesk Terms of Use or other
9// license agreement provided at the time of installation or download, or
10// which otherwise accompanies this software.
11// ===========================================================================
12
13#include "pathComponent.h"
14#include "path.h"
15
16#include <memory>
17#include <unordered_map>
18
20
22
37template<typename T> class Trie;
38
39template<typename T>
40class TrieNode : public std::enable_shared_from_this< TrieNode<T> >
41{
42 friend class Trie<T>;
43public:
44 typedef std::shared_ptr<TrieNode> Ptr;
45
46 TrieNode(const PathComponent& component);
47
48 // Create a trie node with a null component.
49 TrieNode();
50
51 Ptr parent() const;
52
53 // Child addition and removal.
54 void add(const Ptr& child);
55 void remove(const Ptr& child);
56
57 // Child lookup.
58 bool contains(const PathComponent& child) const;
59 Ptr operator[](const PathComponent& child) const;
60
61 // Returns the PathComponents of all the children of this node.
62 std::vector<PathComponent> childrenComponents() const;
63
64 // Number of children TrieNode's.
65 std::size_t size() const;
66
67 // Number of nodes in the tree rooted at this node, including this node.
68 std::size_t treeSize() const;
69
70 // Convenience method for size() == 0.
71 bool empty() const;
72
73 PathComponent component() const;
74
75 // Change the component of this node, adjusting the parent accordingly.
76 void rename(const PathComponent& component);
77
78 // Change the component & parent of this node, adjusting the old and new parent accordingly.
79 void move(const PathComponent& component, const Ptr& newParent);
80
81 // Copy the data into the node, and set the has data flag.
82 void setData(const T& data);
83
84 // Access the node's data. The hasData() method must return true for data
85 // to be valid, else the return data is stale.
86 const T& data() const;
87
88 bool hasData() const;
89
90 // Called to find the depth of the closest common ancestor to all nodes
91 // with data. Calls itself recursively looking for either multiple children
92 // or the first node with associated data.
93 // Increments the depth parameter as it moves down the trie which can then
94 // be used to create a path that matches the specific node.
95 int closestCommonAncestor( int depth ) const;
96
97private:
98
99 typedef std::unordered_map<PathComponent, Ptr> Children;
100 typedef std::weak_ptr<TrieNode> ParentPtr;
101
102 // Clearing out of the TrieNode should be managed by the Trie only,
103 // so keeping these private.
104 void clear();
105 void clearData();
106
107 void setParent(Ptr parent);
108
114};
115
117
125template<typename T>
126class Trie
127{
128public:
129
130 Trie();
131
132 Trie(const Trie&) = delete;
133 Trie& operator=(const Trie&) = delete;
134
135 // Move assignment and construction.
136 Trie(Trie&&);
137 Trie& operator=(Trie&&);
138
139 typename TrieNode<T>::Ptr root() const;
140
141 typename TrieNode<T>::Ptr add(const Path& path, const T& data);
142 typename TrieNode<T>::Ptr remove(const Path&);
143 // Move the entire hierarchy rooted at oldPath to newPath. Returns the resulting
144 // trie node if successful, else a null pointer.
145 typename TrieNode<T>::Ptr move(const Path& oldPath, const Path& newPath);
146 void clear();
147
148 // Lookup. contains() is a convenience for bool(find(path)).
149 typename TrieNode<T>::Ptr find(const Path& path) const;
150 bool contains(const Path& path) const;
151
152 // Return the trie node corresponding to the path, regardless of whether it
153 // has data or not. If the trie has no such node, returns a null pointer.
154 typename TrieNode<T>::Ptr node(const Path& path) const;
155
156 // Query whether the trie contains a descendant of the argument path. Must
157 // be a strict descendant: if the trie contains only the argument
158 // itself, returns false. If the argument path is empty, returns false.
159 bool containsDescendant(const Path& path) const;
160
161 // convenience for contains(path) ||constainsDescendant(path)
162 bool containsDescendantInclusive(const Path& path) const;
163
164 // Query whether the trie contains an ancestor of the argument path.
165 // Must be a strict ancestor: if the trie contains only the argument
166 // itself, returns false. If the argument path is empty, returns false.
167 bool containsAncestor(const Path& path) const;
168
169 // convenience for contains(path) || containsAncestor(path)
170 bool containsAncestorInclusive(const Path& path) const;
171
172 // Returns the depth of the closest common ancestor of all leaf nodes.
173 // Return -1 if the trie is empty.
174 // Return 0 if the common ancestor is the root of the scene graph.
175 int closestCommonAncestor() const;
176
177 // Number of nodes in the trie, including the root node. For testing
178 // and debugging purposes, as it traverses the trie and counts nodes.
179 std::size_t size() const;
180
184 bool empty() const;
185
186private:
187
188 typename TrieNode<T>::Ptr createNode(const Path& path);
189 void cleanUpNode(const typename TrieNode<T>::Ptr& node);
190 template<bool INCLUDE_DESCENDANT>
191 bool containsAncestorHelper(const Path& path) const;
192 template<bool INCLUDE_ANCESTOR>
193 bool containsDescendantHelper(const Path& path) const;
194
196};
197
198}
199
200#endif /* UFE_TRIE_H */
Constant string representation with fixed space and O(1) comparison.
Definition: pathComponent.h:35
Identify an object or 3D path in the scene.
Definition: path.h:40
Node for Universal Front End trie.
Definition: trie.h:127
TrieNode< T >::Ptr fRoot
Definition: trie.h:195
Trie & operator=(const Trie &)=delete
Trie(const Trie &)=delete
Children fChildren
Definition: trie.h:111
std::shared_ptr< TrieNode > Ptr
Definition: trie.h:44
std::unordered_map< PathComponent, Ptr > Children
Definition: trie.h:99
PathComponent fComponent
Definition: trie.h:109
std::weak_ptr< TrieNode > ParentPtr
Definition: trie.h:100
bool fHasData
Definition: trie.h:112
ParentPtr fParent
Definition: trie.h:110
void remove(const std::string &name)
Path path(const std::string &pathString)
#define UFE_NS_DEF
Definition: ufe.h:36