annotate Collections/DictionaryUtils.cs @ 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 Collections/DictionaryToArrayConverter.cs@6b762694f051
children 043a1d8101f2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // DictionaryToArrayConverter.cs is a part of the IBBoard utils library (referred to from here as "this program")
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 // Copyright (C) 2009 IBBoard
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 //
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 // This program is free software: you can redistribute it and/or modify
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 // it under the terms of the GNU Lesser General Public License as published by
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 // the Free Software Foundation, either version 3 of the License, or
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 // (at your option) any later version.
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 //
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 // This program is distributed in the hope that it will be useful,
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 // GNU Lesser General Public License for more details.
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 //
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 // You should have received a copy of the GNU Lesser General Public License
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 //
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 using System;
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 using System.Collections.Generic;
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 namespace IBBoard
14
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
23 {
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
24 public class DictionaryUtils
11
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 {
12
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
26 /// <summary>
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
27 /// Takes the set of values in a dictionary and returns them as an array of typed objects.
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
28 /// </summary>
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
29 /// <param name="dictionary">
14
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
30 /// A <see cref="IDictionary"/> to extract an array of values from
12
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
31 /// </param>
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
32 /// <returns>
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
33 /// An array of <see cref="VALUE_TYPE"/> objects taken from the Values property of the dictionary, or NULL if the dictionary is NULL
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
34 /// </returns>
14
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
35 public static VALUE_TYPE[] Convert<KEY_TYPE, VALUE_TYPE>(IDictionary<KEY_TYPE, VALUE_TYPE> dictionary)
11
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 {
12
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
37 if (dictionary == null)
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
38 {
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
39 return null;
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
40 }
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
41
13
6b762694f051 Check size of dictionary so that we don't have to get the values when we've got an empty dictionary
IBBoard <dev@ibboard.co.uk>
parents: 12
diff changeset
42 int entryCount = dictionary.Count;
6b762694f051 Check size of dictionary so that we don't have to get the values when we've got an empty dictionary
IBBoard <dev@ibboard.co.uk>
parents: 12
diff changeset
43 VALUE_TYPE[] col = new VALUE_TYPE[entryCount];
6b762694f051 Check size of dictionary so that we don't have to get the values when we've got an empty dictionary
IBBoard <dev@ibboard.co.uk>
parents: 12
diff changeset
44
6b762694f051 Check size of dictionary so that we don't have to get the values when we've got an empty dictionary
IBBoard <dev@ibboard.co.uk>
parents: 12
diff changeset
45 if (entryCount > 0)
6b762694f051 Check size of dictionary so that we don't have to get the values when we've got an empty dictionary
IBBoard <dev@ibboard.co.uk>
parents: 12
diff changeset
46 {
6b762694f051 Check size of dictionary so that we don't have to get the values when we've got an empty dictionary
IBBoard <dev@ibboard.co.uk>
parents: 12
diff changeset
47 dictionary.Values.CopyTo(col, 0);
6b762694f051 Check size of dictionary so that we don't have to get the values when we've got an empty dictionary
IBBoard <dev@ibboard.co.uk>
parents: 12
diff changeset
48 }
6b762694f051 Check size of dictionary so that we don't have to get the values when we've got an empty dictionary
IBBoard <dev@ibboard.co.uk>
parents: 12
diff changeset
49
12
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
50 return col;
11
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 }
14
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
52
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
53 /// <summary>
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
54 /// Takes two dictionaries and merges them. If a key exists in both dictionaries then the value in <code>firstDictionary</code> takes priority.
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
55 /// </summary>
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
56 /// <param name="firstDictionary">
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
57 /// The <see cref="IDictionary"/> to merge values in to
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
58 /// </param>
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
59 /// <param name="secondDictionary">
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
60 /// The <see cref="IDictionary"/> of values to merge in
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
61 /// </param>
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
62 /// <returns>
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
63 /// A <see cref="IDictionary"/> made by adding values from <code>secondDictionary</code> where the key didn't exist in <code>firstDictionary</code>
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
64 /// </returns>
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
65 public static IDictionary<KEY_TYPE, VALUE_TYPE> Merge<KEY_TYPE, VALUE_TYPE>(IDictionary<KEY_TYPE, VALUE_TYPE> firstDictionary, IDictionary<KEY_TYPE, VALUE_TYPE> secondDictionary)
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
66 {
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
67 Dictionary<KEY_TYPE, VALUE_TYPE> mergedDictionary = new Dictionary<KEY_TYPE, VALUE_TYPE>(firstDictionary);
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
68
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
69 foreach (KEY_TYPE key in secondDictionary.Keys)
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
70 {
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
71 if (!mergedDictionary.ContainsKey(key))
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
72 {
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
73 mergedDictionary.Add(key, secondDictionary[key]);
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
74 }
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
75 }
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
76
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
77 return mergedDictionary;
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
78 }
11
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 }
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80 }