xosview  1.23
About: xosview is an X Windows based system monitor (cpu, memory, swap and network usage; interrupt and serial activities; load average).
  Fossies Dox: xosview-1.23.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

llist.h
Go to the documentation of this file.
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 #ifndef __LLIST_H__
8 #define __LLIST_H__
9 
10 #include <stdio.h>
11 
12 #define MAXCURR 4 // number of 'current' pointers
13 
14 class LList {
15 public:
16  // The first constructor is used for unordered lists ( stacks, queques ).
17  // The second constructor is used for ordered lists. It's
18  // parameter is a pointer to the function to be used for ordering the
19  // list. This function must behave in the following way:
20  // int Cmp_Fun ( void *data, void *key )
21  //
22  // *data is a pointer to the type of information
23  // to be stored in the list.
24  //
25  // *key is a pointer to the location within the data
26  // which will be used to order the list. In some
27  // cases these two pointers will be of the same
28  // type.
29  //
30  // function returns :
31  //
32  // 0.........*data = *key
33  // > 0.......*data > *key
34  // < 0.......*data < *key
35 
36  LList( void ); // for unordered lists
37  LList( int ( *cmp_fun )( void *data, void *key ) ); // for ordered lists
38 
39  virtual ~LList( void ); // frees nodes but not data
40 
41  // Stack like functions.
42  // pop returns NULL if the list is empty.
43  // push returns 1 on sucess and 0 upon failure.
44  int push( void *data );
45  void *pop( void );
46 
47  // Queue like functions
48  // dequeue returns NULL if the list is empty.
49  // enqueue returns 1 on sucess and 0 upon failure.
50  int enqueue( void *data ) { return( push( data ) ); }
51  void *dequeue( void );
52 
53  // Ordered list functions
54  // for insert *key points to some part of *data used for sorting.
55  // for find and removematch *key points to the "search" key.
56  // - insert returns 1 on sucess and 0 on failure
57  // - find and removematch return a pointer to the data stored in the list if
58  // sucessful and NULL if not.
59  int insert( void *data, void *key );
60  void *find( void *key );
61  void *removematch( void *key );
62 
63  // Misc. llist functions
64  int putontop( void *data ); // oposite of push
65  void remove( void *data ); // removes *data from the list if it's there
66  void *findn( int n ); // returns nth element if found or NULL if not
67  void *operator[](int n) // returns nth element if found or NULL if not
68  { return findn(n); }
69  int index( void *data ); // returns the index of *data or 0 if not there
70 
71  // Sets the a pointer for the linked list to the nth element in
72  // the list. This function and the three that follow are intended to
73  // be used for a stepwise search of a linked list. This function sets
74  // curr_ to the nth element in the list. incc sets the same pointer
75  // to the next element in the list. decc sets it to the previous
76  // element in the list. findc returns a pointer to the element
77  // curr_ is "currently" pointing to. If n is not valid for the list
78  // curr_ is set to NULL.
79  void setc( int n, int which = 0 );
80  void incc( int which = 0 );
81  void decc( int which = 0 );
82  void *findc( int which = 0 );
83 
84  // This function will save a linked list to the file pointed to
85  // by *fp. The file should be binary. n is saved first and then
86  // each item on the list is saved. size is the number of bytes of
87  // each item on the list. The list will remain intact after this
88  // function is called.
89  void save( int size, FILE *fp );
90 
91  // This function reads a linked list from the file pointed to
92  // by *fp ( previously saved by the above function ). Space is
93  // allocated for each item, and it is placed into the list in it's
94  // old position. size is the number of bytes each item occupies.
95  // This function will return 1 upon sucess and 0 upon failure.
96  int restore( int size, FILE *fp );
97 
98  // This function will remaove all of the elements in the linked
99  // list pointed to by L and free the memory each element occupied.
100  void kill( void );
101 
102  int n( void ) const { return( n_ ); } // number of elements in the list
103 protected:
104 
105  class LNode {
106  public:
107  LNode( void *data = NULL );
108 
109  void *data_;
112  };
113 
114  int n_; // number of nodes in the list
115  LNode *top_, *btm_; // pointers to various nodes
117 
118  // a comparison function for ordered lists
119  int ( *cmp_fun_ )( void *data, void *key );
120  LNode *findnode( void *key );
121  void *deletenode( LNode *ptr );
122  LNode *findnnode( int i );
123 private:
124 };
125 
126 #endif
void * data_
Definition: llist.h:109
LNode(void *data=NULL)
Definition: llist.cc:10
LNode * next_
Definition: llist.h:110
LNode * prev_
Definition: llist.h:111
Definition: llist.h:14
virtual ~LList(void)
Definition: llist.cc:34
int enqueue(void *data)
Definition: llist.h:50
void * dequeue(void)
Definition: llist.cc:79
LNode * curr_[4]
Definition: llist.h:116
void kill(void)
Definition: llist.cc:262
LNode * findnnode(int i)
Definition: llist.cc:326
void setc(int n, int which=0)
Definition: llist.cc:216
void * findc(int which=0)
Definition: llist.cc:228
void remove(void *data)
Definition: llist.cc:180
void * deletenode(LNode *ptr)
Definition: llist.cc:288
int push(void *data)
Definition: llist.cc:39
void * operator[](int n)
Definition: llist.h:67
LList(void)
Definition: llist.cc:16
LNode * top_
Definition: llist.h:115
void * find(void *key)
Definition: llist.cc:150
void incc(int which=0)
Definition: llist.cc:220
LNode * btm_
Definition: llist.h:115
void * pop(void)
Definition: llist.cc:56
int restore(int size, FILE *fp)
Definition: llist.cc:248
int n(void) const
Definition: llist.h:102
int n_
Definition: llist.h:114
int putontop(void *data)
Definition: llist.cc:168
int insert(void *data, void *key)
Definition: llist.cc:103
void * findn(int n)
Definition: llist.cc:195
LNode * findnode(void *key)
Definition: llist.cc:272
int index(void *data)
Definition: llist.cc:203
void decc(int which=0)
Definition: llist.cc:224
void save(int size, FILE *fp)
Definition: llist.cc:234
int(* cmp_fun_)(void *data, void *key)
Definition: llist.h:119
void * removematch(void *key)
Definition: llist.cc:158
#define MAXCURR
Definition: llist.h:12