Skip to content

Commit

Permalink
#623 - Use shared instance of constants in ParameterCollection.
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed May 6, 2023
1 parent 48e341a commit c3abc4d
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions xFunc.Maths/Expressions/Collections/ParameterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ namespace xFunc.Maths.Expressions.Collections;
/// </summary>
public class ParameterCollection : IEnumerable<Parameter>, INotifyCollectionChanged
{
private readonly Dictionary<string, Parameter> constants;
private static readonly Dictionary<string, Parameter> Constants;
private readonly Dictionary<string, Parameter> collection;
private readonly bool withConstants;

static ParameterCollection()
{
Constants = new Dictionary<string, Parameter>();

InitializeConstants();
}

/// <summary>
/// Occurs when the collection changes.
Expand Down Expand Up @@ -66,13 +74,11 @@ public ParameterCollection(IEnumerable<Parameter>? parameters, bool initConstant
if (parameters is null)
throw new ArgumentNullException(nameof(parameters));

constants = new Dictionary<string, Parameter>();
collection = new Dictionary<string, Parameter>();
foreach (var item in parameters)
collection.Add(item.Key, item);

if (initConstants)
InitializeConstants();
withConstants = initConstants;
}

/// <summary>
Expand All @@ -82,7 +88,7 @@ public ParameterCollection(IEnumerable<Parameter>? parameters, bool initConstant
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
=> CollectionChanged?.Invoke(this, args);

private void InitializeConstants()
private static void InitializeConstants()
{
// Archimedes' constant
AddConstant(Parameter.Constant("π", AngleValue.Radian(Math.PI)));
Expand Down Expand Up @@ -127,14 +133,20 @@ private void InitializeConstants()
AddConstant(Parameter.Constant("γ", 0.57721566490153286060651));
}

private static void AddConstant(Parameter parameter)
=> Constants.Add(parameter.Key, parameter);

/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

/// <inheritdoc />
public IEnumerator<Parameter> GetEnumerator()
{
foreach (var (_, item) in constants)
yield return item;
if (withConstants)
{
foreach (var (_, item) in Constants)
yield return item;
}

foreach (var (_, item) in collection)
yield return item;
Expand Down Expand Up @@ -165,15 +177,12 @@ public IEnumerator<Parameter> GetEnumerator()
}
}

private void AddConstant(Parameter parameter)
=> constants.Add(parameter.Key, parameter);

private Parameter GetParameterByKey(string key)
{
if (collection.TryGetValue(key, out var param))
return param;

if (constants.TryGetValue(key, out param))
if (withConstants && Constants.TryGetValue(key, out param))
return param;

throw new KeyNotFoundException(string.Format(CultureInfo.InvariantCulture, Resource.VariableNotFoundExceptionError, key));
Expand Down Expand Up @@ -264,5 +273,5 @@ public bool Contains(Parameter param)
/// <param name="key">The name of variable.</param>
/// <returns><c>true</c> if the object contains the specified key; otherwise, <c>false</c>.</returns>
public bool ContainsKey(string key)
=> collection.ContainsKey(key) || constants.ContainsKey(key);
=> collection.ContainsKey(key);
}

0 comments on commit c3abc4d

Please sign in to comment.