Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-04 08:45:52

0001 // Copyright (c) 2023 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef NCollection_IndexedIterator_HeaderFile
0015 #define NCollection_IndexedIterator_HeaderFile
0016 
0017 #include <Standard_Assert.hxx>
0018 #include <iterator>
0019 
0020 //! Helper class that allows to use NCollection iterators as STL iterators.
0021 //! NCollection iterator can be extended to STL iterator of any category by
0022 //! adding necessary methods: STL forward iterator requires IsEqual method,
0023 //! STL bidirectional iterator requires Previous method, and STL random access
0024 //! iterator requires Offset and Differ methods. See NCollection_Vector as
0025 //! example of declaring custom STL iterators.
0026 template <class Category, class BaseIndexedMap, class ItemType, bool IsConstant>
0027 class NCollection_IndexedIterator
0028 {
0029 public:
0030   // Since C++20 inheritance from std::iterator is deprecated, so define predefined types manually:
0031   using iterator_category = Category;
0032   using value_type        = ItemType;
0033   using difference_type   = ptrdiff_t;
0034   using pointer           = typename std::conditional<IsConstant, const ItemType*, ItemType*>::type;
0035   using reference         = typename std::conditional<IsConstant, const ItemType&, ItemType&>::type;
0036 
0037   //! Default constructor
0038   NCollection_IndexedIterator()
0039       : myIndex(0),
0040         myIndexedMap(nullptr)
0041   {
0042   }
0043 
0044   //! Constructor from NCollection_Indexed*Map
0045   NCollection_IndexedIterator(const BaseIndexedMap& theMap)
0046       : myIndex(0),
0047         myIndexedMap((&const_cast<BaseIndexedMap&>(theMap)))
0048   {
0049   }
0050 
0051   //! Constructor from NCollection_Indexed*Map
0052   NCollection_IndexedIterator(const size_t theIndex, const BaseIndexedMap& theMap)
0053       : myIndex(theIndex),
0054         myIndexedMap(&const_cast<BaseIndexedMap&>(theMap))
0055   {
0056   }
0057 
0058   //! Cast from non-const variant to const one
0059   NCollection_IndexedIterator(
0060     const NCollection_IndexedIterator<Category, BaseIndexedMap, ItemType, false>& theOther)
0061       : myIndex(theOther.myIndex),
0062         myIndexedMap(theOther.myIndexedMap)
0063   {
0064   }
0065 
0066   //! Assignment of non-const iterator to const one
0067   NCollection_IndexedIterator& operator=(
0068     const NCollection_IndexedIterator<Category, BaseIndexedMap, ItemType, false>& theOther)
0069   {
0070     myIndex      = theOther.myIndex;
0071     myIndexedMap = theOther.myIndexedMap;
0072     return *this;
0073   }
0074 
0075 protected: //! @name methods related to forward STL iterator
0076   // Note: Here we use SFINAE (Substitution failure is not an error) to choose
0077   // an appropriate method based on template arguments (at instantiation time).
0078 
0079   template <bool Condition>
0080   typename std::enable_if<!Condition, ItemType&>::type Reference() const
0081   {
0082     return myIndexedMap->at(myIndex);
0083   }
0084 
0085   template <bool Condition>
0086   typename std::enable_if<Condition, const ItemType&>::type Reference() const
0087   {
0088     return myIndexedMap->at(myIndex);
0089   }
0090 
0091 public: //! @name methods related to forward STL iterator
0092   //! Test for equality
0093   bool operator==(const NCollection_IndexedIterator& theOther) const
0094   {
0095     return myIndexedMap == theOther.myIndexedMap && myIndex == theOther.myIndex;
0096   }
0097 
0098   template <bool theOtherIsConstant>
0099   bool operator==(
0100     const NCollection_IndexedIterator<Category, BaseIndexedMap, ItemType, theOtherIsConstant>&
0101       theOther) const
0102   {
0103     return myIndexedMap == theOther.myIndexedMap && myIndex == theOther.myIndex;
0104   }
0105 
0106   template <bool theOtherIsConstant>
0107   bool operator!=(
0108     const NCollection_IndexedIterator<Category, BaseIndexedMap, ItemType, theOtherIsConstant>&
0109       theOther) const
0110   {
0111     return myIndexedMap != theOther.myIndexedMap || myIndex != theOther.myIndex;
0112   }
0113 
0114   //! Test for inequality
0115   bool operator!=(const NCollection_IndexedIterator& theOther) const
0116   {
0117     return !(*this == theOther);
0118   }
0119 
0120   //! Get reference to current item
0121   typename NCollection_IndexedIterator::reference operator*() const
0122   {
0123     return Reference<IsConstant>();
0124   }
0125 
0126   //! Dereferencing operator
0127   typename NCollection_IndexedIterator::pointer operator->() const
0128   {
0129     return &Reference<IsConstant>();
0130   }
0131 
0132   //! Prefix increment
0133   NCollection_IndexedIterator& operator++()
0134   {
0135     myIndex++;
0136     return *this;
0137   }
0138 
0139   //! Postfix increment
0140   NCollection_IndexedIterator operator++(int)
0141   {
0142     const NCollection_IndexedIterator theOld(*this);
0143     ++(*this);
0144     return theOld;
0145   }
0146 
0147 public: //! @name methods related to bidirectional STL iterator
0148   //! Prefix decrement
0149   NCollection_IndexedIterator& operator--()
0150   {
0151     Standard_STATIC_ASSERT(
0152       (opencascade::std::is_same<std::bidirectional_iterator_tag, Category>::value
0153        || opencascade::std::is_same<std::random_access_iterator_tag, Category>::value));
0154     myIndex--;
0155     return *this;
0156   }
0157 
0158   //! Postfix decrement
0159   NCollection_IndexedIterator operator--(int)
0160   {
0161     NCollection_IndexedIterator theOld(*this);
0162     --(*this);
0163     return theOld;
0164   }
0165 
0166 public: //! @name methods related to random access STL iterator
0167   //! Move forward
0168   NCollection_IndexedIterator& operator+=(
0169     typename NCollection_IndexedIterator::difference_type theOffset)
0170   {
0171     Standard_STATIC_ASSERT(
0172       (opencascade::std::is_same<std::random_access_iterator_tag, Category>::value));
0173     myIndex += theOffset;
0174     return *this;
0175   }
0176 
0177   //! Addition
0178   NCollection_IndexedIterator operator+(
0179     typename NCollection_IndexedIterator::difference_type theOffset) const
0180   {
0181     NCollection_IndexedIterator aTemp(*this);
0182     return aTemp += theOffset;
0183   }
0184 
0185   //! Move backward
0186   NCollection_IndexedIterator& operator-=(
0187     typename NCollection_IndexedIterator::difference_type theOffset)
0188   {
0189     return *this += -theOffset;
0190   }
0191 
0192   //! Decrease
0193   NCollection_IndexedIterator operator-(
0194     typename NCollection_IndexedIterator::difference_type theOffset) const
0195   {
0196     NCollection_IndexedIterator aTemp(*this);
0197     return aTemp += -theOffset;
0198   }
0199 
0200   //! Difference
0201   typename NCollection_IndexedIterator::difference_type operator-(
0202     const NCollection_IndexedIterator& theOther) const
0203   {
0204     Standard_STATIC_ASSERT(
0205       (opencascade::std::is_same<std::random_access_iterator_tag, Category>::value));
0206     return myIndex - theOther.myIndex;
0207   }
0208 
0209   //! Get item at offset from current
0210   typename NCollection_IndexedIterator::reference operator[](
0211     typename NCollection_IndexedIterator::difference_type theOffset) const
0212   {
0213     return *(*this + theOffset);
0214   }
0215 
0216   //! Comparison
0217   bool operator<(const NCollection_IndexedIterator& theOther) const
0218   {
0219     return (*this - theOther) < 0;
0220   }
0221 
0222   //! Comparison
0223   bool operator>(const NCollection_IndexedIterator& theOther) const { return theOther < *this; }
0224 
0225   //! Comparison
0226   bool operator<=(const NCollection_IndexedIterator& theOther) const { return !(theOther < *this); }
0227 
0228   //! Comparison
0229   bool operator>=(const NCollection_IndexedIterator& theOther) const { return !(*this < theOther); }
0230 
0231   friend class NCollection_IndexedIterator<Category, BaseIndexedMap, ItemType, !IsConstant>;
0232 
0233 private:
0234   //! NCollection iterator
0235   size_t          myIndex;
0236   BaseIndexedMap* myIndexedMap;
0237 };
0238 
0239 #endif // NCollection_IndexedIterator_HeaderFile