|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Collections.Generic; |
|
19 |
|
20 namespace SymbianUtils.Collections |
|
21 { |
|
22 public class SymbianSortedListByValue<TKey, TValue> where TValue : class |
|
23 { |
|
24 #region Constructors |
|
25 public SymbianSortedListByValue( IComparer<TValue> aComparer ) |
|
26 : this( 10, aComparer ) |
|
27 { |
|
28 } |
|
29 |
|
30 public SymbianSortedListByValue( int aGranularity, IComparer<TValue> aComparer ) |
|
31 { |
|
32 iList = new List<KeyValuePair<TValue, TKey>>( aGranularity ); |
|
33 iComparer = new SymbianSortedListByValueComparer( aComparer ); |
|
34 } |
|
35 |
|
36 public SymbianSortedListByValue( SymbianSortedListByValue<TKey, TValue> aCollection ) |
|
37 { |
|
38 int count = aCollection.Count; |
|
39 // |
|
40 iList = new List<KeyValuePair<TValue, TKey>>( count + 1 ); |
|
41 iComparer = new SymbianSortedListByValue<TKey, TValue>.SymbianSortedListByValueComparer( aCollection.iComparer.Comparer ); |
|
42 // |
|
43 for ( int i = 0; i < count; i++ ) |
|
44 { |
|
45 KeyValuePair<TValue, TKey> entry = aCollection.iList[ i ]; |
|
46 Add( entry.Value, entry.Key ); |
|
47 } |
|
48 } |
|
49 #endregion |
|
50 |
|
51 #region API |
|
52 public bool Contains( TKey aKey ) |
|
53 { |
|
54 return iDictionary.ContainsKey( aKey ); |
|
55 } |
|
56 |
|
57 public void Add( TKey aKey, TValue aValue ) |
|
58 { |
|
59 if ( aKey == null ) |
|
60 { |
|
61 throw new ArgumentNullException( "aKey cannot be null" ); |
|
62 } |
|
63 |
|
64 // Add item to dictionary if required |
|
65 if ( !iDictionary.ContainsKey( aKey ) ) |
|
66 { |
|
67 iDictionary.Add( aKey, aValue ); |
|
68 } |
|
69 |
|
70 // Search for item in the list. If it's already present then |
|
71 // this will return the index. Otherwise, it will return a negative |
|
72 // number. |
|
73 // If this index is greater than or equal to the size of the array, |
|
74 // there are no elements larger than value in the array. |
|
75 // |
|
76 // Otherwise, it is the index of the first element that is larger than value. |
|
77 KeyValuePair<TValue, TKey> entry = new KeyValuePair<TValue, TKey>( aValue, aKey ); |
|
78 int index = iList.BinarySearch( entry, iComparer ); |
|
79 if ( index < 0 ) |
|
80 { |
|
81 index = ~index; |
|
82 } |
|
83 iList.Insert( index, entry ); |
|
84 } |
|
85 |
|
86 public void Remove( Predicate<TValue> aValue ) |
|
87 { |
|
88 Predicate<KeyValuePair<TValue, TKey>> matchPredicate = delegate( KeyValuePair<TValue, TKey> aItem ) |
|
89 { |
|
90 bool remove = aValue.Invoke( aItem.Key ); |
|
91 return remove; |
|
92 }; |
|
93 |
|
94 RemoveAll( matchPredicate ); |
|
95 } |
|
96 |
|
97 public void Remove( TValue aValue ) |
|
98 { |
|
99 Predicate<KeyValuePair<TValue, TKey>> matchPredicate = delegate( KeyValuePair<TValue, TKey> aItem ) |
|
100 { |
|
101 return ( aValue == aItem.Key ); |
|
102 }; |
|
103 |
|
104 RemoveAll( matchPredicate ); |
|
105 } |
|
106 |
|
107 public void Remove( TKey aKey ) |
|
108 { |
|
109 TValue v = null; |
|
110 if ( iDictionary.TryGetValue( aKey, out v ) ) |
|
111 { |
|
112 iDictionary.Remove( aKey ); |
|
113 Remove( v ); |
|
114 } |
|
115 } |
|
116 |
|
117 public void Clear() |
|
118 { |
|
119 iList.Clear(); |
|
120 iDictionary.Clear(); |
|
121 } |
|
122 |
|
123 public void RemoveRange( int aStartIndex, int aCount ) |
|
124 { |
|
125 List<KeyValuePair<TValue, TKey>> items = iList.GetRange( aStartIndex, aCount ); |
|
126 RemoveAll( items ); |
|
127 } |
|
128 |
|
129 public void Sort( IComparer<TValue> aComparer ) |
|
130 { |
|
131 SymbianSortedListByValueComparer comparer = new SymbianSortedListByValueComparer( aComparer ); |
|
132 iList.Sort( comparer ); |
|
133 iComparer = comparer; |
|
134 } |
|
135 #endregion |
|
136 |
|
137 #region Properties |
|
138 public int Count |
|
139 { |
|
140 get { return iList.Count; } |
|
141 } |
|
142 |
|
143 public TValue this[ int aIndex ] |
|
144 { |
|
145 get |
|
146 { |
|
147 KeyValuePair< TValue, TKey> ret = iList[ aIndex ]; |
|
148 return ret.Key; |
|
149 } |
|
150 } |
|
151 |
|
152 public TValue this[ TKey aKey ] |
|
153 { |
|
154 get |
|
155 { |
|
156 TValue ret = null; |
|
157 bool found = iDictionary.TryGetValue( aKey, out ret ); |
|
158 return ret; |
|
159 } |
|
160 } |
|
161 #endregion |
|
162 |
|
163 #region Internal methods |
|
164 private void RemoveAll( Predicate<KeyValuePair<TValue, TKey>> aPredicate ) |
|
165 { |
|
166 List<KeyValuePair<TValue, TKey>> items = iList.FindAll( aPredicate ); |
|
167 RemoveAll( items ); |
|
168 } |
|
169 |
|
170 private void RemoveAll( List<KeyValuePair<TValue, TKey>> aItems ) |
|
171 { |
|
172 foreach ( KeyValuePair<TValue, TKey> item in aItems ) |
|
173 { |
|
174 // Remove from dictionary |
|
175 TKey key = item.Value; |
|
176 if ( iDictionary.ContainsKey( key ) ) |
|
177 { |
|
178 iDictionary.Remove( key ); |
|
179 } |
|
180 |
|
181 // Remove from list |
|
182 TValue value = item.Key; |
|
183 iList.Remove( item ); |
|
184 } |
|
185 } |
|
186 #endregion |
|
187 |
|
188 #region Internal classes |
|
189 private class SymbianSortedListByValueComparer : IComparer< KeyValuePair<TValue, TKey> > |
|
190 { |
|
191 #region Constructors |
|
192 public SymbianSortedListByValueComparer( IComparer<TValue> aComparer ) |
|
193 { |
|
194 iComparer = aComparer; |
|
195 } |
|
196 #endregion |
|
197 |
|
198 #region Properties |
|
199 public IComparer<TValue> Comparer |
|
200 { |
|
201 get { return iComparer; } |
|
202 } |
|
203 #endregion |
|
204 |
|
205 #region From IComparer<KeyValuePair<TValue,TKey>> |
|
206 public int Compare( KeyValuePair<TValue, TKey> aLeft, KeyValuePair<TValue, TKey> aRight ) |
|
207 { |
|
208 int ret = iComparer.Compare( aLeft.Key, aRight.Key ); |
|
209 return ret; |
|
210 } |
|
211 #endregion |
|
212 |
|
213 #region Data members |
|
214 private readonly IComparer<TValue> iComparer; |
|
215 #endregion |
|
216 } |
|
217 #endregion |
|
218 |
|
219 #region Data members |
|
220 private List< KeyValuePair<TValue, TKey> > iList = null; |
|
221 private Dictionary<TKey, TValue> iDictionary = new Dictionary<TKey, TValue>(); |
|
222 private SymbianSortedListByValueComparer iComparer = null; |
|
223 #endregion |
|
224 } |
|
225 } |