summaryrefslogtreecommitdiff
path: root/inc/PCollection_HArbitraryTree.gxx
blob: 52f886a24cb9b2bcd1ac4371fbc51524fbf779ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <Standard_OutOfRange.hxx>
#include <Standard_NoSuchObject.hxx>
#include <Standard_NoMoreObject.hxx>
#include <PCollection_IsNotRoot.hxx> 
#include <PCollection_IsNullTree.hxx> 
#include <PCollection_IsContained.hxx>

// ----------------------------------------------------------------------
// Copyright: 	Matra-Datavision 1992
// File:	PCollection_HArbitraryTree.gxx
// Created:	Aug, 5 1992
// Author:      Mireille MERCIEN
// ----------------------------------------------------------------------

// -----------
// constructor :
// -----------
PCollection_HArbitraryTree::PCollection_HArbitraryTree(const Item& T) 
{
  MyItem = T;
  MyChildren = new PCollection_SeqArbitraryTree;
 } 

// -----------------------------------------------
// Children : Returns the list of children of me
// -----------------------------------------------
Handle(PCollection_SeqArbitraryTree) 
       PCollection_HArbitraryTree::Children() const 
{
   return MyChildren;
}

// ----------------------
// Child : returns a child 
// ----------------------
Handle(PCollection_HArbitraryTree) 
    PCollection_HArbitraryTree::Child(const Standard_Integer Index) const
{
   return MyChildren->Value(Index);
}  

// ----------------------------------------------
// Value : returns the value of the current tree
// ----------------------------------------------
Item PCollection_HArbitraryTree::Value() const
{
   return MyItem;
}

// ----------------------------------------------------
// NbChildren : Number of children in the current tree 
// ----------------------------------------------------
Standard_Integer PCollection_HArbitraryTree::NbChildren() const
{
      return (MyChildren->Length());
}

// -------------------------------------------------------
// IsLeaf : Returns True if the current tree has no child
// -------------------------------------------------------
Standard_Boolean PCollection_HArbitraryTree::IsLeaf() const
{
   return (MyChildren->IsEmpty());
}

// ----------------------
// Parent
// ----------------------
Handle(PCollection_HArbitraryTree) PCollection_HArbitraryTree::Parent() const
{
   return MyParent;
}

// ----------------------
// IsRoot
// ---------------------
Standard_Boolean PCollection_HArbitraryTree::IsRoot() const
{
   return (MyParent.IsNull());
}
 
// ---------------------------------------------
// Root : Returns the root of the current tree
// ---------------------------------------------
Handle(PCollection_HArbitraryTree) PCollection_HArbitraryTree::Root() const
{
   Handle(PCollection_HArbitraryTree) AParent;
   AParent = this;
   while ( !AParent->IsRoot() )  AParent = AParent->Parent();
   return AParent;
}

// ----------------------------------------------------------------
   // LeftSibling : returns the left neighbour of the current tree
// ----------------------------------------------------------------
Handle(PCollection_HArbitraryTree) PCollection_HArbitraryTree::LeftSibling() const
{
   Handle(PCollection_HArbitraryTree) MySelf,TheParent,NullTree;
   Handle(PCollection_SeqArbitraryTree) TheBrothers;
   NullTree.Nullify();
   MySelf = this;
   if (MySelf->IsRoot()) return NullTree;
   TheBrothers = (MySelf->Parent())->Children();
   Standard_Integer TheIndex = TheBrothers->Location(1,MySelf);
   if (TheIndex <= 1) 
       return NullTree; 
   else
       return (TheBrothers->Value(TheIndex-1));  
}   

// ---------------------------------------------------------------
// RightSibling : returns the right neighbour of the current tree
// ---------------------------------------------------------------
Handle(PCollection_HArbitraryTree) PCollection_HArbitraryTree::RightSibling() const
{
   Handle(PCollection_HArbitraryTree) MySelf,TheParent,NullTree;
   Handle(PCollection_SeqArbitraryTree) TheBrothers;
   NullTree.Nullify();
   MySelf = this;
    if (MySelf->IsRoot()) return NullTree;
   TheBrothers = (MySelf->Parent())->Children();
   Standard_Integer TheIndex = TheBrothers->Location(1,MySelf);
   if (TheIndex == TheBrothers->Length()) 
       return NullTree; 
   else
       return (TheBrothers->Value(TheIndex+1));  
}   

// --------------------------------------------------------------------
// Contains : checks if the tree Atree is contained by the current one
// --------------------------------------------------------------------
Standard_Boolean PCollection_HArbitraryTree::Contains
     (const Handle(PCollection_HArbitraryTree)& ATree) const
{
  if (ATree.IsNull()) PCollection_IsNullTree::Raise();
  Handle(PCollection_HArbitraryTree) MySelf = this;
  Standard_Boolean IsFound = Standard_False;
  PCollection_ATPreOrderIterator Iter(MySelf); 
  while (!IsFound && Iter.More())  {
      if (Iter.Value() == ATree)  IsFound = Standard_True;
      Iter.Next();
    }
  return IsFound;  
}

// -------------------------------------------------
// SetValue : changes the value of the current tree
// -------------------------------------------------
void PCollection_HArbitraryTree::SetValue(const Item& T) 
{
   MyItem = T;
}

// ---------------------------------------------------------
// SetParent : changes the parent value of the current tree
// ---------------------------------------------------------
void PCollection_HArbitraryTree::SetParent
       (const Handle(PCollection_HArbitraryTree)& ATree) 
{
   MyParent = ATree;
}

