Lolly 1.4.28
Loading...
Searching...
No Matches
Macros | Functions
list.ipp File Reference
#include "list.hpp"
Include dependency graph for list.ipp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define LIST_CC
 

Functions

template<class T >
tm_ostreamoperator<< (tm_ostream &out, list< T > l)
 
template<class T >
list< T > & operator<< (list< T > &l, T item)
 
template<class T >
list< T > & operator<< (list< T > &l1, list< T > l2)
 
template<class T >
list< T > & operator>> (T item, list< T > &l)
 
template<class T >
list< T > & operator<< (T &item, list< T > &l)
 
template<class T >
last_item (list< T > l)
 Return the last item of the list. The input list must not be an empty list.
 
template<class T >
T & access_last (list< T > &l)
 Get a reference to the last item in a list.
 
template<class T >
list< T > & suppress_last (list< T > &l)
 Remove the last item from a list.
 
template<class T >
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).
 
template<class T >
bool operator== (list< T > l1, list< T > l2)
 
template<class T >
bool operator!= (list< T > l1, list< T > l2)
 
template<class T >
bool operator< (list< T > l1, list< T > l2)
 
template<class T >
bool operator<= (list< T > l1, list< T > l2)
 
template<class T >
int N (list< T > l)
 Get the number of items in a list.
 
template<class T >
list< T > copy (list< T > l)
 Create a copy of a list.
 
template<class T >
list< T > operator* (list< T > l1, T x)
 Create a new list by appending an item to the end of an existing list.
 
template<class T >
list< T > operator* (list< T > l1, list< T > l2)
 Create a new list by concatenating two existing lists.
 
template<class T >
list< T > head (list< T > l, int n)
 Get the first n items of a list.
 
template<class T >
list< T > tail (list< T > l, int n)
 Get all but the first n items of a list.
 
template<class T >
list< T > reverse (list< T > l)
 Create a new list with the items in reverse order.
 
template<class T >
list< T > remove (list< T > l, T what)
 Create a new list with a specific item removed.
 
template<class T >
bool contains (list< T > l, T what)
 Check if a list contains a specific item.
 

Macro Definition Documentation

◆ LIST_CC

#define LIST_CC

Definition at line 13 of file list.ipp.

Function Documentation

◆ operator<<() [1/4]

template<class T >
tm_ostream & operator<< ( tm_ostream & out,
list< T > l )

Definition at line 21 of file list.ipp.

22 {
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}
bool is_nil(blackbox x)
Definition blackbox.hpp:29

◆ operator<<() [2/4]

template<class T >
list< T > & operator<< ( list< T > & l,
T item )

Definition at line 37 of file list.ipp.

49 {
50 if (is_nil (l)) l= list<T> (item, list<T> ());
51 else l->next << item;
52 return l;
53}
The list class represents a linked list.
Definition list.hpp:48

◆ operator<<() [3/4]

template<class T >
list< T > & operator<< ( list< T > & l1,
list< T > l2 )

Definition at line 37 of file list.ipp.

57 {
58 if (is_nil (l1)) l1= l2;
59 else l1->next << l2;
60 return l1;
61}

◆ operator>>()

template<class T >
list< T > & operator>> ( T item,
list< T > & l )

Definition at line 65 of file list.ipp.

65 {
66 return (l= list<T> (item, l));
67}

◆ operator<<() [4/4]

template<class T >
list< T > & operator<< ( T & item,
list< T > & l )

Definition at line 65 of file list.ipp.

71 {
72 item= l->item;
73 l = l->next;
74 return l;
75}

◆ last_item()

template<class T >
T last_item ( list< T > l)

Return the last item of the list. The input list must not be an empty list.

Template Parameters
T
Parameters
lthe list
Returns
T

Definition at line 79 of file list.ipp.

79 {
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}
#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

◆ access_last()

template<class T >
T & access_last ( list< T > & l)

Get a reference to the last item in a list.

Template Parameters
TThe type of the data stored in the list.
Parameters
lThe list whose last item is to be accessed.
Returns
T& A reference to the last item in the list.

Definition at line 89 of file list.ipp.

89 {
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}
T & access_last(list< T > &l)
Get a reference to the last item in a list.
Definition list.ipp:89

◆ suppress_last()

template<class T >
list< T > & suppress_last ( list< T > & l)

Remove the last item from a list.

Template Parameters
TThe type of the data stored in the list.
Parameters
lThe list from which the last item is to be removed.
Returns
list<T>& A reference to the modified list.

Definition at line 97 of file list.ipp.

97 {
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}
list< T > & suppress_last(list< T > &l)
Remove the last item from a list.
Definition list.ipp:97

◆ strong_equal()

template<class T >
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).

Parameters
l1The first list to be compared.
l2The second list to be compared.
Returns
true if the lists are strongly equal, false otherwise.

Definition at line 110 of file list.ipp.

