os/kernelhwsrv/userlibandfileserver/fileserver/fs_utils/bit_vector.h
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) 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
#if !defined(__FILESYSTEM_UTILS_BIT_VECTOR__)
sl@0
    26
#define __FILESYSTEM_UTILS_BIT_VECTOR__
sl@0
    27
sl@0
    28
#if !defined(__FILESYSTEM_UTILS_H__)
sl@0
    29
#include "filesystem_utils.h"
sl@0
    30
#endif
sl@0
    31
sl@0
    32
sl@0
    33
//#######################################################################################################################################
sl@0
    34
sl@0
    35
/**
sl@0
    36
    This class represents a bit vector i.e. an array of bits. Vector size can be from 1 to 2^32 bits.
sl@0
    37
    This class can be created on a stack (but needs to be placed into cleanup stack) or in a heap with the help of its factory methods Create/CreateL
sl@0
    38
*/
sl@0
    39
class RBitVector
sl@0
    40
    {
sl@0
    41
 public:
sl@0
    42
    
sl@0
    43
    RBitVector(); //-- Creates an empty vector. see Create() methods for memory allocation
sl@0
    44
   ~RBitVector(); 
sl@0
    45
    
sl@0
    46
    void Close(); 
sl@0
    47
    
sl@0
    48
    TInt Create(TUint32 aNumBits);
sl@0
    49
    void CreateL(TUint32 aNumBits);
sl@0
    50
sl@0
    51
    inline TUint32 Size() const;
sl@0
    52
sl@0
    53
    //-- single bit manipulation methods
sl@0
    54
    inline TBool operator[](TUint32 aIndex) const;
sl@0
    55
    inline void SetBit(TUint32 aIndex);
sl@0
    56
    inline void ResetBit(TUint32 aIndex);
sl@0
    57
    inline void InvertBit(TUint32 aIndex);
sl@0
    58
    inline void SetBitVal(TUint32 aIndex, TBool aVal);
sl@0
    59
    
sl@0
    60
    void Fill(TBool aVal);
sl@0
    61
    void Fill(TUint32 aIndexFrom, TUint32 aIndexTo, TBool aVal);
sl@0
    62
sl@0
    63
    void Invert();
sl@0
    64
   
sl@0
    65
    TBool operator==(const RBitVector& aRhs) const; 
sl@0
    66
    TBool operator!=(const RBitVector& aRhs) const;
sl@0
    67
sl@0
    68
    //-- logical operations between 2 vectors. 
sl@0
    69
    void And(const RBitVector& aRhs);
sl@0
    70
    void Or (const RBitVector& aRhs);
sl@0
    71
    void Xor(const RBitVector& aRhs);
sl@0
    72
sl@0
    73
    TBool Diff(const RBitVector& aRhs, TUint32& aDiffIndex) const;
sl@0
    74
    
sl@0
    75
    TUint32 Num1Bits() const;
sl@0
    76
    TUint32 Num1Bits(TUint32 aIndexFrom, TUint32 aIndexTo) const;
sl@0
    77
sl@0
    78
    TUint32 Num0Bits() const;
sl@0
    79
sl@0
    80
sl@0
    81
    /** Bit search specifiers */
sl@0
    82
    enum TFindDirection
sl@0
    83
        {
sl@0
    84
        ELeft,      ///< Search from the given position to the left (towards lower index)
sl@0
    85
        ERight,     ///< Search from the given position to the right (towards higher index)
sl@0
    86
        ENearestL,  ///< Search in both directions starting from the given position; in the case of the equal distances return the position to the left
sl@0
    87
        ENearestR   ///< Search in both directions starting from the given position; in the case of the equal distances return the position to the right
sl@0
    88
sl@0
    89
        //-- N.B the current position the search starts with isn't included to the search.
sl@0
    90
        };
sl@0
    91
sl@0
    92
    TBool Find(TUint32& aStartPos, TBool aBitVal, TFindDirection aDir) const;
sl@0
    93
sl@0
    94
    /** panic codes */
sl@0
    95
    enum TPanicCode
sl@0
    96
        {
sl@0
    97
        EIndexOutOfRange,       ///< index out of range
sl@0
    98
        EWrondFindDirection,    ///< a value doesn't belong to TFindDirection
sl@0
    99
        ESizeMismatch,          ///< Size mismatch for binary operators
sl@0
   100
        ENotInitialised,        ///< No memory allocated for the array
sl@0
   101
        ENotImplemented,        ///< functionality isn't implemented
sl@0
   102
sl@0
   103
        EDataAlignment,         ///< wrong data alignment when importing / exporting raw data
sl@0
   104
        };
sl@0
   105
sl@0
   106
 protected:
sl@0
   107
    
sl@0
   108
    //-- these are outlawed. Can't use them because memory allocator can leave and we don't have conthrol on it  in these methods. 
sl@0
   109
    RBitVector(const RBitVector& aRhs);            
sl@0
   110
    RBitVector& operator=(const RBitVector& aRhs); 
sl@0
   111
sl@0
   112
    void* operator new(TUint); //-- disable creating objects on heap.
sl@0
   113
    void* operator new(TUint, void*);
sl@0
   114
    //-------------------------------------
sl@0
   115
sl@0
   116
  
sl@0
   117
    void Panic(TPanicCode aPanicCode) const;
sl@0
   118
sl@0
   119
    inline TUint32 WordNum(TUint32 aBitPos)  const;
sl@0
   120
    inline TUint32 BitInWord(TUint32 aBitPos) const;
sl@0
   121
sl@0
   122
 protected:
sl@0
   123
    //-- special interface to acecess raw internal data. It's protected. Derive appropriate class from this one if you wan to use it
sl@0
   124
    void  DoImportData(TUint32 aStartBit, TUint32 aNumBits, const TAny* apData);
sl@0
   125
    void  DoExportData(TUint32 aStartBit, TUint32 aNumBits, TDes8& aData) const;
sl@0
   126
sl@0
   127
sl@0
   128
 private:
sl@0
   129
    TBool FindToRight(TUint32& aStartPos, TBool aBitVal) const;
sl@0
   130
    TBool FindToLeft (TUint32& aStartPos, TBool aBitVal) const;
sl@0
   131
    TBool FindNearest(TUint32& aStartPos, TBool aBitVal, TBool aToLeft) const;
sl@0
   132
   
sl@0
   133
    inline TUint32 MaskLastWord(TUint32 aVal) const; 
sl@0
   134
    inline TBool ItrLeft(TUint32& aIdx) const;
sl@0
   135
    inline TBool ItrRight(TUint32& aIdx) const;
sl@0
   136
sl@0
   137
sl@0
   138
 protected:
sl@0
   139
sl@0
   140
    TUint32   iNumBits; ///< number of bits in the vector
sl@0
   141
    TUint32*  ipData;   ///< pointer to the data 
sl@0
   142
    TUint32   iNumWords;///< number of 32-bit words that store bits
sl@0
   143
    };
