1
+ using System ;
2
+ using System . Collections . Generic ;
3
+
4
+ namespace Deveel . Collections {
5
+ public sealed class SubsetDictionary < TKey , TValue > : IDictionary < TKey , TValue > {
6
+ private SortedDictionary < TKey , TValue > subset ;
7
+
8
+ private static TKey NullKey = default ( TKey ) ;
9
+
10
+ public SubsetDictionary ( SortedDictionary < TKey , TValue > dictionary , TKey startKey , TKey endKey ) {
11
+ subset = new SortedDictionary < TKey , TValue > ( dictionary . Comparer ) ;
12
+ foreach ( KeyValuePair < TKey , TValue > pair in dictionary ) {
13
+ int c1 = 0 ;
14
+ if ( ! ReferenceEquals ( startKey , NullKey ) )
15
+ c1 = dictionary . Comparer . Compare ( pair . Key , startKey ) ;
16
+ int c2 = 0 ;
17
+ if ( ! ReferenceEquals ( endKey , NullKey ) )
18
+ c2 = dictionary . Comparer . Compare ( pair . Key , endKey ) ;
19
+ if ( c1 >= 0 && c2 <= 0 )
20
+ subset . Add ( pair . Key , pair . Value ) ;
21
+ }
22
+ }
23
+
24
+ public TValue this [ TKey key ] {
25
+ get { return subset [ key ] ; }
26
+ }
27
+
28
+ TValue IDictionary < TKey , TValue > . this [ TKey key ] {
29
+ get { return this [ key ] ; }
30
+ set { throw new NotSupportedException ( ) ; }
31
+ }
32
+
33
+ public ICollection < TKey > Keys {
34
+ get { return subset . Keys ; }
35
+ }
36
+
37
+ public ICollection < TValue > Values {
38
+ get { return subset . Values ; }
39
+ }
40
+
41
+ public int Count {
42
+ get { return subset . Count ; }
43
+ }
44
+
45
+ public bool IsReadOnly {
46
+ get { return true ; }
47
+ }
48
+
49
+ public bool ContainsKey ( TKey key ) {
50
+ return subset . ContainsKey ( key ) ;
51
+ }
52
+
53
+ void IDictionary < TKey , TValue > . Add ( TKey key , TValue value ) {
54
+ throw new NotSupportedException ( ) ;
55
+ }
56
+
57
+ bool IDictionary < TKey , TValue > . Remove ( TKey key ) {
58
+ throw new NotSupportedException ( ) ;
59
+ }
60
+
61
+ public bool TryGetValue ( TKey key , out TValue value )
62
+ {
63
+ return subset . TryGetValue ( key , out value ) ;
64
+ }
65
+
66
+ void ICollection < KeyValuePair < TKey , TValue > > . Add ( KeyValuePair < TKey , TValue > item ) {
67
+ throw new NotSupportedException ( ) ;
68
+ }
69
+
70
+ void ICollection < KeyValuePair < TKey , TValue > > . Clear ( ) {
71
+ throw new NotSupportedException ( ) ;
72
+ }
73
+
74
+ public bool Contains ( KeyValuePair < TKey , TValue > item ) {
75
+ return ( ( ICollection < KeyValuePair < TKey , TValue > > ) subset ) . Contains ( item ) ;
76
+ }
77
+
78
+ public void CopyTo ( KeyValuePair < TKey , TValue > [ ] array , int arrayIndex ) {
79
+ subset . CopyTo ( array , arrayIndex ) ;
80
+ }
81
+
82
+ bool ICollection < KeyValuePair < TKey , TValue > > . Remove ( KeyValuePair < TKey , TValue > item ) {
83
+ throw new NotSupportedException ( ) ;
84
+ }
85
+
86
+ public IEnumerator < KeyValuePair < TKey , TValue > > GetEnumerator ( )
87
+ {
88
+ return subset . GetEnumerator ( ) ;
89
+ }
90
+
91
+ System . Collections . IEnumerator System . Collections . IEnumerable . GetEnumerator ( ) {
92
+ return GetEnumerator ( ) ;
93
+ }
94
+
95
+ public static SubsetDictionary < TKey , TValue > Tail ( SortedDictionary < TKey , TValue > dictionary , TKey startKey ) {
96
+ return new SubsetDictionary < TKey , TValue > ( dictionary , startKey , NullKey ) ;
97
+ }
98
+
99
+ public static SubsetDictionary < TKey , TValue > Head ( SortedDictionary < TKey , TValue > dictionary , TKey endKey ) {
100
+ return new SubsetDictionary < TKey , TValue > ( dictionary , NullKey , endKey ) ;
101
+ }
102
+ }
103
+ }
0 commit comments