Lolly 1.4.27
Loading...
Searching...
No Matches
list.hpp
Go to the documentation of this file.
1/** \file list.hpp
2 * \copyright GPLv3
3 * \details linked lists with reference counting
4 * \author Joris van der Hoeven
5 * \date 1999
6 */
7
8#ifndef LIST_H
9#define LIST_H
10
11#include "basic.hpp"
12
13template <class T> class list_rep;
14template <class T> class list;
15
16/**
17 * @brief Check if a list is nil (i.e., an empty list).
18 *
19 * @tparam T The type of the data stored in the list.
20 * @param l The list to be checked.
21 * @return true if the list is nil, false otherwise.
22 */
23template <class T> bool is_nil (list<T> l);
24
25/**
26 * @brief Check if a list is an atom (i.e., a single item).
27 *
28 * @param l The list to be checked.
29 * @return true if the list is an atom, false otherwise.
30 */
31template <class T> bool is_atom (list<T> l);
32
33/**
34 * @brief Check if two lists are strongly equal (i.e., have the same items in
35 * the same order).
36 *
37 * @param l1 The first list to be compared.
38 * @param l2 The second list to be compared.
39 * @return true if the lists are strongly equal, false otherwise.
40 */
41template <class T> bool strong_equal (list<T> l1, list<T> l2);
42
43/**
44 * @brief The list class represents a linked list.
45 *
46 * @tparam T The type of the data stored in the list.
47 */
48template <class T> class list {
50
51 /**
52 * @brief Construct a new list object with a single item.
53 *
54 * @param item The item to be stored in the list.
55 */
56 inline list (T item);
57
58 /**
59 * @brief Construct a new list object with an item and a pointer to the next
60 * node.
61 *
62 * @param item The item to be stored in the list.
63 * @param next A pointer to the next node in the list.
64 */
65 inline list (T item, list<T> next);
66
67 /**
68 * @brief Construct a new list object with two items and a pointer to the next
69 * node.
70 *
71 * @param item1 The first item to be stored in the list.
72 * @param item2 The second item to be stored in the list.
73 * @param next A pointer to the next node in the list.
74 */
75 inline list (T item1, T item2, list<T> next);
76
77 /**
78 * @brief Construct a new list object with three items and a pointer to the
79 * next node.
80 *
81 * @param item1 The first item to be stored in the list.
82 * @param item2 The second item to be stored in the list.
83 * @param item3 The third item to be stored in the list.
84 * @param next A pointer to the next node in the list.
85 */
86 inline list (T item1, T item2, T item3, list<T> next);
87
88 /**
89 * @brief Overloaded subscript operator to access the item at a specific index
90 * in the list.
91 *
92 * @param i The index of the item to be accessed.
93 * @return T& A reference to the item at the specified index.
94 */
95 T& operator[] (int i);
96
97 /**
98 * @brief A static list object used for initializing new list objects.
99 */
100 static list<T> init;
101
102 friend bool is_atom LESSGTR (list<T> l);
104};
105
106extern int list_count;
107
108/**
109 * @brief The list_rep class represents a node in a linked list.
110 *
111 * @tparam T The type of the data stored in the list.
112 */
113template <class T> class list_rep : concrete_struct {
114public:
115 T item; /**< The data stored in the node. */
116 list<T> next; /**< A pointer to the next node in the list. */
117
118 /**
119 * @brief Construct a new list_rep object.
120 *
121 * @param item2 The data to be stored in this node.
122 * @param next2 A pointer to the next node in the list.
123 */
124 inline list_rep<T> (T item2, list<T> next2) : item (item2), next (next2) {
126 }
127
128 /**
129 * @brief Destroy the list_rep object.
130 */
131 inline ~list_rep<T> () { TM_DEBUG (list_count--); }
132 friend class list<T>;
133};
134
136#define TMPL template <class T>
137TMPL inline list<T>::list (T item)
138 : rep (tm_new<list_rep<T>> (item, list<T> ())) {}
139TMPL inline list<T>::list (T item, list<T> next)
140 : rep (tm_new<list_rep<T>> (item, next)) {}
142 : rep (tm_new<list_rep<T>> (item1, list<T> (item2, next))) {}
144 : rep (tm_new<list_rep<T>> (item1, list<T> (item2, item3, next))) {}
145TMPL inline bool
147 return (!is_nil (l)) && is_nil (l->next);
148}
150
151/**
152 * @brief Get the number of items in a list.
153 *
154 * @tparam T The type of the data stored in the list.
155 * @param l The list whose length is to be calculated.
156 * @return int The number of items in the list.
157 */
158TMPL int N (list<T> l);
159
160/**
161 * @brief Create a copy of a list.
162 *
163 * @tparam T The type of the data stored in the list.
164 * @param l The list to be copied.
165 * @return list<T> A copy of the input list.
166 */
168
169/**
170 * @brief Create a new list by appending an item to the end of an existing list.
171 *
172 * @tparam T The type of the data stored in the lists.
173 * @param l1 The list to which the item will be appended.
174 * @param x The item to be appended.
175 * @return list<T> A new list consisting of the input list with the item
176 * appended.
177 */
179
180/**
181 * @brief Create a new list by concatenating two existing lists.
182 *
183 * @tparam T The type of the data stored in the lists.
184 * @param l1 The first list to be concatenated.
185 * @param l2 The second list to be concatenated.
186 * @return list<T> A new list consisting of the items in the input lists in
187 * order.
188 */
190
191/**
192 * @brief Get the first n items of a list.
193 *
194 * @tparam T The type of the data stored in the list.
195 * @param l The list whose items are to be retrieved.
196 * @param n The number of items to retrieve (default is 1).
197 * @return list<T> A new list consisting of the first n items of the input list.
198 */
199TMPL list<T> head (list<T> l, int n= 1);
200
201/**
202 * @brief Get all but the first n items of a list.
203 *
204 * @tparam T The type of the data stored in the list.
205 * @param l The list whose items are to be retrieved.
206 * @param n The number of items to skip (default is 1).
207 * @return list<T> A new list consisting of all but the first n items of the
208 * input list.
209 */
210TMPL list<T> tail (list<T> l, int n= 1);
211
212/**
213 * @brief Return the last item of the list.
214 * The input list must not be an empty list.
215 *
216 * @tparam T
217 * @param l the list
218 * @return T
219 */
221
222/**
223 * @brief Get a reference to the last item in a list.
224 *
225 * @tparam T The type of the data stored in the list.
226 * @param l The list whose last item is to be accessed.
227 * @return T& A reference to the last item in the list.
228 */
229TMPL T& access_last (list<T>& l);
230
231/**
232 * @brief Remove the last item from a list.
233 *
234 * @tparam T The type of the data stored in the list.
235 * @param l The list from which the last item is to be removed.
236 * @return list<T>& A reference to the modified list.
237 */
239
240/**
241 * @brief Create a new list with the items in reverse order.
242 *
243 * @tparam T The type of the data stored in the list.
244 * @param l The list to be reversed.
245 * @return list<T> A new list with the items in reverse order.
246 */
248
249/**
250 * @brief Create a new list with a specific item removed.
251 *
252 * @tparam T The type of the data stored in the list.
253 * @param l The list from which the item is to be removed.
254 * @param what The item to be removed.
255 * @return list<T> A new list with the specified item removed.
256 */
258
259/**
260 * @brief Check if a list contains a specific item.
261 *
262 * @tparam T The type of the data stored in the list.
263 * @param l The list to be searched.
264 * * @param what The item to search for.
265 * @return true if the item is found in the list, false otherwise.
266 */
267TMPL bool contains (list<T> l, T what);
268
270TMPL list<T>& operator<< (list<T>& l, T item);
272TMPL list<T>& operator>> (T item, list<T>& l);
273TMPL list<T>& operator<< (T& item, list<T>& l);
278#undef TMPL
279
280#include "list.ipp"
281
282#endif // defined LIST_H
#define CONCRETE_NULL_TEMPLATE_CODE(PTR, TT, T)
Code for the templated concrete null pointer type.
Definition classdef.hpp:395
#define TM_DEBUG(x)
Debugging macro used to disable debugging output.
Definition classdef.hpp:13
The list_rep class represents a node in a linked list.
Definition list.hpp:113
list< T > next
Definition list.hpp:116
The list class represents a linked list.
Definition list.hpp:48
list(T item1, T item2, list< T > next)
Construct a new list object with two items and a pointer to the next node.
Definition list.hpp:141
list(T item1, T item2, T item3, list< T > next)
Construct a new list object with three items and a pointer to the next node.
Definition list.hpp:143
list(T item)
Construct a new list object with a single item.
Definition list.hpp:137
list(T item, list< T > next)
Construct a new list object with an item and a pointer to the next node.
Definition list.hpp:139
friend bool is_atom LESSGTR(list< T > l)
CONCRETE_NULL_TEMPLATE(list, T)
static list< T > init
A static list object used for initializing new list objects.
Definition list.hpp:100
T & operator[](int i)
Overloaded subscript operator to access the item at a specific index in the list.
Definition list.ipp:37
friend bool strong_equal LESSGTR(list< T > l1, list< T > l2)
C * tm_new()
int list_count
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
bool is_nil(list< T > l)
Check if a list is nil (i.e., an empty list).
list< T > copy(list< T > l)
Create a copy of a list.
Definition list.ipp:164
list< T > tail(list< T > l, int n=1)
Get all but the first n items of a list.
Definition list.ipp:193
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 > head(list< T > l, int n=1)
Get the first n items of a list.
Definition list.ipp:185
list< T > & operator>>(T item, list< T > &l)
Definition list.ipp:65
bool is_atom(list< T > l)
Check if a list is an atom (i.e., a single item).
Definition list.hpp:146
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
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
#define TMPL
Definition list.hpp:136
bool contains(list< T > l, T what)
Check if a list contains a specific item.
Definition list.ipp:222
Structure representing a concrete object with a reference count.
Definition classdef.hpp:24
base class of resources
Definition resource.hpp:23