NAMD
Set.C
Go to the documentation of this file.
1 
7 #include "InfoStream.h"
8 #include "Set.h"
9 #include "elements.h"
10 
11 //#define DEBUG_IRSET
12 
13 #ifdef DEBUG_IRSET
14 void NAMD_bug(const char *);
15 #endif
16 
18 {
19  head = (listNode *) 0;
20  nElements = 0;
21 }
22 
24  // delete all links; don't delete the InfoRecord objects
25  listNode *tmp;
26  for (listNode *link = head; link; link = tmp) {
27  tmp = link->next;
28  delete link;
29  }
30 }
31 
33 {
34 #ifdef DEBUG_IRSET
35  if (find(info)) NAMD_bug("IRSet::unchecked_insert duplicate");
36 #endif
37  ++nElements;
38  listNode *node = new listNode(info);
39  node->next = head;
40  head = node;
41 #ifdef DEBUG_IRSET
42  int n = 0;
43  while (node) { ++n; node = node->next; }
44  if ( n != nElements ) NAMD_bug("IRSet::unchecked_insert count");
45 #endif
46 }
47 
48 
50 {
51  if (!find(info))
52  {
53  ++nElements;
54  listNode *node = new listNode(info);
55  node->next = head;
56  head = node;
57 #ifdef DEBUG_IRSET
58  int n = 0;
59  while (node) { ++n; node = node->next; }
60  if ( n != nElements ) NAMD_bug("IRSet::insert count");
61 #endif
62  }
63 
64 }
65 
66 
68 {
69  if ((*n)->info == r)
70  *n = (*n)->next;
71  else
72  myRemove(&((*n)->next), r);
73 }
74 
76 {
77 #ifdef DEBUG_IRSET
78  listNode *node = head;
79  int n = 0;
80  while (node) { ++n; node = node->next; }
81  if ( n != nElements ) NAMD_bug("IRSet::remove count");
82 #endif
83 
84  if (!head)
85  return 0;
86 
87  listNode *p = head;
88  listNode *q = p->next;
89 
90  if (p->info == r){
91  head = q;
92  delete p;
93  --nElements;
94  return 1;
95  }
96 
97  while (q){
98  if (q->info == r){
99  p->next = q->next;
100  delete q;
101  --nElements;
102  return 1;
103  }
104  else {
105  p = q;
106  q = q->next;
107  }
108  }
109  return 0;
110 }
111 
113 {
114  listNode *p = head;
115  while (p) {
116  if (p->info == r) return 1;
117  else p = p->next;
118  }
119  return 0;
120 }
121 
123 {
124  if (head){
125  iter->next = head->next;
126  return head->info;
127  }
128  return 0;
129 }
130 
132 {
133  // std::cout << "set::next: " << iter->next << "\n";
134  if (!iter->next)
135  { return 0;
136  }
137  // std::cout << "set::next: iter->next->info=" << iter->next->info << "\n";
138  InfoRecord *temp = iter->next->info;
139  iter->next = iter->next->next;
140  return temp;
141 }
142 
143 
145 {
146  return nElements;
147 }
148 
150 {
151  return ! ! head;
152 }
153 
155 {
156  listNode *p = head;
157  while (p){
158  if ( p->info ) iout << p->info->Id << " ";
159  else iout << "NULL ";
160  p = p->next;
161  }
162 }
IRSet()
Definition: Set.C:17
Definition: Set.h:12
int remove(InfoRecord *)
Definition: Set.C:75
listNode * next
Definition: Set.h:22
int numElements()
Definition: Set.C:144
int Id
Definition: elements.h:16
#define iout
Definition: InfoStream.h:87
void insert(InfoRecord *)
Definition: Set.C:49
InfoRecord *const info
Definition: Set.h:15
InfoRecord * next(Iterator *)
Definition: Set.C:131
void NAMD_bug(const char *err_msg)
Definition: common.C:123
CompAtomList p
Definition: Patch.h:146
int hasElements()
Definition: Set.C:149
listNode * next
Definition: Set.h:14
Definition: Set.h:19
void unchecked_insert(InfoRecord *)
Definition: Set.C:32
void myRemove(listNode **n, InfoRecord *r)
Definition: Set.C:67
void print()
Definition: Set.C:154
InfoRecord * iterator(Iterator *)
Definition: Set.C:122
int find(InfoRecord *)
Definition: Set.C:112
~IRSet()
Definition: Set.C:23