Warning, file /include/opencascade/NCollection_SparseArrayBase.hxx was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef NCollection_SparseArrayBase_HeaderFile
0017 #define NCollection_SparseArrayBase_HeaderFile
0018
0019 #include <Standard.hxx>
0020 #include <Standard_OutOfRange.hxx>
0021
0022 typedef size_t Standard_Size;
0023
0024
0025
0026
0027
0028
0029
0030 class NCollection_SparseArrayBase
0031 {
0032 public:
0033
0034
0035
0036
0037 Standard_EXPORT void Clear();
0038
0039
0040 Standard_Size Size() const { return mySize; }
0041
0042
0043 Standard_EXPORT Standard_Boolean HasValue(const Standard_Size theIndex) const;
0044
0045
0046
0047 Standard_EXPORT Standard_Boolean UnsetValue(const Standard_Size theIndex);
0048
0049
0050
0051 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)
0052 public:
0053 #else
0054 private:
0055 #endif
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069 class Block
0070 {
0071 public:
0072 typedef unsigned char Cell;
0073
0074
0075 static Standard_Size BitsPerCell() { return sizeof(Cell) * 8; }
0076
0077 public:
0078
0079 Block(const Standard_Address theAddr,
0080 const Standard_Size theNbItems,
0081 const Standard_Size theItemSize)
0082 : Count((Standard_Size*)theAddr),
0083 Array((char*)theAddr + sizeof(Standard_Size)),
0084 Bits((Cell*)((char*)theAddr + sizeof(Standard_Size) + theNbItems * theItemSize))
0085 {
0086 }
0087
0088
0089 static Standard_Size Size(const Standard_Size theNbItems, const Standard_Size theItemSize)
0090 {
0091 return sizeof(Standard_Size)
0092 + sizeof(Cell) * ((theNbItems + BitsPerCell() - 1) / BitsPerCell())
0093 + theNbItems * theItemSize;
0094 }
0095
0096
0097 static char* ToArray(const Standard_Address theAddress,
0098 const Standard_Size ,
0099 const Standard_Size )
0100 {
0101 return (char*)theAddress + sizeof(Standard_Size);
0102 }
0103
0104 public:
0105
0106
0107 Cell Set(Standard_Size i)
0108 {
0109 Cell* abyte = Bits + i / BitsPerCell();
0110 Cell amask = (Cell)('\1' << (i % BitsPerCell()));
0111 Cell anold = (Cell)(*abyte & amask);
0112 *abyte = (Cell)(*abyte | amask);
0113 return !anold;
0114 }
0115
0116
0117 Cell IsSet(Standard_Size i)
0118 {
0119 Cell* abyte = Bits + i / BitsPerCell();
0120 Cell amask = (Cell)('\1' << (i % BitsPerCell()));
0121 return (Cell)(*abyte & amask);
0122 }
0123
0124
0125
0126 Cell Unset(Standard_Size i)
0127 {
0128 Cell* abyte = Bits + i / BitsPerCell();
0129 Cell amask = (Cell)('\1' << (i % BitsPerCell()));
0130 Cell anold = (Cell)(*abyte & amask);
0131 *abyte = (Cell)(*abyte & ~amask);
0132 return anold;
0133 }
0134
0135 public:
0136 Standard_Size* Count;
0137 Standard_Address Array;
0138 Cell* Bits;
0139 };
0140
0141 public:
0142
0143
0144
0145
0146 class Iterator
0147 {
0148 public:
0149
0150
0151
0152 void Restart() { init(myArr); }
0153
0154
0155 Standard_Boolean More() const { return myHasMore; }
0156
0157
0158 Standard_EXPORT void Next();
0159
0160
0161 Standard_Size Index() const { return myIBlock * myArr->myBlockSize + myInd; }
0162
0163 protected:
0164
0165
0166
0167 Standard_EXPORT Iterator(const NCollection_SparseArrayBase* theArray = 0);
0168
0169
0170 Standard_EXPORT void init(const NCollection_SparseArrayBase* theArray);
0171
0172
0173 Standard_Address value() const { return myArr->getItem(myBlock, myInd); }
0174
0175 private:
0176 const NCollection_SparseArrayBase* myArr;
0177 Standard_Boolean myHasMore;
0178 Standard_Size myIBlock;
0179 Standard_Size myInd;
0180 Block myBlock;
0181 };
0182 friend class Iterator;
0183
0184 private:
0185
0186 NCollection_SparseArrayBase(const NCollection_SparseArrayBase&);
0187 void operator=(const NCollection_SparseArrayBase&);
0188
0189 protected:
0190
0191
0192
0193 NCollection_SparseArrayBase(Standard_Size theItemSize, Standard_Size theBlockSize)
0194 : myItemSize(theItemSize),
0195 myBlockSize(theBlockSize),
0196 myNbBlocks(0),
0197 mySize(0),
0198 myData(0)
0199 {
0200 }
0201
0202
0203 virtual ~NCollection_SparseArrayBase() { Clear(); }
0204
0205 protected:
0206
0207
0208
0209 Block getBlock(const Standard_Address theAddr) const
0210 {
0211 return Block(theAddr, myBlockSize, myItemSize);
0212 }
0213
0214
0215 Standard_Address getItem(const Block& theBlock, Standard_Size theInd) const
0216 {
0217 return ((char*)theBlock.Array) + myItemSize * theInd;
0218 }
0219
0220
0221 Standard_Address getValue(const Standard_Size theIndex) const
0222 {
0223 Standard_OutOfRange_Raise_if(
0224 !HasValue(theIndex),
0225 "NCollection_SparseArray::Value()") return Block::ToArray(myData[theIndex / myBlockSize],
0226 myBlockSize,
0227 myItemSize)
0228 + myItemSize * (theIndex % myBlockSize);
0229 }
0230
0231
0232 Standard_EXPORT Standard_Address setValue(const Standard_Size theIndex,
0233 const Standard_Address theValue);
0234
0235
0236
0237 Standard_EXPORT void assign(const NCollection_SparseArrayBase& theOther);
0238
0239
0240
0241 Standard_EXPORT void exchange(NCollection_SparseArrayBase& theOther);
0242
0243 protected:
0244
0245
0246
0247
0248
0249
0250
0251 virtual void createItem(Standard_Address theAddress, Standard_Address theOther) = 0;
0252
0253
0254 virtual void destroyItem(Standard_Address theAddress) = 0;
0255
0256
0257 virtual void copyItem(Standard_Address theAddress, Standard_Address theOther) = 0;
0258
0259 private:
0260
0261
0262
0263 void allocData(const Standard_Size iBlock);
0264
0265
0266 void freeBlock(const Standard_Size iBlock);
0267
0268 protected:
0269 Standard_Size myItemSize;
0270 Standard_Size myBlockSize;
0271 Standard_Size myNbBlocks;
0272 Standard_Size mySize;
0273 Standard_Address* myData;
0274 };
0275
0276 #endif