os/kernelhwsrv/userlibandfileserver/fileserver/sfat/sl_utl.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
sl@0
    17
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
sl@0
    18
//!!
sl@0
    19
//!! WARNING!! DO NOT edit this file !! '\sfat' component is obsolete and is not being used. '\sfat32'replaces it
sl@0
    20
//!!
sl@0
    21
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
sl@0
    22
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
sl@0
    23
sl@0
    24
sl@0
    25
#include "sl_std.h"
sl@0
    26
sl@0
    27
//-----------------------------------------------------------------------------
sl@0
    28
/**
sl@0
    29
    Removes trailing dots from aName.
sl@0
    30
    @return new string descriptor that may have its length adjusted
sl@0
    31
*/
sl@0
    32
TPtrC RemoveTrailingDots(const TDesC& aName)
sl@0
    33
{
sl@0
    34
    TInt len = aName.Length();
sl@0
    35
    
sl@0
    36
    while(len > 0)
sl@0
    37
    {
sl@0
    38
        if(aName[len-1] == '.')
sl@0
    39
            len--;
sl@0
    40
        else
sl@0
    41
            break;
sl@0
    42
    }
sl@0
    43
sl@0
    44
    TPtrC ptrNoDots(aName.Ptr(), len);
sl@0
    45
    return ptrNoDots;
sl@0
    46
}
sl@0
    47
sl@0
    48
sl@0
    49
TUint32 Log2(TUint32 aVal)
sl@0
    50
{
sl@0
    51
    __ASSERT_COMPILE(sizeof(TUint32) == 4);
sl@0
    52
    ASSERT(aVal);
sl@0
    53
sl@0
    54
    TUint32 bitPos=31;
sl@0
    55
sl@0
    56
    if(!(aVal >> 16)) {bitPos-=16; aVal<<=16;}
sl@0
    57
    if(!(aVal >> 24)) {bitPos-=8;  aVal<<=8 ;}
sl@0
    58
    if(!(aVal >> 28)) {bitPos-=4;  aVal<<=4 ;}
sl@0
    59
    if(!(aVal >> 30)) {bitPos-=2;  aVal<<=2 ;}
sl@0
    60
    if(!(aVal >> 31)) {bitPos-=1;}
sl@0
    61
    
sl@0
    62
    return bitPos;
sl@0
    63
}
sl@0
    64
sl@0
    65
sl@0
    66
TTime DosTimeToTTime(TInt aDosTime,TInt aDosDate)
sl@0
    67
//
sl@0
    68
//  Deciphers the dos time/date entry information and converts to TTime
sl@0
    69
//
sl@0
    70
    {
sl@0
    71
    TInt secMask=0x1F;
sl@0
    72
    TInt minMask=0x07E0;
sl@0
    73
    TInt hrMask=0xF800;
sl@0
    74
    TInt dayMask=0x1F;
sl@0
    75
    TInt monthMask=0x01E0;
sl@0
    76
    TInt yearMask=0xFE00;
sl@0
    77
sl@0
    78
    TInt secs=(aDosTime&secMask)*2;
sl@0
    79
    TInt mins=(aDosTime&minMask)>>5;
sl@0
    80
    TInt hrs=(aDosTime&hrMask)>>11;
sl@0
    81
    TInt days=(aDosDate&dayMask)-1;
sl@0
    82
    TMonth months=(TMonth)(((aDosDate&monthMask)>>5)-1);
sl@0
    83
    TInt years=((aDosDate&yearMask)>>9)+1980;
sl@0
    84
    
sl@0
    85
    TDateTime datetime;
sl@0
    86
    TInt ret=datetime.Set(years,months,days,hrs,mins,secs,0);
sl@0
    87
    if (ret==KErrNone)
sl@0
    88
        return(TTime(datetime));
sl@0
    89
    return(TTime(0));
sl@0
    90
    }
sl@0
    91
sl@0
    92
TInt DosTimeFromTTime(const TTime& aTime)
sl@0
    93
//
sl@0
    94
// Converts a TTime to a dos time
sl@0
    95
//
sl@0
    96
    {
sl@0
    97
    TDateTime dateTime=aTime.DateTime();
sl@0
    98
    TInt dosSecs=dateTime.Second()/2;
sl@0
    99
    TInt dosMins=dateTime.Minute()<<5;
sl@0
   100
    TInt dosHrs=dateTime.Hour()<<11;
sl@0
   101
    return dosSecs|dosMins|dosHrs;
sl@0
   102
    }
sl@0
   103
sl@0
   104
TInt DosDateFromTTime(const TTime& aTime)
sl@0
   105
//
sl@0
   106
// Converts a TTime to a dos date
sl@0
   107
//
sl@0
   108
    {
sl@0
   109
sl@0
   110
    TDateTime dateTime=aTime.DateTime();
sl@0
   111
    TInt dosDays=dateTime.Day()+1;
sl@0
   112
    TInt dosMonths=(dateTime.Month()+1)<<5;
sl@0
   113
    TInt dosYears=(dateTime.Year()-1980)<<9;
sl@0
   114
    return dosDays|dosMonths|dosYears;
sl@0
   115
    }
sl@0
   116
sl@0
   117
TBuf8<12> DosNameToStdFormat(const TDesC8& aDosName)
sl@0
   118
//
sl@0
   119
// Converts xxx.yyy to standard format aaaaaaaayyy
sl@0
   120
