|
1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include <s32strm.h> |
|
17 #include "GRDPANIC.H" |
|
18 #include "GRDMAP.H" |
|
19 |
|
20 |
|
21 RWriteStream& operator<<(RWriteStream& aStream,const TSizeElement &aElement) |
|
22 { |
|
23 aElement.ExternalizeL(aStream); |
|
24 return aStream; |
|
25 } |
|
26 |
|
27 RReadStream& operator>>(RReadStream &aStream,TSizeElement &aElement) |
|
28 { |
|
29 aElement.InternalizeL(aStream); |
|
30 return aStream; |
|
31 } |
|
32 |
|
33 void TSizeElement::ExternalizeL(RWriteStream& aStream) const |
|
34 { |
|
35 aStream.WriteInt32L(iId); |
|
36 aStream.WriteInt32L(iValueInTwips); |
|
37 } |
|
38 |
|
39 void TSizeElement::InternalizeL(RReadStream& aStream) |
|
40 { |
|
41 iId = aStream.ReadInt32L(); |
|
42 iValueInTwips = aStream.ReadInt32L(); |
|
43 } |
|
44 |
|
45 // |
|
46 |
|
47 CSparseMap* CSparseMap::NewL() |
|
48 { |
|
49 CSparseMap* self = new(ELeave) CSparseMap(); |
|
50 CleanupStack::PushL(self); |
|
51 self->ConstructL(); |
|
52 CleanupStack::Pop(); |
|
53 return self; |
|
54 } |
|
55 |
|
56 CSparseMap* CSparseMap::NewL(const CSparseMap* aSparseMap) |
|
57 { |
|
58 CSparseMap* self = new(ELeave) CSparseMap(); |
|
59 CleanupStack::PushL(self); |
|
60 self->ConstructL(aSparseMap); |
|
61 CleanupStack::Pop(); |
|
62 return self; |
|
63 } |
|
64 |
|
65 void CSparseMap::ConstructL() |
|
66 // |
|
67 { |
|
68 iArray = new(ELeave) CArrayFixFlat<TSizeElement>(1); |
|
69 } |
|
70 |
|
71 void CSparseMap::ConstructL(const CSparseMap* aSparseMap) |
|
72 { |
|
73 iArray = new(ELeave) CArrayFixFlat<TSizeElement>(1); |
|
74 iDefaultValueInTwips=aSparseMap->iDefaultValueInTwips; |
|
75 iDefaultValueInPixels=aSparseMap->iDefaultValueInPixels; |
|
76 TInt end=aSparseMap->iArray->Count(); |
|
77 for (TInt ii=0;ii<end;ii++) |
|
78 iArray->AppendL((*aSparseMap)[ii]); |
|
79 } |
|
80 |
|
81 CSparseMap::CSparseMap() |
|
82 : iDefaultValueInTwips(0), |
|
83 iDefaultValueInPixels(0) |
|
84 { |
|
85 } |
|
86 |
|
87 CSparseMap::~CSparseMap() |
|
88 // |
|
89 { |
|
90 delete iArray; |
|
91 } |
|
92 |
|
93 TInt CSparseMap::Count() const |
|
94 { |
|
95 return iArray->Count(); |
|
96 } |
|
97 |
|
98 void CSparseMap::SetL(TInt aId,TInt aValueInTwips,TInt aValueInPixels) |
|
99 // |
|
100 { |
|
101 TKeyArrayFix key(0,ECmpTInt); |
|
102 TInt pos; |
|
103 TSizeElement element; |
|
104 element.iId = aId; |
|
105 element.iValueInTwips = aValueInTwips; |
|
106 element.iValueInPixels = aValueInPixels; |
|
107 if (iDefaultValueInTwips==aValueInTwips && iDefaultValueInPixels==aValueInPixels) |
|
108 {// remove it from the array if it is already there |
|
109 if (iArray->FindIsq(element,key,pos)==0) |
|
110 {// found |
|
111 iArray->Delete(pos); |
|
112 } |
|
113 } |
|
114 else |
|
115 { |
|
116 if (iArray->FindIsq(element,key,pos)==0) |
|
117 {// found |
|
118 (*iArray)[pos] = element; |
|
119 } |
|
120 else |
|
121 { |
|
122 iArray->InsertIsqL(element,key); |
|
123 } |
|
124 } |
|
125 } |
|
126 |
|
127 TInt CSparseMap::DefaultValueInTwips() const |
|
128 { |
|
129 return iDefaultValueInTwips; |
|
130 } |
|
131 |
|
132 TInt CSparseMap::DefaultValueInPixels() const |
|
133 { |
|
134 return iDefaultValueInPixels; |
|
135 } |
|
136 |
|
137 void CSparseMap::SetDefaultValueInTwips(TInt aValueInTwips) |
|
138 // |
|
139 { |
|
140 iDefaultValueInTwips = aValueInTwips; |
|
141 /* TInt end=iArray->Count(); |
|
142 for (TInt ii=0;ii<end;ii++) |
|
143 { |
|
144 if (*iArray)[ii]==iDefaultValue) |
|
145 { |
|
146 iArray->Delete(ii--); |
|
147 end--; |
|
148 } |
|
149 } //!! Is this a valid optimization? */ |
|
150 } |
|
151 |
|
152 void CSparseMap::SetDefaultValueInPixels(TInt aValueInPixels) |
|
153 { |
|
154 __ASSERT_DEBUG(aValueInPixels,Panic(EGridMapDefaultValueIsZero)); |
|
155 iDefaultValueInPixels = aValueInPixels; |
|
156 } |
|
157 |
|
158 TInt CSparseMap::ValueInTwips(TInt aId) const |
|
159 // |
|
160 { |
|
161 TInt pos; |
|
162 if (FindId(aId,pos)==0) |
|
163 {// found |
|
164 return (*iArray)[pos].iValueInTwips; |
|
165 } |
|
166 return (iDefaultValueInTwips); |
|
167 } |
|
168 |
|
169 TInt CSparseMap::ValueInPixels(TInt aId) const |
|
170 // |
|
171 { |
|
172 TInt pos; |
|
173 if (FindId(aId,pos)==0) |
|
174 {// found |
|
175 return (*iArray)[pos].iValueInPixels; |
|
176 } |
|
177 return (iDefaultValueInPixels); |
|
178 } |
|
179 |
|
180 void CSparseMap::ResetArray() |
|
181 { |
|
182 iArray->Reset(); |
|
183 } |
|
184 |
|
185 void CSparseMap::OpenCloseGap(TInt aStartId,TInt aShiftOffset,TInt aMaxId) |
|
186 // |
|
187 // +ve offsets opens a gap causing shifted ids that are greater than aMaxId to be deleted |
|
188 // -ve offsets close a gap causing shifted ids that are less than aStartId to be deleted |
|
189 { |
|
190 TInt pos; |
|
191 FindId(aStartId,pos); |
|
192 TInt count=iArray->Count(); |
|
193 for (;pos<count;pos++) |
|
194 { |
|
195 TInt& currentId=(*iArray)[pos].iId; |
|
196 currentId+=aShiftOffset; |
|
197 if (currentId<aStartId) |
|
198 { |
|
199 iArray->Delete(pos--); |
|
200 count--; |
|
201 } |
|
202 else if (currentId>aMaxId) |
|
203 { |
|
204 iArray->Delete(pos,count-pos); // All the rest |
|
205 break; |
|
206 } |
|
207 } |
|
208 } |
|
209 |
|
210 void CSparseMap::IdToDisplacement(TInt aId1,TInt aId2,TInt& aReturnDisp) const |
|
211 // |
|
212 // Returns the displacement in pixels between two id's. Skips over sections with default |
|
213 // value to minimize array accesses |
|
214 // |
|
215 { |
|
216 TInt direction=1; |
|
217 if (aId2<aId1) |
|
218 { |
|
219 Mem::Swap(&aId1,&aId2,sizeof(TInt)); |
|
220 direction=-1; |
|
221 } |
|
222 TInt pos=0; |
|
223 FindId(aId1,pos); |
|
224 TInt maxPos=iArray->Count()-1; |
|
225 TInt currentId; |
|
226 if (pos>maxPos || (currentId=(*iArray)[pos].iId)>=aId2) |
|
227 { |
|
228 aReturnDisp=((aId2-aId1)*iDefaultValueInPixels*direction); |
|
229 return; |
|
230 } |
|
231 aReturnDisp=((currentId-aId1)*iDefaultValueInPixels); |
|
232 TInt newId; |
|
233 FOREVER |
|
234 { |
|
235 aReturnDisp+=((*iArray)[pos].iValueInPixels); |
|
236 pos++; |
|
237 if (pos>maxPos || (newId=(*iArray)[pos].iId)>=aId2) |
|
238 break; |
|
239 aReturnDisp+=((newId-currentId-1)*iDefaultValueInPixels); |
|
240 currentId=newId; |
|
241 } |
|
242 aReturnDisp+=((aId2-currentId-1)*iDefaultValueInPixels); |
|
243 aReturnDisp*=direction; |
|
244 } |
|
245 |
|
246 TBool CSparseMap::DisplacementToId(TInt aId,TInt aDisp,TInt& aReturnId) const |
|
247 // |
|
248 // Changes the returnId nearest to the given displacement in pixels from aId. Nearest in this sense |
|
249 // means rounded down to the lowest id. Returns ETrue if the displacement lands on an exact id. |
|
250 // |
|
251 { |
|
252 if (aDisp==0) |
|
253 { |
|
254 aReturnId=aId; |
|
255 return ETrue; |
|
256 } |
|
257 __ASSERT_DEBUG(iDefaultValueInPixels,Panic(EGridMapDefaultValueIsZero)); |
|
258 TInt pos=0; |
|
259 FindId(aId,pos); |
|
260 TInt maxPos=iArray->Count()-1; |
|
261 aReturnId=0; |
|
262 TInt tempDisp=0; |
|
263 if (aDisp>0) |
|
264 { |
|
265 if (pos<=maxPos) |
|
266 { |
|
267 aReturnId=(*iArray)[pos].iId; |
|
268 tempDisp=(aReturnId-aId)*iDefaultValueInPixels; |
|
269 } |
|
270 if (pos>maxPos || tempDisp>aDisp) |
|
271 { |
|
272 aReturnId=aDisp/iDefaultValueInPixels+aId; |
|
273 tempDisp=(aReturnId-aId)*iDefaultValueInPixels; |
|
274 } |
|
275 else |
|
276 { |
|
277 tempDisp+=(*iArray)[pos].iValueInPixels; |
|
278 TInt newDisp=0; |
|
279 TInt newId=0; |
|
280 while (tempDisp<=aDisp) |
|
281 { |
|
282 aReturnId++; |
|
283 pos++; |
|
284 if (pos<=maxPos) |
|
285 { |
|
286 newId=(*iArray)[pos].iId; |
|
287 newDisp=tempDisp+(newId-aReturnId)*iDefaultValueInPixels; |
|
288 } |
|
289 if (pos>maxPos || newDisp>aDisp) |
|
290 { |
|
291 TInt add = (aDisp-tempDisp)/iDefaultValueInPixels; |
|
292 tempDisp+=add*iDefaultValueInPixels; |
|
293 aReturnId+=add; |
|
294 break; |
|
295 } |
|
296 aReturnId=newId; |
|
297 tempDisp=newDisp+(*iArray)[pos].iValueInPixels; |
|
298 } |
|
299 } |
|
300 } |
|
301 else |
|
302 { |
|
303 pos--; |
|
304 if (pos>=0) |
|
305 { |
|
306 aReturnId=(*iArray)[pos].iId; |
|
307 tempDisp=(aReturnId-aId+1)*iDefaultValueInPixels; |
|
308 } |
|
309 if (pos<0 || tempDisp<aDisp) |
|
310 { |
|
311 aReturnId=aDisp/iDefaultValueInPixels+aId-1; |
|
312 tempDisp=(aReturnId-aId+1)*iDefaultValueInPixels; |
|
313 } |
|
314 else |
|
315 { |
|
316 tempDisp-=(*iArray)[pos].iValueInPixels; |
|
317 TInt newDisp=0; |
|
318 TInt newId=0; |
|
319 while (tempDisp>=aDisp) |
|
320 { |
|
321 pos--; |
|
322 if (pos>=0) |
|
323 { |
|
324 newId=(*iArray)[pos].iId; |
|
325 newDisp=tempDisp+(newId-aReturnId+1)*iDefaultValueInPixels; |
|
326 } |
|
327 if (pos<0 || newDisp<aDisp) |
|
328 { |
|
329 TInt add = (aDisp-tempDisp)/iDefaultValueInPixels-1; |
|
330 tempDisp+=(add+1)*iDefaultValueInPixels; |
|
331 aReturnId+=add; |
|
332 break; |
|
333 } |
|
334 aReturnId=newId; |
|
335 tempDisp=newDisp; |
|
336 tempDisp-=(*iArray)[pos].iValueInPixels; |
|
337 } |
|
338 } |
|
339 } |
|
340 return (aDisp==tempDisp); |
|
341 } |
|
342 |
|
343 TInt CSparseMap::FindId(TInt aId,TInt& aFindPos) const |
|
344 { |
|
345 TKeyArrayFix key(0,ECmpTInt); |
|
346 TSizeElement element; |
|
347 element.iId = aId; |
|
348 return iArray->FindIsq(element,key,aFindPos); |
|
349 } |
|
350 |
|
351 const TSizeElement& CSparseMap::operator[](TInt aIndex) const |
|
352 { |
|
353 return (*iArray)[aIndex]; |
|
354 } |
|
355 |
|
356 TSizeElement& CSparseMap::operator[](TInt aIndex) |
|
357 { |
|
358 return (*iArray)[aIndex]; |
|
359 } |
|
360 |
|
361 void CSparseMap::ExternalizeL(RWriteStream& aStream) const |
|
362 { |
|
363 aStream.WriteInt32L(iDefaultValueInTwips); |
|
364 aStream << *iArray; |
|
365 } |
|
366 |
|
367 void CSparseMap::InternalizeL(RReadStream& aStream) |
|
368 { |
|
369 iDefaultValueInTwips = aStream.ReadInt32L(); |
|
370 aStream >> *iArray; |
|
371 } |
|
372 |