0
|
1 |
// Copyright (c) 1998-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 the License "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 |
// e32\common\array.cpp
|
|
15 |
//
|
|
16 |
//
|
|
17 |
|
|
18 |
#include "common.h"
|
|
19 |
#ifdef __KERNEL_MODE__
|
|
20 |
#include <kernel/kernel.h>
|
|
21 |
#endif
|
|
22 |
|
|
23 |
const TInt KDefaultPtrArrayGranularity =8;
|
|
24 |
const TInt KPtrArrayMaxGranularity =0x10000000;
|
|
25 |
const TInt KDefaultSimpleArrayGranularity =8;
|
|
26 |
const TInt KSimpleArrayMaxGranularity =0x10000000;
|
|
27 |
const TInt KSimpleArrayMaxEntrySize =640; // allow room for a unicode TFullName
|
|
28 |
const TInt KMaxArrayGrowBy =65535;
|
|
29 |
const TInt KMinArrayFactor =257;
|
|
30 |
const TInt KMaxArrayFactor =32767;
|
|
31 |
|
|
32 |
EXPORT_C RPointerArrayBase::RPointerArrayBase()
|
|
33 |
: iCount(0), iEntries(NULL), iAllocated(0), iGranularity(KDefaultPtrArrayGranularity), iSpare1(0), iSpare2(0)
|
|
34 |
{}
|
|
35 |
|
|
36 |
EXPORT_C RPointerArrayBase::RPointerArrayBase(TInt aGranularity)
|
|
37 |
: iCount(0), iEntries(NULL), iAllocated(0), iGranularity(aGranularity), iSpare1(0), iSpare2(0)
|
|
38 |
{
|
|
39 |
__ASSERT_ALWAYS(aGranularity>0 && aGranularity<=KPtrArrayMaxGranularity,
|
|
40 |
Panic(EBadArrayGranularity));
|
|
41 |
}
|
|
42 |
|
|
43 |
EXPORT_C RPointerArrayBase::RPointerArrayBase(TInt aMinGrowBy, TInt aFactor)
|
|
44 |
: iCount(0), iEntries(NULL), iAllocated(0), iSpare1(0), iSpare2(0)
|
|
45 |
{
|
|
46 |
__ASSERT_ALWAYS(aMinGrowBy>0 && aMinGrowBy<=KMaxArrayGrowBy, Panic(EBadArrayMinGrowBy));
|
|
47 |
__ASSERT_ALWAYS(aFactor>=KMinArrayFactor && aFactor<=KMaxArrayFactor, Panic(EBadArrayFactor));
|
|
48 |
iGranularity = aMinGrowBy | (aFactor << 16) | 0x80000000;
|
|
49 |
}
|
|
50 |
|
|
51 |
#ifndef __KERNEL_MODE__
|
|
52 |
EXPORT_C RPointerArrayBase::RPointerArrayBase(TAny** aEntries, TInt aCount)
|
|
53 |
: iCount(aCount), iEntries(aEntries), iAllocated(aCount), iGranularity(aCount), iSpare1(0), iSpare2(0)
|
|
54 |
{
|
|
55 |
__ASSERT_ALWAYS(aCount>0,Panic(EBadArrayCount));
|
|
56 |
}
|
|
57 |
#endif
|
|
58 |
|
|
59 |
EXPORT_C void RPointerArrayBase::Close()
|
|
60 |
{
|
|
61 |
iCount=0;
|
|
62 |
STD_CLASS::Free(iEntries);
|
|
63 |
iEntries=NULL;
|
|
64 |
iAllocated=0;
|
|
65 |
}
|
|
66 |
|
|
67 |
EXPORT_C TInt RPointerArrayBase::Count() const
|
|
68 |
{
|
|
69 |
return iCount;
|
|
70 |
}
|
|
71 |
|
|
72 |
#ifndef __ARRAY_MACHINE_CODED__
|
|
73 |
EXPORT_C TAny*& RPointerArrayBase::At(TInt anIndex) const
|
|
74 |
{
|
|
75 |
__ASSERT_ALWAYS((anIndex>=0 && anIndex<iCount),Panic(EBadArrayIndex));
|
|
76 |
return iEntries[anIndex];
|
|
77 |
}
|
|
78 |
#else
|
|
79 |
GLDEF_C void PanicBadArrayIndex()
|
|
80 |
{
|
|
81 |
Panic(EBadArrayIndex);
|
|
82 |
}
|
|
83 |
#endif
|
|
84 |
|
|
85 |
TInt CalculateArraySizeAfterGrow(TInt aOrigSize, TInt aGranularity, TInt aLimit)
|
|
86 |
{
|
|
87 |
if (aGranularity >= 0)
|
|
88 |
{
|
|
89 |
if (aOrigSize > aLimit - aGranularity)
|
|
90 |
return KErrNoMemory; // array can't be >2GB
|
|
91 |
return aOrigSize + aGranularity;
|
|
92 |
}
|
|
93 |
TUint minStep = (TUint)(aGranularity & 65535);
|
|
94 |
TUint factor = TUint(aGranularity & 0x7fff0000) >> 16;
|
|
95 |
Uint64 na64 = aOrigSize;
|
|
96 |
na64 *= (Uint64)factor;
|
|
97 |
na64 += 128;
|
|
98 |
na64 >>= 8;
|
|
99 |
Uint64 min_na64 = (Uint64)aOrigSize + (Uint64)minStep;
|
|
100 |
if (min_na64 > na64)
|
|
101 |
na64 = min_na64;
|
|
102 |
if (na64 > (Uint64)aLimit)
|
|
103 |
return KErrNoMemory; // array can't be >2GB
|
|
104 |
return (TInt)na64;
|
|
105 |
}
|
|
106 |
|
|
107 |
TInt CalculateArraySizeAfterShrink(TInt aOrigSize, TInt aGranularity, TInt aUsed)
|
|
108 |
{
|
|
109 |
TInt step = aGranularity;
|
|
110 |
if (step < 0)
|
|
111 |
step &= 65535;
|
|
112 |
if (aOrigSize - aUsed < step)
|
|
113 |
return aOrigSize;
|
|
114 |
aUsed += step - 1;
|
|
115 |
aUsed /= step;
|
|
116 |
aUsed *= step;
|
|
117 |
return aUsed;
|
|
118 |
}
|
|
119 |
|
|
120 |
TInt RPointerArrayBase::Grow()
|
|
121 |
{
|
|
122 |
TInt newAlloc = CalculateArraySizeAfterGrow(iAllocated, iGranularity, KMaxTInt >> 2);
|
|
123 |
if (newAlloc < 0)
|
|
124 |
return newAlloc;
|
|
125 |
TAny** pA = (TAny**)STD_CLASS::ReAlloc(iEntries, newAlloc*sizeof(TAny*));
|
|
126 |
if (!pA)
|
|
127 |
return KErrNoMemory;
|
|
128 |
iEntries = pA;
|
|
129 |
iAllocated = newAlloc;
|
|
130 |
return KErrNone;
|
|
131 |
}
|
|
132 |
|
|
133 |
#ifndef __ARRAY_MACHINE_CODED__
|
|
134 |
EXPORT_C TInt RPointerArrayBase::Append(const TAny* anEntry)
|
|
135 |
{
|
|
136 |
if (iCount==iAllocated)
|
|
137 |
{
|
|
138 |
TInt r=Grow();
|
|
139 |
if (r!=KErrNone)
|
|
140 |
return r;
|
|
141 |
}
|
|
142 |
iEntries[iCount++]=(TAny*)anEntry;
|
|
143 |
return KErrNone;
|
|
144 |
}
|
|
145 |
#endif
|
|
146 |
|
|
147 |
EXPORT_C TInt RPointerArrayBase::Insert(const TAny* anEntry, TInt aPos)
|
|
148 |
{
|
|
149 |
__ASSERT_ALWAYS((aPos>=0 && aPos<=iCount),Panic(EBadArrayPosition));
|
|
150 |
if (iCount==iAllocated)
|
|
151 |
{
|
|
152 |
TInt r=Grow();
|
|
153 |
if (r!=KErrNone)
|
|
154 |
return r;
|
|
155 |
}
|
|
156 |
TInt entries=iCount-aPos;
|
|
157 |
if (entries!=0)
|
|
158 |
wordmove(iEntries+aPos+1,iEntries+aPos,entries*sizeof(TAny*));
|
|
159 |
iCount++;
|
|
160 |
iEntries[aPos]=(TAny*)anEntry;
|
|
161 |
return KErrNone;
|
|
162 |
}
|
|
163 |
|
|
164 |
EXPORT_C void RPointerArrayBase::Remove(TInt anIndex)
|
|
165 |
{
|
|
166 |
__ASSERT_ALWAYS((anIndex>=0 && anIndex<iCount),Panic(EBadArrayIndex));
|
|
167 |
TInt entries=iCount-anIndex-1;
|
|
168 |
if (entries!=0)
|
|
169 |
wordmove(iEntries+anIndex,iEntries+anIndex+1,entries*sizeof(TAny*));
|
|
170 |
iCount--;
|
|
171 |
}
|
|
172 |
|
|
173 |
EXPORT_C void RPointerArrayBase::Compress()
|
|
174 |
{
|
|
175 |
if (iCount)
|
|
176 |
iEntries=(TAny**)STD_CLASS::ReAlloc(iEntries,iCount*sizeof(TAny*)); // can't fail
|
|
177 |
else
|
|
178 |
{
|
|
179 |
STD_CLASS::Free(iEntries);
|
|
180 |
iEntries=NULL;
|
|
181 |
}
|
|
182 |
iAllocated=iCount;
|
|
183 |
}
|
|
184 |
|
|
185 |
#ifndef __KERNEL_MODE__
|
|
186 |
EXPORT_C void RPointerArrayBase::GranularCompress()
|
|
187 |
{
|
|
188 |
TInt newAlloc = CalculateArraySizeAfterShrink(iAllocated, iGranularity, iCount);
|
|
189 |
if (newAlloc == iAllocated)
|
|
190 |
return;
|
|
191 |
if (newAlloc)
|
|
192 |
iEntries=(TAny**)STD_CLASS::ReAlloc(iEntries,newAlloc*sizeof(TAny*)); // can't fail
|
|
193 |
else
|
|
194 |
{
|
|
195 |
STD_CLASS::Free(iEntries);
|
|
196 |
iEntries=NULL;
|
|
197 |
}
|
|
198 |
iAllocated=newAlloc;
|
|
199 |
}
|
|
200 |
|
|
201 |
EXPORT_C TInt RPointerArrayBase::DoReserve(TInt aCount)
|
|
202 |
{
|
|
203 |
__ASSERT_ALWAYS(aCount>=0, Panic(EArrayBadReserveCount));
|
|
204 |
if (aCount <= iAllocated)
|
|
205 |
return KErrNone; // if allocated space is already sufficient, nothing to do
|
|
206 |
|
|
207 |
const TInt KLimit = TInt(0x80000000u / sizeof(TAny*));
|
|
208 |
if (aCount >= KLimit)
|
|
209 |
return KErrNoMemory;
|
|
210 |
|
|
211 |
TAny** pA = (TAny**)STD_CLASS::ReAlloc(iEntries, aCount*sizeof(TAny*));
|
|
212 |
if (!pA)
|
|
213 |
return KErrNoMemory;
|
|
214 |
iEntries = pA;
|
|
215 |
iAllocated = aCount;
|
|
216 |
return KErrNone;
|
|
217 |
}
|
|
218 |
#endif
|
|
219 |
|
|
220 |
EXPORT_C void RPointerArrayBase::Reset()
|
|
221 |
{
|
|
222 |
iCount=0;
|
|
223 |
STD_CLASS::Free(iEntries);
|
|
224 |
iEntries=NULL;
|
|
225 |
iAllocated=0;
|
|
226 |
}
|
|
227 |
|
|
228 |
EXPORT_C TInt RPointerArrayBase::BinarySearch(const TAny* anEntry, TInt& anIndex, TGeneralLinearOrder anOrder) const
|
|
229 |
{
|
|
230 |
return BinarySearch(anEntry, anIndex, anOrder, EArrayFindMode_Any);
|
|
231 |
}
|
|
232 |
|
|
233 |
#ifndef __ARRAY_MACHINE_CODED__
|
|
234 |
EXPORT_C TInt RPointerArrayBase::Find(const TAny* anEntry) const
|
|
235 |
{
|
|
236 |
TInt i;
|
|
237 |
for (i=0; i<iCount; i++)
|
|
238 |
{
|
|
239 |
if (iEntries[i]==anEntry)
|
|
240 |
return i;
|
|
241 |
}
|
|
242 |
return KErrNotFound;
|
|
243 |
}
|
|
244 |
|
|
245 |
EXPORT_C TInt RPointerArrayBase::Find(const TAny* anEntry, TGeneralIdentityRelation anIdentity) const
|
|
246 |
{
|
|
247 |
TInt i;
|
|
248 |
for (i=0; i<iCount; i++)
|
|
249 |
{
|
|
250 |
if ((*anIdentity)(anEntry,iEntries[i]))
|
|
251 |
return i;
|
|
252 |
}
|
|
253 |
return KErrNotFound;
|
|
254 |
}
|
|
255 |
|
|
256 |
EXPORT_C TInt RPointerArrayBase::BinarySearchSigned(TInt anEntry, TInt& anIndex) const
|
|
257 |
{
|
|
258 |
return BinarySearchSigned(anEntry, anIndex, EArrayFindMode_Any);
|
|
259 |
}
|
|
260 |
|
|
261 |
EXPORT_C TInt RPointerArrayBase::BinarySearchSigned(TInt anEntry, TInt& anIndex, TInt aMode) const
|
|
262 |
{
|
|
263 |
__ASSERT_DEBUG(TUint(aMode)<TUint(EArrayFindMode_Limit), Panic(EBadArrayFindMode));
|
|
264 |
TInt l=0;
|
|
265 |
TInt r=iCount;
|
|
266 |
TInt ret = KErrNotFound;
|
|
267 |
while(r>l)
|
|
268 |
{
|
|
269 |
TInt m=(l+r)>>1;
|
|
270 |
TInt h=(TInt)iEntries[m];
|
|
271 |
if (anEntry==h)
|
|
272 |
{
|
|
273 |
if (aMode == EArrayFindMode_Any)
|
|
274 |
{
|
|
275 |
anIndex=m;
|
|
276 |
return KErrNone;
|
|
277 |
}
|
|
278 |
ret = KErrNone;
|
|
279 |
if (aMode == EArrayFindMode_First)
|
|
280 |
r=m;
|
|
281 |
else
|
|
282 |
l=m+1;
|
|
283 |
}
|
|
284 |
else if (anEntry>h)
|
|
285 |
l=m+1;
|
|
286 |
else
|
|
287 |
r=m;
|
|
288 |
}
|
|
289 |
anIndex=r;
|
|
290 |
return ret;
|
|
291 |
}
|
|
292 |
|
|
293 |
EXPORT_C TInt RPointerArrayBase::BinarySearchUnsigned(TUint anEntry, TInt& anIndex) const
|
|
294 |
{
|
|
295 |
return BinarySearchUnsigned(anEntry, anIndex, EArrayFindMode_Any);
|
|
296 |
}
|
|
297 |
|
|
298 |
EXPORT_C TInt RPointerArrayBase::BinarySearchUnsigned(TUint anEntry, TInt& anIndex, TInt aMode) const
|
|
299 |
{
|
|
300 |
__ASSERT_DEBUG(TUint(aMode)<TUint(EArrayFindMode_Limit), Panic(EBadArrayFindMode));
|
|
301 |
TInt l=0;
|
|
302 |
TInt r=iCount;
|
|
303 |
TInt ret = KErrNotFound;
|
|
304 |
while(r>l)
|
|
305 |
{
|
|
306 |
TUint m=(l+r)>>1;
|
|
307 |
TUint h=(TUint)iEntries[m];
|
|
308 |
if (anEntry==h)
|
|
309 |
{
|
|
310 |
if (aMode == EArrayFindMode_Any)
|
|
311 |
{
|
|
312 |
anIndex=m;
|
|
313 |
return KErrNone;
|
|
314 |
}
|
|
315 |
ret = KErrNone;
|
|
316 |
if (aMode == EArrayFindMode_First)
|
|
317 |
r=m;
|
|
318 |
else
|
|
319 |
l=m+1;
|
|
320 |
}
|
|
321 |
else if (anEntry>h)
|
|
322 |
l=m+1;
|
|
323 |
else
|
|
324 |
r=m;
|
|
325 |
}
|
|
326 |
anIndex=r;
|
|
327 |
return ret;
|
|
328 |
}
|
|
329 |
|
|
330 |
EXPORT_C TInt RPointerArrayBase::BinarySearch(const TAny* anEntry, TInt& anIndex, TGeneralLinearOrder anOrder, TInt aMode) const
|
|
331 |
{
|
|
332 |
__ASSERT_DEBUG(TUint(aMode)<TUint(EArrayFindMode_Limit), Panic(EBadArrayFindMode));
|
|
333 |
TInt l=0;
|
|
334 |
TInt r=iCount;
|
|
335 |
TInt ret = KErrNotFound;
|
|
336 |
while(r>l)
|
|
337 |
{
|
|
338 |
TInt m=(l+r)>>1;
|
|
339 |
TInt k=(*anOrder)(anEntry,iEntries[m]);
|
|
340 |
if (k==0)
|
|
341 |
{
|
|
342 |
if (aMode == EArrayFindMode_Any)
|
|
343 |
{
|
|
344 |
anIndex=m;
|
|
345 |
return KErrNone;
|
|
346 |
}
|
|
347 |
ret = KErrNone;
|
|
348 |
if (aMode == EArrayFindMode_First)
|
|
349 |
r=m;
|
|
350 |
else
|
|
351 |
l=m+1;
|
|
352 |
}
|
|
353 |
else if (k>0)
|
|
354 |
l=m+1;
|
|
355 |
else
|
|
356 |
r=m;
|
|
357 |
}
|
|
358 |
anIndex=r;
|
|
359 |
return ret;
|
|
360 |
}
|
|
361 |
|
|
362 |
EXPORT_C TInt RPointerArrayBase::FindIsqSigned(TInt anEntry) const
|
|
363 |
{
|
|
364 |
return FindIsqSigned(anEntry, EArrayFindMode_Any);
|
|
365 |
}
|
|
366 |
|
|
367 |
EXPORT_C TInt RPointerArrayBase::FindIsqUnsigned(TUint anEntry) const
|
|
368 |
{
|
|
369 |
return FindIsqUnsigned(anEntry, EArrayFindMode_Any);
|
|
370 |
}
|
|
371 |
|
|
372 |
EXPORT_C TInt RPointerArrayBase::FindIsq(const TAny* anEntry, TGeneralLinearOrder anOrder) const
|
|
373 |
{
|
|
374 |
return FindIsq(anEntry, anOrder, EArrayFindMode_Any);
|
|
375 |
}
|
|
376 |
|
|
377 |
EXPORT_C TInt RPointerArrayBase::FindIsqSigned(TInt anEntry, TInt aMode) const
|
|
378 |
{
|
|
379 |
TInt i;
|
|
380 |
TInt r=BinarySearchSigned(anEntry,i,aMode);
|
|
381 |
return (r==KErrNone)?i:r;
|
|
382 |
}
|
|
383 |
|
|
384 |
EXPORT_C TInt RPointerArrayBase::FindIsqUnsigned(TUint anEntry, TInt aMode) const
|
|
385 |
{
|
|
386 |
TInt i;
|
|
387 |
TInt r=BinarySearchUnsigned(anEntry,i,aMode);
|
|
388 |
return (r==KErrNone)?i:r;
|
|
389 |
}
|
|
390 |
|
|
391 |
EXPORT_C TInt RPointerArrayBase::FindIsq(const TAny* anEntry, TGeneralLinearOrder anOrder, TInt aMode) const
|
|
392 |
{
|
|
393 |
TInt i;
|
|
394 |
TInt r=BinarySearch(anEntry,i,anOrder,aMode);
|
|
395 |
return (r==KErrNone)?i:r;
|
|
396 |
}
|
|
397 |
#else
|
|
398 |
extern "C" void PanicBadArrayFindMode()
|
|
399 |
{
|
|
400 |
Panic(EBadArrayFindMode);
|
|
401 |
}
|
|
402 |
#endif
|
|
403 |
|
|
404 |
|
|
405 |
EXPORT_C TInt RPointerArrayBase::FindReverse(const TAny* anEntry) const
|
|
406 |
{
|
|
407 |
TInt i=iCount;
|
|
408 |
while (i--)
|
|
409 |
{
|
|
410 |
if (iEntries[i]==anEntry)
|
|
411 |
return i;
|
|
412 |
}
|
|
413 |
return KErrNotFound;
|
|
414 |
}
|
|
415 |
|
|
416 |
EXPORT_C TInt RPointerArrayBase::FindReverse(const TAny* anEntry, TGeneralIdentityRelation anIdentity) const
|
|
417 |
{
|
|
418 |
TInt i=iCount;
|
|
419 |
while (i--)
|
|
420 |
{
|
|
421 |
if ((*anIdentity)(anEntry,iEntries[i]))
|
|
422 |
return i;
|
|
423 |
}
|
|
424 |
return KErrNotFound;
|
|
425 |
}
|
|
426 |
|
|
427 |
|
|
428 |
EXPORT_C TInt RPointerArrayBase::InsertIsqSigned(TInt anEntry, TBool aAllowRepeats)
|
|
429 |
{
|
|
430 |
TInt i;
|
|
431 |
TInt mode = aAllowRepeats ? EArrayFindMode_Last : EArrayFindMode_Any;
|
|
432 |
TInt r=BinarySearchSigned(anEntry,i,mode);
|
|
433 |
if (r==KErrNotFound || aAllowRepeats)
|
|
434 |
return Insert((const TAny*)anEntry,i);
|
|
435 |
return KErrAlreadyExists;
|
|
436 |
}
|
|
437 |
|
|
438 |
EXPORT_C TInt RPointerArrayBase::InsertIsqUnsigned(TUint anEntry, TBool aAllowRepeats)
|
|
439 |
{
|
|
440 |
TInt i;
|
|
441 |
TInt mode = aAllowRepeats ? EArrayFindMode_Last : EArrayFindMode_Any;
|
|
442 |
TInt r=BinarySearchUnsigned(anEntry,i,mode);
|
|
443 |
if (r==KErrNotFound || aAllowRepeats)
|
|
444 |
return Insert((const TAny*)anEntry,i);
|
|
445 |
return KErrAlreadyExists;
|
|
446 |
}
|
|
447 |
|
|
448 |
EXPORT_C TInt RPointerArrayBase::InsertIsq(const TAny* anEntry, TGeneralLinearOrder anOrder, TBool aAllowRepeats)
|
|
449 |
{
|
|
450 |
TInt i;
|
|
451 |
TInt mode = aAllowRepeats ? EArrayFindMode_Last : EArrayFindMode_Any;
|
|
452 |
TInt r=BinarySearch(anEntry,i,anOrder,mode);
|
|
453 |
if (r==KErrNotFound || aAllowRepeats)
|
|
454 |
return Insert((const TAny*)anEntry,i);
|
|
455 |
return KErrAlreadyExists;
|
|
456 |
}
|
|
457 |
|
|
458 |
#ifndef __ARRAY_MACHINE_CODED__
|
|
459 |
void HeapSortUnsigned(TUint* aEntries,TInt aCount)
|
|
460 |
{
|
|
461 |
TInt ss = aCount;
|
|
462 |
if (ss>1)
|
|
463 |
{
|
|
464 |
TInt sh = ss>>1;
|
|
465 |
FOREVER
|
|
466 |
{
|
|
467 |
TUint si;
|
|
468 |
if (sh!=0)
|
|
469 |
{
|
|
470 |
--sh;
|
|
471 |
si = aEntries[sh];
|
|
472 |
}
|
|
473 |
else
|
|
474 |
{
|
|
475 |
--ss;
|
|
476 |
si = aEntries[ss];
|
|
477 |
aEntries[ss]=aEntries[0];
|
|
478 |
if (ss==1)
|
|
479 |
{
|
|
480 |
aEntries[0]=si;
|
|
481 |
break;
|
|
482 |
}
|
|
483 |
}
|
|
484 |
TInt ii = sh;
|
|
485 |
TInt jj = sh;
|
|
486 |
FOREVER
|
|
487 |
{
|
|
488 |
jj = (jj+1)<<1;
|
|
489 |
if (jj>=ss || TUint(aEntries[jj-1])>TUint(aEntries[jj]) )
|
|
490 |
--jj;
|
|
491 |
if (jj>=ss || TUint(aEntries[jj])<=si)
|
|
492 |
break;
|
|
493 |
aEntries[ii]=aEntries[jj];
|
|
494 |
ii = jj;
|
|
495 |
}
|
|
496 |
aEntries[ii]=si;
|
|
497 |
}
|
|
498 |
}
|
|
499 |
}
|
|
500 |
#endif // !__ARRAY_MACHINE_CODED__
|
|
501 |
|
|
502 |
|
|
503 |
#ifndef __KERNEL_MODE__
|
|
504 |
#ifndef __ARRAY_MACHINE_CODED__
|
|
505 |
EXPORT_C void RPointerArrayBase::HeapSortSigned()
|
|
506 |
{
|
|
507 |
TInt ss = iCount;
|
|
508 |
if (ss>1)
|
|
509 |
{
|
|
510 |
TInt sh = ss>>1;
|
|
511 |
FOREVER
|
|
512 |
{
|
|
513 |
TInt si;
|
|
514 |
if (sh!=0)
|
|
515 |
{
|
|
516 |
--sh;
|
|
517 |
si = (TInt)iEntries[sh];
|
|
518 |
}
|
|
519 |
else
|
|
520 |
{
|
|
521 |
--ss;
|
|
522 |
si = (TInt)iEntries[ss];
|
|
523 |
iEntries[ss]=iEntries[0];
|
|
524 |
if (ss==1)
|
|
525 |
{
|
|
526 |
iEntries[0]=(TAny*)si;
|
|
527 |
break;
|
|
528 |
}
|
|
529 |
}
|
|
530 |
TInt ii = sh;
|
|
531 |
TInt jj = sh;
|
|
532 |
FOREVER
|
|
533 |
{
|
|
534 |
jj = (jj+1)<<1;
|
|
535 |
if (jj>=ss || TInt(iEntries[jj-1])>TInt(iEntries[jj]) )
|
|
536 |
--jj;
|
|
537 |
if (jj>=ss || TInt(iEntries[jj])<=si)
|
|
538 |
break;
|
|
539 |
iEntries[ii]=iEntries[jj];
|
|
540 |
ii = jj;
|
|
541 |
}
|
|
542 |
iEntries[ii]=(TAny*)si;
|
|
543 |
}
|
|
544 |
}
|
|
545 |
}
|
|
546 |
|
|
547 |
EXPORT_C void RPointerArrayBase::HeapSortUnsigned()
|
|
548 |
{
|
|
549 |
::HeapSortUnsigned((TUint*)iEntries,iCount);
|
|
550 |
}
|
|
551 |
|
|
552 |
EXPORT_C void RPointerArrayBase::HeapSort(TGeneralLinearOrder anOrder)
|
|
553 |
{
|
|
554 |
TInt ss = iCount;
|
|
555 |
if (ss>1)
|
|
556 |
{
|
|
557 |
TInt sh = ss>>1;
|
|
558 |
FOREVER
|
|
559 |
{
|
|
560 |
TAny* si;
|
|
561 |
if (sh!=0)
|
|
562 |
{
|
|
563 |
--sh;
|
|
564 |
si = iEntries[sh];
|
|
565 |
}
|
|
566 |
else
|
|
567 |
{
|
|
568 |
--ss;
|
|
569 |
si = iEntries[ss];
|
|
570 |
iEntries[ss]=iEntries[0];
|
|
571 |
if (ss==1)
|
|
572 |
{
|
|
573 |
iEntries[0]=si;
|
|
574 |
break;
|
|
575 |
}
|
|
576 |
}
|
|
577 |
TInt ii = sh;
|
|
578 |
TInt jj = sh;
|
|
579 |
FOREVER
|
|
580 |
{
|
|
581 |
jj = (jj+1)<<1;
|
|
582 |
if (jj>=ss || (*anOrder)(iEntries[jj-1],iEntries[jj])>0 )
|
|
583 |
--jj;
|
|
584 |
if (jj>=ss || (*anOrder)(iEntries[jj],si)<=0 )
|
|
585 |
break;
|
|
586 |
iEntries[ii]=iEntries[jj];
|
|
587 |
ii = jj;
|
|
588 |
}
|
|
589 |
iEntries[ii]=si;
|
|
590 |
}
|
|
591 |
}
|
|
592 |
}
|
|
593 |
#endif
|
|
594 |
|
|
595 |
EXPORT_C TInt RPointerArrayBase::GetCount(const CBase* aPtr)
|
|
596 |
{
|
|
597 |
return ((RPointerArrayBase*)aPtr)->Count();
|
|
598 |
}
|
|
599 |
|
|
600 |
EXPORT_C const TAny* RPointerArrayBase::GetElementPtr(const CBase* aPtr, TInt aIndex)
|
|
601 |
{
|
|
602 |
return &(((RPointerArrayBase*)aPtr)->At(aIndex));
|
|
603 |
}
|
|
604 |
#endif // __KERNEL_MODE__
|
|
605 |
|
|
606 |
EXPORT_C RArrayBase::RArrayBase(TInt anEntrySize)
|
|
607 |
: iCount(0), iEntries(NULL), iKeyOffset(0), iAllocated(0),
|
|
608 |
iGranularity(KDefaultSimpleArrayGranularity), iSpare1(0), iSpare2(0)
|
|
609 |
{
|
|
610 |
__ASSERT_ALWAYS(anEntrySize>0 && anEntrySize<=KSimpleArrayMaxEntrySize,Panic(EBadArrayEntrySize));
|
|
611 |
iEntrySize=(anEntrySize+(TInt)sizeof(TInt)-1)&~((TInt)sizeof(TInt)-1);
|
|
612 |
}
|
|
613 |
|
|
614 |
EXPORT_C RArrayBase::RArrayBase(TInt anEntrySize, TInt aGranularity)
|
|
615 |
: iCount(0), iEntries(NULL), iKeyOffset(0), iAllocated(0),
|
|
616 |
iGranularity(aGranularity), iSpare1(0), iSpare2(0)
|
|
617 |
{
|
|
618 |
__ASSERT_ALWAYS(anEntrySize>0 && anEntrySize<=KSimpleArrayMaxEntrySize,Panic(EBadArrayEntrySize));
|
|
619 |
__ASSERT_ALWAYS(aGranularity>0 && (aGranularity*anEntrySize)<=KSimpleArrayMaxGranularity, Panic(EBadArrayGranularity));
|
|
620 |
iEntrySize=(anEntrySize+(TInt)sizeof(TInt)-1)&~((TInt)sizeof(TInt)-1);
|
|
621 |
}
|
|
622 |
|
|
623 |
EXPORT_C RArrayBase::RArrayBase(TInt aEntrySize,TAny* aEntries, TInt aCount)
|
|
624 |
: iCount(aCount), iEntries(aEntries), iKeyOffset(0), iAllocated(aCount),
|
|
625 |
iGranularity(KDefaultSimpleArrayGranularity), iSpare1(0), iSpare2(0)
|
|
626 |
{
|
|
627 |
__ASSERT_ALWAYS(aEntrySize>0 && aEntrySize<=KSimpleArrayMaxEntrySize,Panic(EBadArrayEntrySize));
|
|
628 |
__ASSERT_ALWAYS(aCount>0,Panic(EBadArrayCount));
|
|
629 |
iEntrySize=(aEntrySize+(TInt)sizeof(TInt)-1)&~((TInt)sizeof(TInt)-1);
|
|
630 |
}
|
|
631 |
|
|
632 |
EXPORT_C RArrayBase::RArrayBase(TInt anEntrySize, TInt aGranularity, TInt aKeyOffset)
|
|
633 |
: iCount(0), iEntries(NULL), iKeyOffset(aKeyOffset), iAllocated(0),
|
|
634 |
iGranularity(aGranularity), iSpare1(0), iSpare2(0)
|
|
635 |
{
|
|
636 |
__ASSERT_ALWAYS(anEntrySize>0 && anEntrySize<=KSimpleArrayMaxEntrySize,Panic(EBadArrayEntrySize));
|
|
637 |
__ASSERT_ALWAYS(aGranularity>0 && (aGranularity*anEntrySize)<=KSimpleArrayMaxGranularity, Panic(EBadArrayGranularity));
|
|
638 |
__ASSERT_ALWAYS(aKeyOffset>=0 && (aKeyOffset&3)==0 && aKeyOffset<anEntrySize, Panic(EBadArrayKeyOffset));
|
|
639 |
iEntrySize=(anEntrySize+(TInt)sizeof(TInt)-1)&~((TInt)sizeof(TInt)-1);
|
|
640 |
}
|
|
641 |
|
|
642 |
EXPORT_C RArrayBase::RArrayBase(TInt aEntrySize, TInt aMinGrowBy, TInt aKeyOffset, TInt aFactor)
|
|
643 |
: iCount(0), iEntries(NULL), iKeyOffset(aKeyOffset), iAllocated(0), iSpare1(0), iSpare2(0)
|
|
644 |
{
|
|
645 |
__ASSERT_ALWAYS(aEntrySize>0 && aEntrySize<=KSimpleArrayMaxEntrySize, Panic(EBadArrayEntrySize));
|
|
646 |
__ASSERT_ALWAYS(aKeyOffset>=0 && (aKeyOffset&3)==0 && aKeyOffset<aEntrySize, Panic(EBadArrayKeyOffset));
|
|
647 |
__ASSERT_ALWAYS(aMinGrowBy>0 && aMinGrowBy<=KMaxArrayGrowBy, Panic(EBadArrayMinGrowBy));
|
|
648 |
__ASSERT_ALWAYS(aFactor>=KMinArrayFactor && aFactor<=KMaxArrayFactor, Panic(EBadArrayFactor));
|
|
649 |
iEntrySize=(aEntrySize+(TInt)sizeof(TInt)-1)&~((TInt)sizeof(TInt)-1);
|
|
650 |
iGranularity = aMinGrowBy | (aFactor << 16) | 0x80000000;
|
|
651 |
}
|
|
652 |
|
|
653 |
EXPORT_C void RArrayBase::Close()
|
|
654 |
{
|
|
655 |
iCount=0;
|
|
656 |
STD_CLASS::Free(iEntries);
|
|
657 |
iEntries=NULL;
|
|
658 |
iAllocated=0;
|
|
659 |
}
|
|
660 |
|
|
661 |
EXPORT_C TInt RArrayBase::Count() const
|
|
662 |
{
|
|
663 |
return iCount;
|
|
664 |
}
|
|
665 |
|
148
|
666 |
EXPORT_C void RArrayBase::SetKeyOffset(TInt aKeyOffset)
|
|
667 |
{
|
|
668 |
__ASSERT_ALWAYS(TUint(aKeyOffset)<TUint(iEntrySize) && (aKeyOffset&3)==0, Panic(EBadArrayKeyOffset));
|
|
669 |
iKeyOffset = aKeyOffset;
|
|
670 |
}
|
|
671 |
|
0
|
672 |
#ifndef __ARRAY_MACHINE_CODED__
|
|
673 |
EXPORT_C TAny* RArrayBase::At(TInt anIndex) const
|
|
674 |
{
|
|
675 |
__ASSERT_ALWAYS((anIndex>=0 && anIndex<iCount),Panic(EBadArrayIndex));
|
|
676 |
return (((TUint8*)iEntries)+anIndex*iEntrySize);
|
|
677 |
}
|
|
678 |
#endif
|
|
679 |
|
|
680 |
TInt RArrayBase::Grow()
|
|
681 |
{
|
|
682 |
TInt newAlloc = CalculateArraySizeAfterGrow(iAllocated, iGranularity, KMaxTInt/iEntrySize);
|
|
683 |
if (newAlloc < 0)
|
|
684 |
return newAlloc;
|
|
685 |
TAny** pA = (TAny**)STD_CLASS::ReAlloc(iEntries, newAlloc*iEntrySize);
|
|
686 |
if (!pA)
|
|
687 |
return KErrNoMemory;
|
|
688 |
iEntries = pA;
|
|
689 |
iAllocated = newAlloc;
|
|
690 |
return KErrNone;
|
|
691 |
}
|
|
692 |
|
|
693 |
#ifndef __ARRAY_MACHINE_CODED__
|
|
694 |
EXPORT_C TInt RArrayBase::Append(const TAny* anEntry)
|
|
695 |
{
|
|
696 |
if (iCount==iAllocated)
|
|
697 |
{
|
|
698 |
TInt r=Grow();
|
|
699 |
if (r!=KErrNone)
|
|
700 |
return r;
|
|
701 |
}
|
|
702 |
wordmove((TUint8*)iEntries+iCount*iEntrySize, anEntry, iEntrySize);
|
|
703 |
iCount++;
|
|
704 |
return KErrNone;
|
|
705 |
}
|
|
706 |
#endif
|
|
707 |
|
|
708 |
EXPORT_C TInt RArrayBase::Insert(const TAny* anEntry, TInt aPos)
|
|
709 |
{
|
|
710 |
__ASSERT_ALWAYS((aPos>=0 && aPos<=iCount),Panic(EBadArrayPosition));
|
|
711 |
if (iCount==iAllocated)
|
|
712 |
{
|
|
713 |
TInt r=Grow();
|
|
714 |
if (r!=KErrNone)
|
|
715 |
return r;
|
|
716 |
}
|
|
717 |
TUint8 *pS=(TUint8*)iEntries+aPos*iEntrySize;
|
|
718 |
TUint8 *pD=pS+iEntrySize;
|
|
719 |
TInt entries=iCount-aPos;
|
|
720 |
if (entries!=0)
|
|
721 |
wordmove(pD,pS,entries*iEntrySize);
|
|
722 |
wordmove(pS,anEntry,iEntrySize);
|
|
723 |
iCount++;
|
|
724 |
return KErrNone;
|
|
725 |
}
|
|
726 |
|
|
727 |
EXPORT_C void RArrayBase::Remove(TInt anIndex)
|
|
728 |
{
|
|
729 |
__ASSERT_ALWAYS((anIndex>=0 && anIndex<iCount),Panic(EBadArrayIndex));
|
|
730 |
TUint8 *pD=(TUint8*)iEntries+anIndex*iEntrySize;
|
|
731 |
TUint8 *pS=pD+iEntrySize;
|
|
732 |
TInt entries=iCount-anIndex-1;
|
|
733 |
if (entries!=0)
|
|
734 |
wordmove(pD,pS,entries*iEntrySize);
|
|
735 |
iCount--;
|
|
736 |
}
|
|
737 |
|
|
738 |
EXPORT_C void RArrayBase::Compress()
|
|
739 |
{
|
|
740 |
if (iCount)
|
|
741 |
iEntries=STD_CLASS::ReAlloc(iEntries,iCount*iEntrySize); // can't fail
|
|
742 |
else
|
|
743 |
{
|
|
744 |
STD_CLASS::Free(iEntries);
|
|
745 |
iEntries=NULL;
|
|
746 |
}
|
|
747 |
iAllocated=iCount;
|
|
748 |
}
|
|
749 |
|
|
750 |
#ifndef __KERNEL_MODE__
|
|
751 |
EXPORT_C void RArrayBase::GranularCompress()
|
|
752 |
{
|
|
753 |
TInt newAlloc = CalculateArraySizeAfterShrink(iAllocated, iGranularity, iCount);
|
|
754 |
if (newAlloc == iAllocated)
|
|
755 |
return;
|
|
756 |
if (newAlloc)
|
|
757 |
iEntries=STD_CLASS::ReAlloc(iEntries,newAlloc*iEntrySize); // can't fail
|
|
758 |
else
|
|
759 |
{
|
|
760 |
STD_CLASS::Free(iEntries);
|
|
761 |
iEntries=NULL;
|
|
762 |
}
|
|
763 |
iAllocated=newAlloc;
|
|
764 |
}
|
|
765 |
|
|
766 |
EXPORT_C TInt RArrayBase::DoReserve(TInt aCount)
|
|
767 |
{
|
|
768 |
__ASSERT_ALWAYS(aCount>=0, Panic(EArrayBadReserveCount));
|
|
769 |
if (aCount <= iAllocated)
|
|
770 |
return KErrNone; // if allocated space is already sufficient, nothing to do
|
|
771 |
|
|
772 |
Int64 size = Int64(aCount) * Int64(iEntrySize);
|
|
773 |
if (size > Int64(KMaxTInt))
|
|
774 |
return KErrNoMemory;
|
|
775 |
|
|
776 |
TAny** pA = (TAny**)STD_CLASS::ReAlloc(iEntries, aCount*iEntrySize);
|
|
777 |
if (!pA)
|
|
778 |
return KErrNoMemory;
|
|
779 |
iEntries = pA;
|
|
780 |
iAllocated = aCount;
|
|
781 |
return KErrNone;
|
|
782 |
}
|
|
783 |
#endif
|
|
784 |
|
|
785 |
EXPORT_C void RArrayBase::Reset()
|
|
786 |
{
|
|
787 |
iCount=0;
|
|
788 |
STD_CLASS::Free(iEntries);
|
|
789 |
iEntries=NULL;
|
|
790 |
iAllocated=0;
|
|
791 |
}
|
|
792 |
|
|
793 |
EXPORT_C TInt RArrayBase::BinarySearch(const TAny* anEntry, TInt& anIndex, TGeneralLinearOrder anOrder) const
|
|
794 |
{
|
|
795 |
return BinarySearch(anEntry, anIndex, anOrder, EArrayFindMode_Any);
|
|
796 |
}
|
|
797 |
|
|
798 |
#ifndef __ARRAY_MACHINE_CODED__
|
|
799 |
EXPORT_C TInt RArrayBase::Find(const TAny* anEntry) const
|
|
800 |
{
|
|
801 |
TUint8 *pS=(TUint8*)iEntries+iKeyOffset;
|
|
802 |
TInt match=*(TInt*)((TUint8*)anEntry+iKeyOffset);
|
|
803 |
TInt i;
|
|
804 |
for (i=0; i<iCount; i++)
|
|
805 |
{
|
|
806 |
TInt key=*(TInt*)pS;
|
|
807 |
if (key==match)
|
|
808 |
return i;
|
|
809 |
pS+=iEntrySize;
|
|
810 |
}
|
|
811 |
return KErrNotFound;
|
|
812 |
}
|
|
813 |
|
|
814 |
EXPORT_C TInt RArrayBase::Find(const TAny* anEntry, TGeneralIdentityRelation anIdentity) const
|
|
815 |
{
|
|
816 |
TUint8 *pS=(TUint8*)iEntries;
|
|
817 |
TInt i;
|
|
818 |
for (i=0; i<iCount; i++)
|
|
819 |
{
|
|
820 |
if ((*anIdentity)(anEntry,pS))
|
|
821 |
return i;
|
|
822 |
pS+=iEntrySize;
|
|
823 |
}
|
|
824 |
return KErrNotFound;
|
|
825 |
}
|
|
826 |
|
|
827 |
EXPORT_C TInt RArrayBase::BinarySearchSigned(const TAny* anEntry, TInt& anIndex) const
|
|
828 |
{
|
|
829 |
return BinarySearchSigned(anEntry, anIndex, EArrayFindMode_Any);
|
|
830 |
}
|
|
831 |
|
|
832 |
EXPORT_C TInt RArrayBase::BinarySearchSigned(const TAny* anEntry, TInt& anIndex, TInt aMode) const
|
|
833 |
{
|
|
834 |
__ASSERT_DEBUG(TUint(aMode)<TUint(EArrayFindMode_Limit), Panic(EBadArrayFindMode));
|
|
835 |
TInt match=*(TInt*)((TUint8*)anEntry+iKeyOffset);
|
|
836 |
TUint8* pK=(TUint8*)iEntries+iKeyOffset;
|
|
837 |
TInt l=0;
|
|
838 |
TInt r=iCount;
|
|
839 |
TInt ret = KErrNotFound;
|
|
840 |
while(r>l)
|
|
841 |
{
|
|
842 |
TInt m=(l+r)>>1;
|
|
843 |
TInt h=*(TInt*)(pK+m*iEntrySize);
|
|
844 |
if (match==h)
|
|
845 |
{
|
|
846 |
if (aMode == EArrayFindMode_Any)
|
|
847 |
{
|
|
848 |
anIndex=m;
|
|
849 |
return KErrNone;
|
|
850 |
}
|
|
851 |
ret = KErrNone;
|
|
852 |
if (aMode == EArrayFindMode_First)
|
|
853 |
r=m;
|
|
854 |
else
|
|
855 |
l=m+1;
|
|
856 |
}
|
|
857 |
else if (match>h)
|
|
858 |
l=m+1;
|
|
859 |
else
|
|
860 |
r=m;
|
|
861 |
}
|
|
862 |
anIndex=r;
|
|
863 |
return ret;
|
|
864 |
}
|
|
865 |
|
|
866 |
EXPORT_C TInt RArrayBase::BinarySearchUnsigned(const TAny* anEntry, TInt& anIndex) const
|
|
867 |
{
|
|
868 |
return BinarySearchUnsigned(anEntry, anIndex, EArrayFindMode_Any);
|
|
869 |
}
|
|
870 |
|
|
871 |
EXPORT_C TInt RArrayBase::BinarySearchUnsigned(const TAny* anEntry, TInt& anIndex, TInt aMode) const
|
|
872 |
{
|
|
873 |
__ASSERT_DEBUG(TUint(aMode)<TUint(EArrayFindMode_Limit), Panic(EBadArrayFindMode));
|
|
874 |
TUint match=*(TUint*)((TUint8*)anEntry+iKeyOffset);
|
|
875 |
TUint8* pK=(TUint8*)iEntries+iKeyOffset;
|
|
876 |
TInt l=0;
|
|
877 |
TInt r=iCount;
|
|
878 |
TInt ret = KErrNotFound;
|
|
879 |
while(r>l)
|
|
880 |
{
|
|
881 |
TInt m=(l+r)>>1;
|
|
882 |
TUint h=*(TUint*)(pK+m*iEntrySize);
|
|
883 |
if (match==h)
|
|
884 |
{
|
|
885 |
if (aMode == EArrayFindMode_Any)
|
|
886 |
{
|
|
887 |
anIndex=m;
|
|
888 |
return KErrNone;
|
|
889 |
}
|
|
890 |
ret = KErrNone;
|
|
891 |
if (aMode == EArrayFindMode_First)
|
|
892 |
r=m;
|
|
893 |
else
|
|
894 |
l=m+1;
|
|
895 |
}
|
|
896 |
else if (match>h)
|
|
897 |
l=m+1;
|
|
898 |
else
|
|
899 |
r=m;
|
|
900 |
}
|
|
901 |
anIndex=r;
|
|
902 |
return ret;
|
|
903 |
}
|
|
904 |
|
|
905 |
EXPORT_C TInt RArrayBase::BinarySearch(const TAny* anEntry, TInt& anIndex, TGeneralLinearOrder anOrder, TInt aMode) const
|
|
906 |
{
|
|
907 |
__ASSERT_DEBUG(TUint(aMode)<TUint(EArrayFindMode_Limit), Panic(EBadArrayFindMode));
|
|
908 |
TInt l=0;
|
|
909 |
TInt r=iCount;
|
|
910 |
TInt ret = KErrNotFound;
|
|
911 |
while(r>l)
|
|
912 |
{
|
|
913 |
TInt m=(l+r)>>1;
|
|
914 |
TInt k=(*anOrder)(anEntry,(TUint8*)iEntries+m*iEntrySize);
|
|
915 |
if (k==0)
|
|
916 |
{
|
|
917 |
if (aMode == EArrayFindMode_Any)
|
|
918 |
{
|
|
919 |
anIndex=m;
|
|
920 |
return KErrNone;
|
|
921 |
}
|
|
922 |
ret = KErrNone;
|
|
923 |
if (aMode == EArrayFindMode_First)
|
|
924 |
r=m;
|
|
925 |
else
|
|
926 |
l=m+1;
|
|
927 |
}
|
|
928 |
else if (k>0)
|
|
929 |
l=m+1;
|
|
930 |
else
|
|
931 |
r=m;
|
|
932 |
}
|
|
933 |
anIndex=r;
|
|
934 |
return ret;
|
|
935 |
}
|
|
936 |
|
|
937 |
EXPORT_C TInt RArrayBase::FindIsqSigned(const TAny* anEntry) const
|
|
938 |
{
|
|
939 |
return FindIsqSigned(anEntry, EArrayFindMode_Any);
|
|
940 |
}
|
|
941 |
|
|
942 |
EXPORT_C TInt RArrayBase::FindIsqUnsigned(const TAny* anEntry) const
|
|
943 |
{
|
|
944 |
return FindIsqUnsigned(anEntry, EArrayFindMode_Any);
|
|
945 |
}
|
|
946 |
|
|
947 |
EXPORT_C TInt RArrayBase::FindIsq(const TAny* anEntry, TGeneralLinearOrder anOrder) const
|
|
948 |
{
|
|
949 |
return FindIsq(anEntry, anOrder, EArrayFindMode_Any);
|
|
950 |
}
|
|
951 |
|
|
952 |
EXPORT_C TInt RArrayBase::FindIsqSigned(const TAny* anEntry, TInt aMode) const
|
|
953 |
{
|
|
954 |
TInt i;
|
|
955 |
TInt r=BinarySearchSigned(anEntry,i,aMode);
|
|
956 |
return (r==KErrNone)?i:r;
|
|
957 |
}
|
|
958 |
|
|
959 |
EXPORT_C TInt RArrayBase::FindIsqUnsigned(const TAny* anEntry, TInt aMode) const
|
|
960 |
{
|
|
961 |
TInt i;
|
|
962 |
TInt r=BinarySearchUnsigned(anEntry,i,aMode);
|
|
963 |
return (r==KErrNone)?i:r;
|
|
964 |
}
|
|
965 |
|
|
966 |
EXPORT_C TInt RArrayBase::FindIsq(const TAny* anEntry, TGeneralLinearOrder anOrder, TInt aMode) const
|
|
967 |
{
|
|
968 |
TInt i;
|
|
969 |
TInt r=BinarySearch(anEntry,i,anOrder,aMode);
|
|
970 |
return (r==KErrNone)?i:r;
|
|
971 |
}
|
|
972 |
#endif
|
|
973 |
|
|
974 |
EXPORT_C TInt RArrayBase::FindReverse(const TAny* anEntry) const
|
|
975 |
{
|
|
976 |
TUint8 *pS=(TUint8*)iEntries+(iCount-1)*iEntrySize+iKeyOffset;
|
|
977 |
TInt match=*(TInt*)((TUint8*)anEntry+iKeyOffset);
|
|
978 |
TInt i=iCount;
|
|
979 |
while(i--)
|
|
980 |
{
|
|
981 |
TInt key=*(TInt*)pS;
|
|
982 |
if (key==match)
|
|
983 |
return i;
|
|
984 |
pS-=iEntrySize;
|
|
985 |
}
|
|
986 |
return KErrNotFound;
|
|
987 |
}
|
|
988 |
|
|
989 |
EXPORT_C TInt RArrayBase::FindReverse(const TAny* anEntry, TGeneralIdentityRelation anIdentity) const
|
|
990 |
{
|
|
991 |
TUint8 *pS=(TUint8*)iEntries+(iCount-1)*iEntrySize;
|
|
992 |
TInt i=iCount;
|
|
993 |
while (i--)
|
|
994 |
{
|
|
995 |
if ((*anIdentity)(anEntry,pS))
|
|
996 |
return i;
|
|
997 |
pS-=iEntrySize;
|
|
998 |
}
|
|
999 |
return KErrNotFound;
|
|
1000 |
}
|
|
1001 |
|
|
1002 |
EXPORT_C TInt RArrayBase::InsertIsqSigned(const TAny* anEntry, TBool aAllowRepeats)
|
|
1003 |
{
|
|
1004 |
TInt i;
|
|
1005 |
TInt mode = aAllowRepeats ? EArrayFindMode_Last : EArrayFindMode_Any;
|
|
1006 |
TInt r=BinarySearchSigned(anEntry,i,mode);
|
|
1007 |
if (r==KErrNotFound || aAllowRepeats)
|
|
1008 |
return Insert((const TAny*)anEntry,i);
|
|
1009 |
return KErrAlreadyExists;
|
|
1010 |
}
|
|
1011 |
|
|
1012 |
EXPORT_C TInt RArrayBase::InsertIsqUnsigned(const TAny* anEntry, TBool aAllowRepeats)
|
|
1013 |
{
|
|
1014 |
TInt i;
|
|
1015 |
TInt mode = aAllowRepeats ? EArrayFindMode_Last : EArrayFindMode_Any;
|
|
1016 |
TInt r=BinarySearchUnsigned(anEntry,i,mode);
|
|
1017 |
if (r==KErrNotFound || aAllowRepeats)
|
|
1018 |
return Insert((const TAny*)anEntry,i);
|
|
1019 |
return KErrAlreadyExists;
|
|
1020 |
}
|
|
1021 |
|
|
1022 |
EXPORT_C TInt RArrayBase::InsertIsq(const TAny* anEntry, TGeneralLinearOrder anOrder, TBool aAllowRepeats)
|
|
1023 |
{
|
|
1024 |
TInt i;
|
|
1025 |
TInt mode = aAllowRepeats ? EArrayFindMode_Last : EArrayFindMode_Any;
|
|
1026 |
TInt r=BinarySearch(anEntry,i,anOrder,mode);
|
|
1027 |
if (r==KErrNotFound || aAllowRepeats)
|
|
1028 |
return Insert((const TAny*)anEntry,i);
|
|
1029 |
return KErrAlreadyExists;
|
|
1030 |
}
|
|
1031 |
|
|
1032 |
#ifndef __KERNEL_MODE__
|
|
1033 |
#ifndef __ARRAY_MACHINE_CODED__
|
|
1034 |
EXPORT_C void RArrayBase::HeapSortSigned()
|
|
1035 |
{
|
|
1036 |
TUint32 si[KSimpleArrayMaxEntrySize/4];
|
|
1037 |
TInt ss = iCount;
|
|
1038 |
if (ss>1)
|
|
1039 |
{
|
|
1040 |
TInt sh = ss>>1;
|
|
1041 |
FOREVER
|
|
1042 |
{
|
|
1043 |
if (sh!=0)
|
|
1044 |
{
|
|
1045 |
--sh;
|
|
1046 |
wordmove(si,(TUint8*)iEntries+sh*iEntrySize,iEntrySize);
|
|
1047 |
}
|
|
1048 |
else
|
|
1049 |
{
|
|
1050 |
--ss;
|
|
1051 |
wordmove(si,(TUint8*)iEntries+ss*iEntrySize,iEntrySize);
|
|
1052 |
wordmove((TUint8*)iEntries+ss*iEntrySize,iEntries,iEntrySize);
|
|
1053 |
if (ss==1)
|
|
1054 |
{
|
|
1055 |
wordmove(iEntries,si,iEntrySize);
|
|
1056 |
break;
|
|
1057 |
}
|
|
1058 |
}
|
|
1059 |
TInt ii = sh;
|
|
1060 |
TInt jj = sh;
|
|
1061 |
TInt sikey=*(TInt*)((TUint8*)si+iKeyOffset);
|
|
1062 |
FOREVER
|
|
1063 |
{
|
|
1064 |
jj = (jj+1)<<1;
|
|
1065 |
TUint8* pKey=((TUint8*)iEntries+jj*iEntrySize+iKeyOffset);
|
|
1066 |
if (jj>=ss || (*(TInt*)(pKey-iEntrySize))>*(TInt*)pKey )
|
|
1067 |
{
|
|
1068 |
--jj;
|
|
1069 |
pKey-=iEntrySize;
|
|
1070 |
}
|
|
1071 |
if (jj>=ss || *(TInt*)pKey<=sikey)
|
|
1072 |
break;
|
|
1073 |
wordmove((TUint8*)iEntries+ii*iEntrySize,(TUint8*)iEntries+jj*iEntrySize,iEntrySize);
|
|
1074 |
ii = jj;
|
|
1075 |
}
|
|
1076 |
wordmove((TUint8*)iEntries+ii*iEntrySize,si,iEntrySize);
|
|
1077 |
}
|
|
1078 |
}
|
|
1079 |
}
|
|
1080 |
|
|
1081 |
EXPORT_C void RArrayBase::HeapSortUnsigned()
|
|
1082 |
{
|
|
1083 |
TUint32 si[KSimpleArrayMaxEntrySize/4];
|
|
1084 |
TInt ss = iCount;
|
|
1085 |
if (ss>1)
|
|
1086 |
{
|
|
1087 |
TInt sh = ss>>1;
|
|
1088 |
FOREVER
|
|
1089 |
{
|
|
1090 |
if (sh!=0)
|
|
1091 |
{
|
|
1092 |
--sh;
|
|
1093 |
wordmove(si,(TUint8*)iEntries+sh*iEntrySize,iEntrySize);
|
|
1094 |
}
|
|
1095 |
else
|
|
1096 |
{
|
|
1097 |
--ss;
|
|
1098 |
wordmove(si,(TUint8*)iEntries+ss*iEntrySize,iEntrySize);
|
|
1099 |
wordmove((TUint8*)iEntries+ss*iEntrySize,iEntries,iEntrySize);
|
|
1100 |
if (ss==1)
|
|
1101 |
{
|
|
1102 |
wordmove(iEntries,si,iEntrySize);
|
|
1103 |
break;
|
|
1104 |
}
|
|
1105 |
}
|
|
1106 |
TInt ii = sh;
|
|
1107 |
TInt jj = sh;
|
|
1108 |
TUint sikey=*(TUint*)((TUint8*)si+iKeyOffset);
|
|
1109 |
FOREVER
|
|
1110 |
{
|
|
1111 |
jj = (jj+1)<<1;
|
|
1112 |
TUint8* pKey=((TUint8*)iEntries+jj*iEntrySize+iKeyOffset);
|
|
1113 |
if (jj>=ss || (*(TUint*)(pKey-iEntrySize))>*(TUint*)pKey )
|
|
1114 |
{
|
|
1115 |
--jj;
|
|
1116 |
pKey-=iEntrySize;
|
|
1117 |
}
|
|
1118 |
if (jj>=ss || *(TUint*)pKey<=sikey)
|
|
1119 |
break;
|
|
1120 |
wordmove((TUint8*)iEntries+ii*iEntrySize,(TUint8*)iEntries+jj*iEntrySize,iEntrySize);
|
|
1121 |
ii = jj;
|
|
1122 |
}
|
|
1123 |
wordmove((TUint8*)iEntries+ii*iEntrySize,si,iEntrySize);
|
|
1124 |
}
|
|
1125 |
}
|
|
1126 |
}
|
|
1127 |
|
|
1128 |
EXPORT_C void RArrayBase::HeapSort(TGeneralLinearOrder anOrder)
|
|
1129 |
{
|
|
1130 |
TUint32 si[KSimpleArrayMaxEntrySize/4];
|
|
1131 |
TInt ss = iCount;
|
|
1132 |
if (ss>1)
|
|
1133 |
{
|
|
1134 |
TInt sh = ss>>1;
|
|
1135 |
FOREVER
|
|
1136 |
{
|
|
1137 |
if (sh!=0)
|
|
1138 |
{
|
|
1139 |
--sh;
|
|
1140 |
wordmove(si,(TUint8*)iEntries+sh*iEntrySize,iEntrySize);
|
|
1141 |
}
|
|
1142 |
else
|
|
1143 |
{
|
|
1144 |
--ss;
|
|
1145 |
wordmove(si,(TUint8*)iEntries+ss*iEntrySize,iEntrySize);
|
|
1146 |
wordmove((TUint8*)iEntries+ss*iEntrySize,iEntries,iEntrySize);
|
|
1147 |
if (ss==1)
|
|
1148 |
{
|
|
1149 |
wordmove(iEntries,si,iEntrySize);
|
|
1150 |
break;
|
|
1151 |
}
|
|
1152 |
}
|
|
1153 |
TInt ii = sh;
|
|
1154 |
TInt jj = sh;
|
|
1155 |
FOREVER
|
|
1156 |
{
|
|
1157 |
jj = (jj+1)<<1;
|
|
1158 |
TUint8* pJJ=((TUint8*)iEntries+jj*iEntrySize);
|
|
1159 |
if (jj>=ss || (*anOrder)(pJJ-iEntrySize,pJJ)>0)
|
|
1160 |
{
|
|
1161 |
--jj;
|
|
1162 |
pJJ-=iEntrySize;
|
|
1163 |
}
|
|
1164 |
if (jj>=ss || (*anOrder)(pJJ,si)<=0)
|
|
1165 |
break;
|
|
1166 |
wordmove((TUint8*)iEntries+ii*iEntrySize,(TUint8*)iEntries+jj*iEntrySize,iEntrySize);
|
|
1167 |
ii = jj;
|
|
1168 |
}
|
|
1169 |
wordmove((TUint8*)iEntries+ii*iEntrySize,si,iEntrySize);
|
|
1170 |
}
|
|
1171 |
}
|
|
1172 |
}
|
|
1173 |
#endif
|
|
1174 |
|
|
1175 |
EXPORT_C TInt RArrayBase::GetCount(const CBase* aPtr)
|
|
1176 |
{
|
|
1177 |
return ((RArrayBase*)aPtr)->Count();
|
|
1178 |
}
|
|
1179 |
|
|
1180 |
EXPORT_C const TAny* RArrayBase::GetElementPtr(const CBase* aPtr, TInt aIndex)
|
|
1181 |
{
|
|
1182 |
return ((RArrayBase*)aPtr)->At(aIndex);
|
|
1183 |
}
|
|
1184 |
#endif // __KERNEL_MODE__
|
|
1185 |
|