From 295498ae048aa5e56f245976dd3e82bfcf69f36b Mon Sep 17 00:00:00 2001 From: Ogoun Date: Sat, 14 Jan 2023 20:57:02 +0300 Subject: [PATCH] Fix build Configuration --- ZeroLevel/Services/Cache/LRUCache.cs | 122 ++++++++++++++++++ .../Implementation/CommandLineReader.cs | 2 +- .../Reflection/StringToTypeConverter.cs | 2 +- ZeroLevel/Services/Reflection/TypeHelpers.cs | 20 ++- ZeroLevel/ZeroLevel.csproj | 8 +- 5 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 ZeroLevel/Services/Cache/LRUCache.cs diff --git a/ZeroLevel/Services/Cache/LRUCache.cs b/ZeroLevel/Services/Cache/LRUCache.cs new file mode 100644 index 0000000..24db5d9 --- /dev/null +++ b/ZeroLevel/Services/Cache/LRUCache.cs @@ -0,0 +1,122 @@ +using System.Collections.Generic; + +namespace ZeroLevel.Services.Cache +{ + /// + /// A least-recently-used cache stored like a dictionary. + /// + /// The type of the key to the cached item. + /// The type of the cached item. + public sealed class LRUCache + { + /// + /// Default maximum number of elements to cache. + /// + private const int DefaultCapacity = 255; + + private readonly object _lockObj = new object(); + private readonly int _capacity; + private readonly Dictionary _cacheMap; + private readonly LinkedList _cacheList; + + /// + /// Initializes a new instance of the class. + /// + public LRUCache() + : this(DefaultCapacity) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// Maximum number of elements to cache. + public LRUCache(int capacity) + { + _capacity = capacity > 0 ? capacity : DefaultCapacity; + _cacheMap = new Dictionary(); + _cacheList = new LinkedList(); + } + + /// + /// Gets the value associated with the specified key. + /// + /// The key of the value to get. + /// When this method returns, contains the value associated with + /// the specified key, if the key is found; otherwise, the default value for the + /// type of the parameter. + /// true if contains an element with the specified key; otherwise, false. + public bool TryGet(TKey key, out TValue value) + { + lock (_lockObj) + { + if (_cacheMap.TryGetValue(key, out var entry)) + { + Touch(entry.Node); + value = entry.Value; + return true; + } + } + + value = default(TValue); + return false; + } + + /// + /// Adds the specified key and value to the cache. + /// + /// The key of the element to add. + /// The value of the element to add. + public void Set(TKey key, TValue value) + { + lock (_lockObj) + { + if (!_cacheMap.TryGetValue(key, out var entry)) + { + LinkedListNode node; + if (_cacheMap.Count >= _capacity) + { + node = _cacheList.Last; + _cacheMap.Remove(node.Value); + _cacheList.RemoveLast(); + node.Value = key; + } + else + { + node = new LinkedListNode(key); + } + + _cacheList.AddFirst(node); + _cacheMap.Add(key, new Entry(node, value)); + } + else + { + entry.Value = value; + _cacheMap[key] = entry; + Touch(entry.Node); + } + } + } + + private void Touch(LinkedListNode node) + { + if (node != _cacheList.First) + { + _cacheList.Remove(node); + _cacheList.AddFirst(node); + } + } + + private struct Entry + { + public LinkedListNode Node; + public TValue Value; + + public Entry(LinkedListNode node, TValue value) + { + this.Node = node; + this.Value = value; + } + } + } +} diff --git a/ZeroLevel/Services/Config/Implementation/CommandLineReader.cs b/ZeroLevel/Services/Config/Implementation/CommandLineReader.cs index 83db33e..44e8ebc 100644 --- a/ZeroLevel/Services/Config/Implementation/CommandLineReader.cs +++ b/ZeroLevel/Services/Config/Implementation/CommandLineReader.cs @@ -27,7 +27,7 @@ namespace ZeroLevel.Services.Config.Implementation string value; if (index >= 0) { - key = arg.Substring(0, index).TrimStart('/').Trim().ToLower(CultureInfo.CurrentCulture); + key = arg.Substring(0, index).TrimStart('-', '/').Trim().ToLower(CultureInfo.CurrentCulture); value = arg.Substring(index + 1, arg.Length - index - 1).Trim(' ', '"'); } else diff --git a/ZeroLevel/Services/Reflection/StringToTypeConverter.cs b/ZeroLevel/Services/Reflection/StringToTypeConverter.cs index fdce165..cd088b8 100644 --- a/ZeroLevel/Services/Reflection/StringToTypeConverter.cs +++ b/ZeroLevel/Services/Reflection/StringToTypeConverter.cs @@ -15,7 +15,7 @@ namespace ZeroLevel.Services.Reflection { try { - if (TypeHelpers.IsNumericTypeWithFloating(to)) + if (TypeHelpers.IsFloatingNumericType(to)) { input = input. Replace(",", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator). diff --git a/ZeroLevel/Services/Reflection/TypeHelpers.cs b/ZeroLevel/Services/Reflection/TypeHelpers.cs index 1fc203c..c8c674e 100644 --- a/ZeroLevel/Services/Reflection/TypeHelpers.cs +++ b/ZeroLevel/Services/Reflection/TypeHelpers.cs @@ -48,7 +48,25 @@ namespace ZeroLevel.Services.Reflection } } - public static bool IsNumericTypeWithFloating(Type type) + public static bool IsIntegerNumericType(Type type) + { + switch (Type.GetTypeCode(type)) + { + case TypeCode.Byte: + case TypeCode.SByte: + case TypeCode.UInt16: + case TypeCode.UInt32: + case TypeCode.UInt64: + case TypeCode.Int16: + case TypeCode.Int32: + case TypeCode.Int64: + return true; + default: + return false; + } + } + + public static bool IsFloatingNumericType(Type type) { switch (Type.GetTypeCode(type)) { diff --git a/ZeroLevel/ZeroLevel.csproj b/ZeroLevel/ZeroLevel.csproj index 614a07d..4a4d4c5 100644 --- a/ZeroLevel/ZeroLevel.csproj +++ b/ZeroLevel/ZeroLevel.csproj @@ -6,16 +6,16 @@ ogoun ogoun - 3.3.8.6 - Partition storage. Fixes bugs related to multithreading. + 3.3.8.7 + Fix build configuration from commandf line args. https://github.com/ogoun/Zero/wiki Copyright Ogoun 2022 https://github.com/ogoun/Zero git - 3.3.8.6 - 3.3.8.6 + 3.3.8.7 + 3.3.8.7 AnyCPU;x64;x86 zero.png full