Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:20

0001 // Created on: 2006-11-23
0002 // Created by: Andrey BETENEV
0003 // Copyright (c) 2006-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef NCollection_SparseArray_HeaderFile
0017 #define NCollection_SparseArray_HeaderFile
0018 
0019 #include <NCollection_SparseArrayBase.hxx>
0020 
0021 /**
0022 * Dynamically resizable sparse array of objects
0023 *
0024 * This class is similar to NCollection_Vector: it works like virtually
0025 * unlimited array of items accessible by index; however unlike simple
0026 * Vector it distinguishes items that have been set from the ones that
0027 * have not been set explicitly.
0028 *
0029 * This class can be also seen as equivalence of
0030 * NCollection_DataMap<Standard_Integer,TheItemType>
0031 * with the only one practical difference: it can be much less 
0032 * memory-expensive if items are small (e.g. Integer or Handle).
0033 * 
0034 * The index starts from 0, i.e. should be non-negative. Memory is allocated
0035 * when item is set by SetValue(). 
0036 *
0037 * Iterator returns only defined items; 
0038 * the item can be tested for being defined by IsSet(), 
0039 * and undefined by UnsetValue().
0040 *
0041 * The attempt to access the item that has not been set will result
0042 * in OutOfRange exception in Debug mode; in Release mode this will either
0043 * return null-filled object or cause access violation.
0044 */
0045 
0046 template <class TheItemType> class NCollection_SparseArray 
0047                                  : public NCollection_SparseArrayBase
0048 {
0049 public:
0050 
0051   //! Constructor; accepts size of blocks 
0052   explicit NCollection_SparseArray (Standard_Size theIncrement)
0053     : NCollection_SparseArrayBase(sizeof(TheItemType),theIncrement)
0054   { 
0055   }
0056 
0057   //! Explicit assignment operator
0058   NCollection_SparseArray& Assign (const NCollection_SparseArray& theOther) 
0059   {
0060     this->assign (theOther);
0061     return *this;
0062   }
0063 
0064   //! Exchange the data of two arrays;
0065   //! can be used primarily to move contents of theOther into the new array
0066   //! in a fast way (without creation of duplicated data)
0067   void Exchange (NCollection_SparseArray& theOther) 
0068   {
0069     this->exchange (theOther);
0070   }
0071 
0072   //! Destructor
0073   virtual ~NCollection_SparseArray ()
0074   {
0075     Clear();
0076   }
0077 
0078 public:
0079   //!@name Array-like interface (in addition to inherited methods)
0080   //!@{
0081 
0082   //! Direct const access to the item 
0083   const TheItemType& Value (const Standard_Size theIndex) const 
0084   {
0085     return *(const TheItemType*)this->getValue(theIndex);
0086   }
0087 
0088   //! Const access to the item - operator()
0089   const TheItemType& operator () (const Standard_Size theIndex) const
0090   { 
0091     return Value (theIndex); 
0092   }
0093 
0094   //! Modification access to the item
0095   TheItemType& ChangeValue (const Standard_Size theIndex) 
0096   {
0097     return *(TheItemType*)(this->getValue (theIndex));
0098   }
0099 
0100   //! Access to the item - operator()
0101   TheItemType& operator () (const Standard_Size theIndex)
0102   { 
0103     return ChangeValue (theIndex); 
0104   }
0105   
0106   //! Set a value at specified index method
0107   TheItemType& SetValue (const Standard_Size theIndex,
0108                          const TheItemType&     theValue) 
0109   {
0110     return *(TheItemType*)this->setValue(theIndex, (Standard_Address)&theValue);
0111   }
0112 
0113   //!@}
0114   
0115 public:
0116   //!@name DataMap-like interface
0117   //!@{
0118 
0119   //! Returns number of items in the array
0120   Standard_Size Extent () const 
0121   {
0122     return Size();
0123   }
0124 
0125   //! Returns True if array is empty
0126   Standard_Boolean IsEmpty () const 
0127   {
0128     return Size() == 0;
0129   }
0130 
0131   //! Direct const access to the item 
0132   const TheItemType& Find (const Standard_Size theIndex) const 
0133   {
0134     return Value(theIndex);
0135   }
0136 
0137   //! Modification access to the item
0138   TheItemType& ChangeFind (const Standard_Size theIndex) 
0139   {
0140     return ChangeValue(theIndex);
0141   }
0142 
0143   //! Set a value as explicit method
0144   TheItemType& Bind (const Standard_Size theIndex,
0145              const TheItemType&     theValue) 
0146   {
0147     return SetValue(theIndex, theValue);
0148   }
0149   
0150   //! Returns True if the item is defined
0151   Standard_Boolean IsBound (const Standard_Size theIndex) const
0152   {
0153     return this->HasValue(theIndex);
0154   }
0155   
0156   //! Remove the item from array
0157   Standard_Boolean UnBind (const Standard_Size theIndex) 
0158   {
0159     return this->UnsetValue(theIndex);
0160   }
0161   
0162   //!@}
0163 
0164 public:
0165   // Iterator interface
0166 
0167   /**
0168    * Implementation of type-specific const Iterator class
0169    */
0170   class ConstIterator : public NCollection_SparseArrayBase::Iterator
0171   {
0172   public:
0173 
0174     //! Empty constructor - for later Init
0175     ConstIterator () {}
0176 
0177     //! Constructor with initialisation
0178     ConstIterator (const NCollection_SparseArray& theVector) :
0179       NCollection_SparseArrayBase::Iterator (&theVector) {}
0180 
0181     //! Initialisation
0182     void Init (const NCollection_SparseArray& theVector) 
0183     { 
0184       this->init (&theVector); 
0185     } 
0186 
0187     //! Constant value access
0188     const TheItemType& Value (void) const
0189     {
0190       return *(const TheItemType*)this->value(); 
0191     }
0192 
0193     //! Constant value access operator
0194     const TheItemType& operator () (void) const
0195     {
0196       return *(const TheItemType*)this->value(); 
0197     }
0198 
0199     //! Access current index with 'a-la map' interface
0200     Standard_Size Key (void) const { return Index(); }
0201   };
0202 
0203   /**
0204    * Implementation of type-specific non-const Iterator class
0205    */
0206   class Iterator : public ConstIterator
0207   {
0208   public:
0209 
0210     //! Empty constructor - for later Init
0211     Iterator () {}
0212 
0213     //! Constructor with initialisation
0214     Iterator (NCollection_SparseArray& theVector) :
0215       ConstIterator (theVector) {}
0216 
0217     //! Initialisation
0218     void Init (const NCollection_SparseArray& theVector) 
0219     { 
0220       this->init (&theVector); 
0221     } 
0222 
0223     //! Value access
0224     TheItemType& ChangeValue (void)
0225     {
0226       return *(TheItemType*)this->value(); 
0227     }
0228 
0229     //! Value access operator
0230     TheItemType& operator () (void)
0231     {
0232       return *(TheItemType*)this->value(); 
0233     }
0234 
0235     //! Const access operator - the same as in parent class
0236     const TheItemType& operator () (void) const
0237     {
0238       return *(const TheItemType*)this->value(); 
0239     }
0240   };
0241 
0242 private:
0243   // Implementation of virtual methods providing type-specific behaviour
0244 
0245   //! Create new item at the specified address with default constructor
0246 //  virtual void createItem (Standard_Address theAddress) 
0247 //  {
0248 //    new (theAddress) TheItemType;
0249 //  }
0250   
0251   //! Create new item at the specified address with copy constructor
0252   //! from existing item
0253   virtual void createItem (Standard_Address theAddress, Standard_Address theOther)
0254   {
0255     new (theAddress) TheItemType(*(const TheItemType*)theOther);
0256   }
0257   
0258   //! Call destructor to the item at given address
0259   virtual void destroyItem (Standard_Address theAddress)
0260   {
0261     ((TheItemType*)theAddress)->TheItemType::~TheItemType();
0262   }
0263 
0264   //! Call assignment operator to the item
0265   virtual void copyItem (Standard_Address theAddress, Standard_Address theOther)
0266   {
0267     (*(TheItemType*)theAddress) = *(const TheItemType*)theOther;
0268   }
0269 
0270 };
0271 
0272 #endif
0273