Mercurial > silverbladetech
comparison Chronosv2/source/Extensions/ListExtensions.cs @ 10:443821e55f06
Initial cleaned up add from Codeplex files
author | stevenh7776 stevenhollidge@hotmail.com |
---|---|
date | Tue, 21 Feb 2012 17:25:44 +0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:904a9faadf8b | 10:443821e55f06 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 | |
4 namespace Chronos.Extensions | |
5 { | |
6 /// <summary> | |
7 /// Extension methods for all kind of Lists implementing the IList<T> interface | |
8 /// </summary> | |
9 public static class ListExtensions | |
10 { | |
11 #region · Extensions · | |
12 | |
13 /// <summary> | |
14 /// Inserts an item uniquely to to a list and returns a value whether the item was inserted or not. | |
15 /// </summary> | |
16 /// <typeparam name="T">The generic list item type.</typeparam> | |
17 /// <param name="list">The list to be inserted into.</param> | |
18 /// <param name="index">The index to insert the item at.</param> | |
19 /// <param name="item">The item to be added.</param> | |
20 /// <returns>Indicates whether the item was inserted or not</returns> | |
21 public static bool InsertUnqiue<T>(this IList<T> list, int index, T item) | |
22 { | |
23 if (list.Contains(item) == false) | |
24 { | |
25 list.Insert(index, item); | |
26 return true; | |
27 } | |
28 | |
29 return false; | |
30 } | |
31 | |
32 /// <summary> | |
33 /// Inserts a range of items uniquely to a list starting at a given index and returns the amount of items inserted. | |
34 /// </summary> | |
35 /// <typeparam name="T">The generic list item type.</typeparam> | |
36 /// <param name="list">The list to be inserted into.</param> | |
37 /// <param name="startIndex">The start index.</param> | |
38 /// <param name="items">The items to be inserted.</param> | |
39 /// <returns>The amount if items that were inserted.</returns> | |
40 public static int InsertRangeUnique<T>(this IList<T> list, int startIndex, IEnumerable<T> items) | |
41 { | |
42 var index = startIndex; | |
43 | |
44 foreach (var item in items) | |
45 { | |
46 if (list.InsertUnqiue(startIndex, item)) | |
47 { | |
48 index++; | |
49 } | |
50 } | |
51 | |
52 return (index - startIndex); | |
53 } | |
54 | |
55 /// <summary> | |
56 /// Return the index of the first matching item or -1. | |
57 /// </summary> | |
58 /// <typeparam name="T"></typeparam> | |
59 /// <param name="list">The list.</param> | |
60 /// <param name="comparison">The comparison.</param> | |
61 /// <returns>The item index</returns> | |
62 public static int IndexOf<T>(this IList<T> list, Func<T, bool> comparison) | |
63 { | |
64 for (var i = 0; i < list.Count; i++) | |
65 { | |
66 if (comparison(list[i])) | |
67 { | |
68 return i; | |
69 } | |
70 } | |
71 | |
72 return -1; | |
73 } | |
74 | |
75 #endregion | |
76 } | |
77 } |