~swag/armagetronad/0.2.9-sty+ct+ap-fork

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
/*

*************************************************************************

ArmageTron -- Just another Tron Lightcycle Game in 3D.
Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)

**************************************************************************

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  
***************************************************************************

*/

#include "tHeap.h"
#include <iostream>

/* ************************
   Heap data structure
   ************************ */


bool tHeapBase::SwapIf(int i,int j)
{
    if (i==j || i<0) return false; // safety

    tHeapElement *e1=operator()(i),*e2=operator()(j);

    tASSERT( e1->hID == i );
    tASSERT( e2->hID == j );

    if (e1->Val() > e2->Val()){
        Swap(tList< tHeapElement >::operator()(i),tList< tHeapElement >::operator()(j));
        e1->hID=j;
        e2->hID=i;
        return true;
    }
    else
        return false;
}

tHeapBase::~tHeapBase(){}

//#ifdef EVENT_DEB
void tHeapBase::CheckHeap(){
    for(int i=Len()-1;i>0;i--){
        tHeapElement *current=operator()(i);
        tHeapElement *low=operator()(Lower(i));
        if (Lower(UpperL(i))!=i || Lower(UpperR(i))!=i)
            tERR_ERROR_INT("Error in lower/upper " << i << "!");

        if (low->Val() > current->Val() )
            tERR_ERROR_INT("Heap structure corrupt!");

        if ( current->hID != i )
            tERR_ERROR_INT("Heap list structure corrupt!");
    }
}
//#endif

void tHeapBase::SwapDown(int j){
    int i=j;
    // e is now at position i. swap it down
    // as far as it goes:
    do{
        j=i;
        i=Lower(j);
    }
    while(SwapIf(i,j)); // mean: relies on the fact that SwapIf returns -1
    // if i<0.

#ifdef EVENT_DEB
    CheckHeap();
#endif
}

void tHeapBase::SwapUp(int i){
#ifdef EVENT_DEB
    //  static int su=0;
    //  if (su%100 ==0 )
    //    con << "su=" << su << '\n';
    //  if (su > 11594 )
    // con << "su=" << su << '\n';
    //  su ++;
#endif

    int ul,ur;
    bool goon=1;
    while(goon && UpperL(i)<Len()){
        ul=UpperL(i);
        ur=UpperR(i);
        if(ur>=Len() ||
                operator()(ul)->Val() < operator()(ur)->Val() ){
            goon=SwapIf(i,ul);
            i=ul;
        }
        else{
            goon=SwapIf(i,ur);
            i=ur;
        }
    }

#ifdef EVENT_DEB
    CheckHeap();
#endif

}

void tHeapBase::Insert(tHeapElement *e){
#ifdef EVENT_DEB
    CheckHeap();
#endif

    Add(e,e->hID); // relies on the implementation of List: e is
    // put to the back of the heap.
    tASSERT(e->hID == Len()-1);
    SwapDown(Len()-1);  // bring it to the right place

#ifdef EVENT_DEB
    CheckHeap();
#endif
}

void tHeapBase::Remove(tHeapElement *e){
    int i=e->hID;

#ifdef EVENT_DEB
    CheckHeap();
#endif

    if(i<0 || this != e->Heap())
        tERR_ERROR_INT("Element is not in this heap! (Note: this usually is a followup error when the system fails to recover from another error. When reporting, please also include whatever happened before this.)");

    Remove(i);

#ifdef EVENT_DEB
    CheckHeap();
#endif
}

void tHeapBase::Replace(int i){
    if (i>=0 && i < Len()){
        if (i==0 || operator()(i)->Val() > operator()(Lower(i))->Val() )
            SwapUp(i);          // put it up where it belongs
        else
            SwapDown(i);

#ifdef EVENT_DEB
        CheckHeap();
#endif
    }
}

void tHeapBase::Replace(tHeapElement *e){
    int i=e->hID;

    if(i<0 || this != e->Heap())
        tERR_ERROR_INT("Element is not in this heap! (Note: this usually is a followup error when the system fails to recover from another error. When reporting, please also include whatever happened before this.)");

    Replace(i);

#ifdef EVENT_DEB
    CheckHeap();
#endif
}

tHeapElement * tHeapBase::Remove(int i){
#ifdef EVENT_DEB
    CheckHeap();
#endif

    if (i>=0 && i<Len())
    {
        tHeapElement *ret=operator()(i);

        tASSERT( ret->hID == i );

        tList<tHeapElement>::Remove(ret,ret->hID);

        // now we have an misplaced element at pos i. (if i was not at the end..)
        if (i<Len())
            Replace(i);

#ifdef EVENT_DEB
        CheckHeap();
#endif

        return ret;
    }
    return NULL;
}



/* ************************
   Events:
   ************************ */

tHeapElement::~tHeapElement()
{
    tASSERT( hID < 0 );
}

void tHeapElement::RemoveFromHeap(){
    tASSERT( this );

    if (hID>=0)
    {
        tASSERT( Heap() );
        Heap()->Remove(this);
    }
}

void tHeapElement::SetVal( REAL value, tHeapBase& heap )
{
    tASSERT( !Heap() || Heap() == &heap );

    this->value_ = value;

    if ( hID>=0 )
    {
        tASSERT( heap( hID ) == this );

        heap.Replace( this );
    }
    else
    {
        heap.Insert( this );
    }
}

// in wich heap are we?
tHeapBase *tHeapElement::Heap() const
{
    tASSERT( 0 );
    return NULL;
}