Lolly 1.4.27
Loading...
Searching...
No Matches
list.ipp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : list.cpp
4 * DESCRIPTION: linked lists with reference counting
5 * COPYRIGHT : (C) 1999 Joris van der Hoeven
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12#ifndef LIST_CC
13#define LIST_CC
14#include "list.hpp"
15
16/******************************************************************************
17 * output and convertion
18 ******************************************************************************/
19
20template <class T>
23 out << "[";
24 if (!is_nil (l)) {
25 out << " " << l->item;
26 l= l->next;
27 }
28 while (!is_nil (l)) {
29 out << ", " << l->item;
30 l= l->next;
31 }
32 return out << " ]";
33}
34
35template <class T>
36T&
38 ASSERT (rep != NULL, "list too short");
39 if (i == 0) return rep->item;
40 return rep->next[i - 1];
41}
42
43/******************************************************************************
44 * insertion and suppression
45 ******************************************************************************/
46
47template <class T>
49operator<< (list<T>& l, T item) {
50 if (is_nil (l)) l= list<T> (item, list<T> ());
51 else l->next << item;
52 return l;
53}
54
55template <class T>
58 if (is_nil (l1)) l1= l2;
59 else l1->next << l2;
60 return l1;
61}
62
63template <class T>
65operator>> (T item, list<T>& l) {
66 return (l= list<T> (item, l));
67}
68
69template <class T>
71operator<< (T& item, list<T>& l) {
72 item= l->item;
73 l = l->next;
74 return l;
75}
76
77template <class T>
78T
80 ASSERT (!is_nil (l), "last_item on nil list");
81 while (!is_nil (l->next)) {
82 l= l->next;
83 }
84 return l->item;
85}
86
87template <class T>
88T&
90 ASSERT (!is_nil (l), "access_last on nil list");
91 if (is_nil (l->next)) return l->item;
92 return access_last (l->next);
93}
94
95template <class T>
98 ASSERT (!is_nil (l), "empty path");
99 if (is_nil (l->next)) l= list<T> ();
100 else suppress_last (l->next);
101 return l;
102}
103
104/******************************************************************************
105 * tests
106 ******************************************************************************/
107
108template <class T>
109bool
111 return l1.rep == l2.rep;
112}
113
114template <class T>
115bool
117 bool l1_nil= is_nil (l1), l2_nil= is_nil (l2);
118 if (l1_nil || l2_nil) return l1_nil == l2_nil;
119 if (l1->item != l2->item) return false;
120
121 return l1->next == l2->next;
122}
123
124template <class T>
125bool
127 if (is_nil (l1) || is_nil (l2)) return (is_nil (l1) != is_nil (l2));
128 return (l1->item != l2->item) || (l1->next != l2->next);
129}
130
131template <class T>
132bool
134 if (is_nil (l1) || is_nil (l2)) return !is_nil (l2);
135 return (l1->item == l2->item) && (l1->next < l2->next);
136}
137
138template <class T>
139bool
141 if (is_nil (l1) || is_nil (l2)) return is_nil (l1);
142 return (l1->item == l2->item) && (l1->next <= l2->next);
143}
144
145/******************************************************************************
146 * computations with list<T> structures
147 ******************************************************************************/
148
149template <class T>
150int
152 if (is_nil (l)) return 0;
153
154 int cnt= 1;
155 while (!is_nil (l->next)) {
156 cnt= cnt + 1;
157 l = l->next;
158 }
159 return cnt;
160}
161
162template <class T>
165 if (is_nil (l)) return list<T> ();
166 else return list<T> (l->item, copy (l->next));
167}
168
169template <class T>
172 if (is_nil (l1)) return x;
173 else return list<T> (l1->item, l1->next * x);
174}
175
176template <class T>
179 if (is_nil (l1)) return copy (l2);
180 else return list<T> (l1->item, l1->next * l2);
181}
182
183template <class T>
185head (list<T> l, int n) {
186 if (n == 0) return list<T> ();
187 ASSERT (!is_nil (l), "list too short to get the head");
188 return list<T> (l->item, head (l->next, n - 1));
189}
190
191template <class T>
193tail (list<T> l, int n) {
194 for (; n > 0; n--) {
195 ASSERT (!is_nil (l), "list too short to get the tail");
196 l= l->next;
197 }
198 return l;
199}
200
201template <class T>
204 list<T> r;
205 while (!is_nil (l)) {
206 r= list<T> (l->item, r);
207 l= l->next;
208 }
209 return r;
210}
211
212template <class T>
215 if (is_nil (l)) return l;
216 else if (l->item == what) return remove<T> (l->next, what);
217 else return list<T> (l->item, remove<T> (l->next, what));
218}
219
220template <class T>
221bool
223 if (is_nil (l)) return false;
224 if (l->item == what) return true;
225
226 return contains (l->next, what);
227}
228
229#endif // defined LIST_CC
#define ASSERT(cond, msg)
Macro used to assert that a condition is true, and throw an exception with an error message if the co...
Definition basic.hpp:85
bool is_nil(blackbox x)
Definition blackbox.hpp:29
The list class represents a linked list.
Definition list.hpp:48
T & operator[](int i)
Overloaded subscript operator to access the item at a specific index in the list.
Definition list.ipp:37
bool operator!=(list< T > l1, list< T > l2)
Definition list.ipp:126
list< T > remove(list< T > l, T what)
Create a new list with a specific item removed.
Definition list.ipp:214
bool strong_equal(list< T > l1, list< T > l2)
Check if two lists are strongly equal (i.e., have the same items in the same order).
Definition list.ipp:110
bool operator<(list< T > l1, list< T > l2)
Definition list.ipp:133
bool operator==(list< T > l1, list< T > l2)
Definition list.ipp:116
list< T > copy(list< T > l)
Create a copy of a list.
Definition list.ipp:164
list< T > operator*(list< T > l1, T x)
Create a new list by appending an item to the end of an existing list.
Definition list.ipp:171
list< T > reverse(list< T > l)
Create a new list with the items in reverse order.
Definition list.ipp:203
list< T > & operator>>(T item, list< T > &l)
Definition list.ipp:65
T last_item(list< T > l)
Return the last item of the list. The input list must not be an empty list.
Definition list.ipp:79
bool operator<=(list< T > l1, list< T > l2)
Definition list.ipp:140
list< T > head(list< T > l, int n)
Get the first n items of a list.
Definition list.ipp:185
list< T > tail(list< T > l, int n)
Get all but the first n items of a list.
Definition list.ipp:193
int N(list< T > l)
Get the number of items in a list.
Definition list.ipp:151
T & access_last(list< T > &l)
Get a reference to the last item in a list.
Definition list.ipp:89
tm_ostream & operator<<(tm_ostream &out, list< T > l)
Definition list.ipp:22
list< T > & suppress_last(list< T > &l)
Remove the last item from a list.
Definition list.ipp:97
bool contains(list< T > l, T what)
Check if a list contains a specific item.
Definition list.ipp:222
base class of resources
Definition resource.hpp:23