ufe  2.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 "S:/jenkins/workspace/ufe-full-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  // Number of children TrieNode's.
63  std::size_t size() const;
64 
65  // Number of nodes in the tree rooted at this node, including this node.
66  std::size_t treeSize() const;
67 
68  // Convenience method for size() == 0.
69  bool empty() const;
70 
71  PathComponent component() const;
72 
73  // Change the component of this node, adjusting the parent accordingly.
74  void rename(const PathComponent& component);
75 
76  // Copy the data into the node, and set the has data flag.
77  void setData(const T& data);
78 
79  // Access the node's data. The hasData() method must return true for data
80  // to be valid, else the return data is stale.
81  const T& data() const;
82 
83  bool hasData() const;
84 
85  // Called to find the depth of the closest common ancestor to all nodes
86  // with data. Calls itself recursively looking for either multiple children
87  // or the first node with associated data.
88  // Increments the depth parameter as it moves down the trie which can then
89  // be used to create a path that matches the specific node.
90  int closestCommonAncestor( int depth ) const;
91 
92 private:
93 
94  typedef std::unordered_map<PathComponent, Ptr> Children;
95  typedef std::weak_ptr<TrieNode> ParentPtr;
96 
97  // Clearing out of the TrieNode should be managed by the Trie only,
98  // so keeping these private.
99  void clear();
100  void clearData();
101 
102  void setParent(Ptr parent);
103 
107  bool fHasData;
108  T fData;
109 };
110 
112 
120 template<typename T>
121 class Trie
122 {
123 public:
124 
125  Trie();
126 
127  Trie(const Trie&) = delete;
128  Trie& operator=(const Trie&) = delete;
129 
130  // Move assignment and construction.
131  Trie(Trie&&);
132  Trie& operator=(Trie&&);
133 
134  typename TrieNode<T>::Ptr root() const;
135 
136  typename TrieNode<T>::Ptr add(const Path& path, const T& data);
137  typename TrieNode<T>::Ptr remove(const Path&);
138  void clear();
139 
140  // Lookup. contains() is a convenience for bool(find(path)).
141  typename TrieNode<T>::Ptr find(const Path& path) const;
142  bool contains(const Path& path) const;
143 
144  // Return the trie node corresponding to the path, regardless of whether it
145  // has data or not. If the trie has no such node, returns a null pointer.
146  typename TrieNode<T>::Ptr node(const Path& path) const;
147 
148  // Query whether the trie contains a descendant of the argument path. Must
149  // be a strict descendant: if the trie contains only the argument
150  // itself, returns false. If the argument path is empty, returns false.
151  bool containsDescendant(const Path& path) const;
152 
153  // Query whether the trie contains an ancestor of the argument path.
154  // Must be a strict ancestor: if the trie contains only the argument
155  // itself, returns false. If the argument path is empty, returns false.
156  bool containsAncestor(const Path& path) const;
157 
158  // Returns the depth of the closest common ancestor of all leaf nodes.
159  // Return -1 if the trie is empty.
160  // Return 0 if the common ancestor is the root of the scene graph.
161  int closestCommonAncestor() const;
162 
163  // Number of nodes in the trie, including the root node. For testing
164  // and debugging purposes, as it traverses the trie and counts nodes.
165  std::size_t size() const;
166 
167  // Convenience method for size() == 0.
168  bool empty() const;
169 
170 private:
171 
173 };
174 
175 }
176 
177 #endif /* _ufeTrie */
bool fHasData
Definition: trie.h:107
std::unordered_map< PathComponent, Ptr > Children
Definition: trie.h:94
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:105
std::weak_ptr< TrieNode > ParentPtr
Definition: trie.h:95
std::shared_ptr< TrieNode > Ptr
Definition: trie.h:45
Path path(const std::string &pathString)
TrieNode< T >::Ptr fRoot
Definition: trie.h:172
PathComponent fComponent
Definition: trie.h:104
Children fChildren
Definition: trie.h:106