forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryGraph.cs
More file actions
200 lines (167 loc) · 6.76 KB
/
Copy pathQueryGraph.cs
File metadata and controls
200 lines (167 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
namespace UnityEditor.Search
{
/// <summary>
/// Structure containing the different options used to optimize a query graph.
/// </summary>
public struct QueryGraphOptimizationOptions
{
/// <summary>
/// Propagate "Not" operations to leaves, so only leaves can have "Not" operations as parents.
/// </summary>
public bool propagateNotToLeaves;
/// <summary>
/// Swaps "Not" operations to the right hand side of combining operations (i.e. "And", "Or"). Useful if a "Not" operation is slow.
/// </summary>
public bool swapNotToRightHandSide;
/// <summary>
/// Swaps filter functions to the right hand side of combining operations (i.e. "And", "Or"). Useful if those filter operations are slow.
/// </summary>
public bool swapFilterFunctionsToRightHandSide;
}
/// <summary>
/// Class that represents a query graph.
/// </summary>
public class QueryGraph
{
/// <summary>
/// Root node of the graph. Can be null.
/// </summary>
public IQueryNode root { get; private set; }
/// <summary>
/// Returns true if the graph is empty.
/// </summary>
public bool empty => root == null;
/// <summary>
/// Constructor. Creates a new query graph.
/// </summary>
/// <param name="root">Root node of the graph.</param>
public QueryGraph(IQueryNode root)
{
this.root = root;
}
/// <summary>
/// Optimize the graph.
/// </summary>
/// <param name="propagateNotToLeaves">Propagate "Not" operations to leaves, so only leaves can have "Not" operations as parents.</param>
/// <param name="swapNotToRightHandSide">Swaps "Not" operations to the right hand side of combining operations (i.e. "And", "Or"). Useful if a "Not" operation is slow.</param>
public void Optimize(bool propagateNotToLeaves, bool swapNotToRightHandSide)
{
if (empty)
return;
Optimize(root, new QueryGraphOptimizationOptions {propagateNotToLeaves = propagateNotToLeaves, swapNotToRightHandSide = swapNotToRightHandSide, swapFilterFunctionsToRightHandSide = false});
}
/// <summary>
/// Optimize the graph.
/// </summary>
/// <param name="options">Optimization options.</param>
public void Optimize(QueryGraphOptimizationOptions options)
{
if (empty)
return;
Optimize(root, options);
}
void Optimize(IQueryNode rootNode, QueryGraphOptimizationOptions options)
{
if (rootNode.leaf)
return;
if (options.propagateNotToLeaves)
{
PropagateNotToLeaves(ref rootNode);
}
if (options.swapNotToRightHandSide)
{
SwapNotToRightHandSide(rootNode);
}
if (options.swapFilterFunctionsToRightHandSide)
{
SwapFilterFunctionsToRightHandSide(rootNode);
}
// ReSharper disable once ForCanBeConvertedToForeach
for (var i = 0; i < rootNode.children.Count; ++i)
{
Optimize(rootNode.children[i], options);
}
// Reduce Not depth (do this as last step)
ReduceNotDepth(rootNode);
}
static void SwapChild(IQueryNode parent, IQueryNode oldChild, IQueryNode newChild)
{
if (parent?.children == null || parent.children.Count == 0)
return;
var oldIndex = parent.children.IndexOf(oldChild);
parent.children[oldIndex] = newChild;
oldChild.parent = null;
newChild.parent = parent;
}
void PropagateNotToLeaves(ref IQueryNode rootNode)
{
if (rootNode.leaf || !(rootNode is CombinedNode cn))
return;
if (rootNode.type != QueryNodeType.Not)
return;
var parent = rootNode.parent;
var oldNode = rootNode.children[0];
if (!(oldNode is CombinedNode oldCombinedNode) || (oldCombinedNode.type != QueryNodeType.And && oldCombinedNode.type != QueryNodeType.Or))
return;
CombinedNode newCombinedNode;
if (oldNode.type == QueryNodeType.And)
newCombinedNode = new OrNode();
else
newCombinedNode = new AndNode();
cn.RemoveNode(oldNode);
foreach (var child in oldNode.children)
{
var propagatedNotNode = new NotNode();
propagatedNotNode.AddNode(child);
newCombinedNode.AddNode(propagatedNotNode);
}
oldCombinedNode.Clear();
// If the old not is the root of the evaluationGraph, then the new combined node
// becomes the new root.
if (parent == null)
this.root = newCombinedNode;
else
{
// In order to not change the parent's enumeration, swap directly the old
// children with the new one
SwapChild(parent, rootNode, newCombinedNode);
}
// Set the current tree root to the new combined node.
rootNode = newCombinedNode;
}
static void SwapNotToRightHandSide(IQueryNode rootNode)
{
if (rootNode.leaf || !(rootNode.children[0] is NotNode) || !(rootNode is CombinedNode cn))
return;
cn.SwapChildNodes();
}
static void SwapFilterFunctionsToRightHandSide(IQueryNode rootNode)
{
if (rootNode.leaf || !(rootNode is CombinedNode cn) || !(rootNode.children[0] is FilterNode fn) || !fn.filter.usesParameter)
return;
cn.SwapChildNodes();
}
void ReduceNotDepth(IQueryNode rootNode)
{
if (rootNode.leaf)
return;
if (rootNode.type != QueryNodeType.Not || rootNode.children[0].type != QueryNodeType.Not)
return;
var parent = rootNode.parent;
if (!(rootNode is NotNode notNode) || !(rootNode.children[0] is NotNode childNotNode))
return;
var descendant = childNotNode.children[0];
childNotNode.RemoveNode(descendant);
notNode.RemoveNode(childNotNode);
if (parent == null)
this.root = descendant;
else
{
SwapChild(parent, rootNode, descendant);
}
}
}
}