os/kernelhwsrv/userlibandfileserver/fileserver/fs_utils/bit_vector.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1995-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
//  Collection of common constants, utility functions, etc. for the file server and file systems.
sl@0
    16
//  Definitions here must be filesystem-agnostic, i.e. generic enougs to be used by every file system
sl@0
    17
//
sl@0
    18
//  This is the internal file and must not be exported.
sl@0
    19
sl@0
    20
/**
sl@0
    21
    @file
sl@0
    22
    @internalTechnology
sl@0
    23
*/
sl@0
    24
sl@0
    25
#include "bit_vector.h"
sl@0
    26
sl@0
    27
sl@0
    28
//#######################################################################################################################################
sl@0
    29
//#   RBitVector class implementation
sl@0
    30
//#######################################################################################################################################
sl@0
    31
sl@0
    32
const TUint32 K_FFFF = 0xFFFFFFFF; //-- all one bits, beware rigth shifts of signed integers!
sl@0
    33
sl@0
    34
RBitVector::RBitVector()
sl@0
    35
          :iNumBits(0), ipData(NULL), iNumWords(0)
sl@0
    36
    {
sl@0
    37
    }
sl@0
    38
sl@0
    39
sl@0
    40
RBitVector::~RBitVector()
sl@0
    41
    {
sl@0
    42
    Close();
sl@0
    43
    }
sl@0
    44
sl@0
    45
/**
sl@0
    46
    Panics.
sl@0
    47
    @param aPanicCode   a panic code
sl@0
    48
*/
sl@0
    49
void RBitVector::Panic(TPanicCode aPanicCode) const
sl@0
    50
    {
sl@0
    51
    _LIT(KPanicCat,"RBitVector");
sl@0
    52
    User::Panic(KPanicCat, aPanicCode);
sl@0
    53
    }
sl@0
    54
sl@0
    55
/** explicitly closes the object and deallocates memory */
sl@0
    56
void RBitVector::Close()
sl@0
    57
    {
sl@0
    58
    iNumBits = 0;
sl@0
    59
    iNumWords =0;
sl@0
    60
    User::Free(ipData);
sl@0
    61
    ipData = NULL;
sl@0
    62
    }
sl@0
    63
sl@0
    64
//-----------------------------------------------------------------------------
sl@0
    65
sl@0
    66
/**
sl@0
    67
    Comparison perator.
sl@0
    68
    @param  aRhs a vector to compate with.
sl@0
    69
    @panic ESizeMismatch in the case of different vector sizes
sl@0
    70
*/
sl@0
    71
TBool RBitVector::operator==(const RBitVector& aRhs) const
sl@0
    72
    {
sl@0
    73
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
    74
    __ASSERT_ALWAYS(iNumBits == aRhs.iNumBits, Panic(ESizeMismatch));
sl@0
    75
sl@0
    76
sl@0
    77
    if(!iNumBits)
sl@0
    78
        return ETrue; //-- comparing 0-lenght arrays
sl@0
    79
sl@0
    80
    if(this == &aRhs)
sl@0
    81
        {//-- comparing with itself, potential source of errors
sl@0
    82
        ASSERT(0);
sl@0
    83
        return ETrue; 
sl@0
    84
        }
sl@0
    85
    
sl@0
    86
    if(iNumWords >= 1)
sl@0
    87
        {
sl@0
    88
        const TUint32 cntBytes = (iNumBits >> 5) << 2; //-- bytes to compare
sl@0
    89
        if(memcompare((const TUint8*)ipData, cntBytes, (const TUint8*)aRhs.ipData, cntBytes))
sl@0
    90
            return EFalse;
sl@0
    91
        }
sl@0
    92
sl@0
    93
    const TUint32 bitsRest  = iNumBits & 0x1F;
sl@0
    94
    if(bitsRest)
sl@0
    95
        {
sl@0
    96
        const TUint32 mask = K_FFFF >> (32-bitsRest);
sl@0
    97
        return ( (ipData[iNumWords-1] & mask) == (aRhs.ipData[iNumWords-1] & mask) );
sl@0
    98
        }
sl@0
    99
    
sl@0
   100
    return ETrue;
sl@0
   101
    }
sl@0
   102
sl@0
   103
TBool RBitVector::operator!=(const RBitVector& aRhs) const  
sl@0
   104
    {
sl@0
   105
    return ! ((*this) == aRhs);
sl@0
   106
    } 
sl@0
   107
sl@0
   108
//-----------------------------------------------------------------------------
sl@0
   109
sl@0
   110
/** The same as Create(), but leaves on error */
sl@0
   111
void RBitVector::CreateL(TUint32 aNumBits)
sl@0
   112
    {
sl@0
   113
    User::LeaveIfError(Create(aNumBits));
sl@0
   114
    }
sl@0
   115
sl@0
   116
sl@0
   117
/**
sl@0
   118
    Create the vector with the size of aNumBits bits.
sl@0
   119
    @return system-wide error codes:
sl@0
   120
        KErrNoMemory    unable to allocate sufficient amount of memory for the array
sl@0
   121
        KErrInUse       an attempt to call Create() for non-empty vector. Close it first.
sl@0
   122
        KErrArgument    invalid aNumBits value == 0
sl@0
   123
*/
sl@0
   124