// ---------------------------------------------------------------------
// SwapChild : replace, in the current tree, the child at the range
//             <Index> by the tree <ATree> and conversely
// ---------------------------------------------------------------------
void PCollection_HArbitraryTree::SwapChild
       (const Standard_Integer Index,Handle(PCollection_HArbitraryTree)& ATree) 
{
   if (!ATree.IsNull() && !ATree->IsRoot()) PCollection_IsNotRoot::Raise(); 
   Handle (PCollection_HArbitraryTree) TempTree = ATree;
   Handle (PCollection_HArbitraryTree) NullTree;
   NullTree.Nullify();
// ... Update the returned tree
   ATree = Child(Index);   
   ATree->SetParent(NullTree);
// ... Update the current tree
   if (! TempTree.IsNull()) InsertChildAfter(Index,TempTree);
   MyChildren->Remove(Index);   
}

// -----------------------------------------------------------------
// SwapChildren : TradeOff between two subtrees in the current tree
// -----------------------------------------------------------------
void PCollection_HArbitraryTree::SwapChildren
          ( const Handle(PCollection_HArbitraryTree)& Subtree1,
            const Handle(PCollection_HArbitraryTree)& Subtree2    )  
{  
   if (Subtree1.IsNull()  || Subtree2.IsNull())
                                     PCollection_IsNullTree::Raise();   
   if (!Contains(Subtree1) || !Contains(Subtree2)) 
                                     Standard_NoSuchObject::Raise();
   if (Subtree1->Contains(Subtree2) || Subtree1->Contains(Subtree2))
                                     PCollection_IsContained::Raise(); 
   Handle(PCollection_HArbitraryTree) Parent1 = Subtree1->Parent(); 
   Handle(PCollection_HArbitraryTree) Parent2 = Subtree2->Parent();
   Handle(PCollection_SeqArbitraryTree) 
                                    Children1 = Parent1->Children(); 
   Handle(PCollection_SeqArbitraryTree)  
                                    Children2 = Parent2->Children(); 

// ... Searching the index of the subtrees. 
   Standard_Integer Ind1 = Children1->Location(1,Subtree1);
   Standard_Integer Ind2 = Children2->Location(1,Subtree2);
// ... Insertion of Subtree2
   Handle(PCollection_HArbitraryTree) NullTree;
   NullTree.Nullify();
   Subtree2->SetParent(NullTree);
   Parent1->InsertChildAfter(Ind1,Subtree2);
   Children1->Remove(Ind1);
// ... Insertion of Subtree1
   Subtree1->SetParent(NullTree);
   Parent2->InsertChildAfter(Ind2,Subtree1);
   Children2->Remove(Ind2);
}

// -----------------------------------------------------------------------
// AppendChild : appends the tree <Atree> as last child of the current tree
// -----------------------------------------------------------------------
void PCollection_HArbitraryTree::AppendChild
               (const Handle(PCollection_HArbitraryTree)& ATree)  
{
  if (ATree.IsNull()) PCollection_IsNullTree::Raise();
  if (!ATree->IsRoot()) PCollection_IsNotRoot::Raise();

  Handle (PCollection_HArbitraryTree) MySelf = this;
  ATree->SetParent(MySelf);
  MyChildren->Append(ATree);
}

// --------------------------------------------------------------------------
// PrependChild : appends the tree <ATree> as first child of the current tree
// --------------------------------------------------------------------------
void PCollection_HArbitraryTree::PrependChild
               (const Handle(PCollection_HArbitraryTree)& ATree)  
{
  if (ATree.IsNull()) PCollection_IsNullTree::Raise();
  if (!ATree->IsRoot()) PCollection_IsNotRoot::Raise();

  Handle (PCollection_HArbitraryTree) MySelf = this;
  ATree->SetParent(MySelf);
  MyChildren->Prepend(ATree);

}

// -----------------------------------------------------------------------
// InsertChildBefore : adds the child <ATree> at the index <Index> in the 
//                     current tree  
// -----------------------------------------------------------------------
void PCollection_HArbitraryTree::InsertChildBefore
   (const Standard_Integer Index , 
    const Handle(PCollection_HArbitraryTree)& ATree) 
{
  if (ATree.IsNull()) PCollection_IsNullTree::Raise();
  if (!ATree->IsRoot()) PCollection_IsNotRoot::Raise();

  Handle (PCollection_HArbitraryTree) MySelf = this;
  ATree->SetParent(MySelf);
  MyChildren->InsertBefore(Index,ATree);
}

// ------------------------------------------------------------------------
// InsertChildAfter : adds the child <ATree> at the index <Index+1> in the
//                    current tree
// ------------------------------------------------------------------------
void PCollection_HArbitraryTree::InsertChildAfter
      (const Standard_Integer Index , 
       const Handle(PCollection_HArbitraryTree)& ATree) 
{
  if (ATree.IsNull()) PCollection_IsNullTree::Raise();
  if (!ATree->IsRoot()) PCollection_IsNotRoot::Raise();

  Handle (PCollection_HArbitraryTree) MySelf = this;
  ATree->SetParent(MySelf);
  MyChildren->InsertAfter(Index,ATree);
}

// ---------------------------------------------------------------------
// RemoveChild : removes the child at range <Index> in the current tree
// ---------------------------------------------------------------------
void PCollection_HArbitraryTree::RemoveChild(const Standard_Integer Index)  
{
  MyChildren->Remove(Index);
}

// -------------------------------------------------------------
// RemoveAllChildren : removes all children of the current tree
// -------------------------------------------------------------
void PCollection_HArbitraryTree::RemoveAllChildren()  
{
   MyChildren->Clear();
}

// --------------------------------------------------------------------------
// RemoveChildren : removes the children in range <FromIndex,ToIndex> in the
//                  current tree 
// --------------------------------------------------------------------------
void PCollection_HArbitraryTree::RemoveChildren
       (const Standard_Integer FromIndex , const Standard_Integer ToIndex)  
{
  MyChildren->Remove(FromIndex,ToIndex);
}