//
sl@0
   121
    {
sl@0
   122
sl@0
   123
    __ASSERT_DEBUG(aDosName.Length()>=0 && aDosName.Length()<=12,Fault(EFatBadDosFormatName));
sl@0
   124
    TBuf8<12> result;
sl@0
   125
    Mem::Fill((TUint8*)result.Ptr(),result.MaxSize(),' ');
sl@0
   126
    TInt dotPos=aDosName.Locate('.');
sl@0
   127
    if (dotPos==KErrNotFound)
sl@0
   128
        {
sl@0
   129
        result=aDosName;
sl@0
   130
        result.SetLength(11);
sl@0
   131
        return result;
sl@0
   132
        }
sl@0
   133
    result=aDosName.Left(dotPos);
sl@0
   134
    result.SetLength(11);
sl@0
   135
    TPtr8 ext(&result[8],3);
sl@0
   136
    ext=aDosName.Right(aDosName.Length()-dotPos-1);
sl@0
   137
    return result;
sl@0
   138
    }
sl@0
   139
sl@0
   140
TBuf8<12> DosNameFromStdFormat(const TDesC8& aStdFormatName)
sl@0
   141
//
sl@0
   142
// Converts aaaaaaaayyy to dos name format xxx.yyy
sl@0
   143
//
sl@0
   144
    {
sl@0
   145
sl@0
   146
    __ASSERT_DEBUG(aStdFormatName.Length()==11,Fault(EFatBadStdFormatName));
sl@0
   147
    TBuf8<12> result;
sl@0
   148
    TInt nameLen=aStdFormatName.Locate(' ');
sl@0
   149
    if (nameLen>8 || nameLen==KErrNotFound)
sl@0
   150
        nameLen=8;
sl@0
   151
    result=aStdFormatName.Left(nameLen);
sl@0
   152
    TPtrC8 ext(&aStdFormatName[8],3);
sl@0
   153
    TInt extLen=ext.Locate(' ');
sl@0
   154
    if (extLen)
sl@0
   155
        result.Append(TChar('.'));
sl@0
   156
    if (extLen==KErrNotFound)
sl@0
   157
        extLen=3;
sl@0
   158
    result.Append(ext.Left(extLen));
sl@0
   159
    if(result.Length() && result[0]==0x05 )
sl@0
   160
        {
sl@0
   161
        result[0]=0xE5;
sl@0
   162
        }
sl@0
   163
    return result;
sl@0
   164
    }
sl@0
   165
sl@0
   166
TInt NumberOfVFatEntries(TInt aNameLength)
sl@0
   167
//
sl@0
   168
// Return the number of VFat entries required to describe a filename of length aNameLength
sl@0
   169
//
sl@0
   170
    {
sl@0
   171
    TInt numberOfEntries=0;
sl@0
   172
    if (aNameLength%KMaxVFatEntryName)
sl@0
   173
        aNameLength++;  //  Include a zero terminator
sl@0
   174
//  If aNameLength is a exact multiple of KMaxVFatEntryName, don't bother
sl@0
   175
//  with a zero terminator - it just adds an unnecessary directory entry        
sl@0
   176
    
sl@0
   177
    numberOfEntries=(1+(aNameLength/KMaxVFatEntryName));    
sl@0
   178
    
sl@0
   179
    if (aNameLength%KMaxVFatEntryName)
sl@0
   180
        numberOfEntries++;
sl@0
   181
    
sl@0
   182
    return(numberOfEntries);
sl@0
   183
    }
sl@0
   184
sl@0
   185
//-----------------------------------------------------------------------------
sl@0
   186
/** 
sl@0
   187
    Calculate DOS short name checksum
sl@0
   188
    @param aShortName short name descriptor (must be at least 11 bytes long)
sl@0
   189
    @return checksum
sl@0
   190
*/
sl@0
   191
TUint8 CalculateShortNameCheckSum(const TDesC8& aShortName)
sl@0
   192
    {
sl@0
   193
sl@0
   194
    ASSERT(aShortName.Length() >= KFatDirNameSize);
sl@0
   195
    const TUint8* pName = aShortName.Ptr();
sl@0
   196
sl@0
   197
    const TUint32 w0 = ((const TUint32*)pName)[0];
sl@0
   198
    const TUint32 w1 = ((const TUint32*)pName)[1];
sl@0
   199
sl@0
   200
    TUint32 chkSum = w0 & 0xFF;
sl@0
   201
    
sl@0
   202
    chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ((w0 << 16) >> 24));
sl@0
   203
    chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ((w0 << 8)  >> 24));
sl@0
   204
    chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ( w0 >> 24));
sl@0
   205
sl@0
   206
    chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + (w1) & 0xFF);
sl@0
   207
    chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ((w1 << 16) >> 24));
sl@0
   208
    chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ((w1 << 8)  >> 24));
sl@0
   209
    chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ( w1 >> 24));
sl@0
   210
sl@0
   211
    chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + pName[8]);
sl@0
   212
    chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + pName[9]);
sl@0
   213
    chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + pName[10]);
sl@0
   214
sl@0
   215
    return (TUint8)chkSum;
sl@0
   216
    }
sl@0
   217
sl@0
   218
//-----------------------------------------------------------------------------
sl@0
   219
sl@0
   220
const TUint32 K_FFFF = 0xFFFFFFFF; //-- all one bits, beware rigth shifts of signed integers!
sl@0
   221
sl@0
   222
sl@0
   223
RBitVector::RBitVector()
sl@0
   224
          :iNumBits(0), ipData(NULL), iNumWords(0)
