1 // Created on: 1992-09-24
2 // Created by: Mireille MERCIEN
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
21 #include <Standard_NoSuchObject.hxx>
22 #include <Standard_OutOfRange.hxx>
23 #include <Standard_NotImplemented.hxx>
24 #include <Standard_ProgramError.hxx>
25 #include <Standard_OStream.hxx>
27 // ----------------------------------------------------------------------
28 // ----------------------------------------------------------------------
33 PCollection_HSequence::PCollection_HSequence()
40 // ----------------------------------
41 // Clear : Clear the Current Sequence
42 // ----------------------------------
43 void PCollection_HSequence::Clear()
45 Handle(PCollection_SeqNode) cell;
46 Handle(PCollection_SeqNode) pnul;
51 FirstItem = FirstItem->Next();
52 FirstItem->SetPrevious(pnul);
60 LastItem.Delete(); // free memory
66 // -------------------------------------------------
67 // Append : Push an item at the end of the sequence
68 // -------------------------------------------------
69 void PCollection_HSequence::Append(const Item& T)
71 Handle(PCollection_SeqNode) newcell;
73 newcell = new PCollection_SeqNode(LastItem,T);
75 newcell = new (os_segment::of(this)) PCollection_SeqNode(LastItem,T);
77 if (Size == 0) FirstItem = newcell;
78 if (!LastItem.IsNull()) LastItem->SetNext(newcell);
84 // ---------------------------------------------------
85 // Append : Push a sequence at the end of the sequence
86 // ---------------------------------------------------
87 void PCollection_HSequence::Append(const Handle(PCollection_HSequence)& S)
89 for (Standard_Integer i = 1; i <= S->Length(); i++)
93 // ---------------------------------------------------------
94 // Prepend : Push an element at the begining of the sequence
95 // ---------------------------------------------------------
96 void PCollection_HSequence::Prepend(const Item& T)
98 Handle(PCollection_SeqNode) newcell;
100 newcell = new PCollection_SeqNode(T,FirstItem);
102 newcell = new (os_segment::of(this)) PCollection_SeqNode(T,FirstItem);
104 if (Size == 0) LastItem = newcell;
105 if (!FirstItem.IsNull()) FirstItem->SetPrevious(newcell);
110 // ---------------------------------------------------------
111 // Prepend : Push a sequence at the begining of the sequence
112 // ---------------------------------------------------------
113 void PCollection_HSequence::Prepend(const Handle (PCollection_HSequence)& S)
115 for (Standard_Integer i = S->Length(); i >= 1; i--)
116 Prepend (S->Value(i));
120 // ---------------------------------------------------------
121 // Reverse : Reverse the order of a given sequence
122 // ---------------------------------------------------------
123 void PCollection_HSequence::Reverse()
125 if (Size == 0 || Size == 1) return;
126 Handle(PCollection_SeqNode) back,next,temp;
128 while (!temp.IsNull())
130 back = temp->Previous();
133 temp->SetPrevious(next);
137 FirstItem = LastItem;
141 // -------------------------------------------------------------------
142 // InsertBefore : Insert an item before a given index in the sequence
143 // --------------------------------------------------------------------
144 void PCollection_HSequence::InsertBefore(const Standard_Integer Index,
147 if ( Index <= 0 || Index > Length() ) Standard_OutOfRange::Raise();
154 Standard_Integer i = 1;
155 Handle(PCollection_SeqNode) cell = FirstItem;
161 // Insertion before Index
162 Handle(PCollection_SeqNode) previous = cell->Previous();
164 Handle(PCollection_SeqNode) temp = new PCollection_SeqNode(previous,cell,T);
166 Handle(PCollection_SeqNode) temp = new (os_segment::of(this)) PCollection_SeqNode(previous,cell,T);
168 previous->SetNext(temp);
169 cell->SetPrevious(temp);
173 // ----------------------------------------------------------------------
174 // InsertBefore : Insert a sequence before a specific index in a sequence
175 // ----------------------------------------------------------------------
176 void PCollection_HSequence::InsertBefore(const Standard_Integer Index ,
177 const Handle(PCollection_HSequence)& S)
179 if ( Index <= 0 || Index > Size ) Standard_OutOfRange::Raise();
180 for (Standard_Integer i = 1, j = Index ; i <= S->Length(); i++,j++)
181 InsertBefore(j,S->Value(i));
184 // -----------------------------------------------------------------
185 // InsertAfter : Insert an element after a given index in a sequence
186 // -----------------------------------------------------------------
187 void PCollection_HSequence::InsertAfter(const Standard_Integer Index,
190 if ( Index <= 0 || Index > Length() ) Standard_OutOfRange::Raise();
194 InsertBefore (Index+1,T);
197 // -------------------------------------------------------------------
198 // InsertAfter : Insert a sequence after a given index in the sequence
199 // -------------------------------------------------------------------
200 void PCollection_HSequence::InsertAfter(const Standard_Integer Index,
201 const Handle(PCollection_HSequence)&S)
203 if ( Index <= 0 || Index > Length() ) Standard_OutOfRange::Raise();
204 for (Standard_Integer i = 1, j = Index ; i <= S->Length(); i++,j++)
205 InsertAfter (j,S->Value(i));
208 // ----------------------------------------
209 // Exchange : Exchange two elements in the sequence
210 // ----------------------------------------
211 void PCollection_HSequence::Exchange(const Standard_Integer I,
212 const Standard_Integer J)
214 if ( I <= 0 || J <= 0 || I > Length() || J > Length() )
215 Standard_OutOfRange::Raise();
217 SetValue(J,Value(I));
221 // ----------------------------------------------------
222 // SubSequence : Returns a sub-sequence from a sequence
223 // ----------------------------------------------------
224 Handle(PCollection_HSequence) PCollection_HSequence::SubSequence
225 (const Standard_Integer From,const Standard_Integer To) const
227 if ( From <= 0 || From > Length() ||
228 To <= 0 || To > Length() || To < From ) Standard_OutOfRange::Raise();
230 Handle (PCollection_HSequence) SubSeq = new PCollection_HSequence;
232 Handle (PCollection_HSequence) SubSeq = new (os_segment::of(this)) PCollection_HSequence;
234 for(Standard_Integer i = From ; i <= To; i++)
235 SubSeq->Append(Value(i));
239 // ---------------------------------------------
240 // Split : Split a sequence in two sub-sequences
241 // ---------------------------------------------
242 Handle (PCollection_HSequence)
243 PCollection_HSequence::Split(const Standard_Integer Index)
246 if ( Index <= 0 || Index > Length() ) Standard_OutOfRange::Raise();
248 // construct the new sequence
250 Handle(PCollection_HSequence) Seq = new PCollection_HSequence;
252 Handle(PCollection_HSequence) Seq = new (os_segment::of(this)) PCollection_HSequence;
254 for (i = Index ; i<= Size; i++)
255 Seq->Append(Value(i));
257 // Update the old sequence
264 Handle(PCollection_SeqNode) cell = FirstItem;
265 while (i != Index-1) {
269 // Re-build the Sequence
270 Handle(PCollection_SeqNode) pnul;
273 LastItem->SetNext(pnul);
278 // ----------------------------------------------------------
279 // SetValue : Change the element of a given index in a sequence
280 // ----------------------------------------------------------
281 void PCollection_HSequence::SetValue(const Standard_Integer Index,
284 if (Index <= 0 || Index > Length()) Standard_OutOfRange::Raise();
287 Standard_Integer i = 1;
288 Handle(PCollection_SeqNode) cell = FirstItem;
289 while (i != Index ) {
293 // Change the value of the node
297 // -----------------------------------------
298 // Value : Return the value of a given index
299 // -----------------------------------------
300 Item PCollection_HSequence::Value(const Standard_Integer Index) const
302 if (Index <= 0 || Index > Length()) Standard_OutOfRange::Raise();
304 Standard_Integer i = 1;
305 Handle(PCollection_SeqNode) cell = FirstItem;
306 while (i != Index ) {
310 // returns the value of the node
311 return cell->Value();
314 // ----------------------------------------------------------------
315 // Contains : Returns True if the sequence contains a given element
316 // ----------------------------------------------------------------
317 //Standard_Boolean PCollection_HSequence::Contains(const Item& T) const
318 Standard_Boolean PCollection_HSequence::Contains(const Item& ) const
320 Standard_ProgramError::Raise("PCollection_HSequence::Contains : Obsolete method...");
321 return Standard_False;
324 // -------------------------------------------------------------
325 // Location : returns the index of the nth occurence of an item.
326 // -------------------------------------------------------------
327 //Standard_Integer PCollection_HSequence::Location(const Standard_Integer N,
329 // const Standard_Integer From,
330 // const Standard_Integer To) const
331 Standard_Integer PCollection_HSequence::Location(const Standard_Integer ,
333 const Standard_Integer ,
334 const Standard_Integer ) const
336 Standard_ProgramError::Raise("PCollection_HSequence::Location : Obsolete method...");
340 // -------------------------------------------------------------
341 // Location : returns the index of the nth occurence of an item.
342 // -------------------------------------------------------------
343 //Standard_Integer PCollection_HSequence::
344 // Location(const Standard_Integer N,const Item& T) const
345 Standard_Integer PCollection_HSequence::
346 Location(const Standard_Integer ,const Item& ) const
348 Standard_ProgramError::Raise("PCollection_HSequence::Location : Obsolete method...");
352 // -------------------------------------
353 // Remove : Remove an item in a sequence
354 // -------------------------------------
355 void PCollection_HSequence::Remove(const Standard_Integer Index)
357 if (Index <= 0 || Index > Size ) Standard_OutOfRange::Raise();
362 LastItem.Delete(); // free memory
366 Handle(PCollection_SeqNode) pnul,cell,previous,next;
368 if ( Index == 1 ) { // Remove the first Index
370 FirstItem = FirstItem->Next();
371 FirstItem->SetPrevious(pnul);
373 cell.Delete(); // free memory
376 } else if ( Index == Size ) { // Remove the last Index
378 LastItem = LastItem->Previous();
379 LastItem->SetNext(pnul);
381 cell.Delete(); // free memory
385 Standard_Integer i = 1;
391 previous = cell->Previous();
393 previous->SetNext(next);
394 next->SetPrevious(previous);
396 cell.Delete(); // free memory
403 // ---------------------
404 // Remove a set of items
405 // ---------------------
406 void PCollection_HSequence::Remove(const Standard_Integer From,const Standard_Integer To)
408 if (From <= 0 || From > Size || To <= 0 || To > Size || From > To )
409 Standard_OutOfRange::Raise();
410 for (Standard_Integer i = From; i<= To; i++) Remove(From);
413 // ---------------------------------------------------
414 // First : Returns the first element of the sequence
415 // Raises an exeption if the sequence is empty
416 // ----------------------------------------------------
417 Item PCollection_HSequence::First() const
419 if (Size == 0) Standard_NoSuchObject::Raise();
420 return FirstItem->Value();
423 // ----------------------------------------------------
424 // Last : Returns the last element of the sequence
425 // Raises an exeption if the sequence is empty
426 // ----------------------------------------------------
427 Item PCollection_HSequence::Last() const
429 if (Size == 0) Standard_NoSuchObject::Raise();
430 return LastItem->Value();
434 // ----------------------------------------------------
438 // ----------------------------------------------------
439 Handle(Standard_Persistent) PCollection_HSequence::ShallowCopy() const
441 Handle (PCollection_HSequence) TheCopy ;
442 Handle (PCollection_SeqNode) TheList ;
445 TheCopy = new PCollection_HSequence;
447 TheCopy = new (os_segment::of(this)) PCollection_HSequence;
450 for (Standard_Integer I = 1; I <= Size; I++) {
451 TheCopy->Append(TheList->Value());
452 TheList = TheList->Next();
459 // ----------------------------------------------------
462 // ----------------------------------------------------
463 void PCollection_HSequence::ShallowDump(Standard_OStream& S) const
465 S << "begin class Sequence "<< endl;
466 S << "Size : "<< Size << "element(s)." << endl;
467 Standard_Integer i = 1;
468 Handle(PCollection_SeqNode) cell = FirstItem;
469 while ( !cell.IsNull() ) {
470 S << "Index : "<< i << endl;
471 // ::ShallowDump(cell->Value(),S);
475 S << "end class Sequence" << endl;
479 // IsEmpty : Returns Standard_True if the sequence is empty (i.e. Size = 0)
480 Standard_Boolean PCollection_HSequence::IsEmpty() const
485 // Length : Returns the length of the sequence
486 Standard_Integer PCollection_HSequence::Length() const
491 // GetFirst : Returns the field "FirstItem"
492 Handle(PCollection_SeqNode) PCollection_HSequence::GetFirst() const
497 // GetLast : Returns the field "LastItem"
498 Handle(PCollection_SeqNode) PCollection_HSequence::GetLast() const
503 void PCollection_HSequence::Destroy()