TInt RBitVector::Create(TUint32 aNumBits)
sl@0
   125
    {
sl@0
   126
sl@0
   127
    if(ipData)
sl@0
   128
        return KErrInUse; //-- array is already in use. Close it first.
sl@0
   129
sl@0
   130
    if(!aNumBits)
sl@0
   131
        return KErrArgument;
sl@0
   132
sl@0
   133
    //-- memory is allocated by word (32 bit) quiantities
sl@0
   134
    const TUint32 numWords = (aNumBits >> 5) + ((aNumBits & 0x1F) > 0 ? 1:0);
sl@0
   135
    ipData = (TUint32*)User::AllocZ(numWords << 2);
sl@0
   136
sl@0
   137
    if(!ipData)
sl@0
   138
        return KErrNoMemory;
sl@0
   139
sl@0
   140
    iNumBits  = aNumBits;
sl@0
   141
    iNumWords = numWords;
sl@0
   142
sl@0
   143
    return KErrNone;
sl@0
   144
    }
sl@0
   145
sl@0
   146
sl@0
   147
/**
sl@0
   148
    Fill a bit vector with a given bit value
sl@0
   149
    @param aVal a bit value
sl@0
   150
*/
sl@0
   151
void RBitVector::Fill(TBool aVal)
sl@0
   152
    {
sl@0
   153
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   154
    memset(ipData, (aVal ? 0xFF : 0x00), iNumWords << 2);
sl@0
   155
    }
sl@0
   156
sl@0
   157
/** Invert all bits in a bit vector */
sl@0
   158
void RBitVector::Invert()
sl@0
   159
{
sl@0
   160
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   161
    for(TUint32 i=0; i<iNumWords; ++i)
sl@0
   162
        ipData[i] ^= K_FFFF;
sl@0
   163
}
sl@0
   164
sl@0
   165
sl@0
   166
/**
sl@0
   167
    Perform "And" operation between 2 vectors. They shall be the same size.
sl@0
   168
    @param  aRhs a vector from the right hand side
sl@0
   169
    @panic ESizeMismatch in the case of different vector sizes
sl@0
   170
*/
sl@0
   171
void RBitVector::And(const RBitVector& aRhs)
sl@0
   172
    {
sl@0
   173
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   174
    __ASSERT_ALWAYS(iNumBits == aRhs.iNumBits, Panic(ESizeMismatch));
sl@0
   175
    for(TUint32 i=0; i<iNumWords; ++i)
sl@0
   176
        {
sl@0
   177
        ipData[i] &= aRhs.ipData[i];
sl@0
   178
        }
sl@0
   179
    }
sl@0
   180
sl@0
   181
/**
sl@0
   182
    Perform "Or" operation between 2 vectors. They shall be the same size.    
sl@0
   183
    @param  aRhs a vector from the right hand side
sl@0
   184
    @panic ESizeMismatch in the case of different vector sizes
sl@0
   185
*/
sl@0
   186
void RBitVector::Or(const RBitVector& aRhs)
sl@0
   187
    {
sl@0
   188
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   189
    __ASSERT_ALWAYS(iNumBits == aRhs.iNumBits, Panic(ESizeMismatch));
sl@0
   190
    for(TUint32 i=0; i<iNumWords; ++i)
sl@0
   191
        {
sl@0
   192
        ipData[i] |= aRhs.ipData[i];
sl@0
   193
        }
sl@0
   194
    }
sl@0
   195
sl@0
   196
/**
sl@0
   197
    Perform "Xor" operation between 2 vectors. They shall be the same size.    
sl@0
   198
    @param  aRhs a vector from the right hand side
sl@0
   199
    @panic ESizeMismatch in the case of different vector sizes
sl@0
   200
*/
sl@0
   201
void RBitVector::Xor(const RBitVector& aRhs)
sl@0
   202
    {
sl@0
   203
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   204
    __ASSERT_ALWAYS(iNumBits == aRhs.iNumBits, Panic(ESizeMismatch));
sl@0
   205
    for(TUint32 i=0; i<iNumWords; ++i)
sl@0
   206
        {
sl@0
   207
        ipData[i] ^= aRhs.ipData[i];
sl@0
   208
        }
sl@0
   209
    }
sl@0
   210
sl@0
   211
//-----------------------------------------------------------------------------
sl@0
   212
/**
sl@0
   213
    Fill a range from bit number "aIndexFrom" to "aIndexTo" inclusively with the value of aVal
sl@0
   214
    
sl@0
   215
    @param  aIndexFrom  start bit number (inclusive)
sl@0
   216
    @param  aIndexTo    end bit number (inclusive)
sl@0
   217
    @param  aVal        the value to be used to fill the range (0s or 1s)
sl@0
   218
*/
sl@0
   219