sl@0
   225
    {
sl@0
   226
    }
sl@0
   227
sl@0
   228
sl@0
   229
RBitVector::~RBitVector()
sl@0
   230
    {
sl@0
   231
    Close();
sl@0
   232
    }
sl@0
   233
sl@0
   234
/**
sl@0
   235
    Panics.
sl@0
   236
    @param aPanicCode   a panic code
sl@0
   237
*/
sl@0
   238
void RBitVector::Panic(TPanicCode aPanicCode) const
sl@0
   239
    {
sl@0
   240
    _LIT(KPanicCat,"RBitVector");
sl@0
   241
    User::Panic(KPanicCat, aPanicCode);
sl@0
   242
    }
sl@0
   243
sl@0
   244
/** explicitly closes the object and deallocates memory */
sl@0
   245
void RBitVector::Close()
sl@0
   246
    {
sl@0
   247
    iNumBits = 0;
sl@0
   248
    iNumWords =0;
sl@0
   249
    User::Free(ipData);
sl@0
   250
    ipData = NULL;
sl@0
   251
    }
sl@0
   252
sl@0
   253
//-----------------------------------------------------------------------------
sl@0
   254
sl@0
   255
/**
sl@0
   256
    Comparison perator.
sl@0
   257
    @param  aRhs a vector to compate with.
sl@0
   258
    @panic ESizeMismatch in the case of different vector sizes
sl@0
   259
*/
sl@0
   260
TBool RBitVector::operator==(const RBitVector& aRhs) const
sl@0
   261
    {
sl@0
   262
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   263
    __ASSERT_ALWAYS(iNumBits == aRhs.iNumBits, Panic(ESizeMismatch));
sl@0
   264
sl@0
   265
sl@0
   266
    if(!iNumBits)
sl@0
   267
        return ETrue; //-- comparing 0-lenght arrays
sl@0
   268
sl@0
   269
    if(this == &aRhs)
sl@0
   270
        return ETrue; //-- comparing with itself
sl@0
   271
sl@0
   272
    if(iNumWords >= 1)
sl@0
   273
        {
sl@0
   274
        const TUint32 cntBytes = (iNumBits >> 5) << 2; //-- bytes to compare
sl@0
   275
        if(memcompare((const TUint8*)ipData, cntBytes, (const TUint8*)aRhs.ipData, cntBytes))
sl@0
   276
            return EFalse;
sl@0
   277
        }
sl@0
   278
sl@0
   279
    const TUint32 bitsRest  = iNumBits & 0x1F;
sl@0
   280
    if(bitsRest)
sl@0
   281
        {
sl@0
   282
        const TUint32 mask = K_FFFF >> (32-bitsRest);
sl@0
   283
        return ( (ipData[iNumWords-1] & mask) == (aRhs.ipData[iNumWords-1] & mask) );
sl@0
   284
        }
sl@0
   285
    
sl@0
   286
    return ETrue;
sl@0
   287
    }
sl@0
   288
sl@0
   289
TBool RBitVector::operator!=(const RBitVector& aRhs) const  
sl@0
   290
    {
sl@0
   291
    return ! ((*this) == aRhs);
sl@0
   292
    } 
sl@0
   293
sl@0
   294
//-----------------------------------------------------------------------------
sl@0
   295
sl@0
   296
/** The same as Create(), but leaves on error */
sl@0
   297
void RBitVector::CreateL(TUint32 aNumBits)
sl@0
   298
    {
sl@0
   299
    User::LeaveIfError(Create(aNumBits));
sl@0
   300
    }
sl@0
   301
sl@0
   302
sl@0
   303
/**
sl@0
   304
    Create the vector with the size of aNumBits bits.
sl@0
   305
    @return system-wide error codes:
sl@0
   306
        KErrNoMemory    unable to allocate sufficient amount of memory for the array
sl@0
   307
        KErrInUse       an attempt to call Create() for non-empty vector. Close it first.
sl@0
   308
        KErrArgument    invalid aNumBits value == 0
sl@0
   309
*/
sl@0
   310
TInt RBitVector::Create(TUint32 aNumBits)
sl@0
   311
    {
sl@0
   312
sl@0
   313
    if(ipData)
sl@0
   314
        return KErrInUse; //-- array is already in use. Close it first.
sl@0
   315
sl@0
   316
    if(!aNumBits)
sl@0
   317
        return KErrArgument;
sl@0
   318
sl@0
   319
    //-- memory is allocated by word (32 bit) quiantities
sl@0
   320
    const TUint32 numWords = (aNumBits >> 5) + ((aNumBits & 0x1F) > 0 ? 1:0);
sl@0
   321
    ipData = (TUint32*)User::AllocZ(numWords << 2);
sl@0
   322
sl@0
   323
    if(!ipData)
sl@0
   324
        return KErrNoMemory;
sl@0
   325
sl@0
   326
    iNumBits  = aNumBits;
sl@0
   327
    iNumWords = numWords;
sl@0
   328
sl@0
   329
    return KErrNone;
sl@0
   330
    }
sl@0
   331
sl@0
   332
sl@0
   333
/**
sl@0
   334
    Fill a bit vector with a given bit value
sl@0
   335
    @param aVal a bit value
sl@0
   336
*/
sl@0
   337
void RBitVector::Fill(TBool aVal)
sl@0
   338
    {
sl@0
   339
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   340
    memset(ipData, (aVal ? 0xFF : 0x00), iNumWords << 2);
sl@0
   341
    }
sl@0
   342
sl@0
   343
