Tools.Collections

Details

.gitignore 15(+2 -13)

diff --git a/.gitignore b/.gitignore
index 73f686c..f5eb3a9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,17 +1,4 @@
 
-src/ManagetLinkCollection/.vs/ManagetLinkCollection/*
-
-
-src/ManagetLinkCollection/ManagetLinkCollection.ConsoleTest/bin/*
-src/ManagetLinkCollection/ManagetLinkCollection.ConsoleTest/obj/*
-
-src/ManagetLinkCollection/ManagetLinkCollection/bin/*
-src/ManagetLinkCollection/ManagetLinkCollection/obj/*
-
-src/ManagetLinkCollection/packages/*
-
-
-
 src/Tools.Collections/.vs/*
 /.vs/*
 
@@ -30,3 +17,5 @@ src/Tools.Collections/Tools.Collections.SortedDictionarySortKeyItemKey/obj/*
 src/Tools.Collections/Tools.Collections.ConsoleTest/bin/*
 src/Tools.Collections/Tools.Collections.ConsoleTest/obj/*
 
+/src/Tools.Collections/NugetBuild/Packages/*
+/src/Tools.Collections/NugetBuild/nuget.exe
diff --git a/src/Tools.Collections/NugetBuild/Build.cmd b/src/Tools.Collections/NugetBuild/Build.cmd
new file mode 100644
index 0000000..9544c1b
--- /dev/null
+++ b/src/Tools.Collections/NugetBuild/Build.cmd
@@ -0,0 +1,5 @@
+
+rmdir /q /s Packages
+
+
+nuget pack Tools.Collections.ConcurrentDictonary.RemoveIf.nuspec -OutputDirectory ".\Packages"
\ No newline at end of file
diff --git a/src/Tools.Collections/NugetBuild/Tools.Collections.ConcurrentDictonary.RemoveIf.nuspec b/src/Tools.Collections/NugetBuild/Tools.Collections.ConcurrentDictonary.RemoveIf.nuspec
new file mode 100644
index 0000000..4aa63be
--- /dev/null
+++ b/src/Tools.Collections/NugetBuild/Tools.Collections.ConcurrentDictonary.RemoveIf.nuspec
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
+  <metadata>
+    <id>Tools.Collections.ConcurrentDictonary.RemoveIf</id>
+    <version>1.0.0</version>
+    <authors>cccc1808</authors>
+    <owners>cccc1808</owners>
+    <requireLicenseAcceptance>false</requireLicenseAcceptance>
+    <description>
+		Extensions method for System.Collections.Concurrent.ConcurrentDictionary. Use IValueWrapper for TValue.
+	</description>
+    <dependencies>
+      <group targetFramework=".NETStandard1.1">
+        <dependency id="System.Collections.Concurrent" version="4.0.0"/>
+      </group>
+    </dependencies>
+  </metadata>
+  <files>
+    <file src="..\Tools.Collections.ConcurrentDictonary.Extensions\bin\Release\netstandard1.1\Tools.Collections.Concurrent.Extensions.dll" target="lib\netstandard1.1\Tools.Collections.Concurrent.Extensions.dll" />
+  </files>
+</package>
\ No newline at end of file
diff --git a/src/Tools.Collections/Tools.Collections.ConcurrentDictonary.Extensions/ConcurrentDictionaryExtensions.cs b/src/Tools.Collections/Tools.Collections.ConcurrentDictonary.Extensions/ConcurrentDictionaryExtensions.cs
index d78ecd9..f557ff6 100644
--- a/src/Tools.Collections/Tools.Collections.ConcurrentDictonary.Extensions/ConcurrentDictionaryExtensions.cs
+++ b/src/Tools.Collections/Tools.Collections.ConcurrentDictonary.Extensions/ConcurrentDictionaryExtensions.cs
@@ -15,7 +15,7 @@ namespace Tools.Collections.Concurrent.Extensions
         /// <typeparam name="TData">Data Type</typeparam>
         /// <param name="key">key</param>
         /// <param name="removeExpression">Remove if true</param>
-        public static void RemoveIf<TKey, TData>(
+        public static bool RemoveIf<TKey, TData>(
             this ConcurrentDictionary<TKey, IValueWrapper<TData>> dictionary,
             TKey key,
             Func<IValueWrapper<TData>, bool> removeExpression
@@ -23,7 +23,7 @@ namespace Tools.Collections.Concurrent.Extensions
         {
             var collection = (ICollection<KeyValuePair<TKey, IValueWrapper<TData>>>) dictionary;
 
-            collection
+            return collection
                 .Remove(
                     new KeyValuePair<TKey, IValueWrapper<TData>>(
                         key, 
@@ -36,13 +36,13 @@ namespace Tools.Collections.Concurrent.Extensions
         /// <typeparam name="TData">Data Type</typeparam>
         /// <param name="key">key</param>
         /// <param name="removeExpression">Remove if true</param>
-        public static void RemoveIf<TKey, TData>(
+        public static bool RemoveIf<TKey, TData>(
             this ICollection<KeyValuePair<TKey, IValueWrapper<TData>>> collection,
             TKey key,
             Func<IValueWrapper<TData>, bool> removeExpression
             )
         {
-            collection
+            return collection
                 .Remove(
                     new KeyValuePair<TKey, IValueWrapper<TData>>(
                         key,
diff --git a/src/Tools.Collections/Tools.Collections.ConsoleTest/Program.cs b/src/Tools.Collections/Tools.Collections.ConsoleTest/Program.cs
index 2f54557..62575a8 100644
--- a/src/Tools.Collections/Tools.Collections.ConsoleTest/Program.cs
+++ b/src/Tools.Collections/Tools.Collections.ConsoleTest/Program.cs
@@ -11,9 +11,13 @@ namespace Tools.Collections.ConsoleTest
         private static readonly Test_SortedDictionarySortKeyItemKey test_SortedDictionarySortKeyItemKey 
             = new Test_SortedDictionarySortKeyItemKey();
 
+        private static readonly Test_ConcurrentDictionaryRemoveIf test_ConcurrentDictionaryRemoveIf
+            = new Test_ConcurrentDictionaryRemoveIf();
+
         static void Main(string[] args)
         {
-            test_SortedDictionarySortKeyItemKey.Test();
+            //test_SortedDictionarySortKeyItemKey.Test();
+            test_ConcurrentDictionaryRemoveIf.TestMethod();
         }
     }
 }
diff --git a/src/Tools.Collections/Tools.Collections.ConsoleTest/Test_ConcurrentDictionaryRemoveIf.cs b/src/Tools.Collections/Tools.Collections.ConsoleTest/Test_ConcurrentDictionaryRemoveIf.cs
new file mode 100644
index 0000000..9a0a78a
--- /dev/null
+++ b/src/Tools.Collections/Tools.Collections.ConsoleTest/Test_ConcurrentDictionaryRemoveIf.cs
@@ -0,0 +1,107 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+using System.Threading;
+using System.Collections.Concurrent;
+
+using Tools.Collections.Concurrent.Extensions;
+
+namespace Tools.Collections.ConsoleTest
+{
+    class Test_ConcurrentDictionaryRemoveIf
+    {
+
+        public class LinkItem<T>
+        {
+            public int LinkCount { set; get; } = 0;
+            public T Value { set; get; }
+        }
+
+        public IValueWrapper<LinkItem<TValue>> AddLink<TKey, TValue>(
+            ConcurrentDictionary<TKey, IValueWrapper<LinkItem<TValue>>> dictionary, 
+            TKey key, 
+            TValue value
+            )
+        {
+            return dictionary.AddOrUpdate(
+                key,
+                new RealItemWrapper<LinkItem<TValue>>(
+                    new LinkItem<TValue>()
+                    {
+                        LinkCount = 1,
+                        Value = value
+                    }
+                    ),
+                (TKey tkey, IValueWrapper<LinkItem<TValue>> item) =>
+                    new RealItemWrapper<LinkItem<TValue>>(
+                        new LinkItem<TValue>()
+                        {
+                            LinkCount = item.Value.LinkCount + 1,
+                            Value = item.Value.Value
+                        }
+                        )
+                    );
+        }
+
+        public bool RemoveLink<TKey, TValue>(
+            ConcurrentDictionary<TKey, IValueWrapper<LinkItem<TValue>>> dictionary,
+            TKey key
+            )
+        {
+            var result = dictionary.AddOrUpdate(
+                key,
+                new RealItemWrapper<LinkItem<TValue>>(
+                    new LinkItem<TValue>()
+                    {
+                        LinkCount = 0,
+                        Value = default(TValue)
+                    }
+                    ),
+                (TKey tkey, IValueWrapper<LinkItem<TValue>> item) =>
+                    new RealItemWrapper<LinkItem<TValue>>(
+                        new LinkItem<TValue>()
+                        {
+                            LinkCount = item.Value.LinkCount - 1,
+                            Value = item.Value.Value
+                        }
+                        )
+                    );
+
+            //Remove item if link count = 0
+
+            //if (result.Value.LinkCount != 0)
+            //{
+            //    return false;
+            //}
+
+            return dictionary.RemoveIf(
+                key,
+                (IValueWrapper<LinkItem<TValue>> item) => item.Value.LinkCount == 0
+                );
+        }
+
+
+
+        public void TestMethod()
+        {
+            ConcurrentDictionary<string, IValueWrapper<LinkItem<string>>>
+                dictionary = new ConcurrentDictionary<string, IValueWrapper<LinkItem<string>>>();
+
+            //Add first link to key
+            AddLink(dictionary, "key", "value");
+            //Add second link to key
+            AddLink(dictionary, "key", "value");
+
+            //Remove first link to key
+            var removeResult1 = RemoveLink(dictionary, "key");
+            //Remove first link to key
+            var removeResult2 = RemoveLink(dictionary, "key");
+
+            Console.ReadLine();
+        }
+
+    }
+}
diff --git a/src/Tools.Collections/Tools.Collections.ConsoleTest/Test_LinkItemDictionary.cs b/src/Tools.Collections/Tools.Collections.ConsoleTest/Test_LinkItemDictionary.cs
index bbcf069..6b2248e 100644
--- a/src/Tools.Collections/Tools.Collections.ConsoleTest/Test_LinkItemDictionary.cs
+++ b/src/Tools.Collections/Tools.Collections.ConsoleTest/Test_LinkItemDictionary.cs
@@ -8,7 +8,7 @@ using System.Threading;
 
 using Tools.Collections.Concurrent;
 
-namespace ManagetLinkCollection.ConsoleTest
+namespace Tools.Collections.ConsoleTest
 {
     class Test_LinkItemDictionary
     {
diff --git a/src/Tools.Collections/Tools.Collections.ConsoleTest/Tools.Collections.ConsoleTest.csproj b/src/Tools.Collections/Tools.Collections.ConsoleTest/Tools.Collections.ConsoleTest.csproj
index 51e9c93..dc6550f 100644
--- a/src/Tools.Collections/Tools.Collections.ConsoleTest/Tools.Collections.ConsoleTest.csproj
+++ b/src/Tools.Collections/Tools.Collections.ConsoleTest/Tools.Collections.ConsoleTest.csproj
@@ -45,6 +45,7 @@
   <ItemGroup>
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Test_ConcurrentDictionaryRemoveIf.cs" />
     <Compile Include="Test_LinkItemDictionary.cs" />
     <Compile Include="Test_SortedDictionarySortKeyItemKey.cs" />
   </ItemGroup>