1 // Created on: 2006-11-23
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2006-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef NCollection_SparseArray_HeaderFile
17 #define NCollection_SparseArray_HeaderFile
19 #include <NCollection_SparseArrayBase.hxx>
22 * Dynamically resizable sparse array of objects
24 * This class is similar to NCollection_Vector: it works like virtually
25 * unlimited array of items accessible by index; however unlike simple
26 * Vector it distinguishes items that have been set from the ones that
27 * have not been set explicitly.
29 * This class can be also seen as equivalence of
30 * NCollection_DataMap<Standard_Integer,TheItemType>
31 * with the only one practical difference: it can be much less
32 * memory-expensive if items are small (e.g. Integer or Handle).
34 * The index starts from 0, i.e. should be non-negative. Memory is allocated
35 * when item is set by SetValue().
37 * Iterator returns only defined items;
38 * the item can be tested for being defined by IsSet(),
39 * and undefined by UnsetValue().
41 * The attempt to access the item that has not been set will result
42 * in OutOfRange exception in Debug mode; in Release mode this will either
43 * return null-filled object or cause access violation.
46 template <class TheItemType> class NCollection_SparseArray
47 : public NCollection_SparseArrayBase
51 //! Constructor; accepts size of blocks
52 NCollection_SparseArray (Standard_Size theIncrement)
53 : NCollection_SparseArrayBase(sizeof(TheItemType),theIncrement)
57 //! Explicit assignment operator
58 NCollection_SparseArray& Assign (const NCollection_SparseArray& theOther)
60 this->assign (theOther);
64 //! Exchange the data of two arrays;
65 //! can be used primarily to move contents of theOther into the new array
66 //! in a fast way (without creation of duplicated data)
67 void Exchange (NCollection_SparseArray& theOther)
69 this->exchange (theOther);
73 virtual ~NCollection_SparseArray ()
79 //!@name Array-like interface (in addition to inherited methods)
82 //! Direct const access to the item
83 const TheItemType& Value (const Standard_Size theIndex) const
85 return *(const TheItemType*)this->getValue(theIndex);
88 //! Const access to the item - operator()
89 const TheItemType& operator () (const Standard_Size theIndex) const
91 return Value (theIndex);
94 //! Modification access to the item
95 TheItemType& ChangeValue (const Standard_Size theIndex)
97 return *(TheItemType*)(this->getValue (theIndex));
100 //! Access to the item - operator()
101 TheItemType& operator () (const Standard_Size theIndex)
103 return ChangeValue (theIndex);
106 //! Set a value at specified index method
107 TheItemType& SetValue (const Standard_Size theIndex,
108 const TheItemType& theValue)
110 return *(TheItemType*)this->setValue(theIndex, (Standard_Address)&theValue);
116 //!@name DataMap-like interface
119 //! Returns number of items in the array
120 Standard_Size Extent () const
125 //! Returns True if array is empty
126 Standard_Boolean IsEmpty () const
131 //! Direct const access to the item
132 const TheItemType& Find (const Standard_Size theIndex) const
134 return Value(theIndex);
137 //! Modification access to the item
138 TheItemType& ChangeFind (const Standard_Size theIndex)
140 return ChangeValue(theIndex);
143 //! Set a value as explicit method
144 TheItemType& Bind (const Standard_Size theIndex,
145 const TheItemType& theValue)
147 return SetValue(theIndex, theValue);
150 //! Returns True if the item is defined
151 Standard_Boolean IsBound (const Standard_Size theIndex) const
153 return this->HasValue(theIndex);
156 //! Remove the item from array
157 Standard_Boolean UnBind (const Standard_Size theIndex)
159 return this->UnsetValue(theIndex);
165 // Iterator interface
168 * Implementation of type-specific const Iterator class
170 class ConstIterator : public NCollection_SparseArrayBase::Iterator
174 //! Empty constructor - for later Init
177 //! Constructor with initialisation
178 ConstIterator (const NCollection_SparseArray& theVector) :
179 NCollection_SparseArrayBase::Iterator (&theVector) {}
182 void Init (const NCollection_SparseArray& theVector)
184 this->init (&theVector);
187 //! Constant value access
188 const TheItemType& Value (void) const
190 return *(const TheItemType*)this->value();
193 //! Constant value access operator
194 const TheItemType& operator () (void) const
196 return *(const TheItemType*)this->value();
199 //! Access current index with 'a-la map' interface
200 Standard_Size Key (void) const { return Index(); }
204 * Implementation of type-specific non-const Iterator class
206 class Iterator : public ConstIterator
210 //! Empty constructor - for later Init
213 //! Constructor with initialisation
214 Iterator (NCollection_SparseArray& theVector) :
215 ConstIterator (theVector) {}
218 void Init (const NCollection_SparseArray& theVector)
220 this->init (&theVector);
224 TheItemType& ChangeValue (void)
226 return *(TheItemType*)this->value();
229 //! Value access operator
230 TheItemType& operator () (void)
232 return *(TheItemType*)this->value();
235 //! Const access operator - the same as in parent class
236 const TheItemType& operator () (void) const
238 return *(const TheItemType*)this->value();
243 // Implementation of virtual methods providing type-specific behaviour
245 //! Create new item at the specified address with default constructor
246 // virtual void createItem (Standard_Address theAddress)
248 // new (theAddress) TheItemType;
251 //! Create new item at the specified address with copy constructor
252 //! from existing item
253 virtual void createItem (Standard_Address theAddress, Standard_Address theOther)
255 new (theAddress) TheItemType(*(const TheItemType*)theOther);
258 //! Call destructor to the item at given address
259 virtual void destroyItem (Standard_Address theAddress)
261 ((TheItemType*)theAddress)->TheItemType::~TheItemType();
264 //! Call assignment operator to the item
265 virtual void copyItem (Standard_Address theAddress, Standard_Address theOther)
267 (*(TheItemType*)theAddress) = *(const TheItemType*)theOther;