From 9b41e2230cd75d6a11f1b012eedf3266eec2ceb7 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Sun, 7 Nov 2021 13:04:40 +0300 Subject: [PATCH] some changes --- DTLib.csproj | 2 + DefaultLogger.cs | 16 ++++-- FrameworkFix.cs | 14 ------ MyDict.cs | 127 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 140 insertions(+), 19 deletions(-) create mode 100644 MyDict.cs diff --git a/DTLib.csproj b/DTLib.csproj index 069cfa3..ea97a15 100644 --- a/DTLib.csproj +++ b/DTLib.csproj @@ -35,6 +35,8 @@ + + diff --git a/DefaultLogger.cs b/DefaultLogger.cs index 35bacd2..a53241f 100644 --- a/DefaultLogger.cs +++ b/DefaultLogger.cs @@ -5,19 +5,25 @@ using DTLib.Filesystem; namespace DTLib { // вывод лога в консоль и файл - public static class DefaultLogger + public class DefaultLogger { - public static void SetLogfile(string dir, string programName) + public DefaultLogger() { } + public DefaultLogger(string logfile) => Logfile = logfile; + public DefaultLogger(string dir, string programName) => SetLogfile(dir, programName); + + public void SetLogfile(string dir, string programName) => Logfile = $"{dir}\\{programName}_{DateTime.Now}.log".Replace(':', '-').Replace(' ', '_'); - public static string Logfile { get; set; } - public static void Log(params string[] msg) + public string Logfile { get; set; } + + public void Log(params string[] msg) { if (msg.Length == 1) msg[0] = "[" + DateTime.Now.ToString() + "]: " + msg[0]; else msg[1] = "[" + DateTime.Now.ToString() + "]: " + msg[1]; LogNoTime(msg); } - public static void LogNoTime(params string[] msg) + + public void LogNoTime(params string[] msg) { lock (Logfile) { diff --git a/FrameworkFix.cs b/FrameworkFix.cs index 4188646..f155dc4 100644 --- a/FrameworkFix.cs +++ b/FrameworkFix.cs @@ -215,20 +215,6 @@ namespace DTLib if (condition) if_true(input); } - public static T IfIsNull(this T input, Func if_true, Func if_false) => - input is null ? if_true() : if_false(); - public static void IfIsNull(this T input, Action if_true, Action if_false) - { - if (input is null) if_true(); - else if_false(); - } - public static T IfNull(this T input, Func if_true) => - input is null ? if_true() : input; - public static void IfIsNull(this T input, Action if_true) - { - if (input is null) if_true(); - } - public static string AddZeroes(this T number, int length) { var str = number.ToString(); diff --git a/MyDict.cs b/MyDict.cs new file mode 100644 index 0000000..9f2c720 --- /dev/null +++ b/MyDict.cs @@ -0,0 +1,127 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; + +namespace DTLib +{ + public class MyDict + { + object locker = new object(); + List values; + List keys; + List hashes; + int count; + + public int Count + { + get + { + // lock (count) + lock (locker) return count; + } + } + + public ReadOnlyCollection Values + { + get + { + ReadOnlyCollectionBuilder b; + lock (locker) b = new(values); + return b.ToReadOnlyCollection(); + } + } + + public ReadOnlyCollection Keys + { + get + { + ReadOnlyCollectionBuilder b; + lock (locker) b = new(keys); + return b.ToReadOnlyCollection(); + } + } + + public MyDict() + { + + values = new(); + keys = new(); + hashes = new(); + count = 0; + } + + public MyDict(IList _keys, IList _values) + { + if (_keys.Count != _values.Count) throw new Exception("_keys.Count != _values.Count"); + keys = (List)_keys; + values = (List)_values; + count = _keys.Count; + hashes = new(); + for (int i = 0; i < count; i++) + hashes.Add(keys[i].GetHashCode()); + } + + public TVal this[TKey key] + { + get + { + lock (locker) return values[hashes.IndexOf(key.GetHashCode())]; + } + set + { + lock (locker) values[hashes.IndexOf(key.GetHashCode())] = value; + } + } + + public (TKey, TVal) GetByIndex(int index) + { + (TKey k, TVal v) output; + lock (locker) + { + output.k = keys[index]; + output.v = values[index]; + } + return output; + } + + public void Add(TKey key, TVal val) + { + // lock (keys) lock (values) lock (count) + lock (locker) + { + keys.Add(key); + values.Add(val); + hashes.Add(key.GetHashCode()); + count++; + } + } + + public void Remove(TKey key) + { + var hash = key.GetHashCode(); + lock (locker) + { + var num = hashes.IndexOf(hash); + keys.RemoveAt(num); + values.RemoveAt(num); + hashes.RemoveAt(num); + count--; + } + } + + public void Clear() + { + lock (locker) + { + hashes.Clear(); + keys.Clear(); + values.Clear(); + count = 0; + } + } + } +}