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