From c3abc4d057d38a8e04cca85681017bd2406625eb Mon Sep 17 00:00:00 2001 From: Dmytro Kyshchenko Date: Sat, 6 May 2023 11:52:38 +0300 Subject: [PATCH] #623 - Use shared instance of constants in ParameterCollection. --- .../Collections/ParameterCollection.cs | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/xFunc.Maths/Expressions/Collections/ParameterCollection.cs b/xFunc.Maths/Expressions/Collections/ParameterCollection.cs index 08cae56d5..e7f34faee 100644 --- a/xFunc.Maths/Expressions/Collections/ParameterCollection.cs +++ b/xFunc.Maths/Expressions/Collections/ParameterCollection.cs @@ -13,8 +13,16 @@ namespace xFunc.Maths.Expressions.Collections; /// public class ParameterCollection : IEnumerable, INotifyCollectionChanged { - private readonly Dictionary constants; + private static readonly Dictionary Constants; private readonly Dictionary collection; + private readonly bool withConstants; + + static ParameterCollection() + { + Constants = new Dictionary(); + + InitializeConstants(); + } /// /// Occurs when the collection changes. @@ -66,13 +74,11 @@ public ParameterCollection(IEnumerable? parameters, bool initConstant if (parameters is null) throw new ArgumentNullException(nameof(parameters)); - constants = new Dictionary(); collection = new Dictionary(); foreach (var item in parameters) collection.Add(item.Key, item); - if (initConstants) - InitializeConstants(); + withConstants = initConstants; } /// @@ -82,7 +88,7 @@ public ParameterCollection(IEnumerable? 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))); @@ -127,14 +133,20 @@ private void InitializeConstants() AddConstant(Parameter.Constant("γ", 0.57721566490153286060651)); } + private static void AddConstant(Parameter parameter) + => Constants.Add(parameter.Key, parameter); + /// IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); /// public IEnumerator 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; @@ -165,15 +177,12 @@ public ParameterValue this[string key] } } - 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)); @@ -264,5 +273,5 @@ public bool Contains(Parameter param) /// The name of variable. /// true if the object contains the specified key; otherwise, false. public bool ContainsKey(string key) - => collection.ContainsKey(key) || constants.ContainsKey(key); + => collection.ContainsKey(key); } \ No newline at end of file