-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathMultiSourceIndexList.cs
More file actions
140 lines (128 loc) · 4.49 KB
/
MultiSourceIndexList.cs
File metadata and controls
140 lines (128 loc) · 4.49 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections;
namespace Equin.ApplicationFramework
{
internal class MultiSourceIndexList<T> : List<KeyValuePair<ListItemPair<T>, int>>
{
public void Add(IList sourceList, ObjectView<T> item, int index)
{
Add(new KeyValuePair<ListItemPair<T>, int>(new ListItemPair<T>(sourceList, 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(IList sourceList, int sourceIndex)
{
for (int i = 0; i < Count; i++)
{
if (this[i].Key.List == sourceList && 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.Item.Object.Equals(item) && this[i].Value > -1)
{
return i;
}
}
return -1;
}
/// <summary>
/// Searches for a given item's <see cref="ObjectView<T>"/> wrapper, returning the index of the value in this list.
/// </summary>
/// <param name="item">The <see cref="ObjectView<T>"/> to search for.</param>
/// <returns>Returns the index in this list of the item, or -1 if not found.</returns>
public int IndexOfKey(ObjectView<T> item)
{
for (int i = 0; i < Count; i++)
{
if (this[i].Key.Item.Equals(item) && this[i].Value > -1)
{
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="ObjectView<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(ObjectView<T> key)
{
return (IndexOfKey(key) != -1);
}
/// <summary>
/// Returns an array of all the <see cref="ObjectView<T>"/> keys in the list.
/// </summary>
public ObjectView<T>[] Keys
{
get
{
return ConvertAll<ObjectView<T>>(new Converter<KeyValuePair<ListItemPair<T>, int>, ObjectView<T>>(
delegate(KeyValuePair<ListItemPair<T>, int> kvp)
{ return kvp.Key.Item; }
)).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<ObjectView<T>> GetKeyEnumerator()
{
foreach (KeyValuePair<ListItemPair<T>, int> kvp in this)
{
yield return kvp.Key.Item;
}
}
}
internal class ListItemPair<T>
{
private IList _list;
private ObjectView<T> _item;
public ListItemPair(IList list, ObjectView<T> item)
{
_list = list;
_item = item;
}
public IList List
{
get
{
return _list;
}
}
public ObjectView<T> Item
{
get
{
return _item;
}
}
}
}