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