1 // Created on: 2002-04-10 2 // Created by: Alexander KARTOMIN (akm) 3 // Copyright (c) 2002-2014 OPEN CASCADE SAS 4 // 5 // This file is part of Open CASCADE Technology software library. 6 // 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. 12 // 13 // Alternatively, this file may be used under the terms of Open CASCADE 14 // commercial license or contractual agreement. 15 16 #ifndef NCollection_BaseSequence_HeaderFile 17 #define NCollection_BaseSequence_HeaderFile 18 19 #include <Standard.hxx> 20 #include <NCollection_BaseAllocator.hxx> 21 #include <NCollection_DefineAlloc.hxx> 22 23 // **************************************** Class SeqNode ******************** 24 25 class NCollection_SeqNode 26 { 27 public: 28 // define new operator for use with NCollection allocators 29 DEFINE_NCOLLECTION_ALLOC 30 public: NCollection_SeqNode()31 NCollection_SeqNode () : myNext (NULL), myPrevious (NULL) {} Next() const32 NCollection_SeqNode * Next () const { return myNext; } Previous() const33 NCollection_SeqNode * Previous () const { return myPrevious; } SetNext(NCollection_SeqNode * theNext)34 void SetNext (NCollection_SeqNode * theNext) { myNext = theNext; } SetPrevious(NCollection_SeqNode * thePrev)35 void SetPrevious (NCollection_SeqNode * thePrev) { myPrevious = thePrev; } 36 37 private: 38 NCollection_SeqNode* myNext; 39 NCollection_SeqNode* myPrevious; 40 }; 41 42 typedef void (* NCollection_DelSeqNode) 43 (NCollection_SeqNode*, Handle(NCollection_BaseAllocator)& theAl); 44 45 /** 46 * Purpose: This is a base class for the Sequence. It deals with 47 * an indexed bidirectional list of NCollection_SeqNode's. 48 */ 49 class NCollection_BaseSequence 50 { 51 public: 52 //! Memory allocation 53 DEFINE_STANDARD_ALLOC 54 DEFINE_NCOLLECTION_ALLOC 55 56 public: 57 class Iterator 58 { 59 public: 60 //! Empty constructor Iterator(void)61 Iterator (void) : myCurrent (NULL), myPrevious(NULL) {} 62 63 //! Constructor with initialisation Iterator(const NCollection_BaseSequence & theSeq,const Standard_Boolean isStart)64 Iterator (const NCollection_BaseSequence& theSeq, 65 const Standard_Boolean isStart) 66 { 67 Init (theSeq, isStart); 68 } 69 70 //! Initialisation Init(const NCollection_BaseSequence & theSeq,const Standard_Boolean isStart=Standard_True)71 void Init (const NCollection_BaseSequence& theSeq, 72 const Standard_Boolean isStart = Standard_True) 73 { 74 myCurrent = (isStart ? theSeq.myFirstItem : NULL); 75 myPrevious = (isStart ? NULL : theSeq.myLastItem); 76 } 77 78 //! Assignment operator =(const Iterator & theOther)79 Iterator& operator = (const Iterator& theOther) 80 { 81 myCurrent = theOther.myCurrent; 82 myPrevious = theOther.myPrevious; 83 return *this; 84 } 85 //! Switch to previous element; note that it will reset Previous()86 void Previous() 87 { 88 myCurrent = myPrevious; 89 if (myCurrent) 90 myPrevious = myCurrent->Previous(); 91 } 92 93 protected: 94 NCollection_SeqNode* myCurrent; //!< Pointer to the current node 95 NCollection_SeqNode* myPrevious; //!< Pointer to the previous node 96 friend class NCollection_BaseSequence; 97 }; 98 99 public: 100 // Methods PUBLIC 101 // IsEmpty() const102 Standard_Boolean IsEmpty () const {return (mySize == 0);} Length() const103 Standard_Integer Length () const {return mySize;} 104 105 //! Returns attached allocator Handle(NCollection_BaseAllocator)106 const Handle(NCollection_BaseAllocator)& Allocator() const 107 { return myAllocator; } 108 109 protected: 110 // Methods PROTECTED 111 // NCollection_BaseSequence(const Handle (NCollection_BaseAllocator)& theAllocator)112 NCollection_BaseSequence (const Handle(NCollection_BaseAllocator)& theAllocator) : 113 myFirstItem (NULL), 114 myLastItem (NULL), 115 myCurrentItem (NULL), 116 myCurrentIndex (0), 117 mySize (0) 118 { 119 myAllocator = (theAllocator.IsNull() ? NCollection_BaseAllocator::CommonBaseAllocator() : theAllocator); 120 } 121 122 //! Destructor ~NCollection_BaseSequence()123 virtual ~NCollection_BaseSequence() {} 124 125 Standard_EXPORT void ClearSeq (NCollection_DelSeqNode fDel); 126 Standard_EXPORT void PAppend (NCollection_SeqNode *); 127 Standard_EXPORT void PAppend (NCollection_BaseSequence& S); 128 Standard_EXPORT void PPrepend (NCollection_SeqNode *); 129 Standard_EXPORT void PPrepend (NCollection_BaseSequence& S); 130 Standard_EXPORT void PInsertAfter(Iterator& thePosition, 131 NCollection_SeqNode *); 132 Standard_EXPORT void PInsertAfter(const Standard_Integer Index, 133 NCollection_SeqNode *); 134 Standard_EXPORT void PInsertAfter(const Standard_Integer Index, 135 NCollection_BaseSequence& S); 136 Standard_EXPORT void PSplit (const Standard_Integer Index, 137 NCollection_BaseSequence& Sub); 138 Standard_EXPORT void RemoveSeq (Iterator& thePosition, 139 NCollection_DelSeqNode fDel); 140 Standard_EXPORT void RemoveSeq (const Standard_Integer Index, 141 NCollection_DelSeqNode fDel); 142 Standard_EXPORT void RemoveSeq (const Standard_Integer From, 143 const Standard_Integer To, 144 NCollection_DelSeqNode fDel); 145 Standard_EXPORT void PReverse (); 146 Standard_EXPORT void PExchange (const Standard_Integer I, 147 const Standard_Integer J) ; 148 Standard_EXPORT NCollection_SeqNode * 149 Find (const Standard_Integer) const; 150 151 protected: 152 // Fields PROTECTED 153 // 154 Handle(NCollection_BaseAllocator) myAllocator; 155 NCollection_SeqNode* myFirstItem; 156 NCollection_SeqNode* myLastItem; 157 NCollection_SeqNode* myCurrentItem; 158 Standard_Integer myCurrentIndex; 159 Standard_Integer mySize; 160 161 private: 162 // Methods PRIVATE 163 // 164 Standard_EXPORT NCollection_BaseSequence 165 (const NCollection_BaseSequence& Other); 166 inline void Nullify (); 167 friend class Iterator; 168 }; 169 170 #endif 171