-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathSourceIndexList.cs
More file actions
117 lines (109 loc) · 4.23 KB
/
SourceIndexList.cs
File metadata and controls
117 lines (109 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace Equin.BindingListView
{
/// <summary>
/// A basic list of key-value pairs that store the indicies of items in a source list.
/// The key part is actually an <see cref="EditableObject<T>"/> so we have to extract the inner <typeparamref name="T"/> object
/// when processing the list.
/// </summary>
/// <typeparam name="T">The type of item in the source list.</typeparam>
internal class SourceIndexList<T> : List<KeyValuePair<EditableObject<T>, int>>
{
public void Add(EditableObject<T> item, int index)
{
Add(new KeyValuePair<EditableObject<T>, int>(item, index));
}
/// <summary>
/// Searches for a given source index value, returning the list index of the value.
/// </summary>
/// <param name="sourceIndex">The source index to find.</param>
/// <returns>Returns the index in this list of the source index, or -1 if not found.</returns>
public int IndexOfSourceIndex(int sourceIndex)
{
for (int i = 0; i < Count; i++)
{
if (this[i].Value == sourceIndex)
{
return i;
}
}
return -1;
}
/// <summary>
/// Searches for a given item, returning the index of the value in this list.
/// </summary>
/// <param name="item">The <typeparamref name="T"/> item to search for.</param>
/// <returns>Returns the index in this list of the item, or -1 if not found.</returns>
public int IndexOfItem(T item)
{
for (int i = 0; i < Count; i++)
{
if (this[i].Key.Object.Equals(item))
{
return i;
}
}
return -1;
}
/// <summary>
/// Searches for a given item's <see cref="EditableObject<T>"/> wrapper, returning the index of the value in this list.
/// </summary>
/// <param name="item">The <see cref="EditableObject<T>"/> to search for.</param>
/// <returns>Returns the index in this list of the item, or -1 if not found.</returns>
public int IndexOfKey(EditableObject<T> item)
{
for (int i = 0; i < Count; i++)
{
if (this[i].Key.Equals(item))
{
return i;
}
}
return -1;
}
/// <summary>
/// Checks if the list contains a given item.
/// </summary>
/// <param name="item">The <typeparamref name="T"/> item to check for.</param>
/// <returns>True if the item is contained in the list, otherwise false.</returns>
public bool ContainsItem(T item)
{
return (IndexOfItem(item) != -1);
}
/// <summary>
/// Checks if the list contains a given <see cref="EditableObject<T>"/> key.
/// </summary>
/// <param name="item">The key to search for.</param>
/// <returns>True if the key is contained in the list, otherwise false.</returns>
public bool ContainsKey(EditableObject<T> key)
{
return (IndexOfKey(key) != -1);
}
/// <summary>
/// Returns an array of all the <see cref="EditableObject<T>"/> keys in the list.
/// </summary>
public EditableObject<T>[] Keys
{
get
{
return ConvertAll<EditableObject<T>>(new Converter<KeyValuePair<EditableObject<T>, int>, EditableObject<T>>(
delegate(KeyValuePair<EditableObject<T>, int> kvp)
{ return kvp.Key; }
)).ToArray();
}
}
/// <summary>
/// Returns an <see cref="IEnumerator<T>"/> to iterate over all the keys in this list.
/// </summary>
/// <returns>The <see cref="IEnumerator<T>"/> to use.</returns>
public IEnumerator<EditableObject<T>> GetKeyEnumerator()
{
foreach (KeyValuePair<EditableObject<T>, int> kvp in this)
{
yield return kvp.Key;
}
}
}
}