some changes

This commit is contained in:
Timerix22 2021-11-07 13:04:40 +03:00
parent 63a3c4eef6
commit 9b41e2230c
4 changed files with 140 additions and 19 deletions

View File

@ -35,6 +35,8 @@
<Compile Include="Color.cs" /> <Compile Include="Color.cs" />
<Compile Include="CompressedArray.cs" /> <Compile Include="CompressedArray.cs" />
<Compile Include="DefaultLogger.cs" /> <Compile Include="DefaultLogger.cs" />
<Compile Include="Filesystem\Symlink.cs" />
<Compile Include="MyDict.cs" />
<Compile Include="Dtsod\DtsodV21.cs" /> <Compile Include="Dtsod\DtsodV21.cs" />
<Compile Include="Dtsod\DtsodV22.cs" /> <Compile Include="Dtsod\DtsodV22.cs" />
<Compile Include="Dtsod\V30\DtsodSerializableAttribute.cs" /> <Compile Include="Dtsod\V30\DtsodSerializableAttribute.cs" />

View File

@ -5,19 +5,25 @@ using DTLib.Filesystem;
namespace DTLib 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(' ', '_'); => Logfile = $"{dir}\\{programName}_{DateTime.Now}.log".Replace(':', '-').Replace(' ', '_');
public static string Logfile { get; set; } public string Logfile { get; set; }
public static void Log(params string[] msg)
public void Log(params string[] msg)
{ {
if (msg.Length == 1) msg[0] = "[" + DateTime.Now.ToString() + "]: " + msg[0]; if (msg.Length == 1) msg[0] = "[" + DateTime.Now.ToString() + "]: " + msg[0];
else msg[1] = "[" + DateTime.Now.ToString() + "]: " + msg[1]; else msg[1] = "[" + DateTime.Now.ToString() + "]: " + msg[1];
LogNoTime(msg); LogNoTime(msg);
} }
public static void LogNoTime(params string[] msg)
public void LogNoTime(params string[] msg)
{ {
lock (Logfile) lock (Logfile)
{ {

View File

@ -215,20 +215,6 @@ namespace DTLib
if (condition) if_true(input); if (condition) if_true(input);
} }
public static T IfIsNull<T>(this T input, Func<T> if_true, Func<T> if_false) =>
input is null ? if_true() : if_false();
public static void IfIsNull<T>(this T input, Action if_true, Action if_false)
{
if (input is null) if_true();
else if_false();
}
public static T IfNull<T>(this T input, Func<T> if_true) =>
input is null ? if_true() : input;
public static void IfIsNull<T>(this T input, Action if_true)
{
if (input is null) if_true();
}
public static string AddZeroes<T>(this T number, int length) public static string AddZeroes<T>(this T number, int length)
{ {
var str = number.ToString(); var str = number.ToString();

127
MyDict.cs Normal file
View File

@ -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<TKey, TVal>
{
object locker = new object();
List<TVal> values;
List<TKey> keys;
List<int> hashes;
int count;
public int Count
{
get
{
// lock (count)
lock (locker) return count;
}
}
public ReadOnlyCollection<TVal> Values
{
get
{
ReadOnlyCollectionBuilder<TVal> b;
lock (locker) b = new(values);
return b.ToReadOnlyCollection();
}
}
public ReadOnlyCollection<TKey> Keys
{
get
{
ReadOnlyCollectionBuilder<TKey> b;
lock (locker) b = new(keys);
return b.ToReadOnlyCollection();
}
}
public MyDict()
{
values = new();
keys = new();
hashes = new();
count = 0;
}
public MyDict(IList<TKey> _keys, IList<TVal> _values)
{
if (_keys.Count != _values.Count) throw new Exception("_keys.Count != _values.Count");
keys = (List<TKey>)_keys;
values = (List<TVal>)_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;
}
}
}
}