root/foundation-apps/grosview-maxx/llist.cc

Revision 2, 6.1 KB (checked in by emasson, 3 years ago)

initial import for the community edition

Line 
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: llist.cc,v 1.1.1.1 2008/05/04 15:53:48 emasson Exp $
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
17CVSID("$Id: llist.cc,v 1.1.1.1 2008/05/04 15:53:48 emasson Exp $");
18CVSID_DOT_H(LLIST_H_CVSID);
19
20LList::LNode::LNode( void *data ){
21  data_ = data;
22  next_ = NULL;
23  prev_ = NULL;
24}
25
26LList::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
35LList::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
44LList::~LList( void ){
45  while ( n_ )
46    pop();
47}
48
49int 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
66void *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
89void *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
113int 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
160void *LList::find( void *key ){
161  LNode *temp;
162
163  temp = findnode( key );
164  if ( temp == NULL ) return ( NULL );
165  return ( temp->data_ );
166}
167
168void *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
178int 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
190void 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
205void *LList::findn( int n ){
206  LNode *temp;
207
208  temp = findnnode( n );
209  if ( temp == NULL ) return ( NULL );
210  return ( temp->data_ );
211}
212
213int 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
226void LList::setc( int n, int which ){
227  curr_[which] = findnnode( n );
228}
229
230void LList::incc( int which ){
231  if ( curr_[which] != NULL ) curr_[which] = curr_[which]->next_;
232}
233
234void LList::decc( int which ){
235  if ( curr_[which] != NULL ) curr_[which] = curr_[which]->prev_;
236}
237
238void *LList::findc( int which ){
239  if ( curr_[which] == NULL ) return ( NULL );
240
241  return ( curr_[which]->data_ );
242}
243
244void 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
258int 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
272void LList::kill( void ){
273//  while ( n_ ) {
274//    delete pop();
275//  }
276}
277
278
279
280
281
282LList::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
298void *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
336LList::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}
Note: See TracBrowser for help on using the browser.