First public contribution.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
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
18 // This is the internal file and must not be exported.
25 #if !defined(__FILESYSTEM_UTILS_BIT_VECTOR__)
26 #define __FILESYSTEM_UTILS_BIT_VECTOR__
28 #if !defined(__FILESYSTEM_UTILS_H__)
29 #include "filesystem_utils.h"
33 //#######################################################################################################################################
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
43 RBitVector(); //-- Creates an empty vector. see Create() methods for memory allocation
48 TInt Create(TUint32 aNumBits);
49 void CreateL(TUint32 aNumBits);
51 inline TUint32 Size() const;
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);
60 void Fill(TBool aVal);
61 void Fill(TUint32 aIndexFrom, TUint32 aIndexTo, TBool aVal);
65 TBool operator==(const RBitVector& aRhs) const;
66 TBool operator!=(const RBitVector& aRhs) const;
68 //-- logical operations between 2 vectors.
69 void And(const RBitVector& aRhs);
70 void Or (const RBitVector& aRhs);
71 void Xor(const RBitVector& aRhs);
73 TBool Diff(const RBitVector& aRhs, TUint32& aDiffIndex) const;
75 TUint32 Num1Bits() const;
76 TUint32 Num1Bits(TUint32 aIndexFrom, TUint32 aIndexTo) const;
78 TUint32 Num0Bits() const;
81 /** Bit search specifiers */
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
89 //-- N.B the current position the search starts with isn't included to the search.
92 TBool Find(TUint32& aStartPos, TBool aBitVal, TFindDirection aDir) const;
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
103 EDataAlignment, ///< wrong data alignment when importing / exporting raw data
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);
112 void* operator new(TUint); //-- disable creating objects on heap.
113 void* operator new(TUint, void*);
114 //-------------------------------------
117 void Panic(TPanicCode aPanicCode) const;
119 inline TUint32 WordNum(TUint32 aBitPos) const;
120 inline TUint32 BitInWord(TUint32 aBitPos) const;
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;
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;
133 inline TUint32 MaskLastWord(TUint32 aVal) const;
134 inline TBool ItrLeft(TUint32& aIdx) const;
135 inline TBool ItrRight(TUint32& aIdx) const;
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
146 //#######################################################################################################################################
147 //# inline functions area
148 //#######################################################################################################################################
151 //---------------------------------------------------------------------------------------------------------------------------------
152 //-- class RBitVector
154 /** @return size of the vector (number of bits) */
155 inline TUint32 RBitVector::Size() const
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
167 inline TBool RBitVector::operator[](TUint32 aIndex) const
169 __ASSERT_ALWAYS(aIndex < iNumBits, Panic(EIndexOutOfRange));
170 return (ipData[WordNum(aIndex)] & (1<<BitInWord(aIndex)));
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
178 inline void RBitVector::SetBit(TUint32 aIndex)
180 __ASSERT_ALWAYS(aIndex < iNumBits, Panic(EIndexOutOfRange));
181 ipData[WordNum(aIndex)] |= (1<<BitInWord(aIndex));
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
189 inline void RBitVector::ResetBit(TUint32 aIndex)
191 __ASSERT_ALWAYS(aIndex < iNumBits, Panic(EIndexOutOfRange));
192 ipData[WordNum(aIndex)] &= ~(1<<BitInWord(aIndex));
196 Invert a bit at pos aIndex
197 @param aIndex index in a bit vector
198 @panic EIndexOutOfRange if aIndex is out of range
200 inline void RBitVector::InvertBit(TUint32 aIndex)
202 __ASSERT_ALWAYS(aIndex < iNumBits, Panic(EIndexOutOfRange));
203 ipData[WordNum(aIndex)] ^= (1<<BitInWord(aIndex));
207 Set bit value at position aIndex
208 @param aIndex index in a bit vector
209 @panic EIndexOutOfRange if aIndex is out of range
211 inline void RBitVector::SetBitVal(TUint32 aIndex, TBool aVal)
220 inline TUint32 RBitVector::MaskLastWord(TUint32 aVal) const
222 const TUint32 shift = (32-(iNumBits & 0x1F)) & 0x1F;
223 return (aVal << shift) >> shift; //-- mask unused high bits
226 inline TUint32 RBitVector::WordNum(TUint32 aBitPos) const
231 inline TUint32 RBitVector::BitInWord(TUint32 aBitPos) const
233 return aBitPos & 0x1F;
238 #endif //__FILESYSTEM_UTILS_BIT_VECTOR__