void RBitVector::Fill(TUint32 aIndexFrom, TUint32 aIndexTo, TBool aVal)
sl@0
   220
    {
sl@0
   221
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   222
sl@0
   223
    //-- swap indexes if they are not in order
sl@0
   224
    if(aIndexFrom > aIndexTo)
sl@0
   225
        {
sl@0
   226
        const TUint32 tmp = aIndexFrom;
sl@0
   227
        aIndexFrom = aIndexTo;
sl@0
   228
        aIndexTo = tmp;
sl@0
   229
        }
sl@0
   230
sl@0
   231
    __ASSERT_ALWAYS((aIndexFrom < iNumBits) && (aIndexTo < iNumBits), Panic(EIndexOutOfRange));
sl@0
   232
sl@0
   233
    const TUint32 wordStart = WordNum(aIndexFrom);
sl@0
   234
    const TUint32 wordTo    = WordNum(aIndexTo);
sl@0
   235
sl@0
   236
    if(aVal)
sl@0
   237
        {//-- filling a range with '1'
sl@0
   238
        
sl@0
   239
        TUint32 shift = BitInWord(aIndexFrom);
sl@0
   240
        const TUint32 mask1 = (K_FFFF >> shift) << shift;
sl@0
   241
sl@0
   242
        TUint32 mask2 = K_FFFF;
sl@0
   243
        shift = 1+BitInWord(aIndexTo);
sl@0
   244
        if(shift < 32)
sl@0
   245
            {
sl@0
   246
            mask2 = ~((mask2 >> shift) << shift);
sl@0
   247
            }
sl@0
   248
sl@0
   249
        if(wordTo == wordStart)
sl@0
   250
            {//-- a special case, filling is in the same word
sl@0
   251
            ipData[wordStart] |= (mask1 & mask2);
sl@0
   252
            }
sl@0
   253
        else
sl@0
   254
            {
sl@0
   255
            ipData[wordStart] |= mask1; 
sl@0
   256
            ipData[wordTo]    |= mask2;
sl@0
   257
            
sl@0
   258
            const TUint32 wholeWordsBetween = wordTo - wordStart - 1; //-- whole words that can be bulk filled
sl@0
   259
sl@0
   260
            if(wholeWordsBetween)
sl@0
   261
                memset(ipData+wordStart+1, 0xFF, wholeWordsBetween << 2);
sl@0
   262
                            
sl@0
   263
            }
sl@0
   264
        }
sl@0
   265
    else
sl@0
   266
        {//-- filling a range with '0'
sl@0
   267
        
sl@0
   268
        TUint32 shift = BitInWord(aIndexFrom);
sl@0
   269
        const TUint32 mask1 = ~((K_FFFF >> shift) << shift);
sl@0
   270
sl@0
   271
        TUint32 mask2 = 0;
sl@0
   272
        shift = 1+BitInWord(aIndexTo);
sl@0
   273
        if(shift < 32)
sl@0
   274
            {
sl@0
   275
            mask2 = ((K_FFFF >> shift) << shift);
sl@0
   276
            }
sl@0
   277
sl@0
   278
        if(wordTo == wordStart)
sl@0
   279
            {//-- a special case, filling is in the same word
sl@0
   280
            ipData[wordStart] &= (mask1 | mask2);
sl@0
   281
            }
sl@0
   282
        else
sl@0
   283
            {
sl@0
   284
            ipData[wordStart] &= mask1; 
sl@0
   285
            ipData[wordTo]    &= mask2;
sl@0
   286
            
sl@0
   287
            const TUint32 wholeWordsBetween = wordTo - wordStart - 1; //-- whole words that can be bulk filled
sl@0
   288
sl@0
   289
            if(wholeWordsBetween)
sl@0
   290
                memset(ipData+wordStart+1, 0x00, wholeWordsBetween << 2);
sl@0
   291
                            
sl@0
   292
            }
sl@0
   293
        }
sl@0
   294
sl@0
   295
    }
sl@0
   296
sl@0
   297
//-----------------------------------------------------------------------------
sl@0
   298
sl@0
   299
/**
sl@0
   300
    Search for a specified bit value ('0' or '1') in the vector from the given position.
sl@0
   301
    @param  aStartPos   zero-based index; from this position the search will start. This position isn't included to the search.
sl@0
   302
                        On return may contain a new position if the specified bit is found in specified direction.
sl@0
   303
    @param  aBitVal     zero or non-zero bit to search.
sl@0
   304
    @param  aDir        Specifies the search direction
sl@0
   305
sl@0
   306
    @return ETrue if the specified bit value is found; aStartPos gets updated.
sl@0
   307
            EFalse otherwise.
sl@0
   308
sl@0
   309
*/
sl@0
   310
TBool RBitVector::Find(TUint32& aStartPos, TBool aBitVal, TFindDirection aDir) const
sl@0
   311
    {
sl@0
   312
    __ASSERT_ALWAYS(aStartPos < iNumBits, Panic(EIndexOutOfRange));
sl@0
   313
    ASSERT(iNumWords && ipData);
sl@0
   314
sl@0
   315
    switch(aDir)
sl@0
   316
        {
sl@0
   317
        case ERight:    //-- Search from the given position to the right
sl@0
   318
            return FindToRight(aStartPos, aBitVal);
sl@0
   319
sl@0
   320
        case ELeft:     //-- Search from the given position to the left (towards lower index)
sl@0
   321
            return FindToLeft(aStartPos, aBitVal);
sl@0
   322
sl@0
   323
        case ENearestL: //-- Search for the nearest value in both directions starting from left
sl@0
   324
            return FindNearest(aStartPos, aBitVal, ETrue);
sl@0
   325
sl@0
   326
        case ENearestR: //-- Search for the nearest value in both directions starting from right
sl@0
   327
            return FindNearest(aStartPos, aBitVal, EFalse);
sl@0
   328
sl@0
   329
        default:
sl@0
   330
            Panic(EWrondFindDirection);
sl@0
   331
            return EFalse;
sl@0
   332
sl@0
   333
        };
sl@0
   334
    
sl@0
   335
    }
sl@0
   336
sl@0
   337
//-----------------------------------------------------------------------------
sl@0
   338
/**
sl@0
   339
    Internal method to look for a given bit value in the right direction.
sl@0
   340
    see TBool RBitVector::Find(...)
sl@0
   341
*/
sl@0
   342