110 {
111 return l1.rep == l2.rep;
112}

◆ operator==()

template<class T >
bool operator== ( list< T > l1,
list< T > l2 )

Definition at line 116 of file list.ipp.

116 {
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}

◆ operator!=()

template<class T >
bool operator!= ( list< T > l1,
list< T > l2 )

Definition at line 126 of file list.ipp.

126 {
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}

◆ operator<()

template<class T >
bool operator< ( list< T > l1,
list< T > l2 )

Definition at line 132 of file list.ipp.

133 {
134 if (is_nil (l1) || is_nil (l2)) return !is_nil (l2);
135 return (l1->item == l2->item) && (l1->next < l2->next);
136}

◆ operator<=()

template<class T >
bool operator<= ( list< T > l1,
list< T > l2 )

Definition at line 139 of file list.ipp.

140 {
141 if (is_nil (l1) || is_nil (l2)) return is_nil (l1);
142 return (l1->item == l2->item) && (l1->next <= l2->next);
143}

◆ N()

template<class T >
int N ( list< T > l)

Get the number of items in a list.

Template Parameters
TThe type of the data stored in the list.
Parameters
lThe list whose length is to be calculated.
Returns
int The number of items in the list.

Definition at line 151 of file list.ipp.

151 {
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}

◆ copy()

template<class T >
list< T > copy ( list< T > l)

Create a copy of a list.

Template Parameters
TThe type of the data stored in the list.
Parameters
lThe list to be copied.
Returns
list<T> A copy of the input list.

Definition at line 164 of file list.ipp.

164 {
165 if (is_nil (l)) return list<T> ();
166 else return list<T> (l->item, copy (l->next));
167}
list< T > copy(list< T > l)
Create a copy of a list.
Definition list.ipp:164

◆ operator*() [1/2]

template<class T >
list< T > operator* ( list< T > l1,
T x )

Create a new list by appending an item to the end of an existing list.

Template Parameters
TThe type of the data stored in the lists.
Parameters
l1The list to which the item will be appended.
xThe item to be appended.
Returns
list<T> A new list consisting of the input list with the item appended.

Definition at line 171 of file list.ipp.

171 {
172 if (is_nil (l1)) return x;
173 else return list<T> (l1->item, l1->next * x);
174}

◆ operator*() [2/2]

template<class T >
list< T > operator* ( list< T > l1,
list< T > l2 )

Create a new list by concatenating two existing lists.

Template Parameters
TThe type of the data stored in the lists.
Parameters
l1The first list to be concatenated.
l2The second list to be concatenated.
Returns
list<T> A new list consisting of the items in the input lists in order.

Definition at line 178 of file list.ipp.

178 {
179 if (is_nil (l1)) return copy (l2);
180 else return list<T> (l1->item, l1->next * l2);
181}

◆ head()

template<class T >
list< T > head ( list< T > l,
int n = 1 )

Get the first n items of a list.

Template Parameters
TThe type of the data stored in the list.
Parameters
lThe list whose items are to be retrieved.
nThe number of items to retrieve (default is 1).
Returns
list<T> A new list consisting of the first n items of the input list.

Definition at line 185 of file list.ipp.

185 {
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}
list< T > head(list< T > l, int n)
Get the first n items of a list.
Definition list.ipp:185

◆ tail()

template<class T >
list< T > tail ( list< T > l,
int n = 1 )

Get all but the first n items of a list.

Template Parameters
TThe type of the data stored in the list.
Parameters
lThe list whose items are to be retrieved.
nThe number of items to skip (default is 1).
Returns
list<T> A new list consisting of all but the first n items of the input list.

Definition at line 193 of file list.ipp.

193 {
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}

◆ reverse()

template<class T >
list< T > reverse ( list< T > l)

Create a new list with the items in reverse order.

Template Parameters
TThe type of the data stored in the list.
Parameters
lThe list to be reversed.
Returns
list<T> A new list with the items in reverse order.

Definition at line 203 of file list.ipp.

203 {
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}

◆ remove()

template<class T >
list< T > remove ( list< T > l,
T what )

Create a new list with a specific item removed.

Template Parameters
TThe type of the data stored in the list.
Parameters
lThe list from which the item is to be removed.
whatThe item to be removed.
Returns
list<T> A new list with the specified item removed.

Definition at line 214 of file list.ipp.

214 {
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}

◆ contains()

template<class T >
bool contains ( list< T > l,
T what )

Check if a list contains a specific item.

Template Parameters
TThe type of the data stored in the list.
Parameters
lThe list to be searched.
whatThe item to search for.
Returns
true if the item is found in the list, false otherwise.

Definition at line 222 of file list.ipp.

222 {
223 if (is_nil (l)) return false;
224 if (l->item == what) return true;
225
226 return contains (l->next, what);
227}
bool contains(list< T > l, T what)
Check if a list contains a specific item.
Definition list.ipp:222