Integration of OCCT 6.5.0 from SVN
[occt.git] / src / TCollection / TCollection_Sequence.gxx
1 // ----------------------------------------------------------------------
2 // Copyright:   Matra-Datavision 1992
3 // File:        TCollection_Sequence.gxx
4 // Created:     Sep, 24 1992
5 // Author:      Mireille MERCIEN
6 // Updated:     Remi Lequette,J.P. TIRAULT February,23 1995
7 //              we added a basic class BaseSequence where all
8 //              instantiations of Sequence inherit.
9 // ----------------------------------------------------------------------
10
11 static void delnode(TCollection_SeqNode* p)
12 {
13   TCollection_SequenceNode* q = (TCollection_SequenceNode*) p;
14   delete q;
15 }
16
17 // ----------------------------------
18 // Clear : Clear the Current Sequence
19 // ----------------------------------
20 void TCollection_Sequence::Clear()
21 {
22   TCollection_BaseSequence::Clear((void*)&delnode);
23 }
24
25 //=======================================================================
26 //function : Assign
27 //purpose  : 
28 //=======================================================================
29
30 const TCollection_Sequence& TCollection_Sequence::Assign
31   (const TCollection_Sequence& Other)
32 {
33   if (this == &Other) return *this;
34   Clear();
35   TCollection_SequenceNode* current  = (TCollection_SequenceNode*) Other.FirstItem;
36   TCollection_SequenceNode* previous = NULL;
37   TCollection_SequenceNode* newnode = NULL;
38   FirstItem = NULL;
39   while (current) {
40     newnode = new TCollection_SequenceNode(current->Value(),previous,(TCollection_SeqNode*)0L);
41     if (previous) previous->Next() = newnode;
42     else          FirstItem      = newnode;
43     current = (TCollection_SequenceNode *) current->Next();
44     previous = newnode;
45   }
46   LastItem = newnode;
47   Size = Other.Size;
48   CurrentItem = FirstItem;
49   CurrentIndex = 1;
50   return *this;
51 }
52
53
54 // -------------------------------------------------
55 // Append : Push an item  at the end of the sequence
56 // -------------------------------------------------
57 void TCollection_Sequence::Append(const SeqItem& T)
58 {
59   TCollection_SequenceNode* newnode = new TCollection_SequenceNode(T,(TCollection_SeqNode*)LastItem,(TCollection_SeqNode*)0L);
60   PAppend ((void*)newnode);
61 }
62
63
64 // ---------------------------------------------------------
65 // Prepend : Push an element at the begining of the sequence
66 // ---------------------------------------------------------
67 void TCollection_Sequence::Prepend(const SeqItem& T)
68 {
69   TCollection_SequenceNode* newnode = new TCollection_SequenceNode(T,(TCollection_SeqNode*)0L,(TCollection_SequenceNode*)FirstItem);
70   PPrepend ((void*)newnode);
71 }
72
73 // -----------------------------------------------------------------
74 // InsertAfter : Insert an element after a given index in a sequence
75 // -----------------------------------------------------------------
76 void TCollection_Sequence::InsertAfter(const Standard_Integer Index, 
77                                        const SeqItem& T)
78 {
79    Standard_OutOfRange_Raise_if ( Index < 0 || Index > Size, "");
80    TCollection_SequenceNode* newnode = new TCollection_SequenceNode(T,(TCollection_SeqNode*)0L,(TCollection_SeqNode*)0L);
81    PInsertAfter (Index,(void*)newnode);
82 }
83
84 // ---------------------------------------------------
85 // First : Returns the first element of the sequence
86 //         Raises an exeption if the sequence is empty
87 // ----------------------------------------------------
88 const SeqItem& TCollection_Sequence::First() const 
89 {
90    Standard_NoSuchObject_Raise_if(Size == 0,"");
91    return ((TCollection_SequenceNode*) FirstItem)->Value();
92 }
93
94 // ----------------------------------------------------
95 // Last : Returns the last element of the sequence
96 //         Raises an exeption if the sequence is empty
97 // ----------------------------------------------------
98 const SeqItem& TCollection_Sequence::Last() const 
99 {
100    Standard_NoSuchObject_Raise_if(Size == 0,"");
101    return ((TCollection_SequenceNode*) LastItem)->Value();
102 }
103
104 // -----------------------------------------
105 // Value : Return the value of a given index
106 // -----------------------------------------
107 const SeqItem& TCollection_Sequence::Value(const Standard_Integer Index) const
108 {
109    Standard_OutOfRange_Raise_if(Index <= 0 || Index > Size,"");
110    TCollection_Sequence* const aLocalTHIS = (TCollection_Sequence* const) this;
111    aLocalTHIS->CurrentItem  =  (TCollection_SequenceNode*) Find(Index);
112    aLocalTHIS->CurrentIndex = Index;
113    return ((TCollection_SequenceNode*)CurrentItem)->Value();
114 }
115
116 // -------------------------------------------------------
117 // ChangeValue : Return the modifiable value of a given index
118 // -------------------------------------------------------
119 SeqItem & TCollection_Sequence::ChangeValue(const Standard_Integer Index)  
120 {
121    Standard_OutOfRange_Raise_if(Index <= 0 || Index > Size,"");
122
123    CurrentItem =  (TCollection_SequenceNode*) Find(Index);
124    CurrentIndex = Index;
125    return ((TCollection_SequenceNode*)CurrentItem)->Value();
126 }
127
128 void TCollection_Sequence::Remove(const Standard_Integer Index)
129 {
130   TCollection_BaseSequence::Remove(Index,(void*)&delnode);
131 }
132
133 // ---------------------
134 // Remove a set of items
135 // --------------------- 
136 void TCollection_Sequence::Remove(const Standard_Integer FromIndex, const Standard_Integer ToIndex)
137 {
138   TCollection_BaseSequence::Remove(FromIndex,ToIndex,(void*)&delnode);
139 }
140
141
142 //=======================================================================
143 //function : SetValue
144 //purpose  : Sets a value to an index
145 //=======================================================================
146 void TCollection_Sequence::SetValue (const Standard_Integer Index,
147                                             const SeqItem& I)
148 {
149   ChangeValue(Index) = I ;
150 }