Skip to content

Commit

Permalink
Updated code formatting to match rules in .editorconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Jun 8, 2024
1 parent 5f714db commit c2f3504
Showing 1 changed file with 98 additions and 98 deletions.
196 changes: 98 additions & 98 deletions src/Hyperbee.Json/JsonPathBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ public JsonPathBuilder( JsonElement rootElement )
}

// OPTIMIZED TO USE SEGMENT DICTIONARY CACHE

public string GetPath( JsonElement targetElement )
{
// quick out

var targetId = GetIdx( targetElement );

if ( _parentMap.ContainsKey( targetId ) )
return BuildPath( targetId, _parentMap );

// take a walk

var stack = new Stack<JsonElement>( [_rootElement] );

while ( stack.Count > 0 )
{
var currentElement = stack.Pop();
var elementId = GetIdx( currentElement );

if ( _comparer.Equals( currentElement, targetElement ) )
return BuildPath( elementId, _parentMap );

Expand All @@ -54,10 +54,10 @@ public string GetPath( JsonElement targetElement )
foreach ( var property in currentElement.EnumerateObject() )
{
var childElementId = GetIdx( property.Value );

if ( !_parentMap.ContainsKey( childElementId ) )
_parentMap[childElementId] = (elementId, $".{property.Name}");

stack.Push( property.Value );
}
break;
Expand All @@ -70,7 +70,7 @@ public string GetPath( JsonElement targetElement )

if ( !_parentMap.ContainsKey( childElementId ) )
_parentMap[childElementId] = (elementId, $"[{arrayIdx}]");

stack.Push( element );
arrayIdx++;
}
Expand Down Expand Up @@ -100,116 +100,116 @@ private static string BuildPath( int elementId, Dictionary<int, (int parentId, s

return string.Join( string.Empty, pathSegments );
}

/*
#if USE_OPTIMIZED

// OPTIMIZED TO USE SEGMENT DICTIONARY
/*
#if USE_OPTIMIZED
// avoid allocating full paths for every node by building a dictionary
// of (parentId, segment) pairs.
// OPTIMIZED TO USE SEGMENT DICTIONARY
public string GetPath( JsonElement targetElement )
{
var stack = new Stack<(int elementId, JsonElement element)>();
var parentMap = new Dictionary<int, (int parentId, string segment)>();
var currentId = 0;
// avoid allocating full paths for every node by building a dictionary
// of (parentId, segment) pairs.
stack.Push( (currentId, _rootElement) );
parentMap[currentId] = (-1, "$");
currentId++;
while ( stack.Count > 0 )
public string GetPath( JsonElement targetElement )
{
var (elementId, currentElement) = stack.Pop();
var stack = new Stack<(int elementId, JsonElement element)>();
var parentMap = new Dictionary<int, (int parentId, string segment)>();
var currentId = 0;
if ( _comparer.Equals( currentElement, targetElement ) )
return BuildPath( elementId, parentMap );
stack.Push( (currentId, _rootElement) );
parentMap[currentId] = (-1, "$");
currentId++;
switch ( currentElement.ValueKind )
while ( stack.Count > 0 )
{
case JsonValueKind.Object:
foreach ( var property in currentElement.EnumerateObject() )
{
var childElementId = currentId++;
parentMap[childElementId] = (elementId, $".{property.Name}");
stack.Push( (childElementId, property.Value) );
}
break;
case JsonValueKind.Array:
var arrayIdx = 0;
foreach ( var element in currentElement.EnumerateArray() )
{
var childElementId = currentId++;
parentMap[childElementId] = (elementId, $"[{arrayIdx}]");
stack.Push( (childElementId, element) );
arrayIdx++;
}
break;
var (elementId, currentElement) = stack.Pop();
if ( _comparer.Equals( currentElement, targetElement ) )
return BuildPath( elementId, parentMap );
switch ( currentElement.ValueKind )
{
case JsonValueKind.Object:
foreach ( var property in currentElement.EnumerateObject() )
{
var childElementId = currentId++;
parentMap[childElementId] = (elementId, $".{property.Name}");
stack.Push( (childElementId, property.Value) );
}
break;
case JsonValueKind.Array:
var arrayIdx = 0;
foreach ( var element in currentElement.EnumerateArray() )
{
var childElementId = currentId++;
parentMap[childElementId] = (elementId, $"[{arrayIdx}]");
stack.Push( (childElementId, element) );
arrayIdx++;
}
break;
}
}
return null; // Target not found
}
return null; // Target not found
}
private static string BuildPath( int elementId, Dictionary<int, (int parentId, string segment)> parentMap )
{
var pathSegments = new Stack<string>();
var currentId = elementId;
private static string BuildPath( int elementId, Dictionary<int, (int parentId, string segment)> parentMap )
{
var pathSegments = new Stack<string>();
var currentId = elementId;
while ( currentId != -1 )
{
var (parentId, segment) = parentMap[currentId];
pathSegments.Push( segment );
currentId = parentId;
}
while ( currentId != -1 )
{
var (parentId, segment) = parentMap[currentId];
pathSegments.Push( segment );
currentId = parentId;
return string.Join( string.Empty, pathSegments );
}
return string.Join( string.Empty, pathSegments );
}
#else
#else
// NAIVE IMPLEMENTATION
public string GetPath( JsonElement targetElement )
{
var stack = new Stack<(JsonElement element, string path)>( 4 );
stack.Push( (_rootElement, "$") );
// NAIVE IMPLEMENTATION
while ( stack.Count > 0 )
public string GetPath( JsonElement targetElement )
{
var (currentElement, currentPath) = stack.Pop();
if ( _comparer.Equals( currentElement, targetElement ) )
return currentPath;
var stack = new Stack<(JsonElement element, string path)>( 4 );
stack.Push( (_rootElement, "$") );
switch ( currentElement.ValueKind )
while ( stack.Count > 0 )
{
case JsonValueKind.Object:
foreach ( var property in currentElement.EnumerateObject() )
{
var newPath = $"{currentPath}.{property.Name}";
stack.Push( (property.Value, newPath) );
}
break;
case JsonValueKind.Array:
var index = 0;
foreach ( var element in currentElement.EnumerateArray() )
{
var newPath = $"{currentPath}[{index++}]";
stack.Push( (element, newPath) );
}
break;
var (currentElement, currentPath) = stack.Pop();
if ( _comparer.Equals( currentElement, targetElement ) )
return currentPath;
switch ( currentElement.ValueKind )
{
case JsonValueKind.Object:
foreach ( var property in currentElement.EnumerateObject() )
{
var newPath = $"{currentPath}.{property.Name}";
stack.Push( (property.Value, newPath) );
}
break;
case JsonValueKind.Array:
var index = 0;
foreach ( var element in currentElement.EnumerateArray() )
{
var newPath = $"{currentPath}[{index++}]";
stack.Push( (element, newPath) );
}
break;
}
}
}
return null; // Target no
}
return null; // Target no
}
#endif
*/
#endif
*/
}

0 comments on commit c2f3504

Please sign in to comment.