sl@0
   144
sl@0
   145
sl@0
   146
//#######################################################################################################################################
sl@0
   147
//#   inline functions area
sl@0
   148
//#######################################################################################################################################
sl@0
   149
sl@0
   150
sl@0
   151
//--------------------------------------------------------------------------------------------------------------------------------- 
sl@0
   152
//-- class RBitVector
sl@0
   153
sl@0
   154
/** @return size of the vector (number of bits) */
sl@0
   155
inline TUint32 RBitVector::Size() const
sl@0
   156
    {
sl@0
   157
    return iNumBits;
sl@0
   158
    } 
sl@0
   159
sl@0
   160
/**
sl@0
   161
    Get a bit by index
sl@0
   162
    
sl@0
   163
    @param aIndex  index in a bit vector
sl@0
   164
    @return 0 if the bit at pos aIndex is 0, not zero otherwise
sl@0
   165
    @panic EIndexOutOfRange if aIndex is out of range
sl@0
   166
*/
sl@0
   167
inline TBool RBitVector::operator[](TUint32 aIndex) const
sl@0
   168
    {
sl@0
   169
    __ASSERT_ALWAYS(aIndex < iNumBits, Panic(EIndexOutOfRange));
sl@0
   170
    return (ipData[WordNum(aIndex)] & (1<<BitInWord(aIndex)));
sl@0
   171
    }