TBool RBitVector::FindToRight(TUint32& aStartPos, TBool aBitVal) const
sl@0
   343
    {
sl@0
   344
    if(aStartPos >= iNumBits-1)
sl@0
   345
        return EFalse; //-- no way to the right
sl@0
   346
sl@0
   347
    const TUint32 startPos = aStartPos+1;
sl@0
   348
    const TUint32 fInvert = aBitVal ? 0 : K_FFFF; //-- invert everything if we are looking for '0' bit
sl@0
   349
sl@0
   350
    TUint32 wordNum = WordNum(startPos);
sl@0
   351
    TUint32 val = ipData[wordNum] ^ fInvert;
sl@0
   352
sl@0
   353
    if(wordNum == iNumWords-1)
sl@0
   354
        {//-- process the last word in the array, some higher bits might not belong to the bit vector
sl@0
   355
        val = MaskLastWord(val);
sl@0
   356
        }
sl@0
   357
sl@0
   358
    const TUint32 shift = BitInWord(startPos);
sl@0
   359
    val = (val >> shift) << shift; //-- mask unused low bits
sl@0
   360
sl@0
   361
    if(val)
sl@0
   362
        {//-- there are '1' bits in the current word
sl@0
   363
        goto found;
sl@0
   364
        }
sl@0
   365
    else
sl@0
   366
        {//-- search in higher words
sl@0
   367
        wordNum++;
sl@0
   368
sl@0
   369
        while(iNumWords-wordNum > 1)
sl@0
   370
            {
sl@0
   371
            val = ipData[wordNum] ^ fInvert;
sl@0
   372
            if(val)
sl@0
   373
                goto found;
sl@0
   374
sl@0
   375
            wordNum++;
sl@0
   376
            }
sl@0
   377
sl@0
   378
        if(wordNum == iNumWords-1)
sl@0
   379
            {//-- process the last word in the array, some higher bith might not belong to the bit vector
sl@0
   380
            val = ipData[wordNum] ^ fInvert;
sl@0
   381
            val = MaskLastWord(val);
sl@0
   382
sl@0
   383
            if(val)
sl@0
   384
                goto found;
sl@0
   385
            }
sl@0
   386
        }
sl@0
   387
sl@0
   388
    return EFalse; //-- haven't found anything
sl@0
   389
sl@0
   390
  found:
sl@0
   391
sl@0
   392
    val &= (~val+1); //-- select rightmost bit
sl@0
   393
    aStartPos = (wordNum << 5)+Log2(val);
sl@0
   394
    return ETrue;
sl@0
   395
    }
sl@0
   396
sl@0
   397
sl@0
   398
//-----------------------------------------------------------------------------
sl@0
   399
sl@0
   400
/**
sl@0
   401
    Internal method to look for a given bit value in the left direction.
sl@0
   402
    see TBool RBitVector::Find(...)
sl@0
   403
*/
sl@0
   404
TBool RBitVector::FindToLeft(TUint32& aStartPos, TBool aBitVal) const
sl@0
   405
{
sl@0
   406
    if(!aStartPos)
sl@0
   407
        return EFalse; //-- no way to the left
sl@0
   408
    
sl@0
   409
    const TUint32 startPos=aStartPos-1;
sl@0
   410
    const TUint32 fInvert = aBitVal ? 0 : K_FFFF; //-- invert everything if we are looking for '0' bit
sl@0
   411
sl@0
   412
    TUint32 wordNum = WordNum(startPos);
sl@0
   413
    TUint32 val = ipData[wordNum] ^ fInvert;
sl@0
   414
sl@0
   415
    const TUint32 shift = 31-(BitInWord(startPos));
sl@0
   416
    val = (val << shift) >> shift; //-- mask unused high bits
sl@0
   417
sl@0
   418
    if(val)
sl@0
   419
    {//-- there are '1' bits in the current word
sl@0
   420
        goto found;
sl@0
   421
    }
sl@0
   422
    else
sl@0
   423
    {//-- search in the lower words
sl@0
   424
        while(wordNum)
sl@0
   425
        {
sl@0
   426
            wordNum--;
sl@0
   427
            val=ipData[wordNum] ^ fInvert;
sl@0
   428
            if(val)
sl@0
   429
                goto found;
sl@0
   430
        }
sl@0
   431
    }
sl@0
   432
sl@0
   433
    return EFalse; //-- nothing found
sl@0
   434
sl@0
   435
 found:
sl@0
   436
    aStartPos = (wordNum << 5)+Log2(val);
sl@0
   437
    return ETrue;
sl@0
   438
}
sl@0
   439
sl@0
   440
//-----------------------------------------------------------------------------
sl@0
   441
sl@0
   442
/**
sl@0
   443
    Internal method to look for a given bit value in the both directions.
sl@0
   444
    see TBool RBitVector::Find(...)
sl@0
   445
*/
sl@0
   446
