summaryrefslogtreecommitdiff
path: root/inc/PCollection_HQueue.gxx
blob: 2f16eaf2535ed517ea8eea33940dd662bcb1b433 (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
#include <Standard_NoSuchObject.hxx>
#include <Standard_NotImplemented.hxx>
#include <Standard_NoMoreObject.hxx>

// ------------
// constructor
// -----------

PCollection_HQueue::PCollection_HQueue() 
{
    TheLength = 0;
    TheFront  = new PCollection_QueueNode;
    TheBack   = TheFront;
}

// -----------------------------
// IsEmpty : is the queue empty ? 
// -----------------------------
Standard_Boolean PCollection_HQueue::IsEmpty() const {
    return TheLength == 0;
} 

// --------------------------------------
// Front : item at the front of the Queue 
// --------------------------------------
Item PCollection_HQueue::Front() const 
{
    if (TheFront->IsEmpty()) Standard_NoSuchObject::Raise();
    return TheFront->Value();
}

// --------------------------------------
// Clear : remove all items in the Queue 
// --------------------------------------
void PCollection_HQueue::Clear()
{   
   Handle(PCollection_QueueNode) temp; 
   while (TheLength != 0) {    
      temp = TheFront;
      TheFront = TheFront->Tail();
      if (TheLength == 1) TheBack = TheFront;
      temp.Delete();
      --TheLength;
   }  
}

// ------------------------------------
// Push : insert an item at the back
// ------------------------------------
void PCollection_HQueue::Push(const Item& T)
{   
    Handle(PCollection_QueueNode) L;
    L = new PCollection_QueueNode;
    if (TheLength == 0) 
     {
        L->ChangeForwardPointer(TheFront);
        TheBack = L;
        TheFront = TheBack;
     }
    else
     { 
        L->ChangeForwardPointer(TheBack->Tail()); 
        TheBack->ChangeForwardPointer(L);
        TheBack = L;
      };
    TheBack->SetValue(T);
    TheLength = TheLength + 1;          
  }

// ------------------------------------
// Pop : remove an item from the front
// ------------------------------------
void PCollection_HQueue::Pop()
{   
    if (TheFront->IsEmpty()) Standard_NoSuchObject::Raise();
    Handle(PCollection_QueueNode) temp = TheFront;
    TheFront = TheFront->Tail();
    temp.Delete();
    TheLength = TheLength - 1;
    if (TheLength == 0) TheBack = TheFront;
}

// ------------------------------------
// ChangeFront : replace the front by T
// ------------------------------------
void PCollection_HQueue::ChangeFront(const Item& T)
{   
    if (TheFront->IsEmpty()) Standard_NoSuchObject::Raise();
    TheFront->SetValue(T);       
}

// ------------------------------------
// ShallowCopy
// ------------------------------------
Handle(Standard_Persistent) PCollection_HQueue::ShallowCopy() const
{
  Handle(PCollection_HQueue) TheCopy;
  Handle(PCollection_QueueNode) TheList;

  TheCopy = new PCollection_HQueue;
  TheList = TheFront;


  for (Standard_Integer I = 1; I <= TheLength; I++){
    TheCopy->Push(TheList->Value());
    TheList = TheList->Tail();
  }

  return TheCopy;
  
}

// ------------------------------------
// ShallowDump
// ------------------------------------
void PCollection_HQueue::ShallowDump(Standard_OStream& S) const
{
  S << "begin class Queue "<< endl;
  S << "Length of Queue : "<< TheLength << endl;
  TheFront->ShallowDump(cout);
  S << "end of class Queue." << endl;
}



// -----------------------------
// Length : numbers of items 
// -----------------------------
Standard_Integer PCollection_HQueue::Length() const {
    return TheLength;
} 

// -----------------------------
// FFront : front of the queue  
// -----------------------------
Handle(PCollection_QueueNode) PCollection_HQueue::FFront() const {
    return TheFront;
} 

// -----------------------------
// FBack : the back of the queue  
// -----------------------------
Handle(PCollection_QueueNode) PCollection_HQueue::FBack() const {
    return TheBack;
} 

void PCollection_HQueue::Destroy()
{
#ifdef CSFDB
  Clear();
#endif
}