Skip to content

Commit

Permalink
[Quantum] Optimize logic based on enumerations
Browse files Browse the repository at this point in the history
  • Loading branch information
Eideren committed Jun 2, 2020
1 parent 67f2aa6 commit 4140892
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ public override ObjectReference FindTargetReference(IGraphNode sourceNode, IGrap
var targetIds = CollectionItemIdHelper.GetCollectionItemIds(targetNode.Retrieve());
var itemId = sourceIds[sourceReference.Index.Value];
var targetKey = targetIds.GetKey(itemId);
return targetReference.FirstOrDefault(x => Equals(x.Index.Value, targetKey));
foreach (var targetRef in targetReference)
{
if (Equals(targetRef.Index.Value, targetKey))
return targetRef;
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ public void Validate(bool isList)
public object GetKey(ItemId itemId)
{
// TODO: add indexing by guid to avoid O(n)
return keyToIdMap.SingleOrDefault(x => x.Value == itemId).Key;
foreach( var kvp in keyToIdMap )
{
if( kvp.Value == itemId )
return kvp.Key;
}
return null;
}

public void CloneInto(CollectionItemIdentifiers target, IReadOnlyDictionary<object, object> referenceTypeClonedKeys)
Expand Down
24 changes: 21 additions & 3 deletions sources/presentation/Stride.Core.Quantum/GraphNodeLinker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,28 @@ protected override void VisitChildren(IObjectNode node)
{
if (VisitedLinks.TryGetValue(node, out IGraphNode targetNodeParent))
{
foreach (var child in node.Members)
var objNode = targetNodeParent as IObjectNode;
var members = node.Members;
if (members is List<IMemberNode> asList)
{
string name = child.Name;
VisitedLinks.Add(child, ((IObjectNode)targetNodeParent)?.TryGetChild(name));
foreach (var child in asList)
{
VisitedLinks.Add(child, objNode?.TryGetChild(child.Name));
}
}
else if(members is Dictionary<string, IMemberNode>.ValueCollection asVCol)
{
foreach (var child in asVCol)
{
VisitedLinks.Add(child, objNode?.TryGetChild(child.Name));
}
}
else
{
foreach (var child in members)
{
VisitedLinks.Add(child, objNode?.TryGetChild(child.Name));
}
}
}
base.VisitChildren(node);
Expand Down
38 changes: 30 additions & 8 deletions sources/presentation/Stride.Core.Quantum/GraphVisitorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ protected virtual void VisitNode([NotNull] IGraphNode node)
{
Visiting?.Invoke(node, CurrentPath);
}
var objectNode = node as IObjectNode;
if (objectNode != null)

if (node is IObjectNode objectNode)
{
VisitChildren(objectNode);
VisitItemTargets(objectNode);
}
var memberNode = node as IMemberNode;
if (memberNode != null)

if (node is IMemberNode memberNode)
{
VisitMemberTarget(memberNode);
}
Expand All @@ -85,11 +85,33 @@ protected virtual void VisitNode([NotNull] IGraphNode node)
protected virtual void VisitChildren([NotNull] IObjectNode node)
{
if (node == null) throw new ArgumentNullException(nameof(node));
foreach (var child in node.Members)
var members = node.Members;
if (members is List<IMemberNode> asList)
{
foreach (var child in asList)
{
CurrentPath.PushMember(child.Name);
VisitNode(child);
CurrentPath.Pop();
}
}
else if(members is Dictionary<string, IMemberNode>.ValueCollection asVCol)
{
CurrentPath.PushMember(child.Name);
VisitNode(child);
CurrentPath.Pop();
foreach (var child in asVCol)
{
CurrentPath.PushMember(child.Name);
VisitNode(child);
CurrentPath.Pop();
}
}
else
{
foreach (var child in members)
{
CurrentPath.PushMember(child.Name);
VisitNode(child);
CurrentPath.Pop();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ public void Refresh(IGraphNode ownerNode, NodeContainer nodeContainer)
}

/// <inheritdoc/>
public IEnumerator<ObjectReference> GetEnumerator()
public ReferenceEnumerator GetEnumerator() => new ReferenceEnumerator(this);

IEnumerator<ObjectReference> IEnumerable<ObjectReference>.GetEnumerator()
{
return new ReferenceEnumerator(this);
}
Expand Down Expand Up @@ -237,7 +239,7 @@ private static object GetValue([NotNull] object keyValuePair)
/// <summary>
/// An enumerator for <see cref="ReferenceEnumerable"/> that enumerates in proper item order.
/// </summary>
private class ReferenceEnumerator : IEnumerator<ObjectReference>
public struct ReferenceEnumerator : IEnumerator<ObjectReference>
{
private readonly IEnumerator<NodeIndex> indexEnumerator;
private ReferenceEnumerable obj;
Expand Down

0 comments on commit 4140892

Please sign in to comment.