/** Invert all bits in a bit vector */
sl@0
   344
void RBitVector::Invert()
sl@0
   345
{
sl@0
   346
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   347
    for(TUint32 i=0; i<iNumWords; ++i)
sl@0
   348
        ipData[i] ^= K_FFFF;
sl@0
   349
}
sl@0
   350
sl@0
   351
sl@0
   352
/**
sl@0
   353
    Perform "And" operation between 2 vectors. They shall be the same size.
sl@0
   354
    @param  aRhs a vector from the right hand side
sl@0
   355
    @panic ESizeMismatch in the case of different vector sizes
sl@0
   356
*/
sl@0
   357
void RBitVector::And(const RBitVector& aRhs)
sl@0
   358
    {
sl@0
   359
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   360
    __ASSERT_ALWAYS(iNumBits == aRhs.iNumBits, Panic(ESizeMismatch));
sl@0
   361
    for(TUint32 i=0; i<iNumWords; ++i)
sl@0
   362
        {
sl@0
   363
        ipData[i] &= aRhs.ipData[i];
sl@0
   364
        }
sl@0
   365
    }
sl@0
   366
sl@0
   367
/**
sl@0
   368
    Perform "Or" operation between 2 vectors. They shall be the same size.    
sl@0
   369
    @param  aRhs a vector from the right hand side
sl@0
   370
    @panic ESizeMismatch in the case of different vector sizes
sl@0
   371
*/
sl@0
   372
void RBitVector::Or(const RBitVector& aRhs)
sl@0
   373
    {
sl@0
   374
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   375
    __ASSERT_ALWAYS(iNumBits == aRhs.iNumBits, Panic(ESizeMismatch));
sl@0
   376
    for(TUint32 i=0; i<iNumWords; ++i)
sl@0
   377
        {
sl@0
   378
        ipData[i] |= aRhs.ipData[i];
sl@0
   379
        }
sl@0
   380
    }
sl@0
   381
sl@0
   382
/**
sl@0
   383
    Perform "Xor" operation between 2 vectors. They shall be the same size.    
sl@0
   384
    @param  aRhs a vector from the right hand side
sl@0
   385
    @panic ESizeMismatch in the case of different vector sizes
sl@0
   386
*/
sl@0
   387
void RBitVector::Xor(const RBitVector& aRhs)
sl@0
   388
    {
sl@0
   389
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   390
    __ASSERT_ALWAYS(iNumBits == aRhs.iNumBits, Panic(ESizeMismatch));
sl@0
   391
    for(TUint32 i=0; i<iNumWords; ++i)
sl@0
   392
        {
sl@0
   393
        ipData[i] ^= aRhs.ipData[i];
sl@0
   394
        }
sl@0
   395
    }
sl@0
   396
sl@0
   397
//-----------------------------------------------------------------------------
sl@0
   398
/**
sl@0
   399
    Fill a range from bit number "aIndexFrom" to "aIndexTo" inclusively with the value of aVal
sl@0
   400
    
sl@0
   401
    @param  aIndexFrom  start bit number (inclusive)
sl@0
   402
    @param  aIndexTo    end bit number (inclusive)
sl@0
   403
    @param  aVal        the value to be used to fill the range (0s or 1s)
sl@0
   404
*/
sl@0
   405
void RBitVector::Fill(TUint32 aIndexFrom, TUint32 aIndexTo, TBool aVal)
sl@0
   406
    {
sl@0
   407
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   408
sl@0
   409
    //-- swap indexes if they are not in order
sl@0
   410
    if(aIndexFrom > aIndexTo)
sl@0
   411
        {
sl@0
   412
        const TUint32 tmp = aIndexFrom;
sl@0
   413
        aIndexFrom = aIndexTo;
sl@0
   414
        aIndexTo = tmp;
sl@0
   415
        }
sl@0
   416
sl@0
   417
    __ASSERT_ALWAYS((aIndexFrom < iNumBits) && (aIndexTo < iNumBits), Panic(EIndexOutOfRange));
sl@0
   418
sl@0
   419
    const TUint32 wordStart = WordNum(aIndexFrom);
sl@0
   420
    const TUint32 wordTo    = WordNum(aIndexTo);
sl@0
   421
sl@0
   422
    if(aVal)
sl@0
   423
        {//-- filling a range with '1'
sl@0
   424
        
sl@0
   425
        TUint32 shift = BitInWord(aIndexFrom);
sl@0
   426
        const TUint32 mask1 = (K_FFFF >> shift) << shift;
sl@0
   427
sl@0
   428
        TUint32 mask2 = K_FFFF;
sl@0
   429
        shift = 1+BitInWord(aIndexTo);
sl@0
   430
        if(shift < 32)
sl@0
   431
            {
sl@0
   432
            mask2 = ~((mask2 >> shift) << shift);
sl@0
   433
            }
sl@0
   434
sl@0
   435
        if(wordTo == wordStart)
sl@0
   436
            {//-- a special case, filling is in the same word
sl@0
   437
            ipData[wordStart] |= (mask1 & mask2);
sl@0
   438
            }
sl@0
   439
        else
sl@0
   440
            {
sl@0
   441
            ipData[wordStart] |= mask1; 
sl@0
   442
            ipData[wordTo]    |= mask2;
sl@0
   443
            
sl@0
   444
            const TUint32 wholeWordsBetween = wordTo - wordStart - 1; //-- whole words that can be bulk filled
sl@0
   445
sl@0
   446
            if(wholeWordsBetween)
sl@0
   447
                memset(ipData+wordStart+1, 0xFF, wholeWordsBetween << 2);
sl@0
   448
                            
sl@0
   449
            }
sl@0
   450
        }
sl@0
   451
    else
sl@0
   452
        {//-- filling a range with '0'
sl@0
   453
        
sl@0
   454
        //-- if you need this functionality, remove the panic and uncomment the code below.
sl@0
   455
sl@0
   456
        Panic(ENotImplemented);
sl@0
   457
        
sl@0
   458
        /*
sl@0
   459
        TUint32 shift = BitInWord(aIndexFrom);
sl@0
   460
        const TUint32 mask1 = ~((K_FFFF >> shift) << shift);
sl@0
   461
sl@0
   462
        TUint32 mask2 = 0;
sl@0
   463
        shift = 1+BitInWord(aIndexTo);
sl@0
   464
        if(shift < 32)
sl@0
   465
            {
sl@0
   466
            mask2 = ((K_FFFF >> shift) << shift);
sl@0
   467
            }
sl@0
   468
sl@0
   469
        if(wordTo == wordStart)
sl@0
   470
            {//-- a special case, filling is in the same word
sl@0
   471
            ipData[wordStart] &= (mask1 | mask2);
sl@0
   472
            }
sl@0
   473
        else
sl@0
   474
            {
sl@0
   475
            ipData[wordStart] &= mask1; 
sl@0
   476
            ipData[wordTo]    &= mask2;
sl@0
   477
            
sl@0
   478
            const TUint32 wholeWordsBetween = wordTo - wordStart - 1; //-- whole words that can be bulk filled
sl@0
   479
sl@0
   480
            if(wholeWordsBetween)
sl@0
   481
                memset(ipData+wordStart+1, 0x00, wholeWordsBetween << 2);
sl@0
   482
                            
sl@0
   483
            }
sl@0
   484
        */
sl@0
   485
        }
sl@0
   486
sl@0
   487
    }
