0027490: BRepMesh: Reduce number of memory allocations
[occt.git] / src / NCollection / NCollection_BaseSequence.hxx
CommitLineData
b311480e 1// Created on: 2002-04-10
2// Created by: Alexander KARTOMIN (akm)
973c2be1 3// Copyright (c) 2002-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
16#ifndef NCollection_BaseSequence_HeaderFile
17#define NCollection_BaseSequence_HeaderFile
18
19#include <Standard.hxx>
20#include <NCollection_BaseAllocator.hxx>
ddf2fe8e 21#include <NCollection_DefineAlloc.hxx>
7fd59977 22
23// **************************************** Class SeqNode ********************
24
25class NCollection_SeqNode
26{
ddf2fe8e 27public:
28 // define new operator for use with NCollection allocators
29 DEFINE_NCOLLECTION_ALLOC
30public:
79a35943 31 NCollection_SeqNode () : myNext (NULL), myPrevious (NULL) {}
32 NCollection_SeqNode * Next () const { return myNext; }
33 NCollection_SeqNode * Previous () const { return myPrevious; }
34 void SetNext (NCollection_SeqNode * theNext) { myNext = theNext; }
35 void SetPrevious (NCollection_SeqNode * thePrev) { myPrevious = thePrev; }
7fd59977 36
37 private:
79a35943 38 NCollection_SeqNode* myNext;
39 NCollection_SeqNode* myPrevious;
7fd59977 40};
41
42typedef 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 */
49class NCollection_BaseSequence
50{
ddf2fe8e 51public:
52 //! Memory allocation
53 DEFINE_STANDARD_ALLOC
54 DEFINE_NCOLLECTION_ALLOC
55
56public:
7fd59977 57 class Iterator
58 {
59 public:
60 //! Empty constructor
79a35943 61 Iterator (void) : myCurrent (NULL), myPrevious(NULL) {}
62
7fd59977 63 //! Constructor with initialisation
79a35943 64 Iterator (const NCollection_BaseSequence& theSeq,
65 const Standard_Boolean isStart)
66 {
67 Init (theSeq, isStart);
68 }
69
70 //! Initialisation
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
7fd59977 78 //! Assignment
79 Iterator& operator = (const Iterator& theOther)
79a35943 80 {
81 myCurrent = theOther.myCurrent;
82 myPrevious = theOther.myPrevious;
83 return *this;
84 }
85 //! Switch to previous element; note that it will reset
86 void Previous()
87 {
88 myCurrent = myPrevious;
89 if (myCurrent)
90 myPrevious = myCurrent->Previous();
91 }
7fd59977 92
93 protected:
79a35943 94 NCollection_SeqNode* myCurrent; //!< Pointer to the current node
95 NCollection_SeqNode* myPrevious; //!< Pointer to the previous node
7fd59977 96 friend class NCollection_BaseSequence;
97 };
98
99 public:
100 // Methods PUBLIC
101 //
102 Standard_Boolean IsEmpty () const {return (mySize == 0);}
103 Standard_Integer Length () const {return mySize;}
104
e1c1b6b9 105 //! Returns attached allocator
106 const Handle(NCollection_BaseAllocator)& Allocator() const
107 { return myAllocator; }
108
7fd59977 109 protected:
110 // Methods PROTECTED
111 //
ddf2fe8e 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
6928e351 122 //! Destructor
123 virtual ~NCollection_BaseSequence() {}
124
ddf2fe8e 125 Standard_EXPORT void ClearSeq (NCollection_DelSeqNode fDel);
7fd59977 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,
ddf2fe8e 139 NCollection_DelSeqNode fDel);
7fd59977 140 Standard_EXPORT void RemoveSeq (const Standard_Integer Index,
ddf2fe8e 141 NCollection_DelSeqNode fDel);
7fd59977 142 Standard_EXPORT void RemoveSeq (const Standard_Integer From,
143 const Standard_Integer To,
ddf2fe8e 144 NCollection_DelSeqNode fDel);
7fd59977 145 Standard_EXPORT void PReverse ();
146 Standard_EXPORT void PExchange (const Standard_Integer I,
147 const Standard_Integer J) ;
79a35943 148 Standard_EXPORT NCollection_SeqNode *
7fd59977 149 Find (const Standard_Integer) const;
150
151 protected:
152 // Fields PROTECTED
153 //
ddf2fe8e 154 Handle(NCollection_BaseAllocator) myAllocator;
79a35943 155 NCollection_SeqNode* myFirstItem;
156 NCollection_SeqNode* myLastItem;
157 NCollection_SeqNode* myCurrentItem;
158 Standard_Integer myCurrentIndex;
159 Standard_Integer mySize;
7fd59977 160
161 private:
162 // Methods PRIVATE
163 //
164 Standard_EXPORT NCollection_BaseSequence
165 (const NCollection_BaseSequence& Other);
ddf2fe8e 166 inline void Nullify ();
7fd59977 167 friend class Iterator;
168};
169
7fd59977 170#endif