~ubuntu-branches/ubuntu/maverick/xosview/maverick

« back to all changes in this revision

Viewing changes to .pc/25_iostream_h_FTBFS_fixes.diff/llist.cc

  • Committer: Bazaar Package Importer
  • Author(s): Kartik Mistry
  • Date: 2010-06-01 20:02:57 UTC
  • mfrom: (6.2.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100601200257-orz2mey0oilujocu
Tags: 1.8.3+debian-21
* debian/source/format:
  + Added to use source format 3.0 (quilt)
* debian/watch:
  + Fixed dversionmangle format
* Update debian/compat to 7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
//  Copyright (c) 1994, 1995, 2006 by Mike Romberg ( mike.romberg@noaa.gov )
 
3
//
 
4
//  This file may be distributed under terms of the GPL
 
5
//
 
6
//
 
7
// $Id$
 
8
//
 
9
#ifdef HAVE_IOSTREAM
 
10
#include <iostream>
 
11
#else
 
12
#include <iostream.h>
 
13
#endif
 
14
#include "general.h"
 
15
#include "llist.h"
 
16
 
 
17
CVSID("$Id$");
 
18
CVSID_DOT_H(LLIST_H_CVSID);
 
19
 
 
20
LList::LNode::LNode( void *data ){
 
21
  data_ = data;
 
22
  next_ = NULL;
 
23
  prev_ = NULL;
 
24
}
 
25
 
 
26
LList::LList( void ){
 
27
  n_ = 0;
 
28
  top_ = NULL;
 
29
  btm_ = NULL;
 
30
  for ( int i = 0 ; i < MAXCURR ; i++ )
 
31
    curr_[i] = NULL;
 
32
  cmp_fun_ = NULL;
 
33
}
 
34
 
 
35
LList::LList( int ( *cmp_fun )( void *data, void *key ) ){
 
36
  n_ = 0;
 
37
  top_ = NULL;
 
38
  btm_ = NULL;
 
39
  for ( int i = 0 ; i < MAXCURR ; i++ )
 
40
    curr_[i] = NULL;
 
41
  cmp_fun_ = cmp_fun;
 
42
}
 
43
 
 
44
LList::~LList( void ){
 
45
  while ( n_ )
 
46
    pop();
 
47
}
 
48
 
 
49
int LList::push( void *data ){
 
50
  if ( !n_ ) {
 
51
    top_ = new LNode( data );
 
52
    if ( top_ == NULL ) return ( 0 );
 
53
    n_ = 1;
 
54
    btm_ = top_;
 
55
    return ( 1 );
 
56
  }
 
57
 
 
58
  btm_->next_ = new LNode( data );
 
59
  if ( btm_->next_ == NULL ) return ( 0 );
 
60
  n_++;
 
61
  btm_->next_->prev_ = btm_;
 
62
  btm_ = btm_->next_;
 
63
  return ( 1 );
 
64
}
 
65
 
 
66
void *LList::pop( void ){
 
67
  void *temp;
 
68
  struct LNode *temp2;
 
69
 
 
70
  if ( !n_ ) return ( NULL );
 
71
 
 
72
  temp = btm_->data_;
 
73
  if ( n_ == 1 ) {
 
74
    delete top_;
 
75
    top_ = NULL;
 
76
    btm_ = NULL;
 
77
    n_ = 0;
 
78
    return ( temp );
 
79
  }
 
80
 
 
81
  n_--;
 
82
  temp2 = btm_->prev_;
 
83
  btm_->prev_->next_ = NULL;
 
84
  delete btm_;
 
85
  btm_ = temp2;
 
86
  return ( temp );
 
87
}
 
88
 
 
89
void *LList::dequeue( void ){
 
90
  void *temp;
 
91
  struct LNode *temp2;
 
92
 
 
93
  if ( !n_ ) return ( NULL );
 
94
 
 
95
  if ( n_ == 1 ) {
 
96
    temp = top_->data_;
 
97
    n_ = 0;
 
98
    delete top_;
 
99
    top_ = NULL;
 
100
    btm_ = NULL;
 
101
    return ( temp );
 
102
  }
 
103
 
 
104
  n_--;
 
105
  temp2 = top_->next_;
 
106
  temp = top_->data_;
 
107
  top_->next_->prev_ = NULL;
 
108
  delete top_;
 
109
  top_ = temp2;
 
110
  return ( temp );
 
111
}
 
112
 
 
113
int LList::insert( void *data, void *key ){
 
114
  LNode *current, *temp;
 
115
 
 
116
  current = top_;
 
117
 
 
118
  /*  Empty List  */
 
119
  if ( !n_ ) {
 
120
    if ( ( top_ = new LNode( data ) ) == NULL ) return ( 0 );
 
121
    btm_ = top_;
 
122
    n_++;
 
123
    return ( 1 );
 
124
  }
 
125
 
 
126
  while ( ( cmp_fun_( current->data_, key ) < 0 ) &&
 
127
          ( current->next_ != NULL ) )
 
128
    current = current->next_;
 
129
 
 
130
  if ( ( temp = new LNode( data ) ) == NULL ) return ( 0 );
 
131
 
 
132
  n_++;
 
133
 
 
134
  /*  Add To End of List  */
 
135
  if ( ( current->next_ == NULL ) &&
 
136
       ( cmp_fun_( current->data_, key ) < 0 ) ) {
 
137
    temp->prev_ = btm_;
 
138
    current->next_ = temp;
 
139
    btm_ = temp;
 
140
    return ( 1 );
 
141
  }
 
142
 
 
143
  /*  Add To Top of List  */
 
144
  if ( ( current->prev_ == NULL ) &&
 
145
       ( cmp_fun_( current->data_, key ) > 0 ) ) {
 
146
    temp->next_ = current;
 
147
    current->prev_ = temp;
 
148
    top_ = temp;
 
149
    return ( 1 );
 
150
  }
 
151
 
 
152
  /*  Middle Of List  */
 
153
  temp->next_ = current;
 
154
  temp->prev_ = current->prev_;
 
155
  current->prev_->next_ = temp;
 
156
  current->prev_ = temp;
 
157
  return ( 1 );
 
158
}
 
159
 
 
160
void *LList::find( void *key ){
 
161
  LNode *temp;
 
162
 
 
163
  temp = findnode( key );
 
164
  if ( temp == NULL ) return ( NULL );
 
165
  return ( temp->data_ );
 
166
}
 
167
 
 
168
void *LList::removematch( void *key ){
 
169
  LNode *ptr;
 
170
 
 
171
  ptr = findnode( key );
 
172
 
 
173
  if ( ptr == NULL ) return ( NULL );
 
174
 
 
175
  return ( deletenode( ptr ) );
 
176
}
 
177
 
 
178
int LList::putontop( void *data ){
 
179
  LNode *buff;
 
180
 
 
181
  if ( ( buff = new LNode( data ) ) == NULL ) return ( 0 );
 
182
 
 
183
  top_->prev_ = buff;
 
184
  buff->next_ = top_;
 
185
  top_ = buff;
 
186
  n_++;
 
187
  return ( 1 );
 
188
}
 
189
 
 
190
void LList::remove( void *data ){
 
191
  LNode *tmp;
 
192
  int found = 0;
 
193
 
 
194
  tmp = top_;
 
195
  while ( (!found) && (tmp != NULL) ) {
 
196
    if ( tmp->data_ == data ) found = 1;
 
197
    if ( !found ) tmp = tmp->next_;
 
198
  }
 
199
 
 
200
  if ( (tmp == NULL) || (!found) ) return;
 
201
 
 
202
  deletenode( tmp );
 
203
}
 
204
 
 
205
void *LList::findn( int n ){
 
206
  LNode *temp;
 
207
 
 
208
  temp = findnnode( n );
 
209
  if ( temp == NULL ) return ( NULL );
 
210
  return ( temp->data_ );
 
211
}
 
212
 
 
213
int LList::index( void *data ){
 
214
  int a = 1;
 
215
  LNode *tmp;
 
216
 
 
217
  tmp = top_;
 
218
  while ( tmp != NULL ) {
 
219
    if ( tmp->data_ == data ) return ( a );
 
220
    a++;
 
221
    tmp = tmp->next_;
 
222
  }
 
223
  return ( 0 );
 
224
}
 
225
 
 
226
void LList::setc( int n, int which ){
 
227
  curr_[which] = findnnode( n );
 
228
}
 
229
 
 
230
void LList::incc( int which ){
 
231
  if ( curr_[which] != NULL ) curr_[which] = curr_[which]->next_;
 
232
}
 
233
 
 
234
void LList::decc( int which ){
 
235
  if ( curr_[which] != NULL ) curr_[which] = curr_[which]->prev_;
 
236
}
 
237
 
 
238
void *LList::findc( int which ){
 
239
  if ( curr_[which] == NULL ) return ( NULL );
 
240
 
 
241
  return ( curr_[which]->data_ );
 
242
}
 
243
 
 
244
void LList::save( int size, FILE *fp ){
 
245
  int i;
 
246
  void *buf;
 
247
 
 
248
  fwrite( &n_, sizeof( int ), 1, fp );  /*  save n  */
 
249
 
 
250
  setc( 1 );
 
251
  for ( i = 1 ; i <= n_ ; i ++ ) {
 
252
    buf = findc();
 
253
    fwrite ( buf, size, 1, fp );
 
254
    incc();
 
255
  }
 
256
}
 
257
 
 
258
int LList::restore( int size, FILE *fp ){
 
259
  int i;
 
260
  void *buf;
 
261
 
 
262
  fread ( &i, sizeof ( int ), 1, fp );
 
263
 
 
264
  for ( ; i > 0 ; i-- ) {
 
265
    if ( ( buf = new char[size] ) == NULL ) return ( 0 );
 
266
    if ( ! push( buf ) ) return ( 0 );
 
267
  }
 
268
 
 
269
  return ( 1 );
 
270
}
 
271
 
 
272
void LList::kill( void ){
 
273
//  while ( n_ ) {
 
274
//    delete pop();
 
275
//  }
 
276
}
 
277
 
 
278
 
 
279
 
 
280
 
 
281
 
 
282
LList::LNode *LList::findnode( void *key ){
 
283
  LNode *current;
 
284
 
 
285
  current = top_;
 
286
 
 
287
  if ( current == NULL ) return ( NULL );
 
288
 
 
289
  while ( ( cmp_fun_( current->data_, key ) ) &&
 
290
          ( current != NULL ) )
 
291
    current = current->next_;
 
292
 
 
293
  if ( current == NULL ) return ( NULL );
 
294
 
 
295
  return ( current );
 
296
}
 
297
 
 
298
void *LList::deletenode( LNode *ptr ){
 
299
  void *rtn;
 
300
 
 
301
  if ( ( top_ == NULL ) || ( ptr == NULL ) ) return ( NULL );
 
302
 
 
303
  if ( n_ == 1 ) {
 
304
    rtn = top_->data_;
 
305
    n_ = 0;
 
306
    delete top_;
 
307
    top_ = btm_ = NULL;
 
308
    return ( rtn );
 
309
  }
 
310
 
 
311
  n_--;
 
312
 
 
313
  if ( ptr->prev_ == NULL ) {
 
314
    rtn = ptr->data_;
 
315
    top_ = top_->next_;
 
316
    top_->prev_ = NULL;
 
317
    delete ptr;
 
318
    return ( rtn );
 
319
  }
 
320
 
 
321
  if ( ptr->next_ == NULL ) {
 
322
    rtn = ptr->data_;
 
323
    btm_ = btm_->prev_;
 
324
    btm_->next_ = NULL;
 
325
    delete ptr;
 
326
    return ( rtn );
 
327
  }
 
328
 
 
329
  ptr->prev_->next_ = ptr->next_;
 
330
  ptr->next_->prev_ = ptr->prev_;
 
331
  rtn = ptr->data_;
 
332
  delete ptr;
 
333
  return ( rtn );
 
334
}
 
335
 
 
336
LList::LNode *LList::findnnode( int i ){
 
337
  int j;
 
338
  LNode *current;
 
339
 
 
340
  if ( (i > n_) || (i < 1) ) return ( NULL );
 
341
 
 
342
  if ( i <= n_ / 2 ) {
 
343
    current = top_;
 
344
    for ( j = 1 ; j < i ; j++ ) current = current->next_;
 
345
    return ( current );
 
346
  }
 
347
 
 
348
  current = btm_;
 
349
  for ( j = n_ ; j > i ; j-- ) current = current->prev_;
 
350
  return ( current );
 
351
}