Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-14 09:15:17

0001 // Created on: 2002-04-17
0002 // Created by: Alexander Kartomin (akm)
0003 // Copyright (c) 2002-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 // Purpose:     This is a base  class  for the  List, Set, Queue  and Stack
0017 //              collections. It offers operations on abstract lists (of the
0018 //              objects of class NCollection_ListNode).
0019 //              Apart from this class being  brand new (in TCollection said
0020 //              collections were independent, only using the same class for
0021 //              node representation),  here is an  important new  feature -
0022 //              the  list  length is  continuously updated,  so the  method
0023 //              Extent is quite quick.
0024 
0025 #ifndef NCollection_BaseList_HeaderFile
0026 #define NCollection_BaseList_HeaderFile
0027 
0028 #include <Standard_NoSuchObject.hxx>
0029 #include <NCollection_DefineAlloc.hxx>
0030 #include <NCollection_ListNode.hxx>
0031 
0032 #include <utility>
0033 
0034 typedef void (*NCollection_DelListNode)(NCollection_ListNode*,
0035                                         occ::handle<NCollection_BaseAllocator>& theAl);
0036 
0037 // ********************************************************** BaseList class
0038 class NCollection_BaseList
0039 {
0040 public:
0041   //! Memory allocation
0042   DEFINE_STANDARD_ALLOC
0043   DEFINE_NCOLLECTION_ALLOC
0044 
0045 public:
0046   class Iterator
0047   {
0048   public:
0049     // ******** Empty constructor
0050     Iterator() noexcept
0051         : myCurrent(nullptr),
0052           myPrevious(nullptr)
0053     {
0054     }
0055 
0056     // ******** Constructor with initialisation
0057     Iterator(const NCollection_BaseList& theList) noexcept
0058         : myCurrent(theList.myFirst),
0059           myPrevious(nullptr)
0060     {
0061     }
0062 
0063     // ******** Initialisation
0064     void Init(const NCollection_BaseList& theList) noexcept
0065     {
0066       myCurrent  = theList.myFirst;
0067       myPrevious = nullptr;
0068     }
0069 
0070     // ******** Initialisation
0071     void Initialize(const NCollection_BaseList& theList) noexcept { Init(theList); }
0072 
0073     // ******** More
0074     bool More() const noexcept { return (myCurrent != nullptr); }
0075 
0076     // ******** Comparison operator
0077     bool operator==(const Iterator& theIt) const noexcept { return myCurrent == theIt.myCurrent; }
0078 
0079     //! Performs comparison of two iterators
0080     bool IsEqual(const Iterator& theOther) const noexcept { return *this == theOther; }
0081 
0082   protected:
0083     void Init(const NCollection_BaseList& theList, NCollection_ListNode* const thePrev) noexcept
0084     {
0085       myCurrent  = thePrev ? thePrev->Next() : (NCollection_ListNode*)theList.PLast();
0086       myPrevious = thePrev;
0087     }
0088 
0089   public:
0090     NCollection_ListNode* myCurrent;  // Pointer to the current node
0091     NCollection_ListNode* myPrevious; // Pointer to the previous one
0092     friend class NCollection_BaseList;
0093   }; // End of nested class Iterator
0094 
0095 public:
0096   // ---------- PUBLIC METHODS ------------
0097   // ******** Extent
0098   // Purpose: Returns the number of nodes in the list
0099   int Extent() const noexcept { return static_cast<int>(myLength); }
0100 
0101   //! Length - number of nodes (legacy int-returning API, synonym of Extent()).
0102   int Length() const noexcept { return static_cast<int>(myLength); }
0103 
0104   //! Size - number of nodes.
0105   size_t Size() const noexcept { return myLength; }
0106 
0107   // ******** IsEmpty
0108   // Purpose: Query if the list is empty
0109   bool IsEmpty() const noexcept { return (myFirst == nullptr); }
0110 
0111   // ******** Allocator
0112   //! Returns attached allocator
0113   const occ::handle<NCollection_BaseAllocator>& Allocator() const noexcept { return myAllocator; }
0114 
0115   // ******** Destructor
0116   // Purpose: defines virtual interface
0117   virtual ~NCollection_BaseList() = default;
0118 
0119 protected:
0120   // --------- PROTECTED METHODS ----------
0121 
0122   // ******** Constructor
0123   // Purpose: Initializes an empty list
0124   NCollection_BaseList(const occ::handle<NCollection_BaseAllocator>& theAllocator = nullptr)
0125       : myFirst(nullptr),
0126         myLast(nullptr),
0127         myLength(0)
0128   {
0129     myAllocator =
0130       (theAllocator.IsNull() ? NCollection_BaseAllocator::CommonBaseAllocator() : theAllocator);
0131   }
0132 
0133   // ******** PClear
0134   // Purpose: deletes all nodes
0135   Standard_EXPORT void PClear(NCollection_DelListNode fDel);
0136 
0137   // ******** PFirst
0138   // Purpose: Returns pointer to the first node
0139   const NCollection_ListNode* PFirst() const noexcept { return myFirst; }
0140 
0141   // ******** PLast
0142   // Purpose: Returns pointer to the last node
0143   const NCollection_ListNode* PLast() const noexcept { return myLast; }
0144 
0145   // ******** PAppend
0146   // Purpose: Appends theNode at the end
0147   Standard_EXPORT void PAppend(NCollection_ListNode* theNode) noexcept;
0148 
0149   // ******** PAppend
0150   // Purpose: Appends theNode at the end, returns iterator to the previous
0151   void PAppend(NCollection_ListNode* theNode, Iterator& theIt) noexcept
0152   {
0153     NCollection_ListNode* aPrev = myLast;
0154     PAppend(theNode);
0155     theIt.Init(*this, aPrev);
0156   }
0157 
0158   // ******** PAppend
0159   // Purpose: Appends theOther list at the end (clearing it)
0160   Standard_EXPORT void PAppend(NCollection_BaseList& theOther) noexcept;
0161 
0162   // ******** PPrepend
0163   // Purpose: Prepends theNode at the beginning
0164   Standard_EXPORT void PPrepend(NCollection_ListNode* theNode) noexcept;
0165 
0166   // ******** PPrepend
0167   // Purpose: Prepends theOther list at the beginning (clearing it)
0168   Standard_EXPORT void PPrepend(NCollection_BaseList& theOther) noexcept;
0169 
0170   // ******** PRemoveFirst
0171   // Purpose: Removes first node
0172   Standard_EXPORT void PRemoveFirst(NCollection_DelListNode fDel);
0173 
0174   // ******** PRemove
0175   // Purpose: Removes the node pointed by theIter[ator]
0176   Standard_EXPORT void PRemove(Iterator& theIter, NCollection_DelListNode fDel);
0177 
0178   // ******** PInsertBefore
0179   // Purpose: Inserts theNode before one pointed by theIter[ator]
0180   Standard_EXPORT void PInsertBefore(NCollection_ListNode* theNode, Iterator& theIter);
0181 
0182   // ******** PInsertBefore
0183   // Purpose: Inserts theOther list before the node pointed by theIter[ator]
0184   Standard_EXPORT void PInsertBefore(NCollection_BaseList& theOther, Iterator& theIter);
0185 
0186   // ******** PInsertAfter
0187   // Purpose: Inserts theNode after one pointed by theIter[ator]
0188   Standard_EXPORT void PInsertAfter(NCollection_ListNode* theNode, Iterator& theIter);
0189 
0190   // ******** PInsertAfter
0191   // Purpose: Inserts theOther list after the node pointed by theIter[ator]
0192   Standard_EXPORT void PInsertAfter(NCollection_BaseList& theOther, Iterator& theIter);
0193 
0194   // ******** PReverse
0195   // Purpose: Reverse the list
0196   Standard_EXPORT void PReverse() noexcept;
0197 
0198   // ******** PExchange
0199   // Purpose: Exchange contents with another list.
0200   //          Swaps all internal state including allocators, ensuring that
0201   //          nodes are always deallocated by their original allocator.
0202   void PExchange(NCollection_BaseList& theOther) noexcept
0203   {
0204     std::swap(myAllocator, theOther.myAllocator);
0205     std::swap(myFirst, theOther.myFirst);
0206     std::swap(myLast, theOther.myLast);
0207     std::swap(myLength, theOther.myLength);
0208   }
0209 
0210 protected:
0211   // ------------ PROTECTED FIELDS ------------
0212   occ::handle<NCollection_BaseAllocator> myAllocator;
0213   NCollection_ListNode*                  myFirst;  // Pointer to the head
0214   NCollection_ListNode*                  myLast;   // Pointer to the tail
0215   size_t                                 myLength; // Actual length
0216 
0217   // ------------ FRIEND CLASSES ------------
0218   friend class Iterator;
0219 };
0220 
0221 #endif