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