|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions are met: |
|
7 * |
|
8 * - Redistributions of source code must retain the above copyright notice, |
|
9 * this list of conditions and the following disclaimer. |
|
10 * - Redistributions in binary form must reproduce the above copyright notice, |
|
11 * this list of conditions and the following disclaimer in the documentation |
|
12 * and/or other materials provided with the distribution. |
|
13 * - Neither the name of Nokia Corporation nor the names of its contributors |
|
14 * may be used to endorse or promote products derived from this software |
|
15 * without specific prior written permission. |
|
16 * |
|
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
27 * POSSIBILITY OF SUCH DAMAGE. |
|
28 * |
|
29 * Initial Contributors: |
|
30 * Nokia Corporation - initial contribution. |
|
31 * |
|
32 * Contributors: |
|
33 * |
|
34 * Description: |
|
35 * |
|
36 */ |
|
37 |
|
38 using System; |
|
39 using System.Text; |
|
40 using System.Collections; |
|
41 using System.Collections.Generic; |
|
42 using HeapLib.Cells; |
|
43 using SymbianUtils.Collections; |
|
44 |
|
45 namespace HeapLib.Array |
|
46 { |
|
47 public class HeapCellArray : HeapCellArrayBase |
|
48 { |
|
49 #region Constructors & destructor |
|
50 public HeapCellArray() |
|
51 : this( 100 ) |
|
52 { |
|
53 } |
|
54 |
|
55 public HeapCellArray( int aGranularity ) |
|
56 { |
|
57 iComparer = new HeapCellComparerByAddress(); |
|
58 iSortedList = new SortedList<uint, HeapCell>(); |
|
59 iFlatList = new List<HeapCell>( aGranularity ); |
|
60 } |
|
61 |
|
62 public HeapCellArray( HeapCellArray aCopy ) |
|
63 : this( aCopy.Count + 1 ) |
|
64 { |
|
65 foreach( HeapCell cell in aCopy ) |
|
66 { |
|
67 Add( cell ); |
|
68 } |
|
69 } |
|
70 #endregion |
|
71 |
|
72 #region API |
|
73 public override void Clear() |
|
74 { |
|
75 iFlatList.Clear(); |
|
76 iSortedList.Clear(); |
|
77 } |
|
78 |
|
79 public override void Add( HeapCell aCell ) |
|
80 { |
|
81 uint address = aCell.Address; |
|
82 // |
|
83 iSortedList.Add( address, aCell ); |
|
84 // |
|
85 int insertPos = iFlatList.BinarySearch( aCell, iComparer ); |
|
86 System.Diagnostics.Debug.Assert( insertPos < 0 ); |
|
87 insertPos = ~insertPos; |
|
88 iFlatList.Insert( insertPos, aCell ); |
|
89 } |
|
90 |
|
91 public override void Remove( HeapCell aCell ) |
|
92 { |
|
93 uint address = aCell.Address; |
|
94 if ( iSortedList.ContainsKey( address ) ) |
|
95 { |
|
96 HeapCell c = iSortedList[ address ]; |
|
97 iSortedList.Remove( address ); |
|
98 iFlatList.Remove( c ); |
|
99 } |
|
100 } |
|
101 |
|
102 public override int CellIndex( HeapCell aCell ) |
|
103 { |
|
104 int ret = -1; |
|
105 // |
|
106 uint address = aCell.Address; |
|
107 int pos = iFlatList.BinarySearch( aCell, iComparer ); |
|
108 if ( pos >= 0 ) |
|
109 { |
|
110 ret = pos; |
|
111 } |
|
112 // |
|
113 return ret; |
|
114 } |
|
115 |
|
116 public override HeapCell CellByAddress( uint aAddress, out int aIndex ) |
|
117 { |
|
118 HeapCell ret = null; |
|
119 |
|
120 // If we're using an address-based comparer, we can optimise the lookup. |
|
121 // Otherwise, we have to fall back to the slow iterative base class algorithm. |
|
122 if ( iComparer is HeapCellComparerByAddress ) |
|
123 { |
|
124 HeapCell temp = new HeapCell( aAddress, 0, HeapCell.TType.EAllocated ); |
|
125 aIndex = iFlatList.BinarySearch( temp, iComparer ); |
|
126 if ( aIndex < 0 ) |
|
127 { |
|
128 // There wasn't an exact match, so the binary search algorithm returns |
|
129 // us the next largest entry - or in other words, the insertion point |
|
130 // if we were about to insert a new cell into the list. |
|
131 // |
|
132 // Because we want to locate the cell that contains the specified |
|
133 // address value, then most likely it's the prior cell. |
|
134 aIndex = ~aIndex - 1; |
|
135 } |
|
136 if ( aIndex >= 0 && aIndex < iFlatList.Count ) |
|
137 { |
|
138 ret = iFlatList[ aIndex ]; |
|
139 } |
|
140 else |
|
141 { |
|
142 aIndex = -1; |
|
143 } |
|
144 } |
|
145 else |
|
146 { |
|
147 ret = base.CellByAddress( aAddress, out aIndex ); |
|
148 } |
|
149 // |
|
150 return ret; |
|
151 } |
|
152 |
|
153 public override HeapCell CellByExactAddress( uint aAddress, out int aIndex ) |
|
154 { |
|
155 aIndex = -1; |
|
156 HeapCell ret = null; |
|
157 iSortedList.TryGetValue( aAddress, out ret ); |
|
158 if ( ret != null ) |
|
159 { |
|
160 aIndex = CellIndex( ret ); |
|
161 } |
|
162 return ret; |
|
163 } |
|
164 #endregion |
|
165 |
|
166 #region Properties |
|
167 public override int Count |
|
168 { |
|
169 get { return iFlatList.Count; } |
|
170 } |
|
171 |
|
172 public override HeapCell this[ int aIndex ] |
|
173 { |
|
174 get |
|
175 { |
|
176 HeapCell ret = iFlatList[ aIndex ]; |
|
177 return ret; |
|
178 } |
|
179 } |
|
180 #endregion |
|
181 |
|
182 #region Sorting |
|
183 public void SortByAddress() |
|
184 { |
|
185 IComparer<HeapCell> comparer = new HeapCellComparerByAddress(); |
|
186 Sort( comparer ); |
|
187 } |
|
188 |
|
189 public void SortByType() |
|
190 { |
|
191 IComparer<HeapCell> comparer = new HeapCellComparerByType(); |
|
192 Sort( comparer ); |
|
193 } |
|
194 |
|
195 public void SortByLength() |
|
196 { |
|
197 IComparer<HeapCell> comparer = new HeapCellComparerByLength(); |
|
198 Sort( comparer ); |
|
199 } |
|
200 #endregion |
|
201 |
|
202 #region Internal methods |
|
203 private void Sort( IComparer<HeapCell> aComparer ) |
|
204 { |
|
205 iComparer = aComparer; |
|
206 iFlatList.Sort( iComparer ); |
|
207 } |
|
208 #endregion |
|
209 |
|
210 #region Data members |
|
211 private IComparer<HeapCell> iComparer = null; |
|
212 private List<HeapCell> iFlatList = new List<HeapCell>(); |
|
213 private SortedList<uint, HeapCell> iSortedList = new SortedList<uint, HeapCell>(); |
|
214 #endregion |
|
215 } |
|
216 } |