Edinburgh Speech Tools  2.1-release
EST_TList example

Example of list class use.

Inserting items into a list

There is no easy way to initialise a list so we'll just set it from the strings array.

//@ code
// append adds items on to the end of a list
for (unsigned int i1 = 0; i1 < sizeof(strings) /sizeof(strings[0]); i1++)
slist.append(strings[i1]);
// add item to start of list
slist.prepend("dove");
// find pointer to "eagle", add "hawk" before it, and then add sparrow
// after "hawk"
for (p = slist.head(); p != 0; p = p->next())
if (slist(p) == "eagle")
{
p = slist.insert_before(p,"hawk");
p = slist.insert_after(p,"sparrow");
} //@ endcode

Iteration over a list

A dummy pointer of type EST_Litem is used to iterate through a list. This acts somewhat like the index in an array in that it is used to access an item, in the list but does not contain a value itself.

Iteration is usually done in a for loop. Initialisation involves setting the pointer to the head() function. Increments are done by the next() function. At the end of the list, the pointer will be set to null, and this can be used to check for the end.

Items in the list are accessed by passing the pointer is as the argument to the function operator(), as in the following example.

//@ code
cout << "[ List Accessed by LItem\n";
// print out contents of array.
for (p = slist.head(); p != 0; p = p->next())
cout << " " << slist(p) << "\n";
cout << "]\n";
// items can also be accessed by their position in the list by using the
// nth() function. The length() function returns the number of items
// in a list.
cout << "\n[ List Accessed by integer index\n";
for (int i2 = 0; i2 < slist.length(); ++i2)
cout << " " << slist.nth(i2) << "\n";
cout << "]\n";
//@ endcode

Accessing elements of a list

The normal way to access an item is to use the EST_Litem in conjunction with the () operator. Other functions also exist, eg. EST_TList::first(), EST_TList::last() and EST_TList::nth(). Const and non-const version of each access function exist, allowing both reading and writing.

//@ code
// Capital;ise all 'e's in all strings
for (p = slist.head(); p != 0; p = p->next())
slist(p).gsub("e", "E");
// print out last item in list
p = slist.tail();
cout << "Last item: " << slist(p) << endl;
// but a more direct method is
cout << "Last item: " << slist.last() << endl;
// likewise with the head of the list:
cout << "First item: " << slist.first() << endl;
// print out the 4th item:
cout << "4th item: " << slist.nth(4) << endl;
// All these can be used for overwriting existing members in the list.
// To add new members use append(), prepend(), insert_before() or
// insert_after() as shown in \ref Addition
slist.first() = "Swallow";
slist.last() = "TurkEy";
slist.nth(2) = "SEagull";
//@ endcode

Removing items from a list.

Removing items from lists is done by having the EST_Litem point to a particular item, and then passing this pointer to the remove function. This can be tricky as this leaves the EST_Litem pointer pointing to a non-existent item. To get round this, the EST_TList::remove() function returns a pointer to the previous item in the list.

//@ code
// In the following example, the item "eagle" is removed and a
// pointer to the previous item is returned. The for loop then
// points this to the next item in the loop, giving the appearance
// of seamless iteration.
for (p = slist.head(); p != 0; p = p->next())
if (slist(p) == "EaglE")
p = slist.remove(p);
//@ endcode

Reverse a list

//@ code
slist.reverse();
//@ endcode

Sorting a list

A number of sort routines for lists are defined. The most useful are probably sort (a simple bubble sort, quick for small lists) and qsort (quick-sort, faster for long lists).

If the default collation order is not what you want you can pass a comparison operator to the sort routine.

//@ code
// Sort into alphabetical order
sort(slist);
cout << "\n[ Sorted\n";
for(p=slist.head(); p ; p=p->next())
cout << " " << slist(p) << "\n";
cout << "]\n";
// Sort by second character.
cout << "\n[ Sorted by second character\n";
for(p=slist.head(); p ; p=p->next())
cout << " " << slist(p) << "\n";
cout << "]\n";
//@ endcode

Operation Used in Sort

Compares the second character of Strings.

//@ code
bool second_char_gt(const EST_UItem *uv1, const EST_UItem *uv2)
{
const EST_TItem<EST_String> *val1 = (const EST_TItem<EST_String> *)uv1;
const EST_TItem<EST_String> *val2 = (const EST_TItem<EST_String> *)uv2;
return (bool)(val1->val(1) > val2->val(1));
}
//@ endcode

See also
EST_TList
EST_TKVL
EST_Option