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