sl@0
   172
sl@0
   173
/**
sl@0
   174
    Set a bit at pos aIndex to '1'
sl@0
   175
    @param aIndex  index in a bit vector
sl@0
   176
    @panic EIndexOutOfRange if aIndex is out of range
sl@0
   177
*/
sl@0
   178
inline void RBitVector::SetBit(TUint32 aIndex)
sl@0
   179
    {
sl@0
   180
    __ASSERT_ALWAYS(aIndex < iNumBits, Panic(EIndexOutOfRange));
sl@0
   181
    ipData[WordNum(aIndex)] |= (1<<BitInWord(aIndex));
sl@0
   182
    }
sl@0
   183
sl@0
   184
/**
sl@0
   185
    Set a bit at pos aIndex to '0'
sl@0
   186
    @param aIndex  index in a bit vector
sl@0
   187
    @panic EIndexOutOfRange if aIndex is out of range
sl@0
   188
*/
sl@0
   189
inline void RBitVector::ResetBit(TUint32 aIndex)
sl@0
   190
    {
sl@0
   191
    __ASSERT_ALWAYS(aIndex < iNumBits, Panic(EIndexOutOfRange));
sl@0
   192
    ipData[WordNum(aIndex)] &= ~(1<<BitInWord(aIndex));
sl@0
   193
    }
sl@0
   194
sl@0
   195
/**
sl@0
   196
    Invert a bit at pos aIndex
sl@0
   197
    @param aIndex  index in a bit vector
sl@0
   198
    @panic EIndexOutOfRange if aIndex is out of range
sl@0
   199
*/
sl@0
   200
inline void RBitVector::InvertBit(TUint32 aIndex)
sl@0
   201
    {
sl@0
   202
    __ASSERT_ALWAYS(aIndex < iNumBits, Panic(EIndexOutOfRange));
sl@0
   203
    ipData[WordNum(aIndex)] ^= (1<<BitInWord(aIndex));
sl@0
   204
    }
sl@0
   205
sl@0
   206
/**
sl@0
   207
    Set bit value at position aIndex
sl@0
   208
    @param aIndex  index in a bit vector
sl@0
   209
    @panic EIndexOutOfRange if aIndex is out of range
sl@0
   210
*/
sl@0
   211
inline void RBitVector::SetBitVal(TUint32 aIndex, TBool aVal)
sl@0
   212
    {
sl@0
   213
    if(aVal) 
sl@0
   214
        SetBit(aIndex);
sl@0
   215
    else 
sl@0
   216
        ResetBit(aIndex);
sl@0
   217
    }
sl@0
   218
sl@0
   219
sl@0
   220
inline TUint32 RBitVector::MaskLastWord(TUint32 aVal) const
sl@0
   221
    {
sl@0
   222
    const TUint32 shift = (32-(iNumBits & 0x1F)) & 0x1F;
sl@0
   223
    return (aVal << shift) >> shift; //-- mask unused high bits
sl@0
   224
    }
sl@0
   225
sl@0
   226
inline TUint32 RBitVector::WordNum(TUint32 aBitPos)  const
sl@0
   227
    {
sl@0
   228
    return aBitPos >> 5;
sl@0
   229
    }
sl@0
   230
sl@0
   231
inline TUint32 RBitVector::BitInWord(TUint32 aBitPos) const 
sl@0
   232
    {
sl@0
   233
    return aBitPos & 0x1F;
sl@0
   234
    }
sl@0
   235
sl@0
   236
sl@0
   237
sl@0
   238
#endif //__FILESYSTEM_UTILS_BIT_VECTOR__
sl@0
   239
sl@0
   240
sl@0
   241
sl@0
   242
sl@0
   243
sl@0
   244
sl@0
   245
sl@0
   246
sl@0
   247
sl@0
   248
sl@0
   249
sl@0
   250
sl@0
   251