annotate Collections/DictionaryUtils.cs @ 97:e0acabb91633

* Yet another target framework update - this time from VS.Net instead of MD no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Wed, 26 Jan 2011 20:22:35 +0000
parents e38192f55d2d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
1 // This file (DictionaryUtils.cs) is a part of the IBBoard library and is copyright 2009 IBBoard.
11
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
16
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
11
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 using System;
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using System.Collections.Generic;
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 namespace IBBoard
14
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
9 {
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
10 public class DictionaryUtils
11
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 {
12
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
12 /// <summary>
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
13 /// 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
14 /// </summary>
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
15 /// <param name="dictionary">
14
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
16 /// 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
17 /// </param>
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
18 /// <returns>
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
19 /// 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
20 /// </returns>
15
043a1d8101f2 * Rename "Convert" method to "ToArray" now that the class is more generic
IBBoard <dev@ibboard.co.uk>
parents: 14
diff changeset
21 public static VALUE_TYPE[] ToArray<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
22 {
12
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
23 if (dictionary == null)
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
24 {
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
25 return null;
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
26 }
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
27
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
28 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
29 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
30
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
31 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
32 {
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
33 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
34 }
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
35
12
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
36 return col;
11
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 }
28
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
38
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
39 /// <summary>
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
40 /// Takes the set of keys in a dictionary and returns them as an array of typed objects.
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
41 /// </summary>
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
42 /// <param name="dictionary">
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
43 /// A <see cref="IDictionary"/> to extract an array of keys from
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
44 /// </param>
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
45 /// <returns>
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
46 /// An array of <see cref="KEY_TYPE"/> objects taken from the Keys property of the dictionary, or NULL if the dictionary is NULL
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
47 /// </returns>
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
48 public static KEY_TYPE[] ToKeyArray<KEY_TYPE, VALUE_TYPE>(IDictionary<KEY_TYPE, VALUE_TYPE> dictionary)
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
49 {
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
50 if (dictionary == null)
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
51 {
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
52 return null;
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
53 }
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
54
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
55 int entryCount = dictionary.Count;
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
56 KEY_TYPE[] col = new KEY_TYPE[entryCount];
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
57
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
58 if (entryCount > 0)
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
59 {
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
60 dictionary.Keys.CopyTo(col, 0);
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
61 }
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
62
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
63 return col;
41ddcd61dc92 Add method to get array of keys from dictionary
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
64 }
14
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
65
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
66 /// <summary>
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
67 /// 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
68 /// </summary>
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
69 /// <param name="firstDictionary">
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
70 /// The <see cref="IDictionary"/> to merge values in to
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
71 /// </param>
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
72 /// <param name="secondDictionary">
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
73 /// The <see cref="IDictionary"/> of values to merge in
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
74 /// </param>
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
75 /// <returns>
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
76 /// 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
77 /// </returns>
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
78 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
79 {
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
80 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
81
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
82 foreach (KEY_TYPE key in secondDictionary.Keys)
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
83 {
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
84 if (!mergedDictionary.ContainsKey(key))
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
85 {
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
86 mergedDictionary.Add(key, secondDictionary[key]);
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
87 }
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
88 }
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
89
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
90 return mergedDictionary;
25b953087465 * Rename DictionaryToArrayConverter to DictionaryUtils
IBBoard <dev@ibboard.co.uk>
parents: 13
diff changeset
91 }
29
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
92
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
93 /// <summary>
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
94 /// Convenience method to get a value from a dictionary for a given key. This method wraps a TryGetValue call in a single function.
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
95 /// </summary>
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
96 /// <param name="dictionary">
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
97 /// The <see cref="IDictionary"/> to get a value from
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
98 /// </param>
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
99 /// <param name="key">
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
100 /// The key to get the value for
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
101 /// </param>
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
102 /// <returns>
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
103 /// The value for <code>key</code>, or <code>null</code> if there was no value for the key
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
104 /// </returns>
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
105 public static VALUE_TYPE GetValue<KEY_TYPE, VALUE_TYPE>(IDictionary<KEY_TYPE, VALUE_TYPE> dictionary, KEY_TYPE key)
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
106 {
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
107 if (dictionary == null)
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
108 {
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
109 return default(VALUE_TYPE);
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
110 }
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
111
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
112 VALUE_TYPE val = default(VALUE_TYPE);
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
113 dictionary.TryGetValue(key, out val);
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
114 return val;
e38192f55d2d * Add convenience method to get a value from a dictionary in a single line
IBBoard <dev@ibboard.co.uk>
parents: 28
diff changeset
115 }
11
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
116 }
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
117 }