Edinburgh Speech Tools 2.4-release
list_example.cc
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1996,1997 */
6 /* All Rights Reserved. */
7 /* Permission is hereby granted, free of charge, to use and distribute */
8 /* this software and its documentation without restriction, including */
9 /* without limitation the rights to use, copy, modify, merge, publish, */
10 /* distribute, sublicense, and/or sell copies of this work, and to */
11 /* permit persons to whom this work is furnished to do so, subject to */
12 /* the following conditions: */
13 /* 1. The code must retain the above copyright notice, this list of */
14 /* conditions and the following disclaimer. */
15 /* 2. Any modifications must be clearly marked as such. */
16 /* 3. Original authors' names are not deleted. */
17 /* 4. The authors' names are not used to endorse or promote products */
18 /* derived from this software without specific prior written */
19 /* permission. */
20 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
21 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
22 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
23 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
24 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
25 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
26 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
27 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
28 /* THIS SOFTWARE. */
29 /* */
30 /*************************************************************************/
31 /* */
32 /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */
33 /* Date: Tue Jul 22 1997 */
34 /* --------------------------------------------------------------------- */
35 /* Example of list class use. */
36 /* */
37 /*************************************************************************/
38
39#include <cstdlib>
40#include <iostream>
41#include "EST_bool.h"
42#include "EST_TList.h"
43#include "EST_String.h"
44#include "EST_util_class.h"
45#include "EST_types.h"
46bool second_char_gt(const EST_UItem *uv1, const EST_UItem *uv2);
47
48/**@name EST_TList:example
49 *
50 * some stuff about lists
51 *
52 * @see EST_TList
53 * @see EST_TKVL
54 * @see EST_Option
55 */
56//@{
57
58int main(void)
59{
60
61 EST_String strings[] = {"quail", "wood pigeon", "eagle", "emu", "rook" }; //decl
62
63 // There are a number of predefined list types for EST_TList.
64 // EST_StrList is EST_TList<EST_String>.
65 EST_StrList slist; // decl
66 EST_Litem *p; //decl
67
68 /**@name Inserting items into a list
69
70 There is no easy way to initialise a list so we'll just set it
71 from the strings array.
72 */
73
74 //@{ code
75 // append adds items on to the end of a list
76 for (unsigned int i1 = 0; i1 < sizeof(strings) /sizeof(strings[0]); i1++)
77 slist.append(strings[i1]);
78
79 // add item to start of list
80 slist.prepend("dove");
81
82 // find pointer to "eagle", add "hawk" before it, and then add sparrow
83 // after "hawk"
84 for (p = slist.head(); p != 0; p = p->next())
85 if (slist(p) == "eagle")
86 {
87 p = slist.insert_before(p,"hawk");
88 p = slist.insert_after(p,"sparrow");
89 }
90
91 //@} code
92
93
94 /**@name Iteration over a list
95
96 A dummy pointer of type \Ref{EST_Litem} is used to iterate
97 through a list. This acts somewhat like the index in an array in
98 that it is used to access an item, in the list but does not
99 contain a value itself.
100
101 Iteration is usually done in a for loop. Initialisation involves
102 setting the pointer to the head() function. Increments are done
103 by the next() function. At the end of the list, the pointer will
104 be set to null, and this can be used to check for the end.
105
106 Items in the list are accessed by passing the pointer is as the
107 argument to the function operator(), as in the following example.
108 */
109 //@{ code
110 cout << "[ List Accessed by LItem\n";
111 // print out contents of array.
112 for (p = slist.head(); p != 0; p = p->next())
113 cout << " " << slist(p) << "\n";
114 cout << "]\n";
115
116 // items can also be accessed by their position in the list by using the
117 // nth() function. The length() function returns the number of items
118 // in a list.
119 cout << "\n[ List Accessed by integer index\n";
120 for (int i2 = 0; i2 < slist.length(); ++i2)
121 cout << " " << slist.nth(i2) << "\n";
122 cout << "]\n";
123 //@} code
124
125 /**@name Accessing elements of a list
126
127 The normal way to access an item is to use the \Ref{EST_Litem}
128 in conjunction with the () operator. Other functions also exist,
129 eg. first(), last() and nth(). Const and non-const version of
130 each access function exist, allowing both reading and writing.
131 */
132
133 //@{ code
134 // Capital;ise all 'e's in all strings
135 for (p = slist.head(); p != 0; p = p->next())
136 slist(p).gsub("e", "E");
137
138 // print out last item in list
139 p = slist.tail();
140 cout << "Last item: " << slist(p) << endl;
141
142 // but a more direct method is
143 cout << "Last item: " << slist.last() << endl;
144
145 // likewise with the head of the list:
146 cout << "First item: " << slist.first() << endl;
147
148 // print out the 4th item:
149 cout << "4th item: " << slist.nth(4) << endl;
150
151 // All these can be used for overwriting existing members in the list.
152 // To add new members use append(), prepend(), insert_before() or
153 // insert_after() as shown in \Ref{Addition}
154
155 slist.first() = "Swallow";
156 slist.last() = "TurkEy";
157 slist.nth(2) = "SEagull";
158
159 //@} code
160
161 cout << "\n[ List After Substitutions and Replacements\n";
162 for (p = slist.head(); p != 0; p = p->next())
163 cout << " " << slist(p) << "\n";
164 cout << "]\n";
165
166 /**@name Removing items from a list.
167 Removing items from lists is done by having the EST_Litem point
168 to a particular item, and then passing this pointer to the
169 remove function. This can be tricky as this leaves the EST_Litem
170 pointer pointing to a non-existent item. To get round this, the
171 remove() function returns a pointer to the previous item in the
172 list.
173 */
174 //@{ code
175
176 // In the following example, the item "eagle" is removed and a
177 // pointer to the previous item is returned. The for loop then
178 // points this to the next item in the loop, giving the appearance
179 // of seamless iteration.
180
181 for (p = slist.head(); p != 0; p = p->next())
182 if (slist(p) == "EaglE")
183 p = slist.remove(p);
184
185 //@} code
186
187 cout << "\n[ List After Removing Eagle\n";
188 for (p = slist.head(); p != 0; p = p->next())
189 cout << " " << slist(p) << "\n";
190 cout << "]\n";
191
192 /**@name reverse the list.
193 */
194
195 //@{
196 slist.reverse();
197 //@}
198
199
200 cout << "\n[ List After Reverse\n";
201 for (p = slist.head(); p != 0; p = p->next())
202 cout << " " << slist(p) << "\n";
203 cout << "]\n";
204
205 /**@name Sorting a list
206 *
207 * A number of sort routines for lists are defined. The most useful
208 * are probably sort (a simple bubble sort, quick for small lists)
209 * and qsort (quick-sort, faster for long lists).
210 *
211 * If the default collation order is not what you want you can pass
212 * a comparison operator to the sort routine.
213 */
214
215 //@{ code
216
217 // Sort into alphabetical order
218 sort(slist);
219
220 cout << "\n[ Sorted\n";
221 for(p=slist.head(); p ; p=p->next())
222 cout << " " << slist(p) << "\n";
223 cout << "]\n";
224
225 // Sort by second character.
226 qsort(slist,&second_char_gt );
227
228 cout << "\n[ Sorted by second character\n";
229 for(p=slist.head(); p ; p=p->next())
230 cout << " " << slist(p) << "\n";
231 cout << "]\n";
232 //@} code
233
234}
235
236/**@name Comparison Operation Used in Sort
237 *
238 * Compares the second character of Strings.
239 */
240
241//@{ code
242bool second_char_gt(const EST_UItem *uv1, const EST_UItem *uv2)
243{
244 const EST_TItem<EST_String> *val1 = (const EST_TItem<EST_String> *)uv1;
245 const EST_TItem<EST_String> *val2 = (const EST_TItem<EST_String> *)uv2;
246
247 return (bool)(val1->val(1) > val2->val(1));
248}
249//@} code
250
251//@}
252
253// we would need to include the following template
254// declarations if lists of strings weren't already declared.
255// then this is only useful for and legal for
256// things which have < == and > defined
257
258// template class EST_TList<EST_String>;
259// template class EST_TItem<EST_String>;
260// template class EST_TSortable<EST_String>;
261
262// declare the template routines we use.
263
264//template void sort(EST_TList<EST_String> &a,
265// bool (*gt)(const EST_UItem *, const EST_UItem *));
266//template void qsort(EST_TList<EST_String> &a,
267// bool (*gt)(const EST_UItem *, const EST_UItem *));
268
const T & last() const
return const reference to last item in list
Definition: EST_TList.h:149
T & nth(int n)
return the Nth value
Definition: EST_TList.h:139
void append(const T &item)
add item onto end of list
Definition: EST_TList.h:191
EST_Litem * remove(EST_Litem *ptr)
Definition: EST_TList.h:175
EST_Litem * insert_after(EST_Litem *ptr, const T &item)
Definition: EST_TList.h:200
EST_Litem * insert_before(EST_Litem *ptr, const T &item)
Definition: EST_TList.h:206
const T & first() const
return const reference to first item in list
Definition: EST_TList.h:146
void prepend(const T &item)
add item onto start of list
Definition: EST_TList.h:194