Skip to content

DeepCompare(...): memoize array lengths #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ $CLIRoot = Join-Path $RepoRoot 'cli'
$DotNetExe = Join-Path $CLIRoot 'dotnet.exe'
$NuGetExe = Join-Path $RepoRoot '.nuget\nuget.exe'


if (Test-Path $ArtifactsDir)
{
rm -r $ArtifactsDir -Force | Out-Null
}

Function Error-Log {
param(
[string]$ErrorMessage,
Expand All @@ -31,8 +37,6 @@ Function Trace-Time() {

$Global:LastTraceTime = Get-Date

rm -r $ArtifactsDir -Force | Out-Null

New-Item -ItemType Directory -Force -Path $CLIRoot | Out-Null
New-Item -ItemType Directory -Force -Path $ArtifactsDir | Out-Null

Expand Down
10 changes: 6 additions & 4 deletions src/json-ld.net/Core/JsonLdUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,16 @@ public static bool DeepCompare(JToken v1, JToken v2, bool listOrderMatters)
{
JArray l1 = (JArray)v1;
JArray l2 = (JArray)v2;
if (l1.Count != l2.Count)
var l1Count = l1.Count;
var l2Count = l2.Count;
if (l1Count != l2Count)
{
return false;
}
// used to mark members of l2 that we have already matched to avoid
// matching the same item twice for lists that have duplicates
bool[] alreadyMatched = new bool[l2.Count];
for (int i = 0; i < l1.Count; i++)
bool[] alreadyMatched = new bool[l2Count];
for (int i = 0; i < l1Count; i++)
{
JToken o1 = l1[i];
bool gotmatch = false;
Expand All @@ -105,7 +107,7 @@ public static bool DeepCompare(JToken v1, JToken v2, bool listOrderMatters)
}
else
{
for (int j = 0; j < l2.Count; j++)
for (int j = 0; j < l2Count; j++)
{
if (!alreadyMatched[j] && DeepCompare(o1, l2[j], listOrderMatters))
{
Expand Down