sl@0
   488
sl@0
   489
//-----------------------------------------------------------------------------
sl@0
   490
sl@0
   491
/**
sl@0
   492
    Search for a specified bit value ('0' or '1') in the vector from the given position.
sl@0
   493
    @param  aStartPos   zero-based index; from this position the search will start. This position isn't included to the search.
sl@0
   494
                        On return may contain a new position if the specified bit is found in specified direction.
sl@0
   495
    @param  aBitVal     zero or non-zero bit to search.
sl@0
   496
    @param  aDir        Specifies the search direction
sl@0
   497
sl@0
   498
    @return ETrue if the specified bit value is found; aStartPos gets updated.
sl@0
   499
            EFalse otherwise.
sl@0
   500
sl@0
   501
*/
sl@0
   502
TBool RBitVector::Find(TUint32& aStartPos, TBool aBitVal, TFindDirection aDir) const
sl@0
   503
    {
sl@0
   504
    __ASSERT_ALWAYS(aStartPos < iNumBits, Panic(EIndexOutOfRange));
sl@0
   505
    ASSERT(iNumWords && ipData);
sl@0
   506
sl@0
   507
    switch(aDir)
sl@0
   508
        {
sl@0
   509
        case ERight:    //-- Search from the given position to the right
sl@0
   510
            return FindToRight(aStartPos, aBitVal);
sl@0
   511
sl@0
   512
        case ELeft:     //-- Search from the given position to the left (towards lower index)
sl@0
   513
            return FindToLeft(aStartPos, aBitVal);
sl@0
   514
sl@0
   515
        case ENearestL: //-- Search for the nearest value in both directions starting from left
sl@0
   516
            return FindNearest(aStartPos, aBitVal, ETrue);
sl@0
   517
sl@0
   518
        case ENearestR: //-- Search for the nearest value in both directions starting from right
sl@0
   519
            return FindNearest(aStartPos, aBitVal, EFalse);
sl@0
   520
sl@0
   521
        default:
sl@0
   522
            Panic(EWrondFindDirection);
sl@0
   523
            return EFalse;
sl@0
   524
sl@0
   525
        };
sl@0
   526
    
sl@0
   527
    }
sl@0
   528
sl@0
   529
//-----------------------------------------------------------------------------
sl@0
   530
/**
sl@0
   531
    Internal method to look for a given bit value in the right direction.
sl@0
   532
    see TBool RBitVector::Find(...)
sl@0
   533
*/
sl@0
   534