TBool RBitVector::FindNearest(TUint32& aStartPos, TBool aBitVal, TBool aToLeft) const
sl@0
   447
{
sl@0
   448
    if(iNumBits < 2)
sl@0
   449
        return EFalse;
sl@0
   450
sl@0
   451
    if(aStartPos == 0)
sl@0
   452
        return FindToRight(aStartPos, aBitVal);
sl@0
   453
sl@0
   454
    if(aStartPos == iNumBits-1)
sl@0
   455
        return FindToLeft(aStartPos, aBitVal);
sl@0
   456
sl@0
   457
    
sl@0
   458
    const TUint32 fInvert = aBitVal ? 0 : K_FFFF; //-- invert everything if we are looking for '0' bit
sl@0
   459
    
sl@0
   460
    TUint32 wordNum = WordNum(aStartPos);
sl@0
   461
    TUint32 l_Idx; //-- index of the word to the left
sl@0
   462
    TUint32 r_Idx; //-- index of the word to the right
sl@0
   463
    
sl@0
   464
    l_Idx = r_Idx = wordNum;
sl@0
   465
sl@0
   466
    TBool   noWayLeft  = (wordNum == 0);            //-- if we are in the first word
sl@0
   467
    TBool   noWayRight = (wordNum == iNumWords-1);  //-- if we are in the last word
sl@0
   468
sl@0
   469
    //-- look in the current word first
sl@0
   470
    TUint32 val = ipData[wordNum] ^ fInvert;
sl@0
   471
    
sl@0
   472
    if(noWayRight)
sl@0
   473
    {   //-- this is the last word in the array, mask unused high bits in the last word
sl@0
   474
        val = MaskLastWord(val);
sl@0
   475
    }
sl@0
   476
sl@0
   477
    const TUint32 bitPos = aStartPos & 0x1F;
sl@0
   478
    val &= ~(1<<bitPos); //-- mask the bit at current position
sl@0
   479
    
sl@0
   480
    if(val == 0)
sl@0
   481
    {//-- no '1' bits in the current word
sl@0
   482
        noWayLeft  = ItrLeft(l_Idx);
sl@0
   483
        noWayRight = ItrRight(r_Idx);
sl@0
   484
    }
sl@0
   485
    else if(bitPos == 0)
sl@0
   486
    {
sl@0
   487
        noWayLeft = ItrLeft(l_Idx); //-- move to the previous word
sl@0
   488
    }
sl@0
   489
    else if(bitPos == 31)
sl@0
   490
    {
sl@0
   491
        noWayRight = ItrRight(r_Idx); //-- move to the next word
sl@0
   492
    }
sl@0
   493
    else
sl@0
   494
    {//-- look in the current word, in both halves to the left and right from the start position
sl@0
   495
        
sl@0
   496
        const TUint32 shift1 = 32-bitPos;
sl@0
   497
        const TUint32 partLo = (val << shift1) >> shift1; //-- towards lower bits
sl@0
   498
sl@0
   499
        const TUint32 shift2 = bitPos+1;
sl@0
   500
        const TUint32 partHi = (val >> shift2) << shift2; //-- towards higher bits 
sl@0
   501
        
sl@0
   502
sl@0
   503
        if(partLo && !partHi) //-- only lower part has '1' bits   
sl@0
   504
        {
sl@0
   505
            aStartPos = (wordNum << 5)+Log2(partLo);
sl@0
   506
            return ETrue;
sl@0
   507
        }
sl@0
   508
        else if(!partLo && partHi) //-- only higher part has '1' bits
sl@0
   509
        {
sl@0
   510
            aStartPos = (wordNum << 5)+Log2( (partHi & (~partHi+1)) );
sl@0
   511
            return ETrue;
sl@0
   512
        }
sl@0
   513
        else if(partLo && partHi) //-- both parts contain '1' bits, select the nearest one
sl@0
   514
        {
sl@0
   515
            const TUint32 posL = (wordNum << 5)+Log2(partLo);
sl@0
   516
            const TUint32 posR = (wordNum << 5)+Log2( (partHi & (~partHi+1)) );
sl@0
   517
        
sl@0
   518
            ASSERT(aStartPos > posL);
sl@0
   519
            ASSERT(posR > aStartPos);
sl@0
   520
            const TUint32 distL = aStartPos-posL;
sl@0
   521
            const TUint32 distR = posR-aStartPos;
sl@0
   522
sl@0
   523
            if(distL < distR)
sl@0
   524
            {
sl@0
   525
                aStartPos = posL;
sl@0
   526
                return ETrue;
sl@0
   527
            }
sl@0
   528
            else if(distL > distR)
sl@0
   529
            {
sl@0
   530
                aStartPos = posR;
sl@0
   531
                return ETrue;
sl@0
   532
            }
sl@0
   533
            else
sl@0
   534
            {//-- distL == distR, take into account search priority
sl@0
   535
                aStartPos = aToLeft ? posL : posR;
sl@0
   536
                return ETrue;
sl@0
   537
            }
sl@0
   538
        }
sl@0
   539
        else //-- (!partLo && !partHi), nothing in the current word
sl@0
   540
        {
sl@0
   541
            ASSERT(0);
sl@0
   542
        }
sl@0
   543
sl@0
   544
    }// if(bitPos > 0 && bitPos < 31)
sl@0
   545
sl@0
   546
    //-- now we are processing separate words from both sides of the search position
sl@0
   547
    for(;;)
sl@0
   548
    { 
sl@0
   549
        TUint32 wL = ipData[l_Idx] ^ fInvert;
sl@0
   550
        TUint32 wR = ipData[r_Idx] ^ fInvert;
sl@0
   551
        if(r_Idx == iNumWords-1)
sl@0
   552
        {   //-- this is the last word in the array, mask unused high bits in the last word
sl@0
   553
            wR = MaskLastWord(wR);
sl@0
   554
        }
sl@0
   555
sl@0
   556
        if(wL && !wR)
sl@0
   557
        {
sl@0
   558
            aStartPos = (l_Idx << 5)+Log2(wL);
sl@0
   559
            return ETrue;
sl@0
   560
        }
sl@0
   561
        else if(!wL && wR)
sl@0
   562
        {
sl@0
   563
            aStartPos = (r_Idx << 5)+Log2( (wR & (~wR+1)) );
sl@0
   564
            return ETrue;
sl@0
   565
        }
sl@0
   566
        else if(wL && wR)
sl@0
   567
        {
sl@0
   568
            const TUint32 posL = (l_Idx << 5)+Log2(wL);
sl@0
   569
            const TUint32 posR = (r_Idx << 5)+Log2( (wR & (~wR+1)) );
sl@0
   570
        
sl@0
   571
            ASSERT(aStartPos > posL);
sl@0
   572
            ASSERT(posR > aStartPos);
sl@0
   573
            const TUint32 distL = aStartPos-posL;
sl@0
   574
            const TUint32 distR = posR-aStartPos;
sl@0
   575
sl@0
   576
            if(distL < distR)
sl@0
   577
            {
sl@0
   578
                aStartPos = posL;
sl@0
   579
                return ETrue;
sl@0
   580
            }
sl@0
   581
            else if(distL > distR)
sl@0
   582
            {
sl@0
   583
                aStartPos = posR;
sl@0
   584
                return ETrue;
sl@0
   585
            }
sl@0
   586
            else
sl@0
   587
            {//-- distL == distR, take into account search priority
sl@0
   588
                aStartPos = aToLeft ? posL : posR;
sl@0
   589
                return ETrue;
sl@0
   590
            }
sl@0
   591
sl@0
   592
        }//else if(wL && wR)
sl@0
   593
sl@0
   594
sl@0
   595
        if(noWayLeft)
sl@0
   596
        {
sl@0
   597
            aStartPos = r_Idx << 5;
sl@0
   598
            return FindToRight(aStartPos, aBitVal);
sl@0
   599
        }
sl@0
   600
        else
sl@0
   601
        {
sl@0
   602
            noWayLeft  = ItrLeft(l_Idx);
sl@0
   603
        }
sl@0
   604
sl@0
   605
        if(noWayRight)
sl@0
   606
        {
sl@0
   607
            aStartPos = l_Idx << 5;
sl@0
   608
            return FindToLeft(aStartPos, aBitVal);
sl@0
   609
        }
sl@0
   610
        else
sl@0
   611
        {    
sl@0
   612
            noWayRight = ItrRight(r_Idx);
sl@0
   613
        }
sl@0
   614
sl@0
   615
   }//for(;;)
sl@0
   616
sl@0
   617
    //return EFalse;
sl@0
   618
}
sl@0
   619
