Edinburgh Speech Tools  2.1-release
list_example.cc
Go to the documentation of this file.
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"
46 
47 using namespace std;
48 
49 bool second_char_gt(const EST_UItem *uv1, const EST_UItem *uv2);
50 
51 
52 int main(void)
53 {
54 
55  EST_String strings[] = {"quail", "wood pigeon", "eagle", "emu", "rook" }; //decl
56 
57  // There are a number of predefined list types for EST_TList.
58  // EST_StrList is EST_TList<EST_String>.
59  EST_StrList slist; // decl
60  EST_Litem *p; //decl
61 
62  //@ code
63  // append adds items on to the end of a list
64  for (unsigned int i1 = 0; i1 < sizeof(strings) /sizeof(strings[0]); i1++)
65  slist.append(strings[i1]);
66 
67  // add item to start of list
68  slist.prepend("dove");
69 
70  // find pointer to "eagle", add "hawk" before it, and then add sparrow
71  // after "hawk"
72  for (p = slist.head(); p != 0; p = p->next())
73  if (slist(p) == "eagle")
74  {
75  p = slist.insert_before(p,"hawk");
76  p = slist.insert_after(p,"sparrow");
77  } //@ endcode
78 
79 
80 
81  //@ code
82  cout << "[ List Accessed by LItem\n";
83  // print out contents of array.
84  for (p = slist.head(); p != 0; p = p->next())
85  cout << " " << slist(p) << "\n";
86  cout << "]\n";
87 
88  // items can also be accessed by their position in the list by using the
89  // nth() function. The length() function returns the number of items
90  // in a list.
91  cout << "\n[ List Accessed by integer index\n";
92  for (int i2 = 0; i2 < slist.length(); ++i2)
93  cout << " " << slist.nth(i2) << "\n";
94  cout << "]\n";
95  //@ endcode
96 
97 
98 
99  //@ code
100  // Capital;ise all 'e's in all strings
101  for (p = slist.head(); p != 0; p = p->next())
102  slist(p).gsub("e", "E");
103 
104  // print out last item in list
105  p = slist.tail();
106  cout << "Last item: " << slist(p) << endl;
107 
108  // but a more direct method is
109  cout << "Last item: " << slist.last() << endl;
110 
111  // likewise with the head of the list:
112  cout << "First item: " << slist.first() << endl;
113 
114  // print out the 4th item:
115  cout << "4th item: " << slist.nth(4) << endl;
116 
117  // All these can be used for overwriting existing members in the list.
118  // To add new members use append(), prepend(), insert_before() or
119  // insert_after() as shown in \ref Addition
120 
121  slist.first() = "Swallow";
122  slist.last() = "TurkEy";
123  slist.nth(2) = "SEagull";
124 
125  //@ endcode
126 
127  cout << "\n[ List After Substitutions and Replacements\n";
128  for (p = slist.head(); p != 0; p = p->next())
129  cout << " " << slist(p) << "\n";
130  cout << "]\n";
131 
132 
133  //@ code
134 
135  // In the following example, the item "eagle" is removed and a
136  // pointer to the previous item is returned. The for loop then
137  // points this to the next item in the loop, giving the appearance
138  // of seamless iteration.
139 
140  for (p = slist.head(); p != 0; p = p->next())
141  if (slist(p) == "EaglE")
142  p = slist.remove(p);
143 
144  //@ endcode
145 
146  cout << "\n[ List After Removing Eagle\n";
147  for (p = slist.head(); p != 0; p = p->next())
148  cout << " " << slist(p) << "\n";
149  cout << "]\n";
150 
151  //@ code
152  slist.reverse();
153  //@ endcode
154 
155 
156  cout << "\n[ List After Reverse\n";
157  for (p = slist.head(); p != 0; p = p->next())
158  cout << " " << slist(p) << "\n";
159  cout << "]\n";
160 
161  //@ code
162 
163  // Sort into alphabetical order
164  sort(slist);
165 
166  cout << "\n[ Sorted\n";
167  for(p=slist.head(); p ; p=p->next())
168  cout << " " << slist(p) << "\n";
169  cout << "]\n";
170 
171  // Sort by second character.
172  qsort(slist,&second_char_gt );
173 
174  cout << "\n[ Sorted by second character\n";
175  for(p=slist.head(); p ; p=p->next())
176  cout << " " << slist(p) << "\n";
177  cout << "]\n";
178  //@ endcode
179 
180 }
181 
182 //@ code
183 bool second_char_gt(const EST_UItem *uv1, const EST_UItem *uv2)
184 {
185  const EST_TItem<EST_String> *val1 = (const EST_TItem<EST_String> *)uv1;
186  const EST_TItem<EST_String> *val2 = (const EST_TItem<EST_String> *)uv2;
187 
188  return (bool)(val1->val(1) > val2->val(1));
189 }
190 //@ endcode
191 
192 //@}
193 
194 // we would need to include the following template
195 // declarations if lists of strings weren't already declared.
196 // then this is only useful for and legal for
197 // things which have < == and > defined
198 
199 // template class EST_TList<EST_String>;
200 // template class EST_TItem<EST_String>;
201 // template class EST_TSortable<EST_String>;
202 
203 // declare the template routines we use.
204 
205 //template void sort(EST_TList<EST_String> &a,
206 // bool (*gt)(const EST_UItem *, const EST_UItem *));
207 //template void qsort(EST_TList<EST_String> &a,
208 // bool (*gt)(const EST_UItem *, const EST_UItem *));
209 
210 
211 
212 /**@page EST_TList-example EST_TList example
213  @brief Example of list class use.
214  @dontinclude list_example.cc
215 
216  @section insert Inserting items into a list
217  There is no easy way to initialise a list so we'll just set it
218  from the strings array.
219 
220  @skipline //@ code
221  @until //@ endcode
222 
223  @section iteration Iteration over a list
224 
225  A dummy pointer of type \ref EST_Litem is used to iterate
226  through a list. This acts somewhat like the index in an array in
227  that it is used to access an item, in the list but does not
228  contain a value itself.
229 
230  Iteration is usually done in a for loop. Initialisation involves
231  setting the pointer to the head() function. Increments are done
232  by the next() function. At the end of the list, the pointer will
233  be set to null, and this can be used to check for the end.
234 
235  Items in the list are accessed by passing the pointer is as the
236  argument to the function operator(), as in the following example.
237 
238  @skipline //@ code
239  @until //@ endcode
240 
241  @section accessing Accessing elements of a list
242 
243  The normal way to access an item is to use the \ref EST_Litem
244  in conjunction with the () operator. Other functions also exist,
245  eg. EST_TList::first(), EST_TList::last() and EST_TList::nth(). Const
246  and non-const version of each access function exist, allowing
247  both reading and writing.
248 
249  @skipline //@ code
250  @until //@ endcode
251 
252  @section removing Removing items from a list.
253  Removing items from lists is done by having the EST_Litem point
254  to a particular item, and then passing this pointer to the
255  remove function. This can be tricky as this leaves the EST_Litem
256  pointer pointing to a non-existent item. To get round this, the
257  EST_TList::remove() function returns a pointer to the previous
258  item in the list.
259 
260  @skipline //@ code
261  @until //@ endcode
262 
263  @section reverse Reverse a list
264 
265  @skipline //@ code
266  @until //@ endcode
267 
268  @section sort Sorting a list
269 
270  A number of sort routines for lists are defined. The most useful
271  are probably sort (a simple bubble sort, quick for small lists)
272  and qsort (quick-sort, faster for long lists).
273 
274  If the default collation order is not what you want you can pass
275  a comparison operator to the sort routine.
276 
277  @skipline //@ code
278  @until //@ endcode
279 
280  @subsection Comparison Operation Used in Sort
281 
282  Compares the second character of Strings.
283 
284  @skipline //@ code
285  @until //@ endcode
286 
287 
288  @see EST_TList
289  @see EST_TKVL
290  @see EST_Option
291  */
int main(void)
Definition: list_example.cc:52
void qsort(EST_TList< T > &a)
EST_Litem * insert_before(EST_Litem *ptr, const T &item)
Definition: EST_TList.h:211
const T & last() const
return const reference to last item in list
Definition: EST_TList.h:155
T & nth(int n)
return the Nth value
Definition: EST_TList.h:145
EST_UItem * next()
Definition: EST_UList.h:55
void reverse()
Definition: EST_UList.cc:239
const T & first() const
return const reference to first item in list
Definition: EST_TList.h:152
EST_UItem * tail() const
Definition: EST_UList.h:99
void append(const T &item)
add item onto end of list
Definition: EST_TList.h:196
int length() const
Definition: EST_UList.cc:57
void sort(EST_TList< T > &a)
bool second_char_gt(const EST_UItem *uv1, const EST_UItem *uv2)
EST_UItem * head() const
Definition: EST_UList.h:97
EST_Litem * insert_after(EST_Litem *ptr, const T &item)
Definition: EST_TList.h:205
EST_Litem * remove(EST_Litem *ptr)
Definition: EST_TList.h:180
void prepend(const T &item)
add item onto start of list
Definition: EST_TList.h:199