TBool RBitVector::FindToRight(TUint32& aStartPos, TBool aBitVal) const
sl@0
   535
    {
sl@0
   536
    if(aStartPos >= iNumBits-1)
sl@0
   537
        return EFalse; //-- no way to the right
sl@0
   538
sl@0
   539
    const TUint32 startPos = aStartPos+1;
sl@0
   540
    const TUint32 fInvert = aBitVal ? 0 : K_FFFF; //-- invert everything if we are looking for '0' bit
sl@0
   541
sl@0
   542
    TUint32 wordNum = WordNum(startPos);
sl@0
   543
    TUint32 val = ipData[wordNum] ^ fInvert;
sl@0
   544
sl@0
   545
    if(wordNum == iNumWords-1)
sl@0
   546
        {//-- process the last word in the array, some higher bits might not belong to the bit vector
sl@0
   547
        val = MaskLastWord(val);
sl@0
   548
        }
sl@0
   549
sl@0
   550
    const TUint32 shift = BitInWord(startPos);
sl@0
   551
    val = (val >> shift) << shift; //-- mask unused low bits
sl@0
   552
sl@0
   553
    if(val)
sl@0
   554
        {//-- there are '1' bits in the current word
sl@0
   555
        goto found;
sl@0
   556
        }
sl@0
   557
    else
sl@0
   558
        {//-- search in higher words
sl@0
   559
        wordNum++;
sl@0
   560
sl@0
   561
        while(iNumWords-wordNum > 1)
sl@0
   562
            {
sl@0
   563
            val = ipData[wordNum] ^ fInvert;
sl@0
   564
            if(val)
sl@0
   565
                goto found;
sl@0
   566
sl@0
   567
            wordNum++;
sl@0
   568
            }
sl@0
   569
sl@0
   570
        if(wordNum == iNumWords-1)
sl@0
   571
            {//-- process the last word in the array, some higher bith might not belong to the bit vector
sl@0
   572
            val = ipData[wordNum] ^ fInvert;
sl@0
   573
            val = MaskLastWord(val);
sl@0
   574
sl@0
   575
            if(val)
sl@0
   576
                goto found;
sl@0
   577
            }
sl@0
   578
        }
sl@0
   579
sl@0
   580
    return EFalse; //-- haven't found anything
sl@0
   581
sl@0
   582
  found:
sl@0
   583
sl@0
   584
    val &= (~val+1); //-- select rightmost bit
sl@0
   585
    aStartPos = (wordNum << 5)+Log2(val);
sl@0
   586
    return ETrue;
sl@0
   587
    }
sl@0
   588
sl@0
   589
sl@0
   590
//-----------------------------------------------------------------------------
sl@0
   591
sl@0
   592
/**
sl@0
   593
    Internal method to look for a given bit value in the left direction.
sl@0
   594
    see TBool RBitVector::Find(...)
sl@0
   595
*/
sl@0
   596
TBool RBitVector::FindToLeft(TUint32& aStartPos, TBool aBitVal) const
sl@0
   597
{
sl@0
   598
    if(!aStartPos)
sl@0
   599
        return EFalse; //-- no way to the left
sl@0
   600
    
sl@0
   601
    const TUint32 startPos=aStartPos-1;
sl@0
   602
    const TUint32 fInvert = aBitVal ? 0 : K_FFFF; //-- invert everything if we are looking for '0' bit
sl@0
   603
sl@0
   604
    TUint32 wordNum = WordNum(startPos);
sl@0
   605
    TUint32 val = ipData[wordNum] ^ fInvert;
sl@0
   606
sl@0
   607
    const TUint32 shift = 31-(BitInWord(startPos));
sl@0
   608
    val = (val << shift) >> shift; //-- mask unused high bits
sl@0
   609
sl@0
   610
    if(val)
sl@0
   611
    {//-- there are '1' bits in the current word
sl@0
   612
        goto found;
sl@0
   613
    }
sl@0
   614
    else
sl@0
   615
    {//-- search in the lower words
sl@0
   616
        while(wordNum)
sl@0
   617
        {
sl@0
   618
            wordNum--;
sl@0
   619
            val=ipData[wordNum] ^ fInvert;
sl@0
   620
            if(val)
sl@0
   621
                goto found;
sl@0
   622
        }
sl@0
   623
    }
sl@0
   624
sl@0
   625
    return EFalse; //-- nothing found
sl@0
   626
sl@0
   627
 found:
sl@0
   628
    aStartPos = (wordNum << 5)+Log2(val);
sl@0
   629
    return ETrue;
sl@0
   630
}
sl@0
   631
sl@0
   632
//-----------------------------------------------------------------------------
sl@0
   633
sl@0
   634
/**
sl@0
   635
    Internal method to look for a given bit value in the both directions.
sl@0
   636
    see TBool RBitVector::Find(...)
sl@0
   637
*/
sl@0
   638