sl@0
   620
//-----------------------------------------------------------------------------
sl@0
   621
/**
sl@0
   622
    Find out if two vectors are different.
sl@0
   623
sl@0
   624
    @param  aRhs        vector to compare with
sl@0
   625
    @param  aDiffIndex  if there is a differene, here will be the number of the first different bit
sl@0
   626
    @return ETrue if vectors differ, EFalse, if they are identical.
sl@0
   627
*/
sl@0
   628
TBool RBitVector::Diff(const RBitVector& aRhs, TUint32& aDiffIndex) const
sl@0
   629
{
sl@0
   630
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   631
    __ASSERT_ALWAYS(iNumBits == aRhs.iNumBits, Panic(ESizeMismatch));
sl@0
   632
    ASSERT(iNumWords > 0);
sl@0
   633
sl@0
   634
    TUint32 diffWord=0;
sl@0
   635
    TUint32 wordNum=0;
sl@0
   636
sl@0
   637
    //-- compare all but the last word in the array
sl@0
   638
    for(wordNum=0; wordNum < iNumWords-1; ++wordNum)
sl@0
   639
    {
sl@0
   640
        diffWord = ipData[wordNum] ^ aRhs.ipData[wordNum];
sl@0
   641
        if(diffWord)
sl@0
   642
            break;  //-- found difference
sl@0
   643
    }
sl@0
   644
sl@0
   645
    //-- process the last word in the array
sl@0
   646
    if(!diffWord)
sl@0
   647
    {
sl@0
   648
        diffWord = MaskLastWord(ipData[wordNum]) ^ MaskLastWord(aRhs.ipData[wordNum]);
sl@0
   649
    }
sl@0
   650
sl@0
   651
    if(!diffWord)
sl@0
   652
        return EFalse; //-- vectors are the same
sl@0
   653
sl@0
   654
    //-- calculate the position of the bit that different.
sl@0
   655
    diffWord &= (~diffWord+1); //-- select rightmost bit
sl@0
   656
    aDiffIndex = (wordNum << 5)+Log2(diffWord);
sl@0
   657
    
sl@0
   658
    return ETrue;
sl@0
   659
}
sl@0
   660
sl@0
   661
//-----------------------------------------------------------------------------
sl@0
   662
sl@0
   663
/**
sl@0
   664
    Iterate to the left (towards lower index) in the array of words ipData
sl@0
   665
sl@0
   666
    @param  aIdx index within ipData array to be decremented; if it's possible to move left, it will be decreased
sl@0
   667
    @return ETrue if there is no way left i.e. aIdx is 0. EFalse otherwise and aIdx decreased.
sl@0
   668
*/
sl@0
   669
