changeset 14:25b953087465

* Rename DictionaryToArrayConverter to DictionaryUtils * Add method to merge arrays no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 25 Jan 2009 11:03:38 +0000
parents 6b762694f051
children 043a1d8101f2
files Collections/DictionaryToArrayConverter.cs Collections/DictionaryUtils.cs IBBoard.mdp
diffstat 3 files changed, 82 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/Collections/DictionaryToArrayConverter.cs	Tue Jan 06 19:27:47 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,55 +0,0 @@
-// DictionaryToArrayConverter.cs is a part of the IBBoard utils library (referred to from here as "this program")
-// 
-// Copyright (C) 2009 IBBoard
-//
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Lesser General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public License
-// along with this program.  If not, see <http://www.gnu.org/licenses/>.
-//
-
-using System;
-using System.Collections.Generic;
-
-namespace IBBoard
-{
-	
-	
-	public class DictionaryToArrayConverter
-	{
-		/// <summary>
-		/// Takes the set of values in a dictionary and returns them as an array of typed objects.
-		/// </summary>
-		/// <param name="dictionary">
-		/// A <see cref="Dictionary`2"/> to extract an array of values from
-		/// </param>
-		/// <returns>
-		/// An array of <see cref="VALUE_TYPE"/> objects taken from the Values property of the dictionary, or NULL if the dictionary is NULL
-		/// </returns>
-		public static  VALUE_TYPE[] Convert<KEY_TYPE, VALUE_TYPE>(Dictionary<KEY_TYPE, VALUE_TYPE> dictionary)
-		{
-			if (dictionary == null)
-			{
-				return null;
-			}
-			
-			int entryCount = dictionary.Count;
-			VALUE_TYPE[] col = new VALUE_TYPE[entryCount];
-			
-			if (entryCount > 0)
-			{
-				dictionary.Values.CopyTo(col, 0);
-			}
-			
-			return col;
-		}
-	}
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Collections/DictionaryUtils.cs	Sun Jan 25 11:03:38 2009 +0000
@@ -0,0 +1,80 @@
+// DictionaryToArrayConverter.cs is a part of the IBBoard utils library (referred to from here as "this program")
+// 
+// Copyright (C) 2009 IBBoard
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+//
+
+using System;
+using System.Collections.Generic;
+
+namespace IBBoard
+{	
+	public class DictionaryUtils
+	{
+		/// <summary>
+		/// Takes the set of values in a dictionary and returns them as an array of typed objects.
+		/// </summary>
+		/// <param name="dictionary">
+		/// A <see cref="IDictionary"/> to extract an array of values from
+		/// </param>
+		/// <returns>
+		/// An array of <see cref="VALUE_TYPE"/> objects taken from the Values property of the dictionary, or NULL if the dictionary is NULL
+		/// </returns>
+		public static  VALUE_TYPE[] Convert<KEY_TYPE, VALUE_TYPE>(IDictionary<KEY_TYPE, VALUE_TYPE> dictionary)
+		{
+			if (dictionary == null)
+			{
+				return null;
+			}
+			
+			int entryCount = dictionary.Count;
+			VALUE_TYPE[] col = new VALUE_TYPE[entryCount];
+			
+			if (entryCount > 0)
+			{
+				dictionary.Values.CopyTo(col, 0);
+			}
+			
+			return col;
+		}
+
+		/// <summary>
+		/// Takes two dictionaries and merges them. If a key exists in both dictionaries then the value in <code>firstDictionary</code> takes priority.
+		/// </summary>
+		/// <param name="firstDictionary">
+		/// The <see cref="IDictionary"/> to merge values in to
+		/// </param>
+		/// <param name="secondDictionary">
+		/// The <see cref="IDictionary"/> of values to merge in
+		/// </param>
+		/// <returns>
+		/// A <see cref="IDictionary"/> made by adding values from <code>secondDictionary</code> where the key didn't exist in <code>firstDictionary</code>
+		/// </returns>
+		public static IDictionary<KEY_TYPE, VALUE_TYPE> Merge<KEY_TYPE, VALUE_TYPE>(IDictionary<KEY_TYPE, VALUE_TYPE> firstDictionary, IDictionary<KEY_TYPE, VALUE_TYPE> secondDictionary)
+		{
+			Dictionary<KEY_TYPE, VALUE_TYPE> mergedDictionary = new Dictionary<KEY_TYPE, VALUE_TYPE>(firstDictionary);
+
+			foreach (KEY_TYPE key in secondDictionary.Keys)
+			{
+				if (!mergedDictionary.ContainsKey(key))
+				{
+					mergedDictionary.Add(key, secondDictionary[key]);
+				}
+			}
+
+			return mergedDictionary;
+		}
+	}
+}
--- a/IBBoard.mdp	Tue Jan 06 19:27:47 2009 +0000
+++ b/IBBoard.mdp	Sun Jan 25 11:03:38 2009 +0000
@@ -45,7 +45,7 @@
     <File name="Collections/SimpleSet.cs" subtype="Code" buildaction="Compile" />
     <File name="dtds" subtype="Directory" buildaction="Compile" />
     <File name="dtds/translation.dtd" subtype="Code" buildaction="Nothing" />
-    <File name="Collections/DictionaryToArrayConverter.cs" subtype="Code" buildaction="Compile" />
+    <File name="Collections/DictionaryUtils.cs" subtype="Code" buildaction="Compile" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
@@ -53,5 +53,6 @@
     <ProjectReference type="Gac" localcopy="True" refto="System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
     <ProjectReference type="Gac" localcopy="True" refto="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
   </References>
+  <GtkDesignInfo />
   <MonoDevelop.ChangeLogAddIn.ChangeLogInfo policy="UseParentPolicy" />
 </Project>
\ No newline at end of file