TBool RBitVector::FindNearest(TUint32& aStartPos, TBool aBitVal, TBool aToLeft) const
sl@0
   639
{
sl@0
   640
    if(iNumBits < 2)
sl@0
   641
        return EFalse;
sl@0
   642
sl@0
   643
    if(aStartPos == 0)
sl@0
   644
        return FindToRight(aStartPos, aBitVal);
sl@0
   645
sl@0
   646
    if(aStartPos == iNumBits-1)
sl@0
   647
        return FindToLeft(aStartPos, aBitVal);
sl@0
   648
sl@0
   649
    
sl@0
   650
    const TUint32 fInvert = aBitVal ? 0 : K_FFFF; //-- invert everything if we are looking for '0' bit
sl@0
   651
    
sl@0
   652
    TUint32 wordNum = WordNum(aStartPos);
sl@0
   653
    TUint32 l_Idx; //-- index of the word to the left
sl@0
   654
    TUint32 r_Idx; //-- index of the word to the right
sl@0
   655
    
sl@0
   656
    l_Idx = r_Idx = wordNum;
sl@0
   657
sl@0
   658
    TBool   noWayLeft  = (wordNum == 0);            //-- if we are in the first word
sl@0
   659
    TBool   noWayRight = (wordNum == iNumWords-1);  //-- if we are in the last word
sl@0
   660
sl@0
   661
    //-- look in the current word first
sl@0
   662
    TUint32 val = ipData[wordNum] ^ fInvert;
sl@0
   663
    
sl@0
   664
    if(noWayRight)
sl@0
   665
    {   //-- this is the last word in the array, mask unused high bits in the last word
sl@0
   666
        val = MaskLastWord(val);
sl@0
   667
    }
sl@0
   668
sl@0
   669
    const TUint32 bitPos = aStartPos & 0x1F;
sl@0
   670
    val &= ~(1<<bitPos); //-- mask the bit at current position
sl@0
   671
    
sl@0
   672
    if(val == 0)
sl@0
   673
    {//-- no '1' bits in the current word
sl@0
   674
        noWayLeft  = ItrLeft(l_Idx);
sl@0
   675
        noWayRight = ItrRight(r_Idx);
sl@0
   676
    }
sl@0
   677
    else if(bitPos == 0)
sl@0
   678
    {
sl@0
   679
        noWayLeft = ItrLeft(l_Idx); //-- move to the previous word
sl@0
   680
    }
sl@0
   681
    else if(bitPos == 31)
sl@0
   682
    {
sl@0
   683
        noWayRight = ItrRight(r_Idx); //-- move to the next word
sl@0
   684
    }
sl@0
   685
    else
sl@0
   686
    {//-- look in the current word, in both halves to the left and right from the start position
sl@0
   687
        
sl@0
   688
        const TUint32 shift1 = 32-bitPos;
sl@0
   689
        const TUint32 partLo = (val << shift1) >> shift1; //-- towards lower bits
sl@0
   690
sl@0
   691
        const TUint32 shift2 = bitPos+1;
sl@0
   692
        const TUint32 partHi = (val >> shift2) << shift2; //-- towards higher bits 
sl@0
   693
        
sl@0
   694
sl@0
   695
        if(partLo && !partHi) //-- only lower part has '1' bits   
sl@0
   696
        {
sl@0
   697
            aStartPos = (wordNum << 5)+Log2(partLo);
sl@0
   698
            return ETrue;
sl@0
   699
        }
sl@0
   700
        else if(!partLo && partHi) //-- only higher part has '1' bits
sl@0
   701
        {
sl@0
   702
            aStartPos = (wordNum << 5)+Log2( (partHi & (~partHi+1)) );
sl@0
   703
            return ETrue;
sl@0
   704
        }
sl@0
   705
        else if(partLo && partHi) //-- both parts contain '1' bits, select the nearest one
sl@0
   706
        {
sl@0
   707
            const TUint32 posL = (wordNum << 5)+Log2(partLo);
sl@0
   708
            const TUint32 posR = (wordNum << 5)+Log2( (partHi & (~partHi+1)) );
sl@0
   709
        
sl@0
   710
            ASSERT(aStartPos > posL);
sl@0
   711
            ASSERT(posR > aStartPos);
sl@0
   712
            const TUint32 distL = aStartPos-posL;
sl@0
   713
            const TUint32 distR = posR-aStartPos;
sl@0
   714
sl@0
   715
            if(distL < distR)
sl@0
   716
            {
sl@0
   717
                aStartPos = posL;
sl@0
   718
                return ETrue;
sl@0
   719
            }
sl@0
   720
            else if(distL > distR)
sl@0
   721
            {
sl@0
   722
                aStartPos = posR;
sl@0
   723
                return ETrue;
sl@0
   724
            }
sl@0
   725
            else
sl@0
   726
            {//-- distL == distR, take into account search priority
sl@0
   727
                aStartPos = aToLeft ? posL : posR;
sl@0
   728
                return ETrue;
sl@0
   729
            }
sl@0
   730
        }
sl@0
   731
        else //-- (!partLo && !partHi), nothing in the current word
sl@0
   732
        {
sl@0
   733
            ASSERT(0);
sl@0
   734
        }
sl@0
   735
sl@0
   736
    }// if(bitPos > 0 && bitPos < 31)
sl@0
   737
sl@0
   738
    //-- now we are processing separate words from both sides of the search position
sl@0
   739
    for(;;)
sl@0
   740
    { 
sl@0
   741
        TUint32 wL = ipData[l_Idx] ^ fInvert;
sl@0
   742
        TUint32 wR = ipData[r_Idx] ^ fInvert;
sl@0
   743
        if(r_Idx == iNumWords-1)
sl@0
   744
        {   //-- this is the last word in the array, mask unused high bits in the last word
sl@0
   745
            wR = MaskLastWord(wR);
sl@0
   746
        }
sl@0
   747
sl@0
   748
        if(wL && !wR)
sl@0
   749
        {
sl@0
   750
            aStartPos = (l_Idx << 5)+Log2(wL);
sl@0
   751
            return ETrue;
sl@0
   752
        }
sl@0
   753
        else if(!wL && wR)
sl@0
   754
        {
sl@0
   755
            aStartPos = (r_Idx << 5)+Log2( (wR & (~wR+1)) );
sl@0
   756
            return ETrue;
sl@0
   757
        }
sl@0
   758
        else if(wL && wR)
sl@0
   759
        {
sl@0
   760
            const TUint32 posL = (l_Idx << 5)+Log2(wL);
sl@0
   761
            const TUint32 posR = (r_Idx << 5)+Log2( (wR & (~wR+1)) );
sl@0
   762
        
sl@0
   763
            ASSERT(aStartPos > posL);
sl@0
   764
            ASSERT(posR > aStartPos);
sl@0
   765
            const TUint32 distL = aStartPos-posL;
sl@0
   766
            const TUint32 distR = posR-aStartPos;
sl@0
   767
sl@0
   768
            if(distL < distR)
sl@0
   769
            {
sl@0
   770
                aStartPos = posL;
sl@0
   771
                return ETrue;
sl@0
   772
            }
sl@0
   773
            else if(distL > distR)
sl@0
   774
            {
sl@0
   775
                aStartPos = posR;
sl@0
   776
                return ETrue;
sl@0
   777
            }
sl@0
   778
            else
sl@0
   779
            {//-- distL == distR, take into account search priority
sl@0
   780
                aStartPos = aToLeft ? posL : posR;
sl@0
   781
                return ETrue;
sl@0
   782
            }
sl@0
   783
sl@0
   784
        }//else if(wL && wR)
sl@0
   785
sl@0
   786
sl@0
   787
        if(noWayLeft)
sl@0
   788
        {
sl@0
   789
            aStartPos = r_Idx << 5;
sl@0
   790
            return FindToRight(aStartPos, aBitVal);
sl@0
   791
        }
sl@0
   792
        else
sl@0
   793
        {
sl@0
   794
            noWayLeft  = ItrLeft(l_Idx);
sl@0
   795
        }
sl@0
   796
sl@0
   797
        if(noWayRight)
sl@0
   798
        {
sl@0
   799
            aStartPos = l_Idx << 5;
sl@0
   800
            return FindToLeft(aStartPos, aBitVal);
sl@0
   801
        }
sl@0
   802
        else
sl@0
   803
        {    
sl@0
   804
            noWayRight = ItrRight(r_Idx);
sl@0
   805
        }
sl@0
   806
sl@0
   807
   }//for(;;)
sl@0
   808
sl@0
   809
    //return EFalse;
sl@0
   810
}
sl@0
   811