TBool RBitVector::ItrLeft(TUint32& aIdx) const
sl@0
   670
{
sl@0
   671
    if(aIdx == 0)
sl@0
   672
        return ETrue;
sl@0
   673
    else
sl@0
   674
    {
sl@0
   675
        aIdx--;
sl@0
   676
        return EFalse;
sl@0
   677
    }
sl@0
   678
}
sl@0
   679
sl@0
   680
//-----------------------------------------------------------------------------
sl@0
   681
sl@0
   682
/**
sl@0
   683
    Iterate to the right (towards higher index) in the array of words ipData
sl@0
   684
sl@0
   685
    @param  aIdx index within ipData array to be incremented; if it's possible to move right, it will be increased
sl@0
   686
    @return ETrue if there is no way right i.e. aIdx corresponds to the last word. EFalse otherwise and aIdx increased.
sl@0
   687
*/
sl@0
   688
TBool RBitVector::ItrRight(TUint32& aIdx) const
sl@0
   689
{
sl@0
   690
    if(aIdx < iNumWords-1)
sl@0
   691
    {
sl@0
   692
        aIdx++;
sl@0
   693
        return EFalse;
sl@0
   694
    }
sl@0
   695
    else
sl@0
   696
        return ETrue;
sl@0
   697
}
sl@0
   698
sl@0
   699
//-----------------------------------------------------------------------------
sl@0
   700
sl@0
   701
/**
sl@0
   702
    Import data to the internal bit vector representation.
sl@0
   703
    Just replaces number of bytes from apData to the ipData.
sl@0
   704
sl@0
   705
    @param aStartBit starting bit number. Must have 8-bit alignment.
sl@0
   706
    @param aNumBits  number of bits to import; granularity: 1 bit, i.e. it can be 177, for example.
sl@0
   707
    @param apData    pointer to the data (bitstream) to import.
sl@0
   708
sl@0
   709
*/
sl@0
   710
void RBitVector::DoImportData(TUint32 aStartBit, TUint32 aNumBits, const TAny* apData)
sl@0
   711
{
sl@0
   712
    ASSERT(aNumBits);
sl@0
   713
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   714
    
sl@0
   715
    //-- check parameters granularity. aStartBit must have 8-bit alignment
sl@0
   716
    __ASSERT_ALWAYS(!(aStartBit & 0x07), Panic(EDataAlignment));
sl@0
   717
sl@0
   718
sl@0
   719
    __ASSERT_ALWAYS(iNumWords && (aStartBit+aNumBits <= iNumBits), Panic(EIndexOutOfRange));
sl@0
   720
sl@0
   721
    const TUint     bitsTail = aNumBits & 0x07;
sl@0
   722
    const TUint32   nBytes = aNumBits >> 3;
sl@0
   723
 
sl@0
   724
    if(nBytes)
sl@0
   725
    {//-- copy full array of bytes
sl@0
   726
        const TUint32   startByte = aStartBit >> 3;
sl@0
   727
        Mem::Copy(((TUint8*)ipData) + startByte, apData, nBytes);
sl@0
   728
    }
sl@0
   729
sl@0
   730
    if(bitsTail)
sl@0
   731
    {//-- we need to copy trailing bits from the input data to the corresponding byte of the internal array
sl@0
   732
        const TUint8 mask   = (TUint8)(0xFF >> (8-bitsTail));
sl@0
   733
        const TUint8 orMask = (TUint8)( *((const TUint8*)apData + nBytes) & mask);
sl@0
   734
        const TUint8 andMask= (TUint8)~mask;
sl@0
   735
sl@0
   736
        TUint8* pbData = (TUint8*)ipData + nBytes;
sl@0
   737
        *pbData &= andMask;
sl@0
   738
        *pbData |= orMask;
sl@0
   739
    }
sl@0
   740
sl@0
   741
}
sl@0
   742
sl@0
   743
//-----------------------------------------------------------------------------
sl@0
   744
sl@0
   745
/**
sl@0
   746
    Export data from the internal bit vector buffer to the external one.
sl@0
   747
sl@0
   748
    @param aStartBit starting bit number. Must have 8-bit alignment.
sl@0
   749
    @param aNumBits  number of bits to export, must comprise the whole byte, i.e. be multiple of 8.
sl@0
   750
                     The client is responsible for masking extra bits it doesn't need. 
sl@0
   751
                     Another implication: e.g. if the bitvector consists of 3 bits, this value must be 8.
sl@0
   752
                     The value of bits 3-7 in the aData[0] will be undefined.
sl@0
   753
sl@0
   754
    @param aData     destination data descriptor   
sl@0
   755
*/
sl@0
   756
void  RBitVector::DoExportData(TUint32 aStartBit, TUint32 aNumBits, TDes8& aData) const
sl@0
   757
{
sl@0
   758
    ASSERT(aNumBits);
sl@0
   759
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   760
sl@0
   761
    //-- check parameters granularity.
sl@0
   762
    __ASSERT_ALWAYS(!(aStartBit & 0x07), Panic(EDataAlignment)); //-- aStartBit must have 8-bit alignment
sl@0
   763
    __ASSERT_ALWAYS(!(aNumBits & 0x07), Panic(EDataAlignment));  //-- number of bits shall comprise a byte
sl@0
   764
sl@0
   765
    __ASSERT_ALWAYS(iNumWords && (aStartBit+aNumBits <= (iNumWords << (KBitsInByteLog2+sizeof(TUint32))) ), Panic(EIndexOutOfRange));
sl@0
   766
sl@0
   767
    const TUint32 nBytes = aNumBits >> 3;
sl@0
   768
    const TUint32 startByte = aStartBit >> 3;
sl@0
   769
    
sl@0
   770
    aData.SetLength(nBytes);
sl@0
   771
    aData.Copy(((const TUint8*)ipData) + startByte, nBytes);
sl@0
   772
}
sl@0
   773
