-
Notifications
You must be signed in to change notification settings - Fork 25
/
ComparisonIndex.cs
134 lines (116 loc) · 4.57 KB
/
ComparisonIndex.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using C5;
using i4o2.i4o;
namespace i4o2
{
public class ComparisonIndex<TChild, TProperty> : IIndex<TChild>
where TProperty : IComparable
{
private readonly TreeDictionary<TProperty, List<TChild>> _index = new TreeDictionary<TProperty, List<TChild>>();
private readonly PropertyReader<TChild> _propertyReader;
public ComparisonIndex(IEnumerable<TChild> collectionToIndex, PropertyInfo property)
{
_propertyReader = new PropertyReader<TChild>(property.Name);
collectionToIndex.Each(Add);
}
public IEnumerator<TChild> GetEnumerator()
{
return _index.Values.SelectMany(list => list).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(TChild item)
{
var propValue = (TProperty)_propertyReader.ReadValue(item);
if (_index.Contains(propValue))
_index[propValue].Add(item);
else
_index.Add(propValue, new List<TChild> { item });
}
public void Clear()
{
_index.Clear();
}
public bool Contains(TChild item)
{
var propValue = (TProperty)_propertyReader.ReadValue(item);
return _index.Contains(propValue);
}
public void CopyTo(TChild[] array, int arrayIndex)
{
var listOfAll = this.ToList();
listOfAll.CopyTo(array, arrayIndex);
}
public bool Remove(TChild item)
{
var propValue = (TProperty)_propertyReader.ReadValue(item);
return _index.Contains(propValue) && _index[propValue].Remove(item);
}
public int Count
{
get { return this.Count(); }
}
public bool IsReadOnly
{
get { return false; }
}
public IEnumerable<TChild> WhereThroughIndex(Expression<Func<TChild, bool>> predicate)
{
if (!(predicate.Body is BinaryExpression))
throw new NotSupportedException();
var equalityExpression = predicate.Body as BinaryExpression;
if (equalityExpression == null) throw new NullReferenceException();
var rightSide = Expression.Lambda(equalityExpression.Right);
var valueToCheck = (TProperty)rightSide.Compile().DynamicInvoke(null);
switch (equalityExpression.NodeType)
{
case ExpressionType.Equal:
foreach (var item in IsEqualTo(valueToCheck)) yield return item;
break;
case ExpressionType.LessThan:
foreach (var item in GetLessThan(valueToCheck)) yield return item;
break;
case ExpressionType.GreaterThan:
foreach (var item in GetGreaterThanOrEqualTo(valueToCheck).Except(IsEqualTo(valueToCheck))) yield return item;
break;
case ExpressionType.LessThanOrEqual:
foreach (var item in GetLessThan(valueToCheck)) yield return item;
foreach (var item in IsEqualTo(valueToCheck)) yield return item;
break;
case ExpressionType.GreaterThanOrEqual:
foreach (var item in GetGreaterThanOrEqualTo(valueToCheck)) yield return item;
break;
default:
throw new NotImplementedException("Unsupported operation");
}
}
public void Reset(TChild changedObject)
{
Remove(changedObject);
Add(changedObject);
}
private IEnumerable<TChild> IsEqualTo(TProperty valueToCheck)
{
if (_index.Contains(valueToCheck))
foreach (var item in _index[valueToCheck])
yield return item;
else
yield break;
}
private IEnumerable<TChild> GetGreaterThanOrEqualTo(TProperty valueToCheck)
{
return _index.RangeFrom(valueToCheck).SelectMany(node => node.Value);
}
private IEnumerable<TChild> GetLessThan(TProperty valueToCheck)
{
return _index.RangeTo(valueToCheck).SelectMany(node => node.Value);
}
}
}