annotate Collections/DictionaryToArrayConverter.cs @ 13:6b762694f051

Check size of dictionary so that we don't have to get the values when we've got an empty dictionary no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Tue, 06 Jan 2009 19:27:47 +0000
parents 465b672e9682
children
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
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 {
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 public class DictionaryToArrayConverter
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 {
12
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 /// 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
30 /// </summary>
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
31 /// <param name="dictionary">
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
32 /// A <see cref="Dictionary`2"/> to extract an array of values from
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
33 /// </param>
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
34 /// <returns>
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
35 /// 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
36 /// </returns>
11
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 public static VALUE_TYPE[] Convert<KEY_TYPE, VALUE_TYPE>(Dictionary<KEY_TYPE, VALUE_TYPE> dictionary)
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 {
12
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
39 if (dictionary == 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 return null;
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
42 }
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
43
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
44 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
45 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
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 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
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 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
50 }
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
51
12
465b672e9682 Closes #5 - Dictionary to array converter
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
52 return col;
11
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 }
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 }
ba9239164de2 Fixes #5 - Converting dictionary to array
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 }