sl@0
   774
//-----------------------------------------------------------------------------
sl@0
   775
sl@0
   776
/**
sl@0
   777
    @return number of bits set to '1' in the vector
sl@0
   778
*/
sl@0
   779
TUint32 RBitVector::Num1Bits() const
sl@0
   780
{
sl@0
   781
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   782
    if(!iNumBits)
sl@0
   783
        return 0;
sl@0
   784
sl@0
   785
    TUint32 cntBits = 0;
sl@0
   786
sl@0
   787
    TUint32 wordNum;
sl@0
   788
    for(wordNum=0; wordNum < iNumWords-1; ++wordNum)
sl@0
   789
    {
sl@0
   790
        cntBits += Count1Bits(ipData[wordNum]);
sl@0
   791
    }
sl@0
   792
sl@0
   793
    //-- process the last word, it shall be masked
sl@0
   794
    cntBits += Count1Bits(MaskLastWord(ipData[wordNum]));
sl@0
   795
    
sl@0
   796
    return cntBits;
sl@0
   797
}
sl@0
   798
sl@0
   799
//-----------------------------------------------------------------------------
sl@0
   800
/**
sl@0
   801
    @return number of bits set to '0' in the vector
sl@0
   802
*/
sl@0
   803
TUint32 RBitVector::Num0Bits() const
sl@0
   804
{
sl@0
   805
    return iNumBits - Num1Bits();
sl@0
   806
}
sl@0
   807
sl@0
   808
sl@0
   809
//-----------------------------------------------------------------------------
sl@0
   810
/**
sl@0
   811
    Calculate number of '1' bits in the range from aIndexFrom to aIndexTo inclusively
sl@0
   812
sl@0
   813
    @param  aIndexFrom  starting index; bit[aIndexFrom] is included to the search
sl@0
   814
    @param  aIndexTo    ending index;   bit[aIndexTo] is included to the search
sl@0
   815
    @return number of bits set to '1' in the slice
sl@0
   816
*/
sl@0
   817
TUint32 RBitVector::Num1Bits(TUint32 aIndexFrom, TUint32 aIndexTo) const
sl@0
   818
{
sl@0
   819
    __ASSERT_ALWAYS(ipData, Panic(ENotInitialised));
sl@0
   820
    __ASSERT_ALWAYS(aIndexTo < iNumBits && aIndexFrom <= aIndexTo, Panic(EIndexOutOfRange));
sl@0
   821
sl@0
   822
    const TUint32 wordFrom = WordNum(aIndexFrom); //?const
sl@0
   823
    const TUint32 wordTo   = WordNum(aIndexTo);
sl@0
   824
sl@0
   825
    if(wordFrom == wordTo)
sl@0
   826
    {//-- the same word
sl@0
   827
        TUint32 word = ipData[wordFrom];
sl@0
   828
sl@0
   829
        const TUint32 shMaskR = BitInWord(aIndexFrom);
sl@0
   830
        word >>= shMaskR; word <<= shMaskR; //-- zero low bits
sl@0
   831
sl@0
   832
        const TUint32 shMaskL = 31-BitInWord(aIndexTo);
sl@0
   833
        word <<= shMaskL;  //-- zero high bits
sl@0
   834
sl@0
   835
        return Count1Bits(word);
sl@0
   836
    } 
sl@0
   837
sl@0
   838
    TUint32 bitsCnt = 0;
sl@0
   839
    TUint32 wordsBetween = wordTo - wordFrom - 1;
sl@0
   840
sl@0
   841
    //-- count '1' bits in the termial words
sl@0
   842
    TUint32 word = ipData[wordFrom];
sl@0
   843
    const TUint32 shMaskR = BitInWord(aIndexFrom);
sl@0
   844
    word >>= shMaskR; //-- zero low bits
sl@0
   845
    bitsCnt += Count1Bits(word);
sl@0
   846
sl@0
   847
    word = ipData[wordTo];
sl@0
   848
    const TUint32 shMaskL = 31-BitInWord(aIndexTo);
sl@0
   849
    word <<= shMaskL;  //-- zero high bits
sl@0
   850
    bitsCnt += Count1Bits(word);
sl@0
   851
sl@0
   852
    //-- count '1' bits in the words between terminal ones
sl@0
   853
    TUint32 wordIdx = wordFrom+1;
sl@0
   854
    while(wordsBetween--)
sl@0
   855
    {
sl@0
   856
        bitsCnt += Count1Bits(ipData[wordIdx]);
sl@0
   857
    }
sl@0
   858
    
sl@0
   859
sl@0
   860
    return bitsCnt;
sl@0
   861
}
sl@0
   862
sl@0
   863
sl@0
   864
sl@0
   865
sl@0
   866
sl@0
   867
sl@0
   868
sl@0
   869
sl@0
   870
sl@0
   871
sl@0
   872
sl@0
   873
sl@0
   874
sl@0
   875
sl@0
   876
sl@0
   877
sl@0
   878
sl@0
   879
sl@0
   880
sl@0
   881
sl@0
   882
sl@0
   883