sl@0
   812
//-----------------------------------------------------------------------------
sl@0
   813
/**
sl@0
   814
    Find out if two vectors are different.
sl@0
   815
sl@0
   816
    @param  aRhs        vector to compare with
sl@0
   817
    @param  aDiffIndex  if there is a differene, here will be the number of the first different bit
sl@0
   818
    @return ETrue if vectors differ, EFalse, if they are identical.
sl@0
   819
*/
sl@0
   820
TBool RBitVector::Diff(const RBitVector& aRhs, TUint32& aDiffIndex) const
sl@0
   821
{
sl@0
   822
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   823
    __ASSERT_ALWAYS(iNumBits == aRhs.iNumBits, Panic(ESizeMismatch));
sl@0
   824
    ASSERT(iNumWords > 0);
sl@0
   825
sl@0
   826
    TUint32 diffWord=0;
sl@0
   827
    TUint32 wordNum=0;
sl@0
   828
sl@0
   829
    //-- compare all but the last word in the array
sl@0
   830
    for(wordNum=0; wordNum < iNumWords-1; ++wordNum)
sl@0
   831
    {
sl@0
   832
        diffWord = ipData[wordNum] ^ aRhs.ipData[wordNum];
sl@0
   833
        if(diffWord)
sl@0
   834
            break;  //-- found difference
sl@0
   835
    }
sl@0
   836
sl@0
   837
    //-- process the last word in the array
sl@0
   838
    if(!diffWord)
sl@0
   839
    {
sl@0
   840
        diffWord = MaskLastWord(ipData[wordNum]) ^ MaskLastWord(aRhs.ipData[wordNum]);
sl@0
   841
    }
sl@0
   842
sl@0
   843
    if(!diffWord)
sl@0
   844
        return EFalse; //-- vectors are the same
sl@0
   845
sl@0
   846
    //-- calculate the position of the bit that different.
sl@0
   847
    diffWord &= (~diffWord+1); //-- select rightmost bit
sl@0
   848
    aDiffIndex = (wordNum << 5)+Log2(diffWord);
sl@0
   849
    
sl@0
   850
    return ETrue;
sl@0
   851
}
sl@0
   852
//-----------------------------------------------------------------------------
sl@0
   853
sl@0
   854
/**
sl@0
   855
    Iterate to the left (towards lower index) in the array of words ipData
sl@0
   856
sl@0
   857
    @param  aIdx index within ipData array to be decremented; if it's possible to move left, it will be decreased
sl@0
   858
    @return ETrue if there is no way left i.e. aIdx is 0. EFalse otherwise and aIdx decreased.
sl@0
   859
*/
sl@0
   860
TBool RBitVector::ItrLeft(TUint32& aIdx) const
sl@0
   861
{
sl@0
   862
    if(aIdx == 0)
sl@0
   863
        return ETrue;
sl@0
   864
    else
sl@0
   865
    {
sl@0
   866
        aIdx--;
sl@0
   867
        return EFalse;
sl@0
   868
    }
sl@0
   869
}
sl@0
   870
sl@0
   871
sl@0
   872
/**
sl@0
   873
    Iterate to the right (towards higher index) in the array of words ipData
sl@0
   874
sl@0
   875
    @param  aIdx index within ipData array to be incremented; if it's possible to move right, it will be increased
sl@0
   876
    @return ETrue if there is no way right i.e. aIdx corresponds to the last word. EFalse otherwise and aIdx increased.
sl@0
   877
*/
sl@0
   878
TBool RBitVector::ItrRight(TUint32& aIdx) const
sl@0
   879
{
sl@0
   880
    if(aIdx < iNumWords-1)
sl@0
   881
    {
sl@0
   882
        aIdx++;
sl@0
   883
        return EFalse;
sl@0
   884
    }
sl@0
   885
    else
sl@0
   886
        return ETrue;
sl@0
   887
}
sl@0
   888
sl@0
   889
sl@0
   890
sl@0
   891
sl@0
   892
sl@0
   893
sl@0
   894
sl@0
   895