|
1 /* |
|
2 * Copyright (c) 2005,2006 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: Optimizer module for ChspsDomStringPool. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include "hspsdomstringpooloptimizer.h" |
|
22 |
|
23 const TInt KMaxEstimateThreshold = 2; |
|
24 |
|
25 // ============================ MEMBER FUNCTIONS =============================== |
|
26 |
|
27 // ----------------------------------------------------------------------------- |
|
28 // ThspsDomStringPoolOptimizerEntry::ThspsDomStringPoolOptimizerEntry |
|
29 // ----------------------------------------------------------------------------- |
|
30 // |
|
31 ThspsDomStringPoolOptimizerEntry::ThspsDomStringPoolOptimizerEntry( TInt aIndex, |
|
32 const TDesC8& aString) : |
|
33 iIndex( aIndex ), |
|
34 iString( aString ) |
|
35 { |
|
36 } |
|
37 |
|
38 // ----------------------------------------------------------------------------- |
|
39 // ThspsDomStringPoolOptimizer::AddEntryL |
|
40 // ----------------------------------------------------------------------------- |
|
41 // |
|
42 void ThspsDomStringPoolOptimizer::AddEntryL( ThspsDomStringPoolOptimizerEntry& aEntry ) |
|
43 { |
|
44 TBool positionFound = EFalse; |
|
45 |
|
46 if( iEntries.Count() > 0 ) |
|
47 { |
|
48 const TInt estimate = FindInsertionIndexEstimate( |
|
49 aEntry.iString, |
|
50 0, |
|
51 iEntries.Count() - 1 ); |
|
52 |
|
53 if( estimate != KErrNotFound ) |
|
54 { |
|
55 for( TInt i = estimate; i < iEntries.Count(); i++ ) |
|
56 { |
|
57 if( aEntry.iString.Compare( iEntries[i].iString ) < 0 ) |
|
58 { |
|
59 positionFound = ETrue; |
|
60 iEntries.InsertL( aEntry, i ); |
|
61 break; |
|
62 } |
|
63 } |
|
64 } |
|
65 } |
|
66 |
|
67 if( !positionFound ) |
|
68 { |
|
69 iEntries.AppendL( aEntry ); |
|
70 } |
|
71 } |
|
72 |
|
73 // ----------------------------------------------------------------------------- |
|
74 // ThspsDomStringPoolOptimizer::ThspsDomStringPoolOptimizerGetIndex |
|
75 // ----------------------------------------------------------------------------- |
|
76 // |
|
77 TInt ThspsDomStringPoolOptimizer::GetIndex( const TDesC8& aString ) |
|
78 { |
|
79 if( iEntries.Count() == 0 ) |
|
80 { |
|
81 return KErrNotFound; |
|
82 } |
|
83 |
|
84 TInt index = FindEntry( aString, 0, iEntries.Count() - 1 ); |
|
85 if( index >= 0 && index < iEntries.Count() ) |
|
86 { |
|
87 return iEntries[index].iIndex; |
|
88 } |
|
89 else |
|
90 { |
|
91 // Error code. |
|
92 return index; |
|
93 } |
|
94 } |
|
95 |
|
96 // ----------------------------------------------------------------------------- |
|
97 // ThspsDomStringPoolOptimizer::Close |
|
98 // ----------------------------------------------------------------------------- |
|
99 // |
|
100 void ThspsDomStringPoolOptimizer::Close() |
|
101 { |
|
102 iEntries.Close(); |
|
103 } |
|
104 |
|
105 // ----------------------------------------------------------------------------- |
|
106 // ThspsDomStringPoolOptimizer::Count |
|
107 // ----------------------------------------------------------------------------- |
|
108 // |
|
109 TInt ThspsDomStringPoolOptimizer::Count() |
|
110 { |
|
111 return iEntries.Count(); |
|
112 } |
|
113 |
|
114 // ----------------------------------------------------------------------------- |
|
115 // ThspsDomStringPoolOptimizer::Entry |
|
116 // ----------------------------------------------------------------------------- |
|
117 // |
|
118 ThspsDomStringPoolOptimizerEntry& ThspsDomStringPoolOptimizer::Entry( |
|
119 const TInt aIndex ) |
|
120 { |
|
121 return iEntries[ aIndex ]; |
|
122 } |
|
123 |
|
124 // ----------------------------------------------------------------------------- |
|
125 // ThspsDomStringPoolOptimizer::Reset |
|
126 // ----------------------------------------------------------------------------- |
|
127 // |
|
128 void ThspsDomStringPoolOptimizer::Reset() |
|
129 { |
|
130 iEntries.Reset(); |
|
131 } |
|
132 |
|
133 // ----------------------------------------------------------------------------- |
|
134 // ThspsDomStringPoolOptimizer::FindEntry |
|
135 // ----------------------------------------------------------------------------- |
|
136 // |
|
137 TInt ThspsDomStringPoolOptimizer::FindEntry( const TDesC8& aString, |
|
138 const TInt aLeft, |
|
139 const TInt aRight ) |
|
140 { |
|
141 if( aLeft > aRight ) |
|
142 { |
|
143 return KErrNotFound; |
|
144 } |
|
145 |
|
146 const TUint middle = ( aLeft + aRight ) >> 1; // >> 1 means "divided by two". |
|
147 ThspsDomStringPoolOptimizerEntry& entryAtMiddle = iEntries[ middle ]; |
|
148 const TInt comparisonResult = aString.Compare( entryAtMiddle.iString ); |
|
149 |
|
150 if( comparisonResult > 0 ) |
|
151 { |
|
152 return FindEntry( aString, middle + 1, aRight); |
|
153 } |
|
154 else if( comparisonResult < 0 ) |
|
155 { |
|
156 return FindEntry( aString, aLeft, middle - 1 ); |
|
157 } |
|
158 else |
|
159 { |
|
160 return middle; |
|
161 } |
|
162 } |
|
163 |
|
164 // ----------------------------------------------------------------------------- |
|
165 // ThspsDomStringPoolOptimizer::FindEntry |
|
166 // ----------------------------------------------------------------------------- |
|
167 // |
|
168 TInt ThspsDomStringPoolOptimizer::FindInsertionIndexEstimate( const TDesC8& aString, |
|
169 const TInt aLeft, |
|
170 const TInt aRight ) |
|
171 { |
|
172 if( ( aRight - aLeft ) <= KMaxEstimateThreshold ) |
|
173 { |
|
174 return aLeft; |
|
175 } |
|
176 |
|
177 const TUint middle = ( aLeft + aRight ) >> 1; |
|
178 |
|
179 ThspsDomStringPoolOptimizerEntry& entryAtMiddle = iEntries[ middle ]; |
|
180 const TInt comparisonResult = aString.Compare( entryAtMiddle.iString ); |
|
181 |
|
182 if( comparisonResult > 0 ) |
|
183 { |
|
184 return FindInsertionIndexEstimate( aString, middle, aRight); |
|
185 } |
|
186 else if( comparisonResult < 0 ) |
|
187 { |
|
188 return FindInsertionIndexEstimate( aString, aLeft, middle ); |
|
189 } |
|
190 else |
|
191 { |
|
192 // Should not go here. There should be only one of a kind in the list. |
|
193 return KErrNotFound; |
|
194 } |
|
195 } |
|
196 |
|
197 // End of File |