forked from ical-org/ical.net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupedList.cs
More file actions
217 lines (179 loc) · 6.08 KB
/
GroupedList.cs
File metadata and controls
217 lines (179 loc) · 6.08 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Ical.Net.Collections
{
/// <summary>
/// A list of objects that are keyed.
/// </summary>
public class GroupedList<TGroup, TItem> :
IGroupedList<TGroup, TItem>
where TItem : class, IGroupedObject<TGroup>
{
readonly List<IMultiLinkedList<TItem>> _lists = new List<IMultiLinkedList<TItem>>();
readonly Dictionary<TGroup, IMultiLinkedList<TItem>> _dictionary = new Dictionary<TGroup, IMultiLinkedList<TItem>>();
IMultiLinkedList<TItem> EnsureList(TGroup group)
{
if (group == null)
{
return null;
}
if (_dictionary.ContainsKey(group))
{
return _dictionary[group];
}
var list = new MultiLinkedList<TItem>();
_dictionary[group] = list;
_lists.Add(list);
return list;
}
IMultiLinkedList<TItem> ListForIndex(int index, out int relativeIndex)
{
foreach (var list in _lists.Where(list => list.StartIndex <= index && list.ExclusiveEnd > index))
{
relativeIndex = index - list.StartIndex;
return list;
}
relativeIndex = -1;
return null;
}
public event EventHandler<ObjectEventArgs<TItem, int>> ItemAdded;
protected void OnItemAdded(TItem obj, int index) => ItemAdded?.Invoke(this, new ObjectEventArgs<TItem, int>(obj, index));
public void Add(TItem item)
{
if (item == null)
{
return;
}
// Add a new list if necessary
var group = item.Group;
var list = EnsureList(group);
var index = list.Count;
list.Add(item);
OnItemAdded(item, list.StartIndex + index);
}
public int IndexOf(TItem item)
{
var group = item.Group;
if (!_dictionary.ContainsKey(group))
{
return -1;
}
// Get the list associated with this object's group
var list = _dictionary[group];
// Find the object within the list.
var index = list.IndexOf(item);
// Return the index within the overall KeyedList
if (index >= 0)
return list.StartIndex + index;
return -1;
}
public void Clear(TGroup group)
{
if (!_dictionary.ContainsKey(group))
{
return;
}
// Clear the list (note that this also clears the list in the _Lists object).
_dictionary[group].Clear();
}
public void Clear()
{
_dictionary.Clear();
_lists.Clear();
}
public bool ContainsKey(TGroup group) => _dictionary.ContainsKey(group);
public int Count => _lists.Sum(list => list.Count);
public int CountOf(TGroup group) => _dictionary.ContainsKey(group)
? _dictionary[group].Count
: 0;
public IEnumerable<TItem> Values() => _dictionary.Values.SelectMany(i => i);
public IEnumerable<TItem> AllOf(TGroup group) => _dictionary.ContainsKey(group)
? (IEnumerable<TItem>) _dictionary[group]
: new TItem[0];
public bool Remove(TItem obj)
{
var group = obj.Group;
if (!_dictionary.ContainsKey(group))
{
return false;
}
var items = _dictionary[group];
var index = items.IndexOf(obj);
if (index < 0)
{
return false;
}
items.RemoveAt(index);
return true;
}
public bool Remove(TGroup group)
{
if (!_dictionary.ContainsKey(group))
{
return false;
}
var list = _dictionary[group];
for (var i = list.Count - 1; i >= 0; i--)
{
list.RemoveAt(i);
}
return true;
}
public bool Contains(TItem item)
{
var group = item.Group;
return _dictionary.ContainsKey(group) && _dictionary[group].Contains(item);
}
public void CopyTo(TItem[] array, int arrayIndex) => _dictionary.SelectMany(kvp => kvp.Value).ToArray().CopyTo(array, arrayIndex);
public bool IsReadOnly => false;
public void Insert(int index, TItem item)
{
int relativeIndex;
var list = ListForIndex(index, out relativeIndex);
if (list == null)
{
return;
}
list.Insert(relativeIndex, item);
OnItemAdded(item, index);
}
public void RemoveAt(int index)
{
int relativeIndex;
var list = ListForIndex(index, out relativeIndex);
if (list == null)
{
return;
}
var item = list[relativeIndex];
list.RemoveAt(relativeIndex);
}
public TItem this[int index]
{
get
{
int relativeIndex;
var list = ListForIndex(index, out relativeIndex);
return list?[relativeIndex];
}
set
{
int relativeIndex;
var list = ListForIndex(index, out relativeIndex);
if (list == null)
{
return;
}
// Remove the item at that index and replace it
var item = list[relativeIndex];
list.RemoveAt(relativeIndex);
list.Insert(relativeIndex, value);
OnItemAdded(item, index);
}
}
public IEnumerator<TItem> GetEnumerator() => new GroupedListEnumerator<TItem>(_lists);
IEnumerator IEnumerable.GetEnumerator() => new GroupedListEnumerator<TItem>(_lists);
}
}