From e01ce9d35c763a3f3002d3b23f053b4274ab37c2 Mon Sep 17 00:00:00 2001 From: b3b00 Date: Thu, 8 Sep 2022 08:55:08 +0200 Subject: [PATCH 01/12] generator : initial --- .../DictionaryGenerator.csproj | 10 ++ DictionaryGenerator/Generator.cs | 134 ++++++++++++++++++ MultiDimensionDictionary.sln | 6 + .../MultiDimensionDictionary3.cs | 2 + 4 files changed, 152 insertions(+) create mode 100644 DictionaryGenerator/DictionaryGenerator.csproj create mode 100644 DictionaryGenerator/Generator.cs diff --git a/DictionaryGenerator/DictionaryGenerator.csproj b/DictionaryGenerator/DictionaryGenerator.csproj new file mode 100644 index 0000000..b9de063 --- /dev/null +++ b/DictionaryGenerator/DictionaryGenerator.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/DictionaryGenerator/Generator.cs b/DictionaryGenerator/Generator.cs new file mode 100644 index 0000000..392e5ca --- /dev/null +++ b/DictionaryGenerator/Generator.cs @@ -0,0 +1,134 @@ +using System.Text; + +namespace DictionaryGenerator; + +public class Generator +{ + + public static string GenerateTypeParameters(int start, int count, bool withValue = true) + { + StringBuilder builder = new StringBuilder(); + builder.Append("<"); + for (int i = start; i < count + 1; i++) + { + builder.Append($"K{i}"); + if (i < count && withValue) + { + builder.Append(", "); + } + } + + if (withValue) + { + builder.Append(",V"); + } + builder.Append(">"); + return builder.ToString(); + } + + public static string GenerateParametersDeclaration(int start, int count) + { + StringBuilder builder = new StringBuilder(); + + for (int i = start; i < count + 1; i++) + { + builder.Append($"K{i} k{i}"); + if (i < count) + { + builder.Append(", "); + } + } + + return builder.ToString(); + } + + public static string GenerateParameters(int start, int count) + { + StringBuilder builder = new StringBuilder(); + + for (int i = start; i < count + 1; i++) + { + builder.Append($"k{i}"); + if (i < count) + { + builder.Append(", "); + } + } + + return builder.ToString(); + } + + public static string GenerateHeader(int count) + { + var header = $@"public class MultiDimensionalDictionary<{GenerateTypeParameters(1, count)}> + {{ + protected ConcurrentDictionary Data MultiDimensionalDictionary<{GenerateTypeParameters(2, count)}> {{ get;set; }} + + public MultiDimensionalDictionary() : base() + {{ + Data = new ConcurrentDictionary>(); + }}"; + return header; + } + + public static string GenerateContains(int count) + { + StringBuilder builder = new StringBuilder(); + for (int i = 1; i < count + 1; i++) + { + builder.AppendLine(GenerateContainsKey(count, i)); + builder.AppendLine(); + } + + return builder.ToString(); + } + + + + public static string GenerateContainsKey(int count, int level) + { + if (level == 1) + { + return "public bool ContainsKey(K1 k1) => Data.ContainsKey(k1);"; + } + else + { + return + $"public bool ContainsKey({GenerateParametersDeclaration(1, level)}) => Data.ContainsKey(k1) && Data[k1].ContainsKey({GenerateParameters(level, count-level)})"; + } + } + + public static string GenerategetKeys(int count) + { + var get = $@"public List<({GenerateTypeParameters(1,count)})> GetKeys() + {{ + List<({GenerateTypeParameters(1,count)})> keys = new List<({GenerateTypeParameters(1,count)})>(); + foreach (var kvp in Data) + {{ + List<(K2, K3)> subkeys = kvp.Value.GetKeys(); + foreach (var subkey in subkeys) + {{ + keys.Add((kvp.Key, subkey.Item1, subkey.Item2)); + }} + }} + return keys; +}}"; + return get; + } + + + public static string Generate(int count) + { + StringBuilder builder = new StringBuilder(); + builder.AppendLine(GenerateHeader(count)) + .AppendLine() + .AppendLine(GenerateContains(count)) + .AppendLine() + .AppendLine(GenerategetKeys(count)) + .AppendLine() + .AppendLine("}"); + + return builder.ToString(); + } + +} \ No newline at end of file diff --git a/MultiDimensionDictionary.sln b/MultiDimensionDictionary.sln index 68b5982..0da8f93 100644 --- a/MultiDimensionDictionary.sln +++ b/MultiDimensionDictionary.sln @@ -6,6 +6,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTests", "ConsoleTest EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MultiDimensionTests", "MultiDimensionTests\MultiDimensionTests.csproj", "{4DEEF201-423C-45EF-9FA9-BCF596DC8BCF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DictionaryGenerator", "DictionaryGenerator\DictionaryGenerator.csproj", "{D0CB6C84-4C66-47F5-98DE-F7CF2ACD1E3E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -24,5 +26,9 @@ Global {4DEEF201-423C-45EF-9FA9-BCF596DC8BCF}.Debug|Any CPU.Build.0 = Debug|Any CPU {4DEEF201-423C-45EF-9FA9-BCF596DC8BCF}.Release|Any CPU.ActiveCfg = Release|Any CPU {4DEEF201-423C-45EF-9FA9-BCF596DC8BCF}.Release|Any CPU.Build.0 = Release|Any CPU + {D0CB6C84-4C66-47F5-98DE-F7CF2ACD1E3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D0CB6C84-4C66-47F5-98DE-F7CF2ACD1E3E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D0CB6C84-4C66-47F5-98DE-F7CF2ACD1E3E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D0CB6C84-4C66-47F5-98DE-F7CF2ACD1E3E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary3.cs b/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary3.cs index 9edc339..86e7375 100644 --- a/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary3.cs +++ b/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary3.cs @@ -16,6 +16,8 @@ public MultiDimensionalDictionary() : base() public bool ContainsKey(K1 k1) => Data.ContainsKey(k1); + public bool ContainsKey(K1 k1, K2 k2) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2); + public bool ContainsKey(K1 k1, K2 k2, K3 k3) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3); public List<(K1, K2, K3)> GetKeys() From 6f7e07df9875f854c85ac9b6eb794f78c7d09655 Mon Sep 17 00:00:00 2001 From: b3b00 Date: Thu, 8 Sep 2022 11:18:03 +0200 Subject: [PATCH 02/12] wip --- DictionaryGenerator/Generator.cs | 174 +++++++++++++++++++++++++++---- DictionaryGenerator/Program.cs | 21 ++++ 2 files changed, 174 insertions(+), 21 deletions(-) create mode 100644 DictionaryGenerator/Program.cs diff --git a/DictionaryGenerator/Generator.cs b/DictionaryGenerator/Generator.cs index 392e5ca..f38e05a 100644 --- a/DictionaryGenerator/Generator.cs +++ b/DictionaryGenerator/Generator.cs @@ -8,11 +8,11 @@ public class Generator public static string GenerateTypeParameters(int start, int count, bool withValue = true) { StringBuilder builder = new StringBuilder(); - builder.Append("<"); + for (int i = start; i < count + 1; i++) { builder.Append($"K{i}"); - if (i < count && withValue) + if (i < count) { builder.Append(", "); } @@ -20,9 +20,8 @@ public static string GenerateTypeParameters(int start, int count, bool withValue if (withValue) { - builder.Append(",V"); - } - builder.Append(">"); + builder.Append(", V"); + } return builder.ToString(); } @@ -42,7 +41,7 @@ public static string GenerateParametersDeclaration(int start, int count) return builder.ToString(); } - public static string GenerateParameters(int start, int count) + public static string GenerateParameters(int start, int count, bool withValue = false) { StringBuilder builder = new StringBuilder(); @@ -55,18 +54,36 @@ public static string GenerateParameters(int start, int count) } } + if (withValue) + { + builder.Append(", value"); + } return builder.ToString(); } - public static string GenerateHeader(int count) + public static string GenerateUsingsAndNamespace() { - var header = $@"public class MultiDimensionalDictionary<{GenerateTypeParameters(1, count)}> + var header = $@" + +using System.Collections.Concurrent; +using System.Collections.Generic; +using multiDimensionalDictionary; + +namespace multiDimensionalDictionary {{"; + return header; + } + + public static string GenerateClassDeclarationGenerateHeader(int count) + { + var header = $@" + +public class Multi<{GenerateTypeParameters(1, count)}> {{ - protected ConcurrentDictionary Data MultiDimensionalDictionary<{GenerateTypeParameters(2, count)}> {{ get;set; }} + protected ConcurrentDictionary> Data{{ get;set; }} - public MultiDimensionalDictionary() : base() + public Multi() : base() {{ - Data = new ConcurrentDictionary>(); + Data = new ConcurrentDictionary>(); }}"; return header; } @@ -94,22 +111,33 @@ public static string GenerateContainsKey(int count, int level) else { return - $"public bool ContainsKey({GenerateParametersDeclaration(1, level)}) => Data.ContainsKey(k1) && Data[k1].ContainsKey({GenerateParameters(level, count-level)})"; + $"public bool ContainsKey({GenerateParametersDeclaration(1, level)}) => Data.ContainsKey(k1) && Data[k1].ContainsKey({GenerateParameters(2, level)});"; } } - public static string GenerategetKeys(int count) + public static string GenerateGetKeys(int count) { - var get = $@"public List<({GenerateTypeParameters(1,count)})> GetKeys() + var get = $@"public List<({GenerateTypeParameters(1, count, false)})> GetKeys() {{ - List<({GenerateTypeParameters(1,count)})> keys = new List<({GenerateTypeParameters(1,count)})>(); + List<({GenerateTypeParameters(1, count, false)})> keys = new List<({GenerateTypeParameters(1, count, false)})>(); foreach (var kvp in Data) {{ - List<(K2, K3)> subkeys = kvp.Value.GetKeys(); + List<({GenerateTypeParameters(2, count, false)})> subkeys = kvp.Value.GetKeys(); foreach (var subkey in subkeys) {{ - keys.Add((kvp.Key, subkey.Item1, subkey.Item2)); - }} + keys.Add((kvp.Key, "; + for (int i = 1; i < count; i++) + { + get += $"subkey.Item{i}"; + if (i < count-1) + { + get += ", "; + } + } + + get += "));"; + + get+=$@"}} }} return keys; }}"; @@ -117,17 +145,121 @@ public static string GenerategetKeys(int count) } - public static string Generate(int count) + public static string GenerateAssert(string type, string name) + { + return $@"DictionaryAssertions.AssertNotNull<{type}>({name}, ""{name}"");"; + } + + public static string GenerateAsserts(int count, bool withValue = true) { StringBuilder builder = new StringBuilder(); - builder.AppendLine(GenerateHeader(count)) + for (int i = 1; i < count + 1; i++) + { + builder.AppendLine(GenerateAssert($"K{i}", $"k{i}")); + builder.AppendLine(); + } + + if (withValue) + { + builder.AppendLine(GenerateAssert($"V", "value")); + } + builder.AppendLine(); + return builder.ToString(); + } + + public static string GeneratePut(int count) + { + var put = $@"public void Put({GenerateParametersDeclaration(1, count)}, V value) + {{ + + {GenerateAsserts(count)} + + var secondDimentionData = new Multi<{GenerateTypeParameters(2,count,true)}>(); + + if (Data.ContainsKey(k1)) + {{ + secondDimentionData = Data[k1]; + }} + + secondDimentionData.Put({GenerateParameters(2,count,true)}); + + Data[k1] = secondDimentionData; + }}"; + return put; + } + + public static string GenerateGet(int count, int level) + { + if (level == count) + { + var get = $@"public V Get({GenerateParametersDeclaration(1, level)}) + {{ +{GenerateAsserts(count, false)} + + if (Data.TryGetValue(k1, out Multi<{GenerateTypeParameters(2, count, true)}> secondDimensionData)) + {{ + return secondDimensionData.Get({GenerateParameters(level-1, count, false)}); + }} + + throw new KeyNotFoundException(); + }}"; + return get; + } + + if (level == 1) + { + return $@"public Multi<{GenerateTypeParameters(2,count, true)}> Get(K1 k1) + {{ + return Data[k1]; + }}"; + } + + return $@"public Multi<{GenerateTypeParameters(level+1,count,true)}> Get({GenerateParametersDeclaration(1,level)}) + {{ + return Data[k1].Get({GenerateParameters(level,count-1,false)}); + }}"; + } + + public static string GenerateGets(int count) + { + StringBuilder builder = new StringBuilder(); + for (int i = 1; i < count + 1; i++) + { + builder.AppendLine(GenerateGet(count, i)); + builder.AppendLine(); + } + + return builder.ToString(); + } + + + public static string GenerateClass(int count) + { + StringBuilder builder = new StringBuilder(); + + builder.AppendLine(GenerateClassDeclarationGenerateHeader(count)) .AppendLine() .AppendLine(GenerateContains(count)) .AppendLine() - .AppendLine(GenerategetKeys(count)) + .AppendLine(GenerateGetKeys(count)) .AppendLine() + .AppendLine(GeneratePut(count)) + .AppendLine() + .AppendLine(GenerateGets(count)) .AppendLine("}"); + return builder.ToString(); + } + + public static string Generate(int count) + { + StringBuilder builder = new StringBuilder(); + builder + .AppendLine(GenerateUsingsAndNamespace()) + .AppendLine(GenerateClass(count)) + .AppendLine("}"); + + return builder.ToString(); } diff --git a/DictionaryGenerator/Program.cs b/DictionaryGenerator/Program.cs new file mode 100644 index 0000000..cca186f --- /dev/null +++ b/DictionaryGenerator/Program.cs @@ -0,0 +1,21 @@ +// See https://aka.ms/new-console-template for more information + +using System.Text; +using DictionaryGenerator; + +for (int i = 2; i <= 6; i++) +{ + + string g = Generator.Generate(i); + + File.WriteAllText( + $@"C:\Users\olduh\dev\MultiDimensionDictionary\MultiDimensionDictionary\multiDimentsionDictionary\multi{i}.cs", g); +} +// g = Generator.GenerateGet(3, 2); +// Console.WriteLine(g); +// g = Generator.GenerateGet(3, 3); +// Console.WriteLine(g); + + + + From f0db0e38928fa18e833dba68b61c9e05c912a970 Mon Sep 17 00:00:00 2001 From: b3b00 Date: Thu, 8 Sep 2022 11:22:28 +0200 Subject: [PATCH 03/12] . --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index f5e07c3..705d05d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ **/Bin/* **/obj/* **/Obj/* +**/*.user +**/lcov.info From aac466eac6f34779a82ab19c8272088d5ddcbd8c Mon Sep 17 00:00:00 2001 From: b3b00 Date: Thu, 8 Sep 2022 13:02:24 +0200 Subject: [PATCH 04/12] fix getkeys for cont == 2 --- DictionaryGenerator/Generator.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/DictionaryGenerator/Generator.cs b/DictionaryGenerator/Generator.cs index f38e05a..071acbb 100644 --- a/DictionaryGenerator/Generator.cs +++ b/DictionaryGenerator/Generator.cs @@ -117,18 +117,29 @@ public static string GenerateContainsKey(int count, int level) public static string GenerateGetKeys(int count) { + + var parameterTypes = count == 2 ? "K2" : $"({GenerateTypeParameters(1, count, false)})"; + var get = $@"public List<({GenerateTypeParameters(1, count, false)})> GetKeys() {{ - List<({GenerateTypeParameters(1, count, false)})> keys = new List<({GenerateTypeParameters(1, count, false)})>(); + var keys = new List<({GenerateTypeParameters(1, count, false)})>(); foreach (var kvp in Data) {{ - List<({GenerateTypeParameters(2, count, false)})> subkeys = kvp.Value.GetKeys(); + List<{parameterTypes}> subkeys = kvp.Value.GetKeys(); foreach (var subkey in subkeys) {{ keys.Add((kvp.Key, "; for (int i = 1; i < count; i++) { - get += $"subkey.Item{i}"; + if (count == 2) + { + get += "subkey"; + } + else + { + get += $"subkey.Item{i}"; + } + if (i < count-1) { get += ", "; From 9bf634be4fc9901ec55f20b97b0164e524e9da54 Mon Sep 17 00:00:00 2001 From: b3b00 Date: Thu, 8 Sep 2022 13:41:22 +0200 Subject: [PATCH 05/12] generator : fixes --- DictionaryGenerator/Generator.cs | 44 ++++++++++++++++++++------------ DictionaryGenerator/Program.cs | 7 +---- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/DictionaryGenerator/Generator.cs b/DictionaryGenerator/Generator.cs index 071acbb..2c732ba 100644 --- a/DictionaryGenerator/Generator.cs +++ b/DictionaryGenerator/Generator.cs @@ -5,7 +5,7 @@ namespace DictionaryGenerator; public class Generator { - public static string GenerateTypeParameters(int start, int count, bool withValue = true) + private static string GenerateTypeParameters(int start, int count, bool withValue = true) { StringBuilder builder = new StringBuilder(); @@ -25,7 +25,7 @@ public static string GenerateTypeParameters(int start, int count, bool withValue return builder.ToString(); } - public static string GenerateParametersDeclaration(int start, int count) + private static string GenerateParametersDeclaration(int start, int count) { StringBuilder builder = new StringBuilder(); @@ -41,7 +41,7 @@ public static string GenerateParametersDeclaration(int start, int count) return builder.ToString(); } - public static string GenerateParameters(int start, int count, bool withValue = false) + private static string GenerateParameters(int start, int count, bool withValue = false) { StringBuilder builder = new StringBuilder(); @@ -61,7 +61,7 @@ public static string GenerateParameters(int start, int count, bool withValue = f return builder.ToString(); } - public static string GenerateUsingsAndNamespace() + private static string GenerateUsingsAndNamespace() { var header = $@" @@ -73,7 +73,7 @@ namespace multiDimensionalDictionary {{"; return header; } - public static string GenerateClassDeclarationGenerateHeader(int count) + private static string GenerateClassDeclarationGenerateHeader(int count) { var header = $@" @@ -88,7 +88,7 @@ public Multi() : base() return header; } - public static string GenerateContains(int count) + private static string GenerateContains(int count) { StringBuilder builder = new StringBuilder(); for (int i = 1; i < count + 1; i++) @@ -102,7 +102,7 @@ public static string GenerateContains(int count) - public static string GenerateContainsKey(int count, int level) + private static string GenerateContainsKey(int count, int level) { if (level == 1) { @@ -115,10 +115,10 @@ public static string GenerateContainsKey(int count, int level) } } - public static string GenerateGetKeys(int count) + private static string GenerateGetKeys(int count) { - var parameterTypes = count == 2 ? "K2" : $"({GenerateTypeParameters(1, count, false)})"; + var parameterTypes = count == 2 ? "K2" : $"({GenerateTypeParameters(2, count, false)})"; var get = $@"public List<({GenerateTypeParameters(1, count, false)})> GetKeys() {{ @@ -156,12 +156,12 @@ public static string GenerateGetKeys(int count) } - public static string GenerateAssert(string type, string name) + private static string GenerateAssert(string type, string name) { return $@"DictionaryAssertions.AssertNotNull<{type}>({name}, ""{name}"");"; } - public static string GenerateAsserts(int count, bool withValue = true) + private static string GenerateAsserts(int count, bool withValue = true) { StringBuilder builder = new StringBuilder(); for (int i = 1; i < count + 1; i++) @@ -178,7 +178,7 @@ public static string GenerateAsserts(int count, bool withValue = true) return builder.ToString(); } - public static string GeneratePut(int count) + private static string GeneratePut(int count) { var put = $@"public void Put({GenerateParametersDeclaration(1, count)}, V value) {{ @@ -199,7 +199,7 @@ public static string GeneratePut(int count) return put; } - public static string GenerateGet(int count, int level) + private static string GenerateGet(int count, int level) { if (level == count) { @@ -209,7 +209,7 @@ public static string GenerateGet(int count, int level) if (Data.TryGetValue(k1, out Multi<{GenerateTypeParameters(2, count, true)}> secondDimensionData)) {{ - return secondDimensionData.Get({GenerateParameters(level-1, count, false)}); + return secondDimensionData.Get({GenerateParameters(2, count, false)}); }} throw new KeyNotFoundException(); @@ -227,11 +227,11 @@ public static string GenerateGet(int count, int level) return $@"public Multi<{GenerateTypeParameters(level+1,count,true)}> Get({GenerateParametersDeclaration(1,level)}) {{ - return Data[k1].Get({GenerateParameters(level,count-1,false)}); + return Data[k1].Get({GenerateParameters(2,level,false)}); }}"; } - public static string GenerateGets(int count) + private static string GenerateGets(int count) { StringBuilder builder = new StringBuilder(); for (int i = 1; i < count + 1; i++) @@ -250,19 +250,31 @@ public static string GenerateClass(int count) builder.AppendLine(GenerateClassDeclarationGenerateHeader(count)) .AppendLine() + .AppendLine("#region ContainsKey") .AppendLine(GenerateContains(count)) + .AppendLine("#endregion") .AppendLine() + .AppendLine("#region getKeys") .AppendLine(GenerateGetKeys(count)) + .AppendLine("#endregion") .AppendLine() + .AppendLine("#region Put") .AppendLine(GeneratePut(count)) + .AppendLine("#endregion") .AppendLine() + .AppendLine("#region Get") .AppendLine(GenerateGets(count)) + .AppendLine("#endregion") .AppendLine("}"); return builder.ToString(); } public static string Generate(int count) { + if (count <= 1) + { + throw new InvalidOperationException("NO ! You can't do so !"); + } StringBuilder builder = new StringBuilder(); builder .AppendLine(GenerateUsingsAndNamespace()) diff --git a/DictionaryGenerator/Program.cs b/DictionaryGenerator/Program.cs index cca186f..b1b4116 100644 --- a/DictionaryGenerator/Program.cs +++ b/DictionaryGenerator/Program.cs @@ -3,7 +3,7 @@ using System.Text; using DictionaryGenerator; -for (int i = 2; i <= 6; i++) +for (int i = 2; i <= 10; i++) { string g = Generator.Generate(i); @@ -11,11 +11,6 @@ File.WriteAllText( $@"C:\Users\olduh\dev\MultiDimensionDictionary\MultiDimensionDictionary\multiDimentsionDictionary\multi{i}.cs", g); } -// g = Generator.GenerateGet(3, 2); -// Console.WriteLine(g); -// g = Generator.GenerateGet(3, 3); -// Console.WriteLine(g); - From aed0e47d92de04d6821829f32674ebe00a478bb2 Mon Sep 17 00:00:00 2001 From: b3b00 Date: Thu, 8 Sep 2022 14:20:29 +0200 Subject: [PATCH 06/12] generator : remove --- DictionaryGenerator/Generator.cs | 42 ++++++++++ .../DictionaryAssertions.cs | 50 ++++++++---- .../MultiDimentsionDictionaryTests.cs | 76 +++++++++++++++++++ 3 files changed, 155 insertions(+), 13 deletions(-) diff --git a/DictionaryGenerator/Generator.cs b/DictionaryGenerator/Generator.cs index 2c732ba..cc2531f 100644 --- a/DictionaryGenerator/Generator.cs +++ b/DictionaryGenerator/Generator.cs @@ -265,9 +265,51 @@ public static string GenerateClass(int count) .AppendLine("#region Get") .AppendLine(GenerateGets(count)) .AppendLine("#endregion") + .AppendLine("#region Remove") + .AppendLine(GenerateRemoves(count)) + .AppendLine("#endregion") + .AppendLine("}"); return builder.ToString(); } + + public static string GenerateRemove(int count, int level) + { + if (level == 1) + { + return @"public new void Remove(K1 k1) + { + Data.Remove(k1, out var ignore); + }"; + } + else + { + StringBuilder builder = new StringBuilder(); + builder.AppendLine($"public void Remove({GenerateParametersDeclaration(1, level)})") + .AppendLine("{") + .Append("Data[k1]"); + for (int i = 2; i < level; i++) + { + builder.Append($".Get(k{i})"); + } + + builder.AppendLine($".Remove(k{level});") + .AppendLine("}"); + return builder.ToString(); + } + } + + public static string GenerateRemoves(int count) + { + StringBuilder builder = new StringBuilder(); + for (int i = 1; i < count + 1; i++) + { + builder.AppendLine(GenerateRemove(count, i)); + builder.AppendLine(); + } + + return builder.ToString(); + } public static string Generate(int count) { diff --git a/MultiDimensionDictionary/DictionaryAssertions.cs b/MultiDimensionDictionary/DictionaryAssertions.cs index 73a414a..870f926 100644 --- a/MultiDimensionDictionary/DictionaryAssertions.cs +++ b/MultiDimensionDictionary/DictionaryAssertions.cs @@ -13,8 +13,9 @@ public static void AssertNotNull(T val, string name) throw new ArgumentNullException(name); } } - - public static ICheckLink>> ContainsKey(this ICheck> context, K key) + + public static ICheckLink>> ContainsKey( + this ICheck> context, K key) { ExtensibilityHelper.BeginCheck(context) .FailWhen(sut => !sut.ContainsKey(key), "dictionary does not contains key {expected}.") @@ -23,8 +24,9 @@ public static void AssertNotNull(T val, string name) .EndCheck(); return ExtensibilityHelper.BuildCheckLink(context); } - - public static ICheckLink>> ContainsKey(this ICheck> context, K1 key1, K2 key2) + + public static ICheckLink>> ContainsKey( + this ICheck> context, K1 key1, K2 key2) { ExtensibilityHelper.BeginCheck(context) .FailWhen(sut => !sut.ContainsKey(key1, key2), "dictionary does not contains key {expected}.") @@ -33,8 +35,9 @@ public static void AssertNotNull(T val, string name) .EndCheck(); return ExtensibilityHelper.BuildCheckLink(context); } - - public static ICheckLink>> ContainsKey(this ICheck> context, K1 key1, K2 key2, K3 key3) + + public static ICheckLink>> ContainsKey( + this ICheck> context, K1 key1, K2 key2, K3 key3) { ExtensibilityHelper.BeginCheck(context) .FailWhen(sut => !sut.ContainsKey(key1, key2, key3), "dictionary does not contains key {expected}.") @@ -43,22 +46,43 @@ public static void AssertNotNull(T val, string name) .EndCheck(); return ExtensibilityHelper.BuildCheckLink(context); } - - public static ICheckLink>> ContainsKey(this ICheck> context, K1 key1, K2 key2, K3 key3, K4 key4) + + public static ICheckLink>> ContainsKey( + this ICheck> context, K1 key1, K2 key2, K3 key3, K4 key4) { ExtensibilityHelper.BeginCheck(context) - .FailWhen(sut => !sut.ContainsKey(key1, key2, key3, key4), "dictionary does not contains key {expected}.") + .FailWhen(sut => !sut.ContainsKey(key1, key2, key3, key4), + "dictionary does not contains key {expected}.") .DefineExpectedValue($"{key1.ToString()}.{key2.ToString()}.{key3.ToString()}.{key4.ToString()}") .OnNegate("dictionary contains key {expected}.") .EndCheck(); return ExtensibilityHelper.BuildCheckLink(context); } - - public static ICheckLink>> ContainsKey(this ICheck> context, K1 key1, K2 key2, K3 key3, K4 key4, K5 key5) + + public static ICheckLink>> + ContainsKey(this ICheck> context, + K1 key1, K2 key2, K3 key3, K4 key4, K5 key5) + { + ExtensibilityHelper.BeginCheck(context) + .FailWhen(sut => !sut.ContainsKey(key1, key2, key3, key4, key5), + "dictionary does not contains key {expected}.") + .DefineExpectedValue( + $"{key1.ToString()}.{key2.ToString()}.{key3.ToString()}.{key4.ToString()}.{key5.ToString()}") + .OnNegate("dictionary contains key {expected}.") + .EndCheck(); + return ExtensibilityHelper.BuildCheckLink(context); + } + + public static ICheckLink>> + ContainsKey( + this ICheck> context, K1 key1, K2 key2, K3 key3, + K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9, K10 key10) { ExtensibilityHelper.BeginCheck(context) - .FailWhen(sut => !sut.ContainsKey(key1, key2, key3, key4,key5), "dictionary does not contains key {expected}.") - .DefineExpectedValue($"{key1.ToString()}.{key2.ToString()}.{key3.ToString()}.{key4.ToString()}.{key5.ToString()}") + .FailWhen(sut => !sut.ContainsKey(key1, key2, key3, key4, key5, key6, key7, key8, key9, key10), + "dictionary does not contains key {expected}.") + .DefineExpectedValue( + $"{key1.ToString()}.{key2.ToString()}.{key3.ToString()}.{key4.ToString()}.{key5.ToString()}") .OnNegate("dictionary contains key {expected}.") .EndCheck(); return ExtensibilityHelper.BuildCheckLink(context); diff --git a/MultiDimensionTests/MultiDimentsionDictionaryTests.cs b/MultiDimensionTests/MultiDimentsionDictionaryTests.cs index 43d7e52..fa0f9f0 100644 --- a/MultiDimensionTests/MultiDimentsionDictionaryTests.cs +++ b/MultiDimensionTests/MultiDimentsionDictionaryTests.cs @@ -168,6 +168,68 @@ static void Test5() Check.That(keys).CountIs(3125); } + [Fact] + static void Test10() + { + Multi fiveDimDic = + new Multi(); + int count = 2; + for (int k1 = 0; k1 < count; k1++) + { + for (int k2 = 0; k2 < count; k2++) + { + for (int k3 = 0; k3 < count; k3++) + { + for (int k4 = 0; k4 < count; k4++) + { + for (int k5 = 0; k5 < count; k5++) + { + for (int k6 = 0; k6 < count; k6++) + { + for (int k7 = 0; k7 < count; k7++) + { + for (int k8 = 0; k8 < count; k8++) + { + for (int k9 = 0; k9 < count; k9++) + { + for (int k10 = 0; k10 < count; k10++) + { + fiveDimDic.Put(k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, + $"{k1}.{k2}.{k3}.{k4}.{k5}.{k6}.{k7}.{k8}.{k9}.{k10}"); + } + } + } + } + } + } + } + } + } + } + + + for (int i = 0; i < 10; i++) + { + Random rnd = new Random(); + int d1 = rnd.Next(0, count-1); + int d2 = rnd.Next(0, count-1); + int d3 = rnd.Next(0, count-1); + int d4 = rnd.Next(0, count-1); + int d5 = rnd.Next(0, count-1); + int d6 = rnd.Next(0, count-1); + int d7 = rnd.Next(0, count-1); + int d8 = rnd.Next(0, count-1); + int d9 = rnd.Next(0, count-1); + int d10 = rnd.Next(0, count-1); + var v = fiveDimDic.Get(d1, d2, d3, d4, d5, d6, d7, d8, d9, d10); + Check.That(v).IsEqualTo($"{d1}.{d2}.{d3}.{d4}.{d5}.{d6}.{d7}.{d8}.{d9}.{d10}"); + } + + var keys = fiveDimDic.GetKeys(); + Check.That(keys).CountIs((int)Math.Pow(count, 10)); + } + + [Fact] static void Test1Remove() { @@ -235,5 +297,19 @@ static void Test5Remove() Check.That(fiveDimDic).Not.ContainsKey(1, 2, 3, 4, 5); } + + [Fact] + static void Test10Remove() + { + Multi tenDimDic = + new Multi(); + + tenDimDic.Put(1, 2, 3, 4, 5,6,7,8,9,10, "10"); + Check.That(tenDimDic).ContainsKey(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + + tenDimDic.Remove(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + + Check.That(tenDimDic).Not.ContainsKey(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + } } } \ No newline at end of file From a4d54ebbbbf50644a7c6866593cae144e8315d3d Mon Sep 17 00:00:00 2001 From: b3b00 Date: Thu, 8 Sep 2022 14:27:12 +0200 Subject: [PATCH 07/12] 10 dimensions --- .../DictionaryAssertions.cs | 4 +- ...{MultiDimensionDictionary.cs => Multi1.cs} | 0 .../MultiDimensionDictionary2.cs | 99 ------- .../MultiDimensionDictionary3.cs | 105 -------- .../MultiDimensionDictionary4.cs | 116 --------- .../MultiDimensionDictionary5.cs | 131 ---------- .../multiDimentsionDictionary/multi10.cs | 244 ++++++++++++++++++ .../multiDimentsionDictionary/multi2.cs | 108 ++++++++ .../multiDimentsionDictionary/multi3.cs | 125 +++++++++ .../multiDimentsionDictionary/multi4.cs | 142 ++++++++++ .../multiDimentsionDictionary/multi5.cs | 159 ++++++++++++ .../multiDimentsionDictionary/multi6.cs | 176 +++++++++++++ .../multiDimentsionDictionary/multi7.cs | 193 ++++++++++++++ .../multiDimentsionDictionary/multi8.cs | 210 +++++++++++++++ .../multiDimentsionDictionary/multi9.cs | 227 ++++++++++++++++ .../MultiDimentsionDictionaryTests.cs | 8 +- 16 files changed, 1590 insertions(+), 457 deletions(-) rename MultiDimensionDictionary/multiDimentsionDictionary/{MultiDimensionDictionary.cs => Multi1.cs} (100%) delete mode 100644 MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary2.cs delete mode 100644 MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary3.cs delete mode 100644 MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary4.cs delete mode 100644 MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary5.cs create mode 100644 MultiDimensionDictionary/multiDimentsionDictionary/multi10.cs create mode 100644 MultiDimensionDictionary/multiDimentsionDictionary/multi2.cs create mode 100644 MultiDimensionDictionary/multiDimentsionDictionary/multi3.cs create mode 100644 MultiDimensionDictionary/multiDimentsionDictionary/multi4.cs create mode 100644 MultiDimensionDictionary/multiDimentsionDictionary/multi5.cs create mode 100644 MultiDimensionDictionary/multiDimentsionDictionary/multi6.cs create mode 100644 MultiDimensionDictionary/multiDimentsionDictionary/multi7.cs create mode 100644 MultiDimensionDictionary/multiDimentsionDictionary/multi8.cs create mode 100644 MultiDimensionDictionary/multiDimentsionDictionary/multi9.cs diff --git a/MultiDimensionDictionary/DictionaryAssertions.cs b/MultiDimensionDictionary/DictionaryAssertions.cs index 870f926..821556d 100644 --- a/MultiDimensionDictionary/DictionaryAssertions.cs +++ b/MultiDimensionDictionary/DictionaryAssertions.cs @@ -73,9 +73,9 @@ public static void AssertNotNull(T val, string name) return ExtensibilityHelper.BuildCheckLink(context); } - public static ICheckLink>> + public static ICheckLink>> ContainsKey( - this ICheck> context, K1 key1, K2 key2, K3 key3, + this ICheck> context, K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9, K10 key10) { ExtensibilityHelper.BeginCheck(context) diff --git a/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary.cs b/MultiDimensionDictionary/multiDimentsionDictionary/Multi1.cs similarity index 100% rename from MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary.cs rename to MultiDimensionDictionary/multiDimentsionDictionary/Multi1.cs diff --git a/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary2.cs b/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary2.cs deleted file mode 100644 index 4c5c254..0000000 --- a/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary2.cs +++ /dev/null @@ -1,99 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; - -namespace multiDimensionalDictionary -{ - public class MultiDimensionalDictionary - { - protected ConcurrentDictionary> Data { get; set; } - - public MultiDimensionalDictionary() - { - Data = new ConcurrentDictionary>(); - } - public bool ContainsKey(K1 k1) => Data.ContainsKey(k1); - public bool ContainsKey(K1 k1, K2 k2) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2); - - public List<(K1, K2)> GetKeys() - { - List<(K1, K2)> keys = new List<(K1, K2)>(); - foreach (var kvp in Data) - { - List subkeys = kvp.Value.GetKeys(); - foreach (var subkey in subkeys) - { - keys.Add((kvp.Key, subkey)); - } - } - return keys; - } - - public void Put(K1 k1, K2 k2, V value) - { - DictionaryAssertions.AssertNotNull(k1, "k1"); - - DictionaryAssertions.AssertNotNull(k2, "k2"); - - DictionaryAssertions.AssertNotNull(value, "value"); - - MultiDimensionalDictionary secondDimentionData = new MultiDimensionalDictionary(); - - if (Data.ContainsKey(k1)) - { - secondDimentionData = Data[k1]; - } - - if (secondDimentionData.ContainsKey(k2)) - { - throw new ArgumentException($"dictionary already contains key {k1};{k2}"); - } - else - { - secondDimentionData.Put(k2, value); - } - - Data[k1] = secondDimentionData; - } - - public MultiDimensionalDictionary Get(K1 k1) - { - return Data[k1]; - } - - public V Get(K1 k1, K2 k2) - { - DictionaryAssertions.AssertNotNull(k1, "k1"); - - DictionaryAssertions.AssertNotNull(k2, "k2"); - - if (Data.TryGetValue(k1, out MultiDimensionalDictionary secondDimensionData)) - { - if (secondDimensionData.ContainsKey(k2)) - { - var value = secondDimensionData.Get(k2); - return value; - } - } - - throw new KeyNotFoundException(); - } - - public void Remove(K1 k1) - { - Data.Remove(k1, out MultiDimensionalDictionary ignore); - } - - public void Remove(K1 k1, K2 k2) - { - Data[k1].Remove(k2); - } - - public void Clear() - { - Data.Clear(); - } - } - -} \ No newline at end of file diff --git a/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary3.cs b/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary3.cs deleted file mode 100644 index 86e7375..0000000 --- a/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary3.cs +++ /dev/null @@ -1,105 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; - -namespace multiDimensionalDictionary -{ - public class MultiDimensionalDictionary - { - protected ConcurrentDictionary> Data { get; set; } - - public MultiDimensionalDictionary() : base() - { - Data = new ConcurrentDictionary>(); - } - - public bool ContainsKey(K1 k1) => Data.ContainsKey(k1); - - public bool ContainsKey(K1 k1, K2 k2) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2); - - public bool ContainsKey(K1 k1, K2 k2, K3 k3) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3); - - public List<(K1, K2, K3)> GetKeys() - { - List<(K1, K2, K3)> keys = new List<(K1, K2, K3)>(); - foreach (var kvp in Data) - { - List<(K2, K3)> subkeys = kvp.Value.GetKeys(); - foreach (var subkey in subkeys) - { - keys.Add((kvp.Key, subkey.Item1, subkey.Item2)); - } - } - return keys; - } - - public void Put(K1 k1, K2 k2, K3 k3, V value) - { - DictionaryAssertions.AssertNotNull(k1, "k1"); - - DictionaryAssertions.AssertNotNull(k2, "k2"); - - DictionaryAssertions.AssertNotNull(k3, "k3"); - - DictionaryAssertions.AssertNotNull(value, "value"); - - var secondDimentionData = new MultiDimensionalDictionary(); - - if (Data.ContainsKey(k1)) - { - secondDimentionData = Data[k1]; - } - - secondDimentionData.Put(k2, k3, value); - - Data[k1] = secondDimentionData; - } - - public MultiDimensionalDictionary Get(K1 k1) - { - return Data[k1]; - } - - public MultiDimensionalDictionary Get(K1 k1, K2 k2) - { - return Data[k1].Get(k2); - } - - public V Get(K1 k1, K2 k2, K3 k3) - { - DictionaryAssertions.AssertNotNull(k1, "k1"); - - DictionaryAssertions.AssertNotNull(k2, "k2"); - - DictionaryAssertions.AssertNotNull(k3, "k3"); - - if (Data.TryGetValue(k1, out MultiDimensionalDictionary secondDimensionData)) - { - return secondDimensionData.Get(k2, k3); - } - - throw new KeyNotFoundException(); - } - - public void Remove(K1 k1) - { - Data.Remove(k1, out MultiDimensionalDictionary ignore); - } - - public void Remove(K1 k1, K2 k2) - { - Data[k1].Remove(k2); - } - - public void Remove(K1 k1, K2 k2, K3 k3) - { - Data[k1].Get(k2).Remove(k3); - } - - public void Clear() - { - Data.Clear(); - } - } -} \ No newline at end of file diff --git a/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary4.cs b/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary4.cs deleted file mode 100644 index 5f57122..0000000 --- a/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary4.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; - -namespace multiDimensionalDictionary -{ - public class MultiDimensionalDictionary : MultiDimensionalDictionary> - { - protected new ConcurrentDictionary> Data { get; set; } - - public MultiDimensionalDictionary() : base() - { - Data = new ConcurrentDictionary>(); - } - - public new bool ContainsKey(K1 k1) => Data.ContainsKey(k1); - - public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4); - public new List<(K1, K2, K3, K4)> GetKeys() - { - List<(K1, K2, K3, K4)> keys = new List<(K1, K2, K3, K4)>(); - foreach (var kvp in Data) - { - List<(K2, K3, K4)> subkeys = kvp.Value.GetKeys(); - foreach (var subkey in subkeys) - { - keys.Add((kvp.Key, subkey.Item1, subkey.Item2, subkey.Item3)); - } - } - return keys; - } - - public void Put(K1 k1, K2 k2, K3 k3, K4 k4, V value) - { - DictionaryAssertions.AssertNotNull(k1, "k1"); - - DictionaryAssertions.AssertNotNull(k2, "k2"); - - DictionaryAssertions.AssertNotNull(k3, "k3"); - - DictionaryAssertions.AssertNotNull(k4, "k4"); - - DictionaryAssertions.AssertNotNull(value, "value"); - - var secondDimentionData = new MultiDimensionalDictionary(); - - if (Data.ContainsKey(k1)) - { - secondDimentionData = Data[k1]; - } - - secondDimentionData.Put(k2, k3, k4, value); ; - Data[k1] = secondDimentionData; - } - - public new MultiDimensionalDictionary Get(K1 k1) - { - return Data[k1]; - } - - public new MultiDimensionalDictionary Get(K1 k1, K2 k2) - { - return Data[k1].Get(k2); - } - - public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3) - { - return Data[k1].Get(k2).Get(k3); - } - - public V Get(K1 k1, K2 k2, K3 k3, K4 k4) - { - DictionaryAssertions.AssertNotNull(k1, "k1"); - - DictionaryAssertions.AssertNotNull(k2, "k2"); - - DictionaryAssertions.AssertNotNull(k3, "k3"); - - DictionaryAssertions.AssertNotNull(k4, "k4"); - - if (Data.TryGetValue(k1, out MultiDimensionalDictionary secondDimensionData)) - { - return secondDimensionData.Get(k2, k3, k4); - } - - throw new KeyNotFoundException(); - } - - public new void Remove(K1 k1) - { - Data.Remove(k1, out MultiDimensionalDictionary ignore); - } - - public new void Remove(K1 k1, K2 k2) - { - Data[k1].Remove(k2); - } - - public void Remove(K1 k1, K2 k2, K3 k3) - { - Data[k1].Get(k2).Remove(k3); - } - - public void Remove(K1 k1, K2 k2, K3 k3, K4 k4) - { - Data[k1].Get(k2).Get(k3).Remove(k4); - } - - public new void Clear() - { - Data.Clear(); - } - } - -} \ No newline at end of file diff --git a/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary5.cs b/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary5.cs deleted file mode 100644 index 98d9d44..0000000 --- a/MultiDimensionDictionary/multiDimentsionDictionary/MultiDimensionDictionary5.cs +++ /dev/null @@ -1,131 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; - -namespace multiDimensionalDictionary -{ - public class MultiDimensionalDictionary : MultiDimensionalDictionary> - { - protected new ConcurrentDictionary> Data { get; set; } - - public MultiDimensionalDictionary() - { - Data = new ConcurrentDictionary>(); - } - - public new bool ContainsKey(K1 k1) => Data.ContainsKey(k1); - - public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5); - - public new List<(K1, K2, K3, K4, K5)> GetKeys() - { - List<(K1, K2, K3, K4, K5)> keys = new List<(K1, K2, K3, K4, K5)>(); - foreach (var kvp in Data) - { - List<(K2, K3, K4, K5)> subkeys = kvp.Value.GetKeys(); - foreach (var subkey in subkeys) - { - keys.Add((kvp.Key, subkey.Item1, subkey.Item2, subkey.Item3, subkey.Item4)); - } - } - return keys; - } - - public void Put(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, V value) - { - DictionaryAssertions.AssertNotNull(k1, "k1"); - - DictionaryAssertions.AssertNotNull(k2, "k2"); - - DictionaryAssertions.AssertNotNull(k3, "k3"); - - DictionaryAssertions.AssertNotNull(k4, "k4"); - - DictionaryAssertions.AssertNotNull(k5, "k5"); - - DictionaryAssertions.AssertNotNull(value, "value"); - - var secondDimentionData = new MultiDimensionalDictionary(); - - if (Data.ContainsKey(k1)) - { - secondDimentionData = Data[k1]; - } - - secondDimentionData.Put(k2, k3, k4, k5, value); - - Data[k1] = secondDimentionData; - } - - public new MultiDimensionalDictionary Get(K1 k1) - { - return Data[k1]; - } - - public new MultiDimensionalDictionary Get(K1 k1, K2 k2) - { - return Data[k1].Get(k2); - } - - public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3) - { - return Data[k1].Get(k2).Get(k3); - } - - public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4) - { - return Data[k1].Get(k2).Get(k3).Get(k4); - } - - public V Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) - { - DictionaryAssertions.AssertNotNull(k1, "k1"); - - DictionaryAssertions.AssertNotNull(k2, "k2"); - - DictionaryAssertions.AssertNotNull(k3, "k3"); - - DictionaryAssertions.AssertNotNull(k4, "k4"); - - DictionaryAssertions.AssertNotNull(k5, "k5"); - - if (Data.TryGetValue(k1, out MultiDimensionalDictionary secondDimensionData)) - { - return secondDimensionData.Get(k2, k3, k4, k5); - } - - throw new KeyNotFoundException(); - } - - public new void Remove(K1 k1) - { - Data.Remove(k1, out MultiDimensionalDictionary ignore); - } - - public new void Remove(K1 k1, K2 k2) - { - Data[k1].Remove(k2); - } - - public void Remove(K1 k1, K2 k2, K3 k3) - { - Data[k1].Get(k2).Remove(k3); - } - - public void Remove(K1 k1, K2 k2, K3 k3, K4 k4) - { - Data[k1].Get(k2).Get(k3).Remove(k4); - } - - public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) - { - Data[k1].Get(k2).Get(k3).Get(k4).Remove(k5); - } - - public new void Clear() - { - Data.Clear(); - } - } -} \ No newline at end of file diff --git a/MultiDimensionDictionary/multiDimentsionDictionary/multi10.cs b/MultiDimensionDictionary/multiDimentsionDictionary/multi10.cs new file mode 100644 index 0000000..80b2697 --- /dev/null +++ b/MultiDimensionDictionary/multiDimentsionDictionary/multi10.cs @@ -0,0 +1,244 @@ + + +using System.Collections.Concurrent; +using System.Collections.Generic; +using multiDimensionalDictionary; + +namespace multiDimensionalDictionary { + + +public class MultiDimensionalDictionary + { + protected ConcurrentDictionary> Data{ get;set; } + + public MultiDimensionalDictionary() : base() + { + Data = new ConcurrentDictionary>(); + } + +#region ContainsKey +public bool ContainsKey(K1 k1) => Data.ContainsKey(k1); + +public bool ContainsKey(K1 k1, K2 k2) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5, k6); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5, k6, k7); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5, k6, k7, k8); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5, k6, k7, k8, k9); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9, K10 k10) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5, k6, k7, k8, k9, k10); + + +#endregion + +#region getKeys +public List<(K1, K2, K3, K4, K5, K6, K7, K8, K9, K10)> GetKeys() + { + var keys = new List<(K1, K2, K3, K4, K5, K6, K7, K8, K9, K10)>(); + foreach (var kvp in Data) + { + List<(K2, K3, K4, K5, K6, K7, K8, K9, K10)> subkeys = kvp.Value.GetKeys(); + foreach (var subkey in subkeys) + { + keys.Add((kvp.Key, subkey.Item1, subkey.Item2, subkey.Item3, subkey.Item4, subkey.Item5, subkey.Item6, subkey.Item7, subkey.Item8, subkey.Item9));} + } + return keys; +} +#endregion + +#region Put +public void Put(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9, K10 k10, V value) + { + + DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(k3, "k3"); + +DictionaryAssertions.AssertNotNull(k4, "k4"); + +DictionaryAssertions.AssertNotNull(k5, "k5"); + +DictionaryAssertions.AssertNotNull(k6, "k6"); + +DictionaryAssertions.AssertNotNull(k7, "k7"); + +DictionaryAssertions.AssertNotNull(k8, "k8"); + +DictionaryAssertions.AssertNotNull(k9, "k9"); + +DictionaryAssertions.AssertNotNull(k10, "k10"); + +DictionaryAssertions.AssertNotNull(value, "value"); + + + + var secondDimentionData = new MultiDimensionalDictionary(); + + if (Data.ContainsKey(k1)) + { + secondDimentionData = Data[k1]; + } + + secondDimentionData.Put(k2, k3, k4, k5, k6, k7, k8, k9, k10, value); + + Data[k1] = secondDimentionData; + } +#endregion + +#region Get +public MultiDimensionalDictionary Get(K1 k1) + { + return Data[k1]; + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2) + { + return Data[k1].Get(k2); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3) + { + return Data[k1].Get(k2, k3); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4) + { + return Data[k1].Get(k2, k3, k4); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + return Data[k1].Get(k2, k3, k4, k5); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + return Data[k1].Get(k2, k3, k4, k5, k6); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) + { + return Data[k1].Get(k2, k3, k4, k5, k6, k7); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) + { + return Data[k1].Get(k2, k3, k4, k5, k6, k7, k8); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9) + { + return Data[k1].Get(k2, k3, k4, k5, k6, k7, k8, k9); + } + +public V Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9, K10 k10) + { +DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(k3, "k3"); + +DictionaryAssertions.AssertNotNull(k4, "k4"); + +DictionaryAssertions.AssertNotNull(k5, "k5"); + +DictionaryAssertions.AssertNotNull(k6, "k6"); + +DictionaryAssertions.AssertNotNull(k7, "k7"); + +DictionaryAssertions.AssertNotNull(k8, "k8"); + +DictionaryAssertions.AssertNotNull(k9, "k9"); + +DictionaryAssertions.AssertNotNull(k10, "k10"); + + + + + if (Data.TryGetValue(k1, out MultiDimensionalDictionary secondDimensionData)) + { + return secondDimensionData.Get(k2, k3, k4, k5, k6, k7, k8, k9, k10); + } + + throw new KeyNotFoundException(); + } + + +#endregion +#region Remove +public void Remove(K1 k1) + { + Data.Remove(k1, out var ignore); + } + +public void Remove(K1 k1, K2 k2) +{ +Data[k1].Remove(k2); +} + + +public void Remove(K1 k1, K2 k2, K3 k3) +{ +Data[k1].Get(k2).Remove(k3); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4) +{ +Data[k1].Get(k2).Get(k3).Remove(k4); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Remove(k5); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Get(k5).Remove(k6); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Remove(k7); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Get(k7).Remove(k8); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Get(k7).Get(k8).Remove(k9); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9, K10 k10) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Get(k7).Get(k8).Get(k9).Remove(k10); +} + + + +#endregion +} + +} diff --git a/MultiDimensionDictionary/multiDimentsionDictionary/multi2.cs b/MultiDimensionDictionary/multiDimentsionDictionary/multi2.cs new file mode 100644 index 0000000..d621c7b --- /dev/null +++ b/MultiDimensionDictionary/multiDimentsionDictionary/multi2.cs @@ -0,0 +1,108 @@ + + +using System.Collections.Concurrent; +using System.Collections.Generic; +using multiDimensionalDictionary; + +namespace multiDimensionalDictionary { + + +public class MultiDimensionalDictionary + { + protected ConcurrentDictionary> Data{ get;set; } + + public MultiDimensionalDictionary() : base() + { + Data = new ConcurrentDictionary>(); + } + +#region ContainsKey +public bool ContainsKey(K1 k1) => Data.ContainsKey(k1); + +public bool ContainsKey(K1 k1, K2 k2) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2); + + +#endregion + +#region getKeys +public List<(K1, K2)> GetKeys() + { + var keys = new List<(K1, K2)>(); + foreach (var kvp in Data) + { + List subkeys = kvp.Value.GetKeys(); + foreach (var subkey in subkeys) + { + keys.Add((kvp.Key, subkey));} + } + return keys; +} +#endregion + +#region Put +public void Put(K1 k1, K2 k2, V value) + { + + DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(value, "value"); + + + + var secondDimentionData = new MultiDimensionalDictionary(); + + if (Data.ContainsKey(k1)) + { + secondDimentionData = Data[k1]; + } + + secondDimentionData.Put(k2, value); + + Data[k1] = secondDimentionData; + } +#endregion + +#region Get +public MultiDimensionalDictionary Get(K1 k1) + { + return Data[k1]; + } + +public V Get(K1 k1, K2 k2) + { +DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + + + + + if (Data.TryGetValue(k1, out MultiDimensionalDictionary secondDimensionData)) + { + return secondDimensionData.Get(k2); + } + + throw new KeyNotFoundException(); + } + + +#endregion +#region Remove +public void Remove(K1 k1) + { + Data.Remove(k1, out var ignore); + } + +public void Remove(K1 k1, K2 k2) +{ +Data[k1].Remove(k2); +} + + + +#endregion +} + +} diff --git a/MultiDimensionDictionary/multiDimentsionDictionary/multi3.cs b/MultiDimensionDictionary/multiDimentsionDictionary/multi3.cs new file mode 100644 index 0000000..a5d7302 --- /dev/null +++ b/MultiDimensionDictionary/multiDimentsionDictionary/multi3.cs @@ -0,0 +1,125 @@ + + +using System.Collections.Concurrent; +using System.Collections.Generic; +using multiDimensionalDictionary; + +namespace multiDimensionalDictionary { + + +public class MultiDimensionalDictionary + { + protected ConcurrentDictionary> Data{ get;set; } + + public MultiDimensionalDictionary() : base() + { + Data = new ConcurrentDictionary>(); + } + +#region ContainsKey +public bool ContainsKey(K1 k1) => Data.ContainsKey(k1); + +public bool ContainsKey(K1 k1, K2 k2) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3); + + +#endregion + +#region getKeys +public List<(K1, K2, K3)> GetKeys() + { + var keys = new List<(K1, K2, K3)>(); + foreach (var kvp in Data) + { + List<(K2, K3)> subkeys = kvp.Value.GetKeys(); + foreach (var subkey in subkeys) + { + keys.Add((kvp.Key, subkey.Item1, subkey.Item2));} + } + return keys; +} +#endregion + +#region Put +public void Put(K1 k1, K2 k2, K3 k3, V value) + { + + DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(k3, "k3"); + +DictionaryAssertions.AssertNotNull(value, "value"); + + + + var secondDimentionData = new MultiDimensionalDictionary(); + + if (Data.ContainsKey(k1)) + { + secondDimentionData = Data[k1]; + } + + secondDimentionData.Put(k2, k3, value); + + Data[k1] = secondDimentionData; + } +#endregion + +#region Get +public MultiDimensionalDictionary Get(K1 k1) + { + return Data[k1]; + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2) + { + return Data[k1].Get(k2); + } + +public V Get(K1 k1, K2 k2, K3 k3) + { +DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(k3, "k3"); + + + + + if (Data.TryGetValue(k1, out MultiDimensionalDictionary secondDimensionData)) + { + return secondDimensionData.Get(k2, k3); + } + + throw new KeyNotFoundException(); + } + + +#endregion +#region Remove +public void Remove(K1 k1) + { + Data.Remove(k1, out var ignore); + } + +public void Remove(K1 k1, K2 k2) +{ +Data[k1].Remove(k2); +} + + +public void Remove(K1 k1, K2 k2, K3 k3) +{ +Data[k1].Get(k2).Remove(k3); +} + + + +#endregion +} + +} diff --git a/MultiDimensionDictionary/multiDimentsionDictionary/multi4.cs b/MultiDimensionDictionary/multiDimentsionDictionary/multi4.cs new file mode 100644 index 0000000..f52b55f --- /dev/null +++ b/MultiDimensionDictionary/multiDimentsionDictionary/multi4.cs @@ -0,0 +1,142 @@ + + +using System.Collections.Concurrent; +using System.Collections.Generic; +using multiDimensionalDictionary; + +namespace multiDimensionalDictionary { + + +public class MultiDimensionalDictionary + { + protected ConcurrentDictionary> Data{ get;set; } + + public MultiDimensionalDictionary() : base() + { + Data = new ConcurrentDictionary>(); + } + +#region ContainsKey +public bool ContainsKey(K1 k1) => Data.ContainsKey(k1); + +public bool ContainsKey(K1 k1, K2 k2) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4); + + +#endregion + +#region getKeys +public List<(K1, K2, K3, K4)> GetKeys() + { + var keys = new List<(K1, K2, K3, K4)>(); + foreach (var kvp in Data) + { + List<(K2, K3, K4)> subkeys = kvp.Value.GetKeys(); + foreach (var subkey in subkeys) + { + keys.Add((kvp.Key, subkey.Item1, subkey.Item2, subkey.Item3));} + } + return keys; +} +#endregion + +#region Put +public void Put(K1 k1, K2 k2, K3 k3, K4 k4, V value) + { + + DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(k3, "k3"); + +DictionaryAssertions.AssertNotNull(k4, "k4"); + +DictionaryAssertions.AssertNotNull(value, "value"); + + + + var secondDimentionData = new MultiDimensionalDictionary(); + + if (Data.ContainsKey(k1)) + { + secondDimentionData = Data[k1]; + } + + secondDimentionData.Put(k2, k3, k4, value); + + Data[k1] = secondDimentionData; + } +#endregion + +#region Get +public MultiDimensionalDictionary Get(K1 k1) + { + return Data[k1]; + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2) + { + return Data[k1].Get(k2); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3) + { + return Data[k1].Get(k2, k3); + } + +public V Get(K1 k1, K2 k2, K3 k3, K4 k4) + { +DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(k3, "k3"); + +DictionaryAssertions.AssertNotNull(k4, "k4"); + + + + + if (Data.TryGetValue(k1, out MultiDimensionalDictionary secondDimensionData)) + { + return secondDimensionData.Get(k2, k3, k4); + } + + throw new KeyNotFoundException(); + } + + +#endregion +#region Remove +public void Remove(K1 k1) + { + Data.Remove(k1, out var ignore); + } + +public void Remove(K1 k1, K2 k2) +{ +Data[k1].Remove(k2); +} + + +public void Remove(K1 k1, K2 k2, K3 k3) +{ +Data[k1].Get(k2).Remove(k3); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4) +{ +Data[k1].Get(k2).Get(k3).Remove(k4); +} + + + +#endregion +} + +} diff --git a/MultiDimensionDictionary/multiDimentsionDictionary/multi5.cs b/MultiDimensionDictionary/multiDimentsionDictionary/multi5.cs new file mode 100644 index 0000000..7434f9e --- /dev/null +++ b/MultiDimensionDictionary/multiDimentsionDictionary/multi5.cs @@ -0,0 +1,159 @@ + + +using System.Collections.Concurrent; +using System.Collections.Generic; +using multiDimensionalDictionary; + +namespace multiDimensionalDictionary { + + +public class MultiDimensionalDictionary + { + protected ConcurrentDictionary> Data{ get;set; } + + public MultiDimensionalDictionary() : base() + { + Data = new ConcurrentDictionary>(); + } + +#region ContainsKey +public bool ContainsKey(K1 k1) => Data.ContainsKey(k1); + +public bool ContainsKey(K1 k1, K2 k2) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5); + + +#endregion + +#region getKeys +public List<(K1, K2, K3, K4, K5)> GetKeys() + { + var keys = new List<(K1, K2, K3, K4, K5)>(); + foreach (var kvp in Data) + { + List<(K2, K3, K4, K5)> subkeys = kvp.Value.GetKeys(); + foreach (var subkey in subkeys) + { + keys.Add((kvp.Key, subkey.Item1, subkey.Item2, subkey.Item3, subkey.Item4));} + } + return keys; +} +#endregion + +#region Put +public void Put(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, V value) + { + + DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(k3, "k3"); + +DictionaryAssertions.AssertNotNull(k4, "k4"); + +DictionaryAssertions.AssertNotNull(k5, "k5"); + +DictionaryAssertions.AssertNotNull(value, "value"); + + + + var secondDimentionData = new MultiDimensionalDictionary(); + + if (Data.ContainsKey(k1)) + { + secondDimentionData = Data[k1]; + } + + secondDimentionData.Put(k2, k3, k4, k5, value); + + Data[k1] = secondDimentionData; + } +#endregion + +#region Get +public MultiDimensionalDictionary Get(K1 k1) + { + return Data[k1]; + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2) + { + return Data[k1].Get(k2); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3) + { + return Data[k1].Get(k2, k3); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4) + { + return Data[k1].Get(k2, k3, k4); + } + +public V Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { +DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(k3, "k3"); + +DictionaryAssertions.AssertNotNull(k4, "k4"); + +DictionaryAssertions.AssertNotNull(k5, "k5"); + + + + + if (Data.TryGetValue(k1, out MultiDimensionalDictionary secondDimensionData)) + { + return secondDimensionData.Get(k2, k3, k4, k5); + } + + throw new KeyNotFoundException(); + } + + +#endregion +#region Remove +public void Remove(K1 k1) + { + Data.Remove(k1, out var ignore); + } + +public void Remove(K1 k1, K2 k2) +{ +Data[k1].Remove(k2); +} + + +public void Remove(K1 k1, K2 k2, K3 k3) +{ +Data[k1].Get(k2).Remove(k3); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4) +{ +Data[k1].Get(k2).Get(k3).Remove(k4); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Remove(k5); +} + + + +#endregion +} + +} diff --git a/MultiDimensionDictionary/multiDimentsionDictionary/multi6.cs b/MultiDimensionDictionary/multiDimentsionDictionary/multi6.cs new file mode 100644 index 0000000..1a566d7 --- /dev/null +++ b/MultiDimensionDictionary/multiDimentsionDictionary/multi6.cs @@ -0,0 +1,176 @@ + + +using System.Collections.Concurrent; +using System.Collections.Generic; +using multiDimensionalDictionary; + +namespace multiDimensionalDictionary { + + +public class MultiDimensionalDictionary + { + protected ConcurrentDictionary> Data{ get;set; } + + public MultiDimensionalDictionary() : base() + { + Data = new ConcurrentDictionary>(); + } + +#region ContainsKey +public bool ContainsKey(K1 k1) => Data.ContainsKey(k1); + +public bool ContainsKey(K1 k1, K2 k2) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5, k6); + + +#endregion + +#region getKeys +public List<(K1, K2, K3, K4, K5, K6)> GetKeys() + { + var keys = new List<(K1, K2, K3, K4, K5, K6)>(); + foreach (var kvp in Data) + { + List<(K2, K3, K4, K5, K6)> subkeys = kvp.Value.GetKeys(); + foreach (var subkey in subkeys) + { + keys.Add((kvp.Key, subkey.Item1, subkey.Item2, subkey.Item3, subkey.Item4, subkey.Item5));} + } + return keys; +} +#endregion + +#region Put +public void Put(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, V value) + { + + DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(k3, "k3"); + +DictionaryAssertions.AssertNotNull(k4, "k4"); + +DictionaryAssertions.AssertNotNull(k5, "k5"); + +DictionaryAssertions.AssertNotNull(k6, "k6"); + +DictionaryAssertions.AssertNotNull(value, "value"); + + + + var secondDimentionData = new MultiDimensionalDictionary(); + + if (Data.ContainsKey(k1)) + { + secondDimentionData = Data[k1]; + } + + secondDimentionData.Put(k2, k3, k4, k5, k6, value); + + Data[k1] = secondDimentionData; + } +#endregion + +#region Get +public MultiDimensionalDictionary Get(K1 k1) + { + return Data[k1]; + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2) + { + return Data[k1].Get(k2); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3) + { + return Data[k1].Get(k2, k3); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4) + { + return Data[k1].Get(k2, k3, k4); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + return Data[k1].Get(k2, k3, k4, k5); + } + +public V Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { +DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(k3, "k3"); + +DictionaryAssertions.AssertNotNull(k4, "k4"); + +DictionaryAssertions.AssertNotNull(k5, "k5"); + +DictionaryAssertions.AssertNotNull(k6, "k6"); + + + + + if (Data.TryGetValue(k1, out MultiDimensionalDictionary secondDimensionData)) + { + return secondDimensionData.Get(k2, k3, k4, k5, k6); + } + + throw new KeyNotFoundException(); + } + + +#endregion +#region Remove +public void Remove(K1 k1) + { + Data.Remove(k1, out var ignore); + } + +public void Remove(K1 k1, K2 k2) +{ +Data[k1].Remove(k2); +} + + +public void Remove(K1 k1, K2 k2, K3 k3) +{ +Data[k1].Get(k2).Remove(k3); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4) +{ +Data[k1].Get(k2).Get(k3).Remove(k4); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Remove(k5); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Get(k5).Remove(k6); +} + + + +#endregion +} + +} diff --git a/MultiDimensionDictionary/multiDimentsionDictionary/multi7.cs b/MultiDimensionDictionary/multiDimentsionDictionary/multi7.cs new file mode 100644 index 0000000..cad0f9d --- /dev/null +++ b/MultiDimensionDictionary/multiDimentsionDictionary/multi7.cs @@ -0,0 +1,193 @@ + + +using System.Collections.Concurrent; +using System.Collections.Generic; +using multiDimensionalDictionary; + +namespace multiDimensionalDictionary { + + +public class MultiDimensionalDictionary + { + protected ConcurrentDictionary> Data{ get;set; } + + public MultiDimensionalDictionary() : base() + { + Data = new ConcurrentDictionary>(); + } + +#region ContainsKey +public bool ContainsKey(K1 k1) => Data.ContainsKey(k1); + +public bool ContainsKey(K1 k1, K2 k2) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5, k6); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5, k6, k7); + + +#endregion + +#region getKeys +public List<(K1, K2, K3, K4, K5, K6, K7)> GetKeys() + { + var keys = new List<(K1, K2, K3, K4, K5, K6, K7)>(); + foreach (var kvp in Data) + { + List<(K2, K3, K4, K5, K6, K7)> subkeys = kvp.Value.GetKeys(); + foreach (var subkey in subkeys) + { + keys.Add((kvp.Key, subkey.Item1, subkey.Item2, subkey.Item3, subkey.Item4, subkey.Item5, subkey.Item6));} + } + return keys; +} +#endregion + +#region Put +public void Put(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, V value) + { + + DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(k3, "k3"); + +DictionaryAssertions.AssertNotNull(k4, "k4"); + +DictionaryAssertions.AssertNotNull(k5, "k5"); + +DictionaryAssertions.AssertNotNull(k6, "k6"); + +DictionaryAssertions.AssertNotNull(k7, "k7"); + +DictionaryAssertions.AssertNotNull(value, "value"); + + + + var secondDimentionData = new MultiDimensionalDictionary(); + + if (Data.ContainsKey(k1)) + { + secondDimentionData = Data[k1]; + } + + secondDimentionData.Put(k2, k3, k4, k5, k6, k7, value); + + Data[k1] = secondDimentionData; + } +#endregion + +#region Get +public MultiDimensionalDictionary Get(K1 k1) + { + return Data[k1]; + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2) + { + return Data[k1].Get(k2); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3) + { + return Data[k1].Get(k2, k3); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4) + { + return Data[k1].Get(k2, k3, k4); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + return Data[k1].Get(k2, k3, k4, k5); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + return Data[k1].Get(k2, k3, k4, k5, k6); + } + +public V Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) + { +DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(k3, "k3"); + +DictionaryAssertions.AssertNotNull(k4, "k4"); + +DictionaryAssertions.AssertNotNull(k5, "k5"); + +DictionaryAssertions.AssertNotNull(k6, "k6"); + +DictionaryAssertions.AssertNotNull(k7, "k7"); + + + + + if (Data.TryGetValue(k1, out MultiDimensionalDictionary secondDimensionData)) + { + return secondDimensionData.Get(k2, k3, k4, k5, k6, k7); + } + + throw new KeyNotFoundException(); + } + + +#endregion +#region Remove +public void Remove(K1 k1) + { + Data.Remove(k1, out var ignore); + } + +public void Remove(K1 k1, K2 k2) +{ +Data[k1].Remove(k2); +} + + +public void Remove(K1 k1, K2 k2, K3 k3) +{ +Data[k1].Get(k2).Remove(k3); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4) +{ +Data[k1].Get(k2).Get(k3).Remove(k4); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Remove(k5); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Get(k5).Remove(k6); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Remove(k7); +} + + + +#endregion +} + +} diff --git a/MultiDimensionDictionary/multiDimentsionDictionary/multi8.cs b/MultiDimensionDictionary/multiDimentsionDictionary/multi8.cs new file mode 100644 index 0000000..4248f85 --- /dev/null +++ b/MultiDimensionDictionary/multiDimentsionDictionary/multi8.cs @@ -0,0 +1,210 @@ + + +using System.Collections.Concurrent; +using System.Collections.Generic; +using multiDimensionalDictionary; + +namespace multiDimensionalDictionary { + + +public class MultiDimensionalDictionary + { + protected ConcurrentDictionary> Data{ get;set; } + + public MultiDimensionalDictionary() : base() + { + Data = new ConcurrentDictionary>(); + } + +#region ContainsKey +public bool ContainsKey(K1 k1) => Data.ContainsKey(k1); + +public bool ContainsKey(K1 k1, K2 k2) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5, k6); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5, k6, k7); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5, k6, k7, k8); + + +#endregion + +#region getKeys +public List<(K1, K2, K3, K4, K5, K6, K7, K8)> GetKeys() + { + var keys = new List<(K1, K2, K3, K4, K5, K6, K7, K8)>(); + foreach (var kvp in Data) + { + List<(K2, K3, K4, K5, K6, K7, K8)> subkeys = kvp.Value.GetKeys(); + foreach (var subkey in subkeys) + { + keys.Add((kvp.Key, subkey.Item1, subkey.Item2, subkey.Item3, subkey.Item4, subkey.Item5, subkey.Item6, subkey.Item7));} + } + return keys; +} +#endregion + +#region Put +public void Put(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, V value) + { + + DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(k3, "k3"); + +DictionaryAssertions.AssertNotNull(k4, "k4"); + +DictionaryAssertions.AssertNotNull(k5, "k5"); + +DictionaryAssertions.AssertNotNull(k6, "k6"); + +DictionaryAssertions.AssertNotNull(k7, "k7"); + +DictionaryAssertions.AssertNotNull(k8, "k8"); + +DictionaryAssertions.AssertNotNull(value, "value"); + + + + var secondDimentionData = new MultiDimensionalDictionary(); + + if (Data.ContainsKey(k1)) + { + secondDimentionData = Data[k1]; + } + + secondDimentionData.Put(k2, k3, k4, k5, k6, k7, k8, value); + + Data[k1] = secondDimentionData; + } +#endregion + +#region Get +public MultiDimensionalDictionary Get(K1 k1) + { + return Data[k1]; + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2) + { + return Data[k1].Get(k2); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3) + { + return Data[k1].Get(k2, k3); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4) + { + return Data[k1].Get(k2, k3, k4); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + return Data[k1].Get(k2, k3, k4, k5); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + return Data[k1].Get(k2, k3, k4, k5, k6); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) + { + return Data[k1].Get(k2, k3, k4, k5, k6, k7); + } + +public V Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) + { +DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(k3, "k3"); + +DictionaryAssertions.AssertNotNull(k4, "k4"); + +DictionaryAssertions.AssertNotNull(k5, "k5"); + +DictionaryAssertions.AssertNotNull(k6, "k6"); + +DictionaryAssertions.AssertNotNull(k7, "k7"); + +DictionaryAssertions.AssertNotNull(k8, "k8"); + + + + + if (Data.TryGetValue(k1, out MultiDimensionalDictionary secondDimensionData)) + { + return secondDimensionData.Get(k2, k3, k4, k5, k6, k7, k8); + } + + throw new KeyNotFoundException(); + } + + +#endregion +#region Remove +public void Remove(K1 k1) + { + Data.Remove(k1, out var ignore); + } + +public void Remove(K1 k1, K2 k2) +{ +Data[k1].Remove(k2); +} + + +public void Remove(K1 k1, K2 k2, K3 k3) +{ +Data[k1].Get(k2).Remove(k3); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4) +{ +Data[k1].Get(k2).Get(k3).Remove(k4); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Remove(k5); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Get(k5).Remove(k6); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Remove(k7); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Get(k7).Remove(k8); +} + + + +#endregion +} + +} diff --git a/MultiDimensionDictionary/multiDimentsionDictionary/multi9.cs b/MultiDimensionDictionary/multiDimentsionDictionary/multi9.cs new file mode 100644 index 0000000..5dc64bb --- /dev/null +++ b/MultiDimensionDictionary/multiDimentsionDictionary/multi9.cs @@ -0,0 +1,227 @@ + + +using System.Collections.Concurrent; +using System.Collections.Generic; +using multiDimensionalDictionary; + +namespace multiDimensionalDictionary { + + +public class MultiDimensionalDictionary + { + protected ConcurrentDictionary> Data{ get;set; } + + public MultiDimensionalDictionary() : base() + { + Data = new ConcurrentDictionary>(); + } + +#region ContainsKey +public bool ContainsKey(K1 k1) => Data.ContainsKey(k1); + +public bool ContainsKey(K1 k1, K2 k2) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5, k6); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5, k6, k7); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5, k6, k7, k8); + +public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9) => Data.ContainsKey(k1) && Data[k1].ContainsKey(k2, k3, k4, k5, k6, k7, k8, k9); + + +#endregion + +#region getKeys +public List<(K1, K2, K3, K4, K5, K6, K7, K8, K9)> GetKeys() + { + var keys = new List<(K1, K2, K3, K4, K5, K6, K7, K8, K9)>(); + foreach (var kvp in Data) + { + List<(K2, K3, K4, K5, K6, K7, K8, K9)> subkeys = kvp.Value.GetKeys(); + foreach (var subkey in subkeys) + { + keys.Add((kvp.Key, subkey.Item1, subkey.Item2, subkey.Item3, subkey.Item4, subkey.Item5, subkey.Item6, subkey.Item7, subkey.Item8));} + } + return keys; +} +#endregion + +#region Put +public void Put(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9, V value) + { + + DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(k3, "k3"); + +DictionaryAssertions.AssertNotNull(k4, "k4"); + +DictionaryAssertions.AssertNotNull(k5, "k5"); + +DictionaryAssertions.AssertNotNull(k6, "k6"); + +DictionaryAssertions.AssertNotNull(k7, "k7"); + +DictionaryAssertions.AssertNotNull(k8, "k8"); + +DictionaryAssertions.AssertNotNull(k9, "k9"); + +DictionaryAssertions.AssertNotNull(value, "value"); + + + + var secondDimentionData = new MultiDimensionalDictionary(); + + if (Data.ContainsKey(k1)) + { + secondDimentionData = Data[k1]; + } + + secondDimentionData.Put(k2, k3, k4, k5, k6, k7, k8, k9, value); + + Data[k1] = secondDimentionData; + } +#endregion + +#region Get +public MultiDimensionalDictionary Get(K1 k1) + { + return Data[k1]; + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2) + { + return Data[k1].Get(k2); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3) + { + return Data[k1].Get(k2, k3); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4) + { + return Data[k1].Get(k2, k3, k4); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + return Data[k1].Get(k2, k3, k4, k5); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + return Data[k1].Get(k2, k3, k4, k5, k6); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) + { + return Data[k1].Get(k2, k3, k4, k5, k6, k7); + } + +public MultiDimensionalDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) + { + return Data[k1].Get(k2, k3, k4, k5, k6, k7, k8); + } + +public V Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9) + { +DictionaryAssertions.AssertNotNull(k1, "k1"); + +DictionaryAssertions.AssertNotNull(k2, "k2"); + +DictionaryAssertions.AssertNotNull(k3, "k3"); + +DictionaryAssertions.AssertNotNull(k4, "k4"); + +DictionaryAssertions.AssertNotNull(k5, "k5"); + +DictionaryAssertions.AssertNotNull(k6, "k6"); + +DictionaryAssertions.AssertNotNull(k7, "k7"); + +DictionaryAssertions.AssertNotNull(k8, "k8"); + +DictionaryAssertions.AssertNotNull(k9, "k9"); + + + + + if (Data.TryGetValue(k1, out MultiDimensionalDictionary secondDimensionData)) + { + return secondDimensionData.Get(k2, k3, k4, k5, k6, k7, k8, k9); + } + + throw new KeyNotFoundException(); + } + + +#endregion +#region Remove +public void Remove(K1 k1) + { + Data.Remove(k1, out var ignore); + } + +public void Remove(K1 k1, K2 k2) +{ +Data[k1].Remove(k2); +} + + +public void Remove(K1 k1, K2 k2, K3 k3) +{ +Data[k1].Get(k2).Remove(k3); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4) +{ +Data[k1].Get(k2).Get(k3).Remove(k4); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Remove(k5); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Get(k5).Remove(k6); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Remove(k7); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Get(k7).Remove(k8); +} + + +public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9) +{ +Data[k1].Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Get(k7).Get(k8).Remove(k9); +} + + + +#endregion +} + +} diff --git a/MultiDimensionTests/MultiDimentsionDictionaryTests.cs b/MultiDimensionTests/MultiDimentsionDictionaryTests.cs index fa0f9f0..755510c 100644 --- a/MultiDimensionTests/MultiDimentsionDictionaryTests.cs +++ b/MultiDimensionTests/MultiDimentsionDictionaryTests.cs @@ -171,8 +171,8 @@ static void Test5() [Fact] static void Test10() { - Multi fiveDimDic = - new Multi(); + MultiDimensionalDictionary fiveDimDic = + new MultiDimensionalDictionary(); int count = 2; for (int k1 = 0; k1 < count; k1++) { @@ -301,8 +301,8 @@ static void Test5Remove() [Fact] static void Test10Remove() { - Multi tenDimDic = - new Multi(); + MultiDimensionalDictionary tenDimDic = + new MultiDimensionalDictionary(); tenDimDic.Put(1, 2, 3, 4, 5,6,7,8,9,10, "10"); Check.That(tenDimDic).ContainsKey(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); From 3e0b3f44683c64fed285412d3415cfdd9d1bdc24 Mon Sep 17 00:00:00 2001 From: b3b00 Date: Thu, 8 Sep 2022 15:32:32 +0200 Subject: [PATCH 08/12] generator expirational dictionaries --- DictionaryGenerator/ExpirationalGenerator.cs | 468 +++++++++++++++++++ DictionaryGenerator/Program.cs | 4 +- 2 files changed, 470 insertions(+), 2 deletions(-) create mode 100644 DictionaryGenerator/ExpirationalGenerator.cs diff --git a/DictionaryGenerator/ExpirationalGenerator.cs b/DictionaryGenerator/ExpirationalGenerator.cs new file mode 100644 index 0000000..aea294c --- /dev/null +++ b/DictionaryGenerator/ExpirationalGenerator.cs @@ -0,0 +1,468 @@ +using System.Text; + +namespace DictionaryGenerator; + +public class ExpirationalGenerator +{ + + private static string GenerateTypeParameters(int start, int count, bool withValue = true) + { + StringBuilder builder = new StringBuilder(); + + for (int i = start; i < count + 1; i++) + { + builder.Append($"K{i}"); + if (i < count) + { + builder.Append(", "); + } + } + + if (withValue) + { + builder.Append(", V"); + } + return builder.ToString(); + } + + private static string GenerateParametersDeclaration(int start, int count) + { + StringBuilder builder = new StringBuilder(); + + for (int i = start; i < count + 1; i++) + { + builder.Append($"K{i} k{i}"); + if (i < count) + { + builder.Append(", "); + } + } + + return builder.ToString(); + } + + private static string GenerateParameters(int start, int count, bool withValue = false) + { + StringBuilder builder = new StringBuilder(); + + for (int i = start; i < count + 1; i++) + { + builder.Append($"k{i}"); + if (i < count) + { + builder.Append(", "); + } + } + + if (withValue) + { + builder.Append(", value"); + } + return builder.ToString(); + } + + private static string GenerateUsingsAndNamespace() + { + var header = $@" + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; + +namespace multiDimensionalDictionary {{"; + return header; + } + + + private static string GenerateExpirations(int count) + { + StringBuilder builder = new StringBuilder(); + for (int i = 1; i <= count; i++) + { + builder.AppendLine($"TimeSpan ExpirationSpan{i};"); + } + + return builder.ToString(); + } + + + private static string GenerateConstructorMillis(int count) + { + var builder = new StringBuilder(); + builder.Append("public ExpirationalMulti("); + for (int i = 1; i <= count; i++) + { + builder.Append($"long expiration{i}"); + if (i < count) + { + builder.Append(", "); + } + } + + builder.Append(") : this("); + for (int i = 1; i <= count; i++) + { + builder.Append($"TimeSpan.FromMilliseconds(expiration{i})"); + if (i < count) + { + builder.Append(", "); + } + } + + builder.Append(")"); + builder.Append(@" +{ + }"); + return builder.ToString(); + } + + private static string GenerateConstructorTimeSpan(int count) + { + var builder = new StringBuilder(); + builder.Append("public ExpirationalMulti("); + for (int i = 1; i <= count; i++) + { + builder.Append($"TimeSpan expiration{i}"); + if (i < count) + { + builder.Append(", "); + } + } + + builder.AppendLine(") "); + builder.Append("{"); + for (int i = 1; i <= count; i++) + { + builder.Append($"ExpirationSpan{i} = expiration{i};"); + } + builder.Append("}"); + return builder.ToString(); + } + + private static string GenerateConstructors(int count) + { + return $@" +{GenerateConstructorTimeSpan(count)} + +{GenerateConstructorMillis(count)}"; + } + + private static string GenerateClassDeclarationGenerateHeader(int count) + { + var header = $@" + +public class ExpirationalMulti<{GenerateTypeParameters(1, count)}> + {{ + {GenerateExpirations(count)} + + {GenerateConstructors(count)} + + protected ConcurrentDictionary subData)> Data{{ get;set; }} + + public ExpirationalMulti() : base() + {{ + Data = new ConcurrentDictionary subData)>(); + }} + + +"; + + + + return header; + } + + private static string GenerateContains(int count) + { + StringBuilder builder = new StringBuilder(); + for (int i = 1; i < count + 1; i++) + { + builder.AppendLine(GenerateContainsKey(count, i)); + builder.AppendLine(); + } + + return builder.ToString(); + } + + + + private static string GenerateContainsKey(int count, int level) + { + if (level == 1) + { + return "public bool ContainsKey(K1 k1) => Data.ContainsKey(k1);"; + } + else + { + return + $"public bool ContainsKey({GenerateParametersDeclaration(1, level)}) => Data.ContainsKey(k1) && Data[k1].subData.ContainsKey({GenerateParameters(2, level)});"; + } + } + + private static string GenerateGetKeys(int count) + { + + var parameterTypes = count == 2 ? "K2" : $"({GenerateTypeParameters(2, count, false)})"; + + var get = $@"public List<({GenerateTypeParameters(1, count, false)})> GetKeys() + {{ + var keys = new List<({GenerateTypeParameters(1, count, false)})>(); + foreach (var kvp in Data) + {{ + List<{parameterTypes}> subkeys = kvp.Value.subData.GetKeys(); + foreach (var subkey in subkeys) + {{ + keys.Add((kvp.Key, "; + for (int i = 1; i < count; i++) + { + if (count == 2) + { + get += "subkey"; + } + else + { + get += $"subkey.Item{i}"; + } + + if (i < count-1) + { + get += ", "; + } + } + + get += "));"; + + get+=$@"}} + }} + return keys; +}}"; + return get; + } + + + private static string GenerateAssert(string type, string name) + { + return $@"DictionaryAssertions.AssertNotNull<{type}>({name}, ""{name}"");"; + } + + private static string GenerateAsserts(int count, bool withValue = true) + { + StringBuilder builder = new StringBuilder(); + for (int i = 1; i < count + 1; i++) + { + builder.AppendLine(GenerateAssert($"K{i}", $"k{i}")); + builder.AppendLine(); + } + + if (withValue) + { + builder.AppendLine(GenerateAssert($"V", "value")); + } + builder.AppendLine(); + return builder.ToString(); + } + + private static string GenerateExpirationParameters(int start, int count) + { + StringBuilder builder = new StringBuilder(); + + for (int i = start; i < count + 1; i++) + { + builder.Append($"ExpirationSpan{i}"); + if (i < count) + { + builder.Append(", "); + } + } + + return builder.ToString(); + } + + private static string GeneratePut(int count) + { + var put = $@"public void Put({GenerateParametersDeclaration(1, count)}, V value) + {{ + + {GenerateAsserts(count)} + + var secondDimentionData = new ExpirationalMulti<{GenerateTypeParameters(2,count,true)}>({GenerateExpirationParameters(2,count)}); + + if (Data.ContainsKey(k1)) + {{ + secondDimentionData = Data[k1].subData; + }} + + secondDimentionData.Put({GenerateParameters(2,count,true)}); + + Data[k1] = (DateTime.Now, secondDimentionData); + }}"; + return put; + } + + private static string GenerateGet(int count, int level) + { + if (level == count) + { + var get = $@"public V Get({GenerateParametersDeclaration(1, level)}) + {{ +{GenerateAsserts(count, false)} + + if (Data.TryGetValue(k1, out var secondDimensionData)) + {{ + return secondDimensionData.subData.Get({GenerateParameters(2, count, false)}); + }} + + throw new KeyNotFoundException(); + }}"; + return get; + } + + if (level == 1) + { + return $@"public ExpirationalMulti<{GenerateTypeParameters(2,count, true)}> Get(K1 k1) + {{ + return Data[k1].subData; + }}"; + } + + return $@"public ExpirationalMulti<{GenerateTypeParameters(level+1,count,true)}> Get({GenerateParametersDeclaration(1,level)}) + {{ + return Data[k1].subData.Get({GenerateParameters(2,level,false)}); + }}"; + } + + private static string GenerateGets(int count) + { + StringBuilder builder = new StringBuilder(); + for (int i = 1; i < count + 1; i++) + { + builder.AppendLine(GenerateGet(count, i)); + builder.AppendLine(); + } + + return builder.ToString(); + } + + + public static string GenerateClass(int count) + { + StringBuilder builder = new StringBuilder(); + + builder.AppendLine(GenerateClassDeclarationGenerateHeader(count)) + .AppendLine() + .AppendLine("#region ContainsKey") + .AppendLine(GenerateContains(count)) + .AppendLine("#endregion") + .AppendLine() + .AppendLine("#region getKeys") + .AppendLine(GenerateGetKeys(count)) + .AppendLine("#endregion") + .AppendLine() + .AppendLine("#region Put") + .AppendLine(GeneratePut(count)) + .AppendLine("#endregion") + .AppendLine() + .AppendLine("#region Get") + .AppendLine(GenerateGets(count)) + .AppendLine("#endregion") + .AppendLine("#region Remove") + .AppendLine(GenerateRemoves(count)) + .AppendLine("#endregion") + .AppendLine(GenerateInvalidate(count)) + .AppendLine("}"); + return builder.ToString(); + } + + public static string GenerateRemove(int count, int level) + { + if (level == 1) + { + return @"public new void Remove(K1 k1) + { + Data.Remove(k1, out var ignore); + }"; + } + else + { + StringBuilder builder = new StringBuilder(); + builder.AppendLine($"public void Remove({GenerateParametersDeclaration(1, level)})") + .AppendLine("{") + .Append("Data[k1].subData"); + for (int i = 2; i < level; i++) + { + builder.Append($".Get(k{i})"); + } + + builder.AppendLine($".Remove(k{level});") + .AppendLine("}"); + return builder.ToString(); + } + } + + + + public static string GenerateRemoves(int count) + { + StringBuilder builder = new StringBuilder(); + for (int i = 1; i < count + 1; i++) + { + builder.AppendLine(GenerateRemove(count, i)); + builder.AppendLine(); + } + + return builder.ToString(); + } + + public static string GenerateInvalidate(int count) + { + string invalidate = $@" + +public void _Remove(K1 k1) + {{ + (DateTime date, ExpirationalMulti<{GenerateTypeParameters(2,count,true)}> subData) ignore = default; + Data.TryRemove(k1, out ignore); + }} + +public void Invalidate() + {{ + DateTime now = DateTime.Now; + List toRemove = new List(); + foreach (var entry in Data) + {{ + var age = now - entry.Value.date; + if (age > ExpirationSpan1) + {{ + toRemove.Add(entry.Key); + }} + }} + + if (toRemove.Any()) + {{ + foreach (var k1 in toRemove) + {{ + _Remove(k1); + }} + }} + }}"; + return invalidate; + } + + public static string Generate(int count) + { + if (count <= 1) + { + throw new InvalidOperationException("NO ! You can't do so !"); + } + StringBuilder builder = new StringBuilder(); + builder + .AppendLine(GenerateUsingsAndNamespace()) + .AppendLine(GenerateClass(count)) + .AppendLine("}"); + + + + return builder.ToString(); + } + +} \ No newline at end of file diff --git a/DictionaryGenerator/Program.cs b/DictionaryGenerator/Program.cs index b1b4116..f5dcdeb 100644 --- a/DictionaryGenerator/Program.cs +++ b/DictionaryGenerator/Program.cs @@ -6,10 +6,10 @@ for (int i = 2; i <= 10; i++) { - string g = Generator.Generate(i); + string g = ExpirationalGenerator.Generate(i); File.WriteAllText( - $@"C:\Users\olduh\dev\MultiDimensionDictionary\MultiDimensionDictionary\multiDimentsionDictionary\multi{i}.cs", g); + $@"C:\Users\olduh\dev\MultiDimensionDictionary\MultiDimensionDictionary\expirationalMultiDimensionDictionary\expirationalMulti{i}.cs", g); } From 92b5d0c1d34922cf520cc647a548ac71d8ee757b Mon Sep 17 00:00:00 2001 From: b3b00 Date: Thu, 8 Sep 2022 16:43:21 +0200 Subject: [PATCH 09/12] generator : missing invalidation --- DictionaryGenerator/ExpirationalGenerator.cs | 39 +- ...ionDictionary.cs => ExpirationalMulti1.cs} | 18 +- .../expirationalMulti10.cs | 391 ++++++++++++++++++ ...onDictionary2.cs => expirationalMulti2.cs} | 147 ++++--- ...onDictionary3.cs => expirationalMulti3.cs} | 193 +++++---- ...onDictionary4.cs => expirationalMulti4.cs} | 239 ++++++----- ...onDictionary5.cs => expirationalMulti5.cs} | 247 +++++++---- .../expirationalMulti6.cs | 287 +++++++++++++ .../expirationalMulti7.cs | 313 ++++++++++++++ .../expirationalMulti8.cs | 340 +++++++++++++++ .../expirationalMulti9.cs | 365 ++++++++++++++++ 11 files changed, 2240 insertions(+), 339 deletions(-) rename MultiDimensionDictionary/expirationalMultiDimensionDictionary/{ExpirationalMultiDimensionDictionary.cs => ExpirationalMulti1.cs} (86%) create mode 100644 MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti10.cs rename MultiDimensionDictionary/expirationalMultiDimensionDictionary/{ExpirationalMultiDimensionDictionary2.cs => expirationalMulti2.cs} (70%) rename MultiDimensionDictionary/expirationalMultiDimensionDictionary/{ExpirationalMultiDimensionDictionary3.cs => expirationalMulti3.cs} (61%) rename MultiDimensionDictionary/expirationalMultiDimensionDictionary/{ExpirationalMultiDimensionDictionary4.cs => expirationalMulti4.cs} (58%) rename MultiDimensionDictionary/expirationalMultiDimensionDictionary/{ExpirationalMultiDimensionDictionary5.cs => expirationalMulti5.cs} (54%) create mode 100644 MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti6.cs create mode 100644 MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti7.cs create mode 100644 MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti8.cs create mode 100644 MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti9.cs diff --git a/DictionaryGenerator/ExpirationalGenerator.cs b/DictionaryGenerator/ExpirationalGenerator.cs index aea294c..c5365a7 100644 --- a/DictionaryGenerator/ExpirationalGenerator.cs +++ b/DictionaryGenerator/ExpirationalGenerator.cs @@ -90,7 +90,7 @@ private static string GenerateExpirations(int count) private static string GenerateConstructorMillis(int count) { var builder = new StringBuilder(); - builder.Append("public ExpirationalMulti("); + builder.Append("public ExpirationalMultiDimensionDictionary("); for (int i = 1; i <= count; i++) { builder.Append($"long expiration{i}"); @@ -120,7 +120,7 @@ private static string GenerateConstructorMillis(int count) private static string GenerateConstructorTimeSpan(int count) { var builder = new StringBuilder(); - builder.Append("public ExpirationalMulti("); + builder.Append("public ExpirationalMultiDimensionDictionary("); for (int i = 1; i <= count; i++) { builder.Append($"TimeSpan expiration{i}"); @@ -136,6 +136,9 @@ private static string GenerateConstructorTimeSpan(int count) { builder.Append($"ExpirationSpan{i} = expiration{i};"); } + + builder.AppendLine( + $"Data = new ConcurrentDictionary subData)>();"); builder.Append("}"); return builder.ToString(); } @@ -152,17 +155,17 @@ private static string GenerateClassDeclarationGenerateHeader(int count) { var header = $@" -public class ExpirationalMulti<{GenerateTypeParameters(1, count)}> +public class ExpirationalMultiDimensionDictionary<{GenerateTypeParameters(1, count)}> {{ {GenerateExpirations(count)} {GenerateConstructors(count)} - protected ConcurrentDictionary subData)> Data{{ get;set; }} + protected ConcurrentDictionary subData)> Data{{ get;set; }} - public ExpirationalMulti() : base() + public ExpirationalMultiDimensionDictionary() : base() {{ - Data = new ConcurrentDictionary subData)>(); + Data = new ConcurrentDictionary subData)>(); }} @@ -191,12 +194,18 @@ private static string GenerateContainsKey(int count, int level) { if (level == 1) { - return "public bool ContainsKey(K1 k1) => Data.ContainsKey(k1);"; + return @"public bool ContainsKey(K1 k1) { +Invalidate(); +return Data.ContainsKey(k1); +}"; } else { return - $"public bool ContainsKey({GenerateParametersDeclaration(1, level)}) => Data.ContainsKey(k1) && Data[k1].subData.ContainsKey({GenerateParameters(2, level)});"; + $@"public bool ContainsKey({GenerateParametersDeclaration(1, level)}) {{ +Invalidate(); +return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey({GenerateParameters(2, level)}); +}}"; } } @@ -207,6 +216,7 @@ private static string GenerateGetKeys(int count) var get = $@"public List<({GenerateTypeParameters(1, count, false)})> GetKeys() {{ +Invalidate(); var keys = new List<({GenerateTypeParameters(1, count, false)})>(); foreach (var kvp in Data) {{ @@ -283,10 +293,10 @@ private static string GeneratePut(int count) { var put = $@"public void Put({GenerateParametersDeclaration(1, count)}, V value) {{ - +Invalidate(); {GenerateAsserts(count)} - var secondDimentionData = new ExpirationalMulti<{GenerateTypeParameters(2,count,true)}>({GenerateExpirationParameters(2,count)}); + var secondDimentionData = new ExpirationalMultiDimensionDictionary<{GenerateTypeParameters(2,count,true)}>({GenerateExpirationParameters(2,count)}); if (Data.ContainsKey(k1)) {{ @@ -306,6 +316,7 @@ private static string GenerateGet(int count, int level) { var get = $@"public V Get({GenerateParametersDeclaration(1, level)}) {{ +Invalidate(); {GenerateAsserts(count, false)} if (Data.TryGetValue(k1, out var secondDimensionData)) @@ -320,13 +331,13 @@ private static string GenerateGet(int count, int level) if (level == 1) { - return $@"public ExpirationalMulti<{GenerateTypeParameters(2,count, true)}> Get(K1 k1) + return $@"public ExpirationalMultiDimensionDictionary<{GenerateTypeParameters(2,count, true)}> Get(K1 k1) {{ return Data[k1].subData; }}"; } - return $@"public ExpirationalMulti<{GenerateTypeParameters(level+1,count,true)}> Get({GenerateParametersDeclaration(1,level)}) + return $@"public ExpirationalMultiDimensionDictionary<{GenerateTypeParameters(level+1,count,true)}> Get({GenerateParametersDeclaration(1,level)}) {{ return Data[k1].subData.Get({GenerateParameters(2,level,false)}); }}"; @@ -380,6 +391,7 @@ public static string GenerateRemove(int count, int level) { return @"public new void Remove(K1 k1) { +Invalidate(); Data.Remove(k1, out var ignore); }"; } @@ -388,6 +400,7 @@ public static string GenerateRemove(int count, int level) StringBuilder builder = new StringBuilder(); builder.AppendLine($"public void Remove({GenerateParametersDeclaration(1, level)})") .AppendLine("{") + .AppendLine("Invalidate();") .Append("Data[k1].subData"); for (int i = 2; i < level; i++) { @@ -420,7 +433,7 @@ public static string GenerateInvalidate(int count) public void _Remove(K1 k1) {{ - (DateTime date, ExpirationalMulti<{GenerateTypeParameters(2,count,true)}> subData) ignore = default; + (DateTime date, ExpirationalMultiDimensionDictionary<{GenerateTypeParameters(2,count,true)}> subData) ignore = default; Data.TryRemove(k1, out ignore); }} diff --git a/MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMultiDimensionDictionary.cs b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMulti1.cs similarity index 86% rename from MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMultiDimensionDictionary.cs rename to MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMulti1.cs index dff2a27..0947dfc 100644 --- a/MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMultiDimensionDictionary.cs +++ b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMulti1.cs @@ -9,17 +9,19 @@ public class ExpirationalMultiDimensionDictionary { private TimeSpan ExpirationSpan; - protected ConcurrentDictionary Data { get; set; } + protected ConcurrentDictionary Data { get; set; } - - public ExpirationalMultiDimensionDictionary(long expiration1MMillis) : this (TimeSpan.FromMilliseconds(expiration1MMillis)) + + public ExpirationalMultiDimensionDictionary(long expiration1MMillis) : this( + TimeSpan.FromMilliseconds(expiration1MMillis)) { - + } + public ExpirationalMultiDimensionDictionary(TimeSpan expirationSpan) { ExpirationSpan = expirationSpan; - Data = new ConcurrentDictionary(); + Data = new ConcurrentDictionary(); } public void Invalidate() @@ -73,10 +75,10 @@ public void Remove(K1 k1) Invalidate(); _Remove(k1); } - + public void _Remove(K1 k1) { - (DateTime,V) ignore; + (DateTime, V) ignore; Data.TryRemove(k1, out ignore); } @@ -91,5 +93,5 @@ public void Clear() - + } \ No newline at end of file diff --git a/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti10.cs b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti10.cs new file mode 100644 index 0000000..dc28a7d --- /dev/null +++ b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti10.cs @@ -0,0 +1,391 @@ + + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; + +namespace multiDimensionalDictionary +{ + + + public class ExpirationalMultiDimensionDictionary + { + TimeSpan ExpirationSpan1; + TimeSpan ExpirationSpan2; + TimeSpan ExpirationSpan3; + TimeSpan ExpirationSpan4; + TimeSpan ExpirationSpan5; + TimeSpan ExpirationSpan6; + TimeSpan ExpirationSpan7; + TimeSpan ExpirationSpan8; + TimeSpan ExpirationSpan9; + TimeSpan ExpirationSpan10; + + + + public ExpirationalMultiDimensionDictionary(TimeSpan expiration1, TimeSpan expiration2, TimeSpan expiration3, + TimeSpan expiration4, TimeSpan expiration5, TimeSpan expiration6, TimeSpan expiration7, + TimeSpan expiration8, TimeSpan expiration9, TimeSpan expiration10) + { + ExpirationSpan1 = expiration1; + ExpirationSpan2 = expiration2; + ExpirationSpan3 = expiration3; + ExpirationSpan4 = expiration4; + ExpirationSpan5 = expiration5; + ExpirationSpan6 = expiration6; + ExpirationSpan7 = expiration7; + ExpirationSpan8 = expiration8; + ExpirationSpan9 = expiration9; + ExpirationSpan10 = expiration10; + Data = + new ConcurrentDictionary subData)>(); + } + + public ExpirationalMultiDimensionDictionary(long expiration1, long expiration2, long expiration3, + long expiration4, long expiration5, long expiration6, long expiration7, long expiration8, long expiration9, + long expiration10) : this(TimeSpan.FromMilliseconds(expiration1), TimeSpan.FromMilliseconds(expiration2), + TimeSpan.FromMilliseconds(expiration3), TimeSpan.FromMilliseconds(expiration4), + TimeSpan.FromMilliseconds(expiration5), TimeSpan.FromMilliseconds(expiration6), + TimeSpan.FromMilliseconds(expiration7), TimeSpan.FromMilliseconds(expiration8), + TimeSpan.FromMilliseconds(expiration9), TimeSpan.FromMilliseconds(expiration10)) + { + } + + protected ConcurrentDictionary subData)> Data { get; set; } + + public ExpirationalMultiDimensionDictionary() : base() + { + Data = + new ConcurrentDictionary subData)>(); + } + + + + + #region ContainsKey + + public bool ContainsKey(K1 k1) + { + Invalidate(); + return Data.ContainsKey(k1); + } + + public bool ContainsKey(K1 k1, K2 k2) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5, k6); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5, k6, k7); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5, k6, k7, k8); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5, k6, k7, k8, k9); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9, K10 k10) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5, k6, k7, k8, k9, k10); + } + + + #endregion + + #region getKeys + + public List<(K1, K2, K3, K4, K5, K6, K7, K8, K9, K10)> GetKeys() + { + Invalidate(); + var keys = new List<(K1, K2, K3, K4, K5, K6, K7, K8, K9, K10)>(); + foreach (var kvp in Data) + { + List<(K2, K3, K4, K5, K6, K7, K8, K9, K10)> subkeys = kvp.Value.subData.GetKeys(); + foreach (var subkey in subkeys) + { + keys.Add((kvp.Key, subkey.Item1, subkey.Item2, subkey.Item3, subkey.Item4, subkey.Item5, + subkey.Item6, subkey.Item7, subkey.Item8, subkey.Item9)); + } + } + + return keys; + } + + #endregion + + #region Put + + public void Put(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9, K10 k10, V value) + { + Invalidate(); + DictionaryAssertions.AssertNotNull(k1, "k1"); + + DictionaryAssertions.AssertNotNull(k2, "k2"); + + DictionaryAssertions.AssertNotNull(k3, "k3"); + + DictionaryAssertions.AssertNotNull(k4, "k4"); + + DictionaryAssertions.AssertNotNull(k5, "k5"); + + DictionaryAssertions.AssertNotNull(k6, "k6"); + + DictionaryAssertions.AssertNotNull(k7, "k7"); + + DictionaryAssertions.AssertNotNull(k8, "k8"); + + DictionaryAssertions.AssertNotNull(k9, "k9"); + + DictionaryAssertions.AssertNotNull(k10, "k10"); + + DictionaryAssertions.AssertNotNull(value, "value"); + + + + var secondDimentionData = new ExpirationalMultiDimensionDictionary( + ExpirationSpan2, ExpirationSpan3, ExpirationSpan4, ExpirationSpan5, ExpirationSpan6, ExpirationSpan7, + ExpirationSpan8, ExpirationSpan9, ExpirationSpan10); + + if (Data.ContainsKey(k1)) + { + secondDimentionData = Data[k1].subData; + } + + secondDimentionData.Put(k2, k3, k4, k5, k6, k7, k8, k9, k10, value); + + Data[k1] = (DateTime.Now, secondDimentionData); + } + + #endregion + + #region Get + + public ExpirationalMultiDimensionDictionary Get(K1 k1) + { + return Data[k1].subData; + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2) + { + return Data[k1].subData.Get(k2); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3) + { + return Data[k1].subData.Get(k2, k3); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4) + { + return Data[k1].subData.Get(k2, k3, k4); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + return Data[k1].subData.Get(k2, k3, k4, k5); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + return Data[k1].subData.Get(k2, k3, k4, k5, k6); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) + { + return Data[k1].subData.Get(k2, k3, k4, k5, k6, k7); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, + K8 k8) + { + return Data[k1].subData.Get(k2, k3, k4, k5, k6, k7, k8); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, + K9 k9) + { + return Data[k1].subData.Get(k2, k3, k4, k5, k6, k7, k8, k9); + } + + public V Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9, K10 k10) + { + Invalidate(); + DictionaryAssertions.AssertNotNull(k1, "k1"); + + DictionaryAssertions.AssertNotNull(k2, "k2"); + + DictionaryAssertions.AssertNotNull(k3, "k3"); + + DictionaryAssertions.AssertNotNull(k4, "k4"); + + DictionaryAssertions.AssertNotNull(k5, "k5"); + + DictionaryAssertions.AssertNotNull(k6, "k6"); + + DictionaryAssertions.AssertNotNull(k7, "k7"); + + DictionaryAssertions.AssertNotNull(k8, "k8"); + + DictionaryAssertions.AssertNotNull(k9, "k9"); + + DictionaryAssertions.AssertNotNull(k10, "k10"); + + + + + if (Data.TryGetValue(k1, out var secondDimensionData)) + { + return secondDimensionData.subData.Get(k2, k3, k4, k5, k6, k7, k8, k9, k10); + } + + throw new KeyNotFoundException(); + } + + + #endregion + + #region Remove + + public new void Remove(K1 k1) + { + Invalidate(); + Data.Remove(k1, out var ignore); + } + + public void Remove(K1 k1, K2 k2) + { + Invalidate(); + Data[k1].subData.Remove(k2); + } + + + public void Remove(K1 k1, K2 k2, K3 k3) + { + Invalidate(); + Data[k1].subData.Get(k2).Remove(k3); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Remove(k4); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Remove(k5); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Get(k5).Remove(k6); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Remove(k7); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Get(k7).Remove(k8); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Get(k7).Get(k8).Remove(k9); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9, K10 k10) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Get(k7).Get(k8).Get(k9).Remove(k10); + } + + + + #endregion + + + public void _Remove(K1 k1) + { + (DateTime date, ExpirationalMultiDimensionDictionary subData) + ignore = default; + Data.TryRemove(k1, out ignore); + } + + public void Invalidate() + { + DateTime now = DateTime.Now; + List toRemove = new List(); + foreach (var entry in Data) + { + var age = now - entry.Value.date; + if (age > ExpirationSpan1) + { + toRemove.Add(entry.Key); + } + } + + if (toRemove.Any()) + { + foreach (var k1 in toRemove) + { + _Remove(k1); + } + } + } + } + +} diff --git a/MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMultiDimensionDictionary2.cs b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti2.cs similarity index 70% rename from MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMultiDimensionDictionary2.cs rename to MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti2.cs index 13cb17b..06b10a6 100644 --- a/MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMultiDimensionDictionary2.cs +++ b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti2.cs @@ -1,3 +1,5 @@ + + using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -5,48 +7,43 @@ namespace multiDimensionalDictionary { + + public class ExpirationalMultiDimensionDictionary { - protected ConcurrentDictionary subData)> Data { get; set; } - TimeSpan ExpirationSpan1; TimeSpan ExpirationSpan2; - - public ExpirationalMultiDimensionDictionary(long expiration1MMillis, long expiration2Millis) : this (TimeSpan.FromMilliseconds(expiration1MMillis), TimeSpan.FromMilliseconds(expiration2Millis)) + + + + public ExpirationalMultiDimensionDictionary(TimeSpan expiration1, TimeSpan expiration2) { - + ExpirationSpan1 = expiration1; + ExpirationSpan2 = expiration2; + Data = new ConcurrentDictionary subData)>(); } - - public ExpirationalMultiDimensionDictionary(TimeSpan expirationSpan1, TimeSpan expirationSpan2) + + public ExpirationalMultiDimensionDictionary(long expiration1, long expiration2) : this( + TimeSpan.FromMilliseconds(expiration1), TimeSpan.FromMilliseconds(expiration2)) { - ExpirationSpan1 = expirationSpan1; - ExpirationSpan2 = expirationSpan2; - Data = new ConcurrentDictionary subData)>(); } - public void Invalidate() + protected ConcurrentDictionary subData)> Data { - DateTime now = DateTime.Now; - List toRemove = new List(); - foreach (var entry in Data) - { - var age = now - entry.Value.date; - if (age > ExpirationSpan1) - { - toRemove.Add(entry.Key); - } - } + get; + set; + } - if (toRemove.Any()) - { - foreach (var k1 in toRemove) - { - _Remove(k1); - } - } + public ExpirationalMultiDimensionDictionary() : base() + { + Data = new ConcurrentDictionary subData)>(); } + + + #region ContainsKey + public bool ContainsKey(K1 k1) { Invalidate(); @@ -58,11 +55,16 @@ public bool ContainsKey(K1 k1, K2 k2) Invalidate(); return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2); } - + + + #endregion + + #region getKeys public List<(K1, K2)> GetKeys() { - List<(K1, K2)> keys = new List<(K1, K2)>(); + Invalidate(); + var keys = new List<(K1, K2)>(); foreach (var kvp in Data) { List subkeys = kvp.Value.subData.GetKeys(); @@ -71,44 +73,43 @@ public List<(K1, K2)> GetKeys() keys.Add((kvp.Key, subkey)); } } + return keys; } + #endregion + + #region Put + public void Put(K1 k1, K2 k2, V value) { - Invalidate(); - DictionaryAssertions.AssertNotNull(k1, "k1"); DictionaryAssertions.AssertNotNull(k2, "k2"); DictionaryAssertions.AssertNotNull(value, "value"); - ExpirationalMultiDimensionDictionary secondDimentionData = new ExpirationalMultiDimensionDictionary(ExpirationSpan2); + var secondDimentionData = new ExpirationalMultiDimensionDictionary(ExpirationSpan2); if (Data.ContainsKey(k1)) { secondDimentionData = Data[k1].subData; } - if (secondDimentionData.ContainsKey(k2)) - { - throw new ArgumentException($"dictionary already contains key {k1};{k2}"); - } - else - { - secondDimentionData.Put(k2, value); - } + secondDimentionData.Put(k2, value); Data[k1] = (DateTime.Now, secondDimentionData); } + #endregion + + #region Get + public ExpirationalMultiDimensionDictionary Get(K1 k1) { - Invalidate(); return Data[k1].subData; } @@ -118,41 +119,67 @@ public V Get(K1 k1, K2 k2) DictionaryAssertions.AssertNotNull(k1, "k1"); DictionaryAssertions.AssertNotNull(k2, "k2"); - - if (Data.TryGetValue(k1, out (DateTime date, ExpirationalMultiDimensionDictionary subData) secondDimensionData )) - { - if (secondDimensionData.subData.ContainsKey(k2)) - { - var value = secondDimensionData.subData.Get(k2); - return value; - } + + + + if (Data.TryGetValue(k1, out var secondDimensionData)) + { + return secondDimensionData.subData.Get(k2); } throw new KeyNotFoundException(); } - public void Remove(K1 k1) + + #endregion + + #region Remove + + public new void Remove(K1 k1) { Invalidate(); - _Remove(k1); + Data.Remove(k1, out var ignore); } + public void Remove(K1 k1, K2 k2) + { + Invalidate(); + Data[k1].subData.Remove(k2); + } + + + + #endregion + + public void _Remove(K1 k1) { (DateTime date, ExpirationalMultiDimensionDictionary subData) ignore = default; Data.TryRemove(k1, out ignore); } - public void Remove(K1 k1, K2 k2) + public void Invalidate() { - Invalidate(); - Data[k1].subData.Remove(k2); - } + DateTime now = DateTime.Now; + List toRemove = new List(); + foreach (var entry in Data) + { + var age = now - entry.Value.date; + if (age > ExpirationSpan1) + { + toRemove.Add(entry.Key); + } + } - public void Clear() - { - Data.Clear(); + if (toRemove.Any()) + { + foreach (var k1 in toRemove) + { + _Remove(k1); + } + } } } -} \ No newline at end of file + +} diff --git a/MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMultiDimensionDictionary3.cs b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti3.cs similarity index 61% rename from MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMultiDimensionDictionary3.cs rename to MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti3.cs index a6b02a3..5d0528a 100644 --- a/MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMultiDimensionDictionary3.cs +++ b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti3.cs @@ -1,3 +1,5 @@ + + using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -5,163 +7,206 @@ namespace multiDimensionalDictionary { + + public class ExpirationalMultiDimensionDictionary { - TimeSpan ExpirationSpan1; TimeSpan ExpirationSpan2; TimeSpan ExpirationSpan3; - - protected ConcurrentDictionary data)> Data { get; set; } - - public ExpirationalMultiDimensionDictionary(TimeSpan expirationSpan1, TimeSpan expirationSpan2, TimeSpan expirationSpan3) : base() + + + + public ExpirationalMultiDimensionDictionary(TimeSpan expiration1, TimeSpan expiration2, TimeSpan expiration3) { - ExpirationSpan1 = expirationSpan1; - ExpirationSpan2 = expirationSpan2; - ExpirationSpan3 = expirationSpan3; - Data = new ConcurrentDictionary data)>(); + ExpirationSpan1 = expiration1; + ExpirationSpan2 = expiration2; + ExpirationSpan3 = expiration3; + Data = + new ConcurrentDictionary subData + )>(); } - public ExpirationalMultiDimensionDictionary(long expiration1MMillis, long expiration2Millis, - long expiration3Millis) : this (TimeSpan.FromMilliseconds(expiration1MMillis), TimeSpan.FromMilliseconds(expiration2Millis), TimeSpan.FromMilliseconds(expiration3Millis)) + public ExpirationalMultiDimensionDictionary(long expiration1, long expiration2, long expiration3) : this( + TimeSpan.FromMilliseconds(expiration1), TimeSpan.FromMilliseconds(expiration2), + TimeSpan.FromMilliseconds(expiration3)) { - } - - public void Invalidate() - { - DateTime now = DateTime.Now; - List toRemove = new List(); - foreach (var entry in Data) - { - var age = now - entry.Value.date; - if (age > ExpirationSpan1) - { - toRemove.Add(entry.Key); - } - } - if (toRemove.Any()) - { - foreach (var k1 in toRemove) - { - _Remove(k1); - } - } - } - - public void _Remove(K1 k1) + protected ConcurrentDictionary subData)> + Data { get; set; } + + public ExpirationalMultiDimensionDictionary() : base() { - (DateTime date, ExpirationalMultiDimensionDictionary data) ignore; - Data.TryRemove(k1, out ignore); + Data = + new ConcurrentDictionary subData + )>(); } + + + + #region ContainsKey + public bool ContainsKey(K1 k1) { Invalidate(); return Data.ContainsKey(k1); } - + public bool ContainsKey(K1 k1, K2 k2) { Invalidate(); - return Data.ContainsKey(k1) && Data[k1].data.ContainsKey(k2); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2); } public bool ContainsKey(K1 k1, K2 k2, K3 k3) { Invalidate(); - return Data.ContainsKey(k1) && Data[k1].data.ContainsKey(k2, k3); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3); } + + #endregion + + #region getKeys + public List<(K1, K2, K3)> GetKeys() { Invalidate(); - List<(K1, K2, K3)> keys = new List<(K1, K2, K3)>(); + var keys = new List<(K1, K2, K3)>(); foreach (var kvp in Data) { - List<(K2, K3)> subkeys = kvp.Value.data.GetKeys(); + List<(K2, K3)> subkeys = kvp.Value.subData.GetKeys(); foreach (var subkey in subkeys) { keys.Add((kvp.Key, subkey.Item1, subkey.Item2)); } } + return keys; } - + + #endregion + + #region Put + public void Put(K1 k1, K2 k2, K3 k3, V value) { Invalidate(); - DictionaryAssertions.AssertNotNull(k1, "k1"); - + DictionaryAssertions.AssertNotNull(k2, "k2"); - + DictionaryAssertions.AssertNotNull(k3, "k3"); - + DictionaryAssertions.AssertNotNull(value, "value"); - - var secondDimentionData = new ExpirationalMultiDimensionDictionary(ExpirationSpan2,ExpirationSpan3); - - - + + + + var secondDimentionData = + new ExpirationalMultiDimensionDictionary(ExpirationSpan2, ExpirationSpan3); + if (Data.ContainsKey(k1)) { - secondDimentionData = Data[k1].data; + secondDimentionData = Data[k1].subData; } - + secondDimentionData.Put(k2, k3, value); - + Data[k1] = (DateTime.Now, secondDimentionData); } - + + #endregion + + #region Get + public ExpirationalMultiDimensionDictionary Get(K1 k1) { - Invalidate(); - return Data[k1].data; + return Data[k1].subData; } - + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2) { - Invalidate(); - return Data[k1].data.Get(k2); + return Data[k1].subData.Get(k2); } - + public V Get(K1 k1, K2 k2, K3 k3) { Invalidate(); DictionaryAssertions.AssertNotNull(k1, "k1"); + DictionaryAssertions.AssertNotNull(k2, "k2"); + DictionaryAssertions.AssertNotNull(k3, "k3"); - if (Data.TryGetValue(k1, out (DateTime date, ExpirationalMultiDimensionDictionary data) secondDimensionData)) + + + + if (Data.TryGetValue(k1, out var secondDimensionData)) { - return secondDimensionData.data.Get(k2, k3); + return secondDimensionData.subData.Get(k2, k3); } + throw new KeyNotFoundException(); } - public void Remove(K1 k1) + + #endregion + + #region Remove + + public new void Remove(K1 k1) { Invalidate(); - _Remove(k1); + Data.Remove(k1, out var ignore); } - + public void Remove(K1 k1, K2 k2) { Invalidate(); - Data[k1].data.Remove(k2); + Data[k1].subData.Remove(k2); } - + + public void Remove(K1 k1, K2 k2, K3 k3) { - Data[k1].data.Get(k2).Remove(k3); + Invalidate(); + Data[k1].subData.Get(k2).Remove(k3); + } + + + + #endregion + + + public void _Remove(K1 k1) + { + (DateTime date, ExpirationalMultiDimensionDictionary subData) ignore = default; + Data.TryRemove(k1, out ignore); } - - public void Clear() + + public void Invalidate() { - Data.Clear(); + DateTime now = DateTime.Now; + List toRemove = new List(); + foreach (var entry in Data) + { + var age = now - entry.Value.date; + if (age > ExpirationSpan1) + { + toRemove.Add(entry.Key); + } + } + + if (toRemove.Any()) + { + foreach (var k1 in toRemove) + { + _Remove(k1); + } + } } } -} \ No newline at end of file +} diff --git a/MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMultiDimensionDictionary4.cs b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti4.cs similarity index 58% rename from MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMultiDimensionDictionary4.cs rename to MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti4.cs index c9e0fb2..d08c220 100644 --- a/MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMultiDimensionDictionary4.cs +++ b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti4.cs @@ -1,3 +1,5 @@ + + using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -5,193 +7,232 @@ namespace multiDimensionalDictionary { - public class ExpirationalMultiDimensionDictionary + + + public class ExpirationalMultiDimensionDictionary { - protected ConcurrentDictionary data)> Data { get; set; } - TimeSpan ExpirationSpan1; TimeSpan ExpirationSpan2; TimeSpan ExpirationSpan3; TimeSpan ExpirationSpan4; - - public ExpirationalMultiDimensionDictionary(long expiration1MMillis, long expiration2Millis, - long expiration3Millis, long expieration4Millis) : this (TimeSpan.FromMilliseconds(expiration1MMillis), TimeSpan.FromMilliseconds(expiration2Millis), TimeSpan.FromMilliseconds(expiration3Millis), TimeSpan.FromMilliseconds(expieration4Millis)) + + + + public ExpirationalMultiDimensionDictionary(TimeSpan expiration1, TimeSpan expiration2, TimeSpan expiration3, + TimeSpan expiration4) { - + ExpirationSpan1 = expiration1; + ExpirationSpan2 = expiration2; + ExpirationSpan3 = expiration3; + ExpirationSpan4 = expiration4; + Data = + new ConcurrentDictionary subData + )>(); } - - public ExpirationalMultiDimensionDictionary(TimeSpan expirationSpan1, TimeSpan expirationSpan2, TimeSpan expirationSpan3, TimeSpan expirationSpan4) + + public ExpirationalMultiDimensionDictionary(long expiration1, long expiration2, long expiration3, + long expiration4) : this(TimeSpan.FromMilliseconds(expiration1), TimeSpan.FromMilliseconds(expiration2), + TimeSpan.FromMilliseconds(expiration3), TimeSpan.FromMilliseconds(expiration4)) { - Data = new ConcurrentDictionary data)>(); - ExpirationSpan1 = expirationSpan1; - ExpirationSpan2 = expirationSpan2; - ExpirationSpan3 = expirationSpan3; - ExpirationSpan4 = expirationSpan4; } - - - public void Invalidate() - { - - DateTime now = DateTime.Now; - List toRemove = new List(); - foreach (var entry in Data) - { - var age = now - entry.Value.date; - if (age > ExpirationSpan1) - { - toRemove.Add(entry.Key); - } - } - if (toRemove.Any()) - { - foreach (var k1 in toRemove) - { - _Remove(k1); - } - } + protected ConcurrentDictionary subData)> + Data { get; set; } + + public ExpirationalMultiDimensionDictionary() : base() + { + Data = + new ConcurrentDictionary subData + )>(); } + + + + #region ContainsKey + public bool ContainsKey(K1 k1) { Invalidate(); - return Data.ContainsKey(k1); - } - - public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) + return Data.ContainsKey(k1); + } + + public bool ContainsKey(K1 k1, K2 k2) { Invalidate(); - bool containsK1 = Data.ContainsKey(k1); - bool containsChildren = Data[k1].data.ContainsKey(k2, k3, k4); - return containsK1 && containsChildren; + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2); } public bool ContainsKey(K1 k1, K2 k2, K3 k3) { Invalidate(); - var containsKey = Data.ContainsKey(k1); - var containsChildren = Data[k1].data.ContainsKey(k2, k3); - return containsKey && containsChildren; + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3); } - public bool ContainsKey(K1 k1, K2 k2) + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) { Invalidate(); - return Data.ContainsKey(k1) && Data[k1].data.ContainsKey(k2); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4); } - - + + + #endregion + + #region getKeys + public List<(K1, K2, K3, K4)> GetKeys() { Invalidate(); - List<(K1, K2, K3, K4)> keys = new List<(K1, K2, K3, K4)>(); + var keys = new List<(K1, K2, K3, K4)>(); foreach (var kvp in Data) { - List<(K2, K3, K4)> subkeys = kvp.Value.data.GetKeys(); + List<(K2, K3, K4)> subkeys = kvp.Value.subData.GetKeys(); foreach (var subkey in subkeys) { keys.Add((kvp.Key, subkey.Item1, subkey.Item2, subkey.Item3)); } } + return keys; } - + + #endregion + + #region Put + public void Put(K1 k1, K2 k2, K3 k3, K4 k4, V value) { Invalidate(); DictionaryAssertions.AssertNotNull(k1, "k1"); - + DictionaryAssertions.AssertNotNull(k2, "k2"); - + DictionaryAssertions.AssertNotNull(k3, "k3"); - + DictionaryAssertions.AssertNotNull(k4, "k4"); - + DictionaryAssertions.AssertNotNull(value, "value"); - - var secondDimentionData = new ExpirationalMultiDimensionDictionary(ExpirationSpan2,ExpirationSpan3,ExpirationSpan4); - + + + + var secondDimentionData = + new ExpirationalMultiDimensionDictionary(ExpirationSpan2, ExpirationSpan3, + ExpirationSpan4); + if (Data.ContainsKey(k1)) { - secondDimentionData = Data[k1].data; + secondDimentionData = Data[k1].subData; } - - secondDimentionData.Put(k2, k3, k4, value); ; - + + secondDimentionData.Put(k2, k3, k4, value); + Data[k1] = (DateTime.Now, secondDimentionData); } - - + + #endregion + + #region Get + public ExpirationalMultiDimensionDictionary Get(K1 k1) { - Invalidate(); - return Data[k1].data; + return Data[k1].subData; } - + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2) { - Invalidate(); - return Data[k1].data.Get(k2); + return Data[k1].subData.Get(k2); } - + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3) { - Invalidate(); - return Data[k1].data.Get(k2).Get(k3); + return Data[k1].subData.Get(k2, k3); } - + public V Get(K1 k1, K2 k2, K3 k3, K4 k4) { Invalidate(); DictionaryAssertions.AssertNotNull(k1, "k1"); + DictionaryAssertions.AssertNotNull(k2, "k2"); + DictionaryAssertions.AssertNotNull(k3, "k3"); + DictionaryAssertions.AssertNotNull(k4, "k4"); - - (DateTime date, ExpirationalMultiDimensionDictionary data) secondDimensionData; - if (Data.TryGetValue(k1, out secondDimensionData)) + + + + + if (Data.TryGetValue(k1, out var secondDimensionData)) { - return secondDimensionData.data.Get(k2, k3, k4); + return secondDimensionData.subData.Get(k2, k3, k4); } - + throw new KeyNotFoundException(); } - - public void Remove(K1 k1) + + + #endregion + + #region Remove + + public new void Remove(K1 k1) { Invalidate(); - _Remove(k1); - } - - public void _Remove(K1 k1) - { - (DateTime date, ExpirationalMultiDimensionDictionary subData) ignore = default; - Data.TryRemove(k1, out ignore); + Data.Remove(k1, out var ignore); } - + public void Remove(K1 k1, K2 k2) { Invalidate(); - Data[k1].data.Remove(k2); + Data[k1].subData.Remove(k2); } - + + public void Remove(K1 k1, K2 k2, K3 k3) { Invalidate(); - Data[k1].data.Get(k2).Remove(k3); + Data[k1].subData.Get(k2).Remove(k3); } - + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4) { Invalidate(); - Data[k1].data.Get(k2).Get(k3).Remove(k4); + Data[k1].subData.Get(k2).Get(k3).Remove(k4); } - - public void Clear() + + + + #endregion + + + public void _Remove(K1 k1) { - Data.Clear(); + (DateTime date, ExpirationalMultiDimensionDictionary subData) ignore = default; + Data.TryRemove(k1, out ignore); + } + + public void Invalidate() + { + DateTime now = DateTime.Now; + List toRemove = new List(); + foreach (var entry in Data) + { + var age = now - entry.Value.date; + if (age > ExpirationSpan1) + { + toRemove.Add(entry.Key); + } + } + + if (toRemove.Any()) + { + foreach (var k1 in toRemove) + { + _Remove(k1); + } + } } } -} \ No newline at end of file +} diff --git a/MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMultiDimensionDictionary5.cs b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti5.cs similarity index 54% rename from MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMultiDimensionDictionary5.cs rename to MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti5.cs index 03ccd42..3b3adba 100644 --- a/MultiDimensionDictionary/expirationalMultiDimensionDictionary/ExpirationalMultiDimensionDictionary5.cs +++ b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti5.cs @@ -1,3 +1,5 @@ + + using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -5,181 +7,256 @@ namespace multiDimensionalDictionary { - public class ExpirationalMultiDimensionDictionary + + + public class ExpirationalMultiDimensionDictionary { - protected ConcurrentDictionary data)> Data { get; set; } TimeSpan ExpirationSpan1; TimeSpan ExpirationSpan2; TimeSpan ExpirationSpan3; TimeSpan ExpirationSpan4; TimeSpan ExpirationSpan5; - - public ExpirationalMultiDimensionDictionary(long expiration1MMillis, long expiration2Millis, - long expiration3Millis, long expieration4Millis, long expiration5Millis) : this (TimeSpan.FromMilliseconds(expiration1MMillis), TimeSpan.FromMilliseconds(expiration2Millis), TimeSpan.FromMilliseconds(expiration3Millis), TimeSpan.FromMilliseconds(expieration4Millis), TimeSpan.FromMilliseconds(expiration5Millis)) + + + + public ExpirationalMultiDimensionDictionary(TimeSpan expiration1, TimeSpan expiration2, TimeSpan expiration3, + TimeSpan expiration4, TimeSpan expiration5) { - + ExpirationSpan1 = expiration1; + ExpirationSpan2 = expiration2; + ExpirationSpan3 = expiration3; + ExpirationSpan4 = expiration4; + ExpirationSpan5 = expiration5; + Data = + new ConcurrentDictionary + subData)>(); } - - public ExpirationalMultiDimensionDictionary(TimeSpan expirationSpan1, TimeSpan expirationSpan2, TimeSpan expirationSpan3, TimeSpan expirationSpan4, TimeSpan expirationSpan5) + + public ExpirationalMultiDimensionDictionary(long expiration1, long expiration2, long expiration3, + long expiration4, long expiration5) : this(TimeSpan.FromMilliseconds(expiration1), + TimeSpan.FromMilliseconds(expiration2), TimeSpan.FromMilliseconds(expiration3), + TimeSpan.FromMilliseconds(expiration4), TimeSpan.FromMilliseconds(expiration5)) { - Data = new ConcurrentDictionary data)>(); - ExpirationSpan1 = expirationSpan1; - ExpirationSpan2 = expirationSpan2; - ExpirationSpan3 = expirationSpan3; - ExpirationSpan4 = expirationSpan4; - ExpirationSpan5 = expirationSpan5; } - - - public void Invalidate() - { - - DateTime now = DateTime.Now; - List toRemove = new List(); - foreach (var entry in Data) - { - var age = now - entry.Value.date; - if (age > ExpirationSpan1) - { - toRemove.Add(entry.Key); - } - } - if (toRemove.Any()) - { - foreach (var k1 in toRemove) - { - _Remove(k1); - } - } + protected ConcurrentDictionary + subData)> Data { get; set; } + + public ExpirationalMultiDimensionDictionary() : base() + { + Data = + new ConcurrentDictionary + subData)>(); } + + + + #region ContainsKey + public bool ContainsKey(K1 k1) { Invalidate(); - return Data.ContainsKey(k1); + return Data.ContainsKey(k1); } - public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) { + + public bool ContainsKey(K1 k1, K2 k2) + { Invalidate(); - bool containsK1 = Data.ContainsKey(k1); - bool containsChildren = Data[k1].data.ContainsKey(k2, k3, k4, k5); - return containsK1 && containsChildren; + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2); } - public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) + public bool ContainsKey(K1 k1, K2 k2, K3 k3) { Invalidate(); - bool containsK1 = Data.ContainsKey(k1); - bool containsChildren = Data[k1].data.ContainsKey(k2, k3, k4); - return containsK1 && containsChildren; + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3); } - public bool ContainsKey(K1 k1, K2 k2, K3 k3) + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) { Invalidate(); - var containsKey = Data.ContainsKey(k1); - var containsChildren = Data[k1].data.ContainsKey(k2, k3); - return containsKey && containsChildren; + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4); } - public bool ContainsKey(K1 k1, K2 k2) + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) { Invalidate(); - return Data.ContainsKey(k1) && Data[k1].data.ContainsKey(k2); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5); } - - + + + #endregion + + #region getKeys + public List<(K1, K2, K3, K4, K5)> GetKeys() { Invalidate(); - List<(K1, K2, K3, K4, K5)> keys = new List<(K1, K2, K3, K4, K5)>(); + var keys = new List<(K1, K2, K3, K4, K5)>(); foreach (var kvp in Data) { - List<(K2, K3, K4, K5)> subkeys = kvp.Value.data.GetKeys(); + List<(K2, K3, K4, K5)> subkeys = kvp.Value.subData.GetKeys(); foreach (var subkey in subkeys) { keys.Add((kvp.Key, subkey.Item1, subkey.Item2, subkey.Item3, subkey.Item4)); } } + return keys; } - + + #endregion + + #region Put + public void Put(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, V value) { Invalidate(); DictionaryAssertions.AssertNotNull(k1, "k1"); + DictionaryAssertions.AssertNotNull(k2, "k2"); + DictionaryAssertions.AssertNotNull(k3, "k3"); + DictionaryAssertions.AssertNotNull(k4, "k4"); + DictionaryAssertions.AssertNotNull(k5, "k5"); + DictionaryAssertions.AssertNotNull(value, "value"); - var secondDimentionData = new ExpirationalMultiDimensionDictionary(ExpirationSpan2,ExpirationSpan3,ExpirationSpan4, ExpirationSpan5); + + + var secondDimentionData = new ExpirationalMultiDimensionDictionary(ExpirationSpan2, + ExpirationSpan3, ExpirationSpan4, ExpirationSpan5); if (Data.ContainsKey(k1)) { - secondDimentionData = Data[k1].data; + secondDimentionData = Data[k1].subData; } - secondDimentionData.Put(k2, k3, k4, k5, value); ; + + secondDimentionData.Put(k2, k3, k4, k5, value); + Data[k1] = (DateTime.Now, secondDimentionData); } + + #endregion + + #region Get + public ExpirationalMultiDimensionDictionary Get(K1 k1) { - Invalidate(); - return Data[k1].data; + return Data[k1].subData; } public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2) { - Invalidate(); - return Data[k1].data.Get(k2); + return Data[k1].subData.Get(k2); } public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3) { - Invalidate(); - DictionaryAssertions.AssertNotNull(k1, "k1"); - DictionaryAssertions.AssertNotNull(k2, "k2"); - DictionaryAssertions.AssertNotNull(k3, "k3"); - return Data[k1].data.Get(k2).Get(k3); + return Data[k1].subData.Get(k2, k3); } - + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4) { - Invalidate(); - return Data[k1].data.Get(k2).Get(k3).Get(k4); + return Data[k1].subData.Get(k2, k3, k4); } - public void Remove(K1 k1) + + public V Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) { Invalidate(); - _Remove(k1); + DictionaryAssertions.AssertNotNull(k1, "k1"); + + DictionaryAssertions.AssertNotNull(k2, "k2"); + + DictionaryAssertions.AssertNotNull(k3, "k3"); + + DictionaryAssertions.AssertNotNull(k4, "k4"); + + DictionaryAssertions.AssertNotNull(k5, "k5"); + + + + + if (Data.TryGetValue(k1, out var secondDimensionData)) + { + return secondDimensionData.subData.Get(k2, k3, k4, k5); + } + + throw new KeyNotFoundException(); } - - public void _Remove(K1 k1) + + + #endregion + + #region Remove + + public new void Remove(K1 k1) { - (DateTime date, ExpirationalMultiDimensionDictionary subData) ignore = default; - Data.TryRemove(k1, out ignore); + Invalidate(); + Data.Remove(k1, out var ignore); } - + public void Remove(K1 k1, K2 k2) { Invalidate(); - Data[k1].data.Remove(k2); + Data[k1].subData.Remove(k2); } - + + public void Remove(K1 k1, K2 k2, K3 k3) { Invalidate(); - Data[k1].data.Get(k2).Remove(k3); + Data[k1].subData.Get(k2).Remove(k3); } + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4) { Invalidate(); - Data[k1].data.Get(k2).Get(k3).Remove(k4); + Data[k1].subData.Get(k2).Get(k3).Remove(k4); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Remove(k5); } - public void Clear() + + + + #endregion + + + public void _Remove(K1 k1) { - Data.Clear(); + (DateTime date, ExpirationalMultiDimensionDictionary subData) ignore = default; + Data.TryRemove(k1, out ignore); + } + + public void Invalidate() + { + DateTime now = DateTime.Now; + List toRemove = new List(); + foreach (var entry in Data) + { + var age = now - entry.Value.date; + if (age > ExpirationSpan1) + { + toRemove.Add(entry.Key); + } + } + + if (toRemove.Any()) + { + foreach (var k1 in toRemove) + { + _Remove(k1); + } + } } } -} \ No newline at end of file + +} diff --git a/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti6.cs b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti6.cs new file mode 100644 index 0000000..249935f --- /dev/null +++ b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti6.cs @@ -0,0 +1,287 @@ + + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; + +namespace multiDimensionalDictionary +{ + + + public class ExpirationalMultiDimensionDictionary + { + TimeSpan ExpirationSpan1; + TimeSpan ExpirationSpan2; + TimeSpan ExpirationSpan3; + TimeSpan ExpirationSpan4; + TimeSpan ExpirationSpan5; + TimeSpan ExpirationSpan6; + + + + public ExpirationalMultiDimensionDictionary(TimeSpan expiration1, TimeSpan expiration2, TimeSpan expiration3, + TimeSpan expiration4, TimeSpan expiration5, TimeSpan expiration6) + { + ExpirationSpan1 = expiration1; + ExpirationSpan2 = expiration2; + ExpirationSpan3 = expiration3; + ExpirationSpan4 = expiration4; + ExpirationSpan5 = expiration5; + ExpirationSpan6 = expiration6; + Data = + new ConcurrentDictionary + subData)>(); + } + + public ExpirationalMultiDimensionDictionary(long expiration1, long expiration2, long expiration3, + long expiration4, long expiration5, long expiration6) : this(TimeSpan.FromMilliseconds(expiration1), + TimeSpan.FromMilliseconds(expiration2), TimeSpan.FromMilliseconds(expiration3), + TimeSpan.FromMilliseconds(expiration4), TimeSpan.FromMilliseconds(expiration5), + TimeSpan.FromMilliseconds(expiration6)) + { + } + + protected ConcurrentDictionary + subData)> Data { get; set; } + + public ExpirationalMultiDimensionDictionary() : base() + { + Data = + new ConcurrentDictionary + subData)>(); + } + + + + + #region ContainsKey + + public bool ContainsKey(K1 k1) + { + Invalidate(); + return Data.ContainsKey(k1); + } + + public bool ContainsKey(K1 k1, K2 k2) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5, k6); + } + + + #endregion + + #region getKeys + + public List<(K1, K2, K3, K4, K5, K6)> GetKeys() + { + Invalidate(); + var keys = new List<(K1, K2, K3, K4, K5, K6)>(); + foreach (var kvp in Data) + { + List<(K2, K3, K4, K5, K6)> subkeys = kvp.Value.subData.GetKeys(); + foreach (var subkey in subkeys) + { + keys.Add((kvp.Key, subkey.Item1, subkey.Item2, subkey.Item3, subkey.Item4, subkey.Item5)); + } + } + + return keys; + } + + #endregion + + #region Put + + public void Put(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, V value) + { + Invalidate(); + DictionaryAssertions.AssertNotNull(k1, "k1"); + + DictionaryAssertions.AssertNotNull(k2, "k2"); + + DictionaryAssertions.AssertNotNull(k3, "k3"); + + DictionaryAssertions.AssertNotNull(k4, "k4"); + + DictionaryAssertions.AssertNotNull(k5, "k5"); + + DictionaryAssertions.AssertNotNull(k6, "k6"); + + DictionaryAssertions.AssertNotNull(value, "value"); + + + + var secondDimentionData = new ExpirationalMultiDimensionDictionary(ExpirationSpan2, + ExpirationSpan3, ExpirationSpan4, ExpirationSpan5, ExpirationSpan6); + + if (Data.ContainsKey(k1)) + { + secondDimentionData = Data[k1].subData; + } + + secondDimentionData.Put(k2, k3, k4, k5, k6, value); + + Data[k1] = (DateTime.Now, secondDimentionData); + } + + #endregion + + #region Get + + public ExpirationalMultiDimensionDictionary Get(K1 k1) + { + return Data[k1].subData; + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2) + { + return Data[k1].subData.Get(k2); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3) + { + return Data[k1].subData.Get(k2, k3); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4) + { + return Data[k1].subData.Get(k2, k3, k4); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + return Data[k1].subData.Get(k2, k3, k4, k5); + } + + public V Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + Invalidate(); + DictionaryAssertions.AssertNotNull(k1, "k1"); + + DictionaryAssertions.AssertNotNull(k2, "k2"); + + DictionaryAssertions.AssertNotNull(k3, "k3"); + + DictionaryAssertions.AssertNotNull(k4, "k4"); + + DictionaryAssertions.AssertNotNull(k5, "k5"); + + DictionaryAssertions.AssertNotNull(k6, "k6"); + + + + + if (Data.TryGetValue(k1, out var secondDimensionData)) + { + return secondDimensionData.subData.Get(k2, k3, k4, k5, k6); + } + + throw new KeyNotFoundException(); + } + + + #endregion + + #region Remove + + public new void Remove(K1 k1) + { + Invalidate(); + Data.Remove(k1, out var ignore); + } + + public void Remove(K1 k1, K2 k2) + { + Invalidate(); + Data[k1].subData.Remove(k2); + } + + + public void Remove(K1 k1, K2 k2, K3 k3) + { + Invalidate(); + Data[k1].subData.Get(k2).Remove(k3); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Remove(k4); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Remove(k5); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Get(k5).Remove(k6); + } + + + + #endregion + + + public void _Remove(K1 k1) + { + (DateTime date, ExpirationalMultiDimensionDictionary subData) ignore = default; + Data.TryRemove(k1, out ignore); + } + + public void Invalidate() + { + DateTime now = DateTime.Now; + List toRemove = new List(); + foreach (var entry in Data) + { + var age = now - entry.Value.date; + if (age > ExpirationSpan1) + { + toRemove.Add(entry.Key); + } + } + + if (toRemove.Any()) + { + foreach (var k1 in toRemove) + { + _Remove(k1); + } + } + } + } + +} diff --git a/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti7.cs b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti7.cs new file mode 100644 index 0000000..978b298 --- /dev/null +++ b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti7.cs @@ -0,0 +1,313 @@ + + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; + +namespace multiDimensionalDictionary +{ + + + public class ExpirationalMultiDimensionDictionary + { + TimeSpan ExpirationSpan1; + TimeSpan ExpirationSpan2; + TimeSpan ExpirationSpan3; + TimeSpan ExpirationSpan4; + TimeSpan ExpirationSpan5; + TimeSpan ExpirationSpan6; + TimeSpan ExpirationSpan7; + + + + public ExpirationalMultiDimensionDictionary(TimeSpan expiration1, TimeSpan expiration2, TimeSpan expiration3, + TimeSpan expiration4, TimeSpan expiration5, TimeSpan expiration6, TimeSpan expiration7) + { + ExpirationSpan1 = expiration1; + ExpirationSpan2 = expiration2; + ExpirationSpan3 = expiration3; + ExpirationSpan4 = expiration4; + ExpirationSpan5 = expiration5; + ExpirationSpan6 = expiration6; + ExpirationSpan7 = expiration7; + Data = + new ConcurrentDictionary subData)>(); + } + + public ExpirationalMultiDimensionDictionary(long expiration1, long expiration2, long expiration3, + long expiration4, long expiration5, long expiration6, long expiration7) : this( + TimeSpan.FromMilliseconds(expiration1), TimeSpan.FromMilliseconds(expiration2), + TimeSpan.FromMilliseconds(expiration3), TimeSpan.FromMilliseconds(expiration4), + TimeSpan.FromMilliseconds(expiration5), TimeSpan.FromMilliseconds(expiration6), + TimeSpan.FromMilliseconds(expiration7)) + { + } + + protected ConcurrentDictionary subData)> Data { get; set; } + + public ExpirationalMultiDimensionDictionary() : base() + { + Data = + new ConcurrentDictionary subData)>(); + } + + + + + #region ContainsKey + + public bool ContainsKey(K1 k1) + { + Invalidate(); + return Data.ContainsKey(k1); + } + + public bool ContainsKey(K1 k1, K2 k2) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5, k6); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5, k6, k7); + } + + + #endregion + + #region getKeys + + public List<(K1, K2, K3, K4, K5, K6, K7)> GetKeys() + { + Invalidate(); + var keys = new List<(K1, K2, K3, K4, K5, K6, K7)>(); + foreach (var kvp in Data) + { + List<(K2, K3, K4, K5, K6, K7)> subkeys = kvp.Value.subData.GetKeys(); + foreach (var subkey in subkeys) + { + keys.Add((kvp.Key, subkey.Item1, subkey.Item2, subkey.Item3, subkey.Item4, subkey.Item5, + subkey.Item6)); + } + } + + return keys; + } + + #endregion + + #region Put + + public void Put(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, V value) + { + Invalidate(); + DictionaryAssertions.AssertNotNull(k1, "k1"); + + DictionaryAssertions.AssertNotNull(k2, "k2"); + + DictionaryAssertions.AssertNotNull(k3, "k3"); + + DictionaryAssertions.AssertNotNull(k4, "k4"); + + DictionaryAssertions.AssertNotNull(k5, "k5"); + + DictionaryAssertions.AssertNotNull(k6, "k6"); + + DictionaryAssertions.AssertNotNull(k7, "k7"); + + DictionaryAssertions.AssertNotNull(value, "value"); + + + + var secondDimentionData = new ExpirationalMultiDimensionDictionary( + ExpirationSpan2, ExpirationSpan3, ExpirationSpan4, ExpirationSpan5, ExpirationSpan6, ExpirationSpan7); + + if (Data.ContainsKey(k1)) + { + secondDimentionData = Data[k1].subData; + } + + secondDimentionData.Put(k2, k3, k4, k5, k6, k7, value); + + Data[k1] = (DateTime.Now, secondDimentionData); + } + + #endregion + + #region Get + + public ExpirationalMultiDimensionDictionary Get(K1 k1) + { + return Data[k1].subData; + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2) + { + return Data[k1].subData.Get(k2); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3) + { + return Data[k1].subData.Get(k2, k3); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4) + { + return Data[k1].subData.Get(k2, k3, k4); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + return Data[k1].subData.Get(k2, k3, k4, k5); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + return Data[k1].subData.Get(k2, k3, k4, k5, k6); + } + + public V Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) + { + Invalidate(); + DictionaryAssertions.AssertNotNull(k1, "k1"); + + DictionaryAssertions.AssertNotNull(k2, "k2"); + + DictionaryAssertions.AssertNotNull(k3, "k3"); + + DictionaryAssertions.AssertNotNull(k4, "k4"); + + DictionaryAssertions.AssertNotNull(k5, "k5"); + + DictionaryAssertions.AssertNotNull(k6, "k6"); + + DictionaryAssertions.AssertNotNull(k7, "k7"); + + + + + if (Data.TryGetValue(k1, out var secondDimensionData)) + { + return secondDimensionData.subData.Get(k2, k3, k4, k5, k6, k7); + } + + throw new KeyNotFoundException(); + } + + + #endregion + + #region Remove + + public new void Remove(K1 k1) + { + Invalidate(); + Data.Remove(k1, out var ignore); + } + + public void Remove(K1 k1, K2 k2) + { + Invalidate(); + Data[k1].subData.Remove(k2); + } + + + public void Remove(K1 k1, K2 k2, K3 k3) + { + Invalidate(); + Data[k1].subData.Get(k2).Remove(k3); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Remove(k4); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Remove(k5); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Get(k5).Remove(k6); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Remove(k7); + } + + + + #endregion + + + public void _Remove(K1 k1) + { + (DateTime date, ExpirationalMultiDimensionDictionary subData) ignore = default; + Data.TryRemove(k1, out ignore); + } + + public void Invalidate() + { + DateTime now = DateTime.Now; + List toRemove = new List(); + foreach (var entry in Data) + { + var age = now - entry.Value.date; + if (age > ExpirationSpan1) + { + toRemove.Add(entry.Key); + } + } + + if (toRemove.Any()) + { + foreach (var k1 in toRemove) + { + _Remove(k1); + } + } + } + } + +} diff --git a/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti8.cs b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti8.cs new file mode 100644 index 0000000..397268e --- /dev/null +++ b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti8.cs @@ -0,0 +1,340 @@ + + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; + +namespace multiDimensionalDictionary +{ + + + public class ExpirationalMultiDimensionDictionary + { + TimeSpan ExpirationSpan1; + TimeSpan ExpirationSpan2; + TimeSpan ExpirationSpan3; + TimeSpan ExpirationSpan4; + TimeSpan ExpirationSpan5; + TimeSpan ExpirationSpan6; + TimeSpan ExpirationSpan7; + TimeSpan ExpirationSpan8; + + + + public ExpirationalMultiDimensionDictionary(TimeSpan expiration1, TimeSpan expiration2, TimeSpan expiration3, + TimeSpan expiration4, TimeSpan expiration5, TimeSpan expiration6, TimeSpan expiration7, + TimeSpan expiration8) + { + ExpirationSpan1 = expiration1; + ExpirationSpan2 = expiration2; + ExpirationSpan3 = expiration3; + ExpirationSpan4 = expiration4; + ExpirationSpan5 = expiration5; + ExpirationSpan6 = expiration6; + ExpirationSpan7 = expiration7; + ExpirationSpan8 = expiration8; + Data = + new ConcurrentDictionary subData)>(); + } + + public ExpirationalMultiDimensionDictionary(long expiration1, long expiration2, long expiration3, + long expiration4, long expiration5, long expiration6, long expiration7, long expiration8) : this( + TimeSpan.FromMilliseconds(expiration1), TimeSpan.FromMilliseconds(expiration2), + TimeSpan.FromMilliseconds(expiration3), TimeSpan.FromMilliseconds(expiration4), + TimeSpan.FromMilliseconds(expiration5), TimeSpan.FromMilliseconds(expiration6), + TimeSpan.FromMilliseconds(expiration7), TimeSpan.FromMilliseconds(expiration8)) + { + } + + protected ConcurrentDictionary subData)> Data { get; set; } + + public ExpirationalMultiDimensionDictionary() : base() + { + Data = + new ConcurrentDictionary subData)>(); + } + + + + + #region ContainsKey + + public bool ContainsKey(K1 k1) + { + Invalidate(); + return Data.ContainsKey(k1); + } + + public bool ContainsKey(K1 k1, K2 k2) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5, k6); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5, k6, k7); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5, k6, k7, k8); + } + + + #endregion + + #region getKeys + + public List<(K1, K2, K3, K4, K5, K6, K7, K8)> GetKeys() + { + Invalidate(); + var keys = new List<(K1, K2, K3, K4, K5, K6, K7, K8)>(); + foreach (var kvp in Data) + { + List<(K2, K3, K4, K5, K6, K7, K8)> subkeys = kvp.Value.subData.GetKeys(); + foreach (var subkey in subkeys) + { + keys.Add((kvp.Key, subkey.Item1, subkey.Item2, subkey.Item3, subkey.Item4, subkey.Item5, + subkey.Item6, subkey.Item7)); + } + } + + return keys; + } + + #endregion + + #region Put + + public void Put(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, V value) + { + Invalidate(); + DictionaryAssertions.AssertNotNull(k1, "k1"); + + DictionaryAssertions.AssertNotNull(k2, "k2"); + + DictionaryAssertions.AssertNotNull(k3, "k3"); + + DictionaryAssertions.AssertNotNull(k4, "k4"); + + DictionaryAssertions.AssertNotNull(k5, "k5"); + + DictionaryAssertions.AssertNotNull(k6, "k6"); + + DictionaryAssertions.AssertNotNull(k7, "k7"); + + DictionaryAssertions.AssertNotNull(k8, "k8"); + + DictionaryAssertions.AssertNotNull(value, "value"); + + + + var secondDimentionData = new ExpirationalMultiDimensionDictionary( + ExpirationSpan2, ExpirationSpan3, ExpirationSpan4, ExpirationSpan5, ExpirationSpan6, ExpirationSpan7, + ExpirationSpan8); + + if (Data.ContainsKey(k1)) + { + secondDimentionData = Data[k1].subData; + } + + secondDimentionData.Put(k2, k3, k4, k5, k6, k7, k8, value); + + Data[k1] = (DateTime.Now, secondDimentionData); + } + + #endregion + + #region Get + + public ExpirationalMultiDimensionDictionary Get(K1 k1) + { + return Data[k1].subData; + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2) + { + return Data[k1].subData.Get(k2); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3) + { + return Data[k1].subData.Get(k2, k3); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4) + { + return Data[k1].subData.Get(k2, k3, k4); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + return Data[k1].subData.Get(k2, k3, k4, k5); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + return Data[k1].subData.Get(k2, k3, k4, k5, k6); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) + { + return Data[k1].subData.Get(k2, k3, k4, k5, k6, k7); + } + + public V Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) + { + Invalidate(); + DictionaryAssertions.AssertNotNull(k1, "k1"); + + DictionaryAssertions.AssertNotNull(k2, "k2"); + + DictionaryAssertions.AssertNotNull(k3, "k3"); + + DictionaryAssertions.AssertNotNull(k4, "k4"); + + DictionaryAssertions.AssertNotNull(k5, "k5"); + + DictionaryAssertions.AssertNotNull(k6, "k6"); + + DictionaryAssertions.AssertNotNull(k7, "k7"); + + DictionaryAssertions.AssertNotNull(k8, "k8"); + + + + + if (Data.TryGetValue(k1, out var secondDimensionData)) + { + return secondDimensionData.subData.Get(k2, k3, k4, k5, k6, k7, k8); + } + + throw new KeyNotFoundException(); + } + + + #endregion + + #region Remove + + public new void Remove(K1 k1) + { + Invalidate(); + Data.Remove(k1, out var ignore); + } + + public void Remove(K1 k1, K2 k2) + { + Invalidate(); + Data[k1].subData.Remove(k2); + } + + + public void Remove(K1 k1, K2 k2, K3 k3) + { + Invalidate(); + Data[k1].subData.Get(k2).Remove(k3); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Remove(k4); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Remove(k5); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Get(k5).Remove(k6); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Remove(k7); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Get(k7).Remove(k8); + } + + + + #endregion + + + public void _Remove(K1 k1) + { + (DateTime date, ExpirationalMultiDimensionDictionary subData) ignore = + default; + Data.TryRemove(k1, out ignore); + } + + public void Invalidate() + { + DateTime now = DateTime.Now; + List toRemove = new List(); + foreach (var entry in Data) + { + var age = now - entry.Value.date; + if (age > ExpirationSpan1) + { + toRemove.Add(entry.Key); + } + } + + if (toRemove.Any()) + { + foreach (var k1 in toRemove) + { + _Remove(k1); + } + } + } + } + +} diff --git a/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti9.cs b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti9.cs new file mode 100644 index 0000000..40ab723 --- /dev/null +++ b/MultiDimensionDictionary/expirationalMultiDimensionDictionary/expirationalMulti9.cs @@ -0,0 +1,365 @@ + + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; + +namespace multiDimensionalDictionary +{ + + + public class ExpirationalMultiDimensionDictionary + { + TimeSpan ExpirationSpan1; + TimeSpan ExpirationSpan2; + TimeSpan ExpirationSpan3; + TimeSpan ExpirationSpan4; + TimeSpan ExpirationSpan5; + TimeSpan ExpirationSpan6; + TimeSpan ExpirationSpan7; + TimeSpan ExpirationSpan8; + TimeSpan ExpirationSpan9; + + + + public ExpirationalMultiDimensionDictionary(TimeSpan expiration1, TimeSpan expiration2, TimeSpan expiration3, + TimeSpan expiration4, TimeSpan expiration5, TimeSpan expiration6, TimeSpan expiration7, + TimeSpan expiration8, TimeSpan expiration9) + { + ExpirationSpan1 = expiration1; + ExpirationSpan2 = expiration2; + ExpirationSpan3 = expiration3; + ExpirationSpan4 = expiration4; + ExpirationSpan5 = expiration5; + ExpirationSpan6 = expiration6; + ExpirationSpan7 = expiration7; + ExpirationSpan8 = expiration8; + ExpirationSpan9 = expiration9; + Data = + new ConcurrentDictionary subData)>(); + } + + public ExpirationalMultiDimensionDictionary(long expiration1, long expiration2, long expiration3, + long expiration4, long expiration5, long expiration6, long expiration7, long expiration8, long expiration9) + : this(TimeSpan.FromMilliseconds(expiration1), TimeSpan.FromMilliseconds(expiration2), + TimeSpan.FromMilliseconds(expiration3), TimeSpan.FromMilliseconds(expiration4), + TimeSpan.FromMilliseconds(expiration5), TimeSpan.FromMilliseconds(expiration6), + TimeSpan.FromMilliseconds(expiration7), TimeSpan.FromMilliseconds(expiration8), + TimeSpan.FromMilliseconds(expiration9)) + { + } + + protected ConcurrentDictionary subData)> Data { get; set; } + + public ExpirationalMultiDimensionDictionary() : base() + { + Data = + new ConcurrentDictionary subData)>(); + } + + + + + #region ContainsKey + + public bool ContainsKey(K1 k1) + { + Invalidate(); + return Data.ContainsKey(k1); + } + + public bool ContainsKey(K1 k1, K2 k2) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5, k6); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5, k6, k7); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5, k6, k7, k8); + } + + public bool ContainsKey(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9) + { + Invalidate(); + return Data.ContainsKey(k1) && Data[k1].subData.ContainsKey(k2, k3, k4, k5, k6, k7, k8, k9); + } + + + #endregion + + #region getKeys + + public List<(K1, K2, K3, K4, K5, K6, K7, K8, K9)> GetKeys() + { + Invalidate(); + var keys = new List<(K1, K2, K3, K4, K5, K6, K7, K8, K9)>(); + foreach (var kvp in Data) + { + List<(K2, K3, K4, K5, K6, K7, K8, K9)> subkeys = kvp.Value.subData.GetKeys(); + foreach (var subkey in subkeys) + { + keys.Add((kvp.Key, subkey.Item1, subkey.Item2, subkey.Item3, subkey.Item4, subkey.Item5, + subkey.Item6, subkey.Item7, subkey.Item8)); + } + } + + return keys; + } + + #endregion + + #region Put + + public void Put(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9, V value) + { + Invalidate(); + DictionaryAssertions.AssertNotNull(k1, "k1"); + + DictionaryAssertions.AssertNotNull(k2, "k2"); + + DictionaryAssertions.AssertNotNull(k3, "k3"); + + DictionaryAssertions.AssertNotNull(k4, "k4"); + + DictionaryAssertions.AssertNotNull(k5, "k5"); + + DictionaryAssertions.AssertNotNull(k6, "k6"); + + DictionaryAssertions.AssertNotNull(k7, "k7"); + + DictionaryAssertions.AssertNotNull(k8, "k8"); + + DictionaryAssertions.AssertNotNull(k9, "k9"); + + DictionaryAssertions.AssertNotNull(value, "value"); + + + + var secondDimentionData = new ExpirationalMultiDimensionDictionary( + ExpirationSpan2, ExpirationSpan3, ExpirationSpan4, ExpirationSpan5, ExpirationSpan6, ExpirationSpan7, + ExpirationSpan8, ExpirationSpan9); + + if (Data.ContainsKey(k1)) + { + secondDimentionData = Data[k1].subData; + } + + secondDimentionData.Put(k2, k3, k4, k5, k6, k7, k8, k9, value); + + Data[k1] = (DateTime.Now, secondDimentionData); + } + + #endregion + + #region Get + + public ExpirationalMultiDimensionDictionary Get(K1 k1) + { + return Data[k1].subData; + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2) + { + return Data[k1].subData.Get(k2); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3) + { + return Data[k1].subData.Get(k2, k3); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4) + { + return Data[k1].subData.Get(k2, k3, k4); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + return Data[k1].subData.Get(k2, k3, k4, k5); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + return Data[k1].subData.Get(k2, k3, k4, k5, k6); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) + { + return Data[k1].subData.Get(k2, k3, k4, k5, k6, k7); + } + + public ExpirationalMultiDimensionDictionary Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) + { + return Data[k1].subData.Get(k2, k3, k4, k5, k6, k7, k8); + } + + public V Get(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9) + { + Invalidate(); + DictionaryAssertions.AssertNotNull(k1, "k1"); + + DictionaryAssertions.AssertNotNull(k2, "k2"); + + DictionaryAssertions.AssertNotNull(k3, "k3"); + + DictionaryAssertions.AssertNotNull(k4, "k4"); + + DictionaryAssertions.AssertNotNull(k5, "k5"); + + DictionaryAssertions.AssertNotNull(k6, "k6"); + + DictionaryAssertions.AssertNotNull(k7, "k7"); + + DictionaryAssertions.AssertNotNull(k8, "k8"); + + DictionaryAssertions.AssertNotNull(k9, "k9"); + + + + + if (Data.TryGetValue(k1, out var secondDimensionData)) + { + return secondDimensionData.subData.Get(k2, k3, k4, k5, k6, k7, k8, k9); + } + + throw new KeyNotFoundException(); + } + + + #endregion + + #region Remove + + public new void Remove(K1 k1) + { + Invalidate(); + Data.Remove(k1, out var ignore); + } + + public void Remove(K1 k1, K2 k2) + { + Invalidate(); + Data[k1].subData.Remove(k2); + } + + + public void Remove(K1 k1, K2 k2, K3 k3) + { + Invalidate(); + Data[k1].subData.Get(k2).Remove(k3); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Remove(k4); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Remove(k5); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Get(k5).Remove(k6); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Remove(k7); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Get(k7).Remove(k8); + } + + + public void Remove(K1 k1, K2 k2, K3 k3, K4 k4, K5 k5, K6 k6, K7 k7, K8 k8, K9 k9) + { + Invalidate(); + Data[k1].subData.Get(k2).Get(k3).Get(k4).Get(k5).Get(k6).Get(k7).Get(k8).Remove(k9); + } + + + + #endregion + + + public void _Remove(K1 k1) + { + (DateTime date, ExpirationalMultiDimensionDictionary subData) ignore = + default; + Data.TryRemove(k1, out ignore); + } + + public void Invalidate() + { + DateTime now = DateTime.Now; + List toRemove = new List(); + foreach (var entry in Data) + { + var age = now - entry.Value.date; + if (age > ExpirationSpan1) + { + toRemove.Add(entry.Key); + } + } + + if (toRemove.Any()) + { + foreach (var k1 in toRemove) + { + _Remove(k1); + } + } + } + } + +} From 62b79c40f11788cb6bc9ab52746762082cdb5aa2 Mon Sep 17 00:00:00 2001 From: b3b00 Date: Thu, 8 Sep 2022 17:03:56 +0200 Subject: [PATCH 10/12] . From 51aa457b4dcf1dafe5c4af3f2be07badf4257529 Mon Sep 17 00:00:00 2001 From: Olivier Duhart <1224790+b3b00@users.noreply.github.com> Date: Thu, 8 Sep 2022 17:14:57 +0200 Subject: [PATCH 11/12] Update dotnet.yml --- .github/workflows/dotnet.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 79b5e5a..18430dd 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -42,7 +42,7 @@ jobs: with: testProject: ${{env.TESTS_PROJECT}} output: 'lcov.info' - threshold: 50 + threshold: 45 outputFormat: 'lcov' excludes: '[ConsoleTests]*' - name: coveralls From 301e181d5552d898744309d1f6eccf0efce83e16 Mon Sep 17 00:00:00 2001 From: Olivier Duhart <1224790+b3b00@users.noreply.github.com> Date: Thu, 8 Sep 2022 17:22:52 +0200 Subject: [PATCH 12/12] Update dotnet.yml --- .github/workflows/dotnet.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 18430dd..34d4c8f 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -42,7 +42,7 @@ jobs: with: testProject: ${{env.TESTS_PROJECT}} output: 'lcov.info' - threshold: 45 + threshold: 30 outputFormat: 'lcov' excludes: '[ConsoleTests]*' - name: coveralls