Skip to content

Commit

Permalink
PT-346: Performance optimization: LINQless BlockConditionAndOr.IsSati…
Browse files Browse the repository at this point in the history
…sfiedBy (#179)
  • Loading branch information
akak1977 authored Mar 12, 2021
1 parent b178b62 commit 898d678
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/VirtoCommerce.CoreModule.Core/Conditions/BlockConditionAndOr.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Linq;
using System.Collections.Generic;
using VirtoCommerce.CoreModule.Core.Common;
using VirtoCommerce.Platform.Core.Common;

namespace VirtoCommerce.CoreModule.Core.Conditions
{
Expand All @@ -16,25 +15,44 @@ public override bool IsSatisfiedBy(IEvaluationContext context)
{
var result = false;

if (Children.IsNullOrEmpty())
{
return true;
}

if (Children != null && Children.Any())
if (Children != null && Children.Count > 0)
{
if (!Not)
{
result = All ? Children.All(ch => ch.IsSatisfiedBy(context)) : Children.Any(ch => ch.IsSatisfiedBy(context));
result = All ? AllSatisfied(Children, context) : AnySatisfied(Children, context);
}
else
{
result = All ? !Children.All(ch => ch.IsSatisfiedBy(context)) : !Children.Any(ch => ch.IsSatisfiedBy(context));
result = All ? !AllSatisfied(Children, context) : !AnySatisfied(Children, context);
}

}
else
{
result = true;
}

return result;
}

private bool AnySatisfied(IList<IConditionTree> children, IEvaluationContext context)
{
foreach (var ch in children)
{
if (ch.IsSatisfiedBy(context)) return true;
}
return false;
}

private bool AllSatisfied(IList<IConditionTree> children, IEvaluationContext context)
{
foreach (var ch in children)
{
if (!ch.IsSatisfiedBy(context)) return false;
}
return true;
}
}


}

0 comments on commit 898d678

Please sign in to comment.