Edinburgh Speech Tools  2.1-release
EST_TList.h
Go to the documentation of this file.
1  /*************************************************************************/
2  /* */
3  /* Centre for Speech Technology Research */
4  /* University of Edinburgh, UK */
5  /* Copyright (c) 1995,1996 */
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 : Paul Taylor */
33  /* Date : April 1995 */
34  /* --------------------------------------------------------------------- */
35  /* Double linked list class */
36  /* */
37  /* Modified by RJC, 21/7/97. Now much of the working code is in the */
38  /* UList class, this template class provides a type safe front end to */
39  /* the untyped list. */
40  /* */
41  /*************************************************************************/
42 
43 /** \file
44  * \typedef EST_Litem
45  */
46 
47 #ifndef __Tlist_H__
48 #define __Tlist_H__
49 
50 #include <iostream>
51 
52 #include "EST_common.h"
53 #include "EST_UList.h"
54 #include "EST_TSortable.h"
55 #include "EST_TIterator.h"
56 
57 #include "instantiate/EST_TListI.h"
58 
59 class EST_String;
60 
61 template<class T> class EST_TList;
62 
63 template<class T> class EST_TItem : public EST_UItem {
64 private:
65  static void *operator new(size_t not_used, void *place)
66  {(void)not_used; return place;}
67  static void *operator new(size_t size)
68  {void *p;
69  p = (void *)walloc(char,size);
70  return p;}
71  static void operator delete(void *p)
72  { wfree(p);}
73 
74  static EST_TItem *s_free;
75  static unsigned int s_nfree;
76  static unsigned int s_maxFree;
77 
78 protected:
79  static EST_TItem *make(const T &val);
80  static void release(EST_TItem<T> *it);
81 
82  friend class EST_TList<T>;
83 
84 public:
85  T val;
86 
87  EST_TItem(const T &v) : val(v)
88  { init(); };
90  { init();};
91 };
92 
93 // pretty name
94 
95 /** \typedef EST_Litem
96  * \brief A pretty name for EST_UItem
97  * \see EST_UItem
98  */
100 
101 /** \class EST_TList
102  * @ingroup containerclasses
103 
104 A Template doubly linked list class. This class contains doubly linked
105 lists of a type denoted by `T`. A pointer of type EST_Litem
106 is used to access items in the list. The class supports a variety of
107 ways of adding, removing and accessing items in the list. For examples
108 of how to operate lists, see \ref list_example.
109 
110 Iteration through the list is performed using a pointer of type
111 EST_Litem. See \ref Iteration for example code.
112 
113 */
114 template <class T> class EST_TList : public EST_UList
115 {
116  private:
117  void copy_items(const EST_TList<T> &l);
118  public:
119  void init() { EST_UList::init(); };
120  static void free_item(EST_UItem *item);
121 
122  /**@name Constructor functions */
123  ///@{
124  /// default constructor
125  EST_TList() { };
126  /// copy constructor
127  EST_TList(const EST_TList<T> &l);
128  ~ EST_TList() { clear_and_free(free_item); }
129  ///@}
130 
131  /**@name Access functions for reading and writing items.
132  See \ref EST_TList_Accessing for examples.*/
133 
134  ///@{
135 
136  /** return the value associated with the EST_Litem pointer. This
137  has the same functionality as the overloaded () operator.
138  */
139  T &item(const EST_Litem *p)
140  { return ((EST_TItem<T> *)p) -> val; };
141  /** return a const value associated with the EST_Litem pointer.*/
142  const T &item(const EST_Litem *p) const
143  { return ((EST_TItem<T> *)p) -> val; };
144  /// return the Nth value
145  T &nth(int n)
146  { return item(nth_pointer(n)); };
147  /// return a const Nth value
148  const T &nth(int n) const
149  { return item(nth_pointer(n)); };
150 
151  /// return const reference to first item in list
152  const T &first() const
153  { return item(head()); };
154  /// return const reference to last item in list
155  const T &last() const
156  { return item(tail()); };
157  /** return reference to first item in list
158  * @see last
159  */
160  T &first()
161  { return item(head()); };
162  /// return reference to last item in list
163  T &last()
164  { return item(tail()); };
165 
166  /// return const reference to item in list pointed to by `ptr`
167  const T &operator () (const EST_Litem *ptr) const
168  { return item(ptr); };
169  /// return non-const reference to item in list pointed to by `ptr`
170  T &operator () (const EST_Litem *ptr)
171  { return item(ptr); };
172 
173  ///@}
174 
175  /**@name Removing items in a list.
176  */
177  ///@{
178  /** remove item pointed to by `ptr`, return pointer to previous item.
179  See \ref Removing for example code.*/
180  EST_Litem *remove(EST_Litem *ptr)
181  { return EST_UList::remove(ptr, free_item); };
182 
183  /// remove nth item, return pointer to previous item
185  { return EST_UList::remove(n, free_item); };
186 
187  ///@}
188 
189 
190  /**@name Adding items to a list.
191  In all cases, a complete copy of
192  the item is made and added to the list. See \ref Addition for examples.
193  */
194  ///@{
195  /// add item onto end of list
196  void append(const T &item)
198  /// add item onto start of list
199  void prepend(const T &item)
201 
202  /** add `item` after position given by `ptr`, return pointer
203  to added item. */
204 
205  EST_Litem *insert_after(EST_Litem *ptr, const T &item)
206  { return EST_UList::insert_after(ptr, EST_TItem<T>::make(item)); };
207 
208  /** add `item` before position given by `ptr`, return
209  pointer to added item. */
210 
211  EST_Litem *insert_before(EST_Litem *ptr, const T &item)
212  { return EST_UList::insert_before(ptr, EST_TItem<T>::make(item)); };
213 
214  ///@}
215 
216  /**@name Exchange */
217  ///@{
218  /// exchange 1
220  { EST_UList::exchange(a, b); };
221  /// exchange 2
222  void exchange(int i, int j)
223  { EST_UList::exchange(i,j); };
224  /// exchange 3
225  static void exchange_contents(EST_Litem *a, EST_Litem *b);
226  ///@}
227 
228  /**@name General functions */
229  ///@{
230  /// make full copy of list
231  EST_TList<T> &operator=(const EST_TList<T> &a);
232  /// Add list onto end of existing list
234 
235  /// print list
236  friend std::ostream& operator << (std::ostream &st, EST_TList<T> const &list) {
237  EST_Litem *ptr;
238  for (ptr = list.head(); ptr != 0; ptr = ptr->next())
239  st << list.item(ptr) << " ";
240  return st;
241  }
242 
243  /// remove all items in list
244  void clear(void)
245  { clear_and_free(free_item); };
246  ///@}
247 
248  // Iteration support
249 
250 protected:
251  class IPointer {
252  public:
254  IPointer() {p=NULL;}
255  };
256 
257  void point_to_first(IPointer &ip) const { ip.p = head(); }
258  void move_pointer_forwards(IPointer &ip) const { ip.p = ip.p->next(); }
259  bool points_to_something(const IPointer &ip) const { return ip.p != NULL; }
260  T &points_at(const IPointer &ip) { return item(ip.p); }
261 
262  friend class EST_TIterator< EST_TList<T>, IPointer, T >;
263  friend class EST_TRwIterator< EST_TList<T>, IPointer, T >;
264 
265 public:
266  typedef T Entry;
267  typedef EST_TIterator< EST_TList<T>, IPointer, T > Entries;
268  typedef EST_TRwIterator< EST_TList<T>, IPointer, T > RwEntries;
269 
270 };
271 
272 
273 template<class T>
274 bool operator==(const EST_TList<T> &a, const EST_TList<T> &b)
275 {
277 }
278 
279 template<class T>
280 bool operator!=(const EST_TList<T> &a, const EST_TList<T> &b)
281 {
282  return !(a==b);
283 }
284 
285 template<class T>
286 int index(EST_TList<T> &l, T& val, bool (*eq)(const EST_UItem *, const EST_UItem *) = NULL)
287 {
288  EST_TItem<T> item(val);
289  return EST_UList::index(l, item, eq?eq:EST_TSortable<T>::items_eq);
290 }
291 
292 template<class T>
293 void sort(EST_TList<T> &a, bool (*gt)(const EST_UItem *, const EST_UItem *) = NULL)
294 {
296 }
297 
298 template<class T>
300 {
302 }
303 
304 template<class T>
305 void qsort(EST_TList<T> &a, bool (*gt)(const EST_UItem *, const EST_UItem *) = NULL)
306 {
308 }
309 
310 template<class T>
312 {
314 }
315 
316 template<class T>
318 {
323 }
324 
325 template<class T>
327 {
332 }
333 
334 template<class T>
335 const char *error_name(EST_TList<T> val) { (void)val; return "<<TLIST>>"; }
336 
337 #endif
T & last()
return reference to last item in list
Definition: EST_TList.h:163
void init()
Definition: EST_TList.h:119
EST_TItem(const T &v)
Definition: EST_TList.h:87
EST_Litem * remove_nth(int n)
remove nth item, return pointer to previous item
Definition: EST_TList.h:184
bool operator==(const EST_TList< T > &a, const EST_TList< T > &b)
Definition: EST_TList.h:274
#define walloc(TYPE, SIZE)
Definition: EST_walloc.h:52
EST_Litem * insert_before(EST_Litem *ptr, const T &item)
Definition: EST_TList.h:211
void ptr_qsort(EST_TList< T > &a)
Definition: EST_TList.h:311
bool operator!=(const EST_TList< T > &a, const EST_TList< T > &b)
Definition: EST_TList.h:280
EST_UItem * remove(EST_UItem *ptr, void(*item_free)(EST_UItem *item))
Definition: EST_UList.cc:92
const char * error_name(EST_TList< T > val)
Definition: EST_TList.h:335
void init()
Definition: EST_UList.h:51
EST_UItem EST_Litem
A pretty name for EST_UItem.
Definition: EST_TList.h:99
const T & last() const
return const reference to last item in list
Definition: EST_TList.h:155
EST_TList()
default constructor
Definition: EST_TList.h:125
T & nth(int n)
return the Nth value
Definition: EST_TList.h:145
int index(EST_TList< T > &l, T &val, bool(*eq)(const EST_UItem *, const EST_UItem *)=NULL)
Definition: EST_TList.h:286
EST_UItem * next()
Definition: EST_UList.h:55
void sort(EST_TList< T > &a, bool(*gt)(const EST_UItem *, const EST_UItem *)=NULL)
Definition: EST_TList.h:293
void ptr_sort(EST_TList< T > &a)
Definition: EST_TList.h:299
EST_TIterator< EST_TList< T >, IPointer, T > Entries
Definition: EST_TList.h:267
EST_TItem()
Definition: EST_TList.h:89
static void merge_sort_unique(EST_UList &l, EST_UList &m, bool(*eq)(const EST_UItem *item1, const EST_UItem *item2), bool(*gt)(const EST_UItem *item1, const EST_UItem *item2), void(*item_free)(EST_UItem *item))
Definition: EST_UList.cc:432
EST_UItem * n
Definition: EST_UList.h:53
EST_Litem * p
Definition: EST_TList.h:253
T & points_at(const IPointer &ip)
Definition: EST_TList.h:260
void sort_unique(EST_TList< T > &l)
Definition: EST_TList.h:317
void qsort(EST_TList< T > &a, bool(*gt)(const EST_UItem *, const EST_UItem *)=NULL)
Definition: EST_TList.h:305
void move_pointer_forwards(IPointer &ip) const
Definition: EST_TList.h:258
EST_UItem * p
Definition: EST_UList.h:54
const T & nth(int n) const
return a const Nth value
Definition: EST_TList.h:148
static EST_TItem * make(const T &val)
Definition: EST_TList.cc:45
EST_Pathname & operator+=(EST_Pathname p, const EST_Pathname addition)
static void release(EST_TItem< T > *it)
Definition: EST_TList.cc:63
NULL
Definition: EST_WFST.cc:55
T & first()
Definition: EST_TList.h:160
static void sort(EST_UList &a, bool(*gt)(const EST_UItem *item1, const EST_UItem *item2))
Definition: EST_UList.cc:317
const T & first() const
return const reference to first item in list
Definition: EST_TList.h:152
void merge_sort_unique(EST_TList< T > &l, EST_TList< T > &m)
Definition: EST_TList.h:326
void prepend(EST_UItem *item)
Definition: EST_UList.cc:268
void append(const T &item)
add item onto end of list
Definition: EST_TList.h:196
void exchange(EST_UItem *a, EST_UItem *b)
Definition: EST_UList.cc:170
const T & item(const EST_Litem *p) const
Definition: EST_TList.h:142
void append(EST_UItem *item)
Definition: EST_UList.cc:254
void init()
Definition: EST_UList.h:65
T & item(const EST_Litem *p)
Definition: EST_TList.h:139
EST_UItem * insert_before(EST_UItem *ptr, EST_UItem *new_item)
Definition: EST_UList.cc:147
bool points_to_something(const IPointer &ip) const
Definition: EST_TList.h:259
static void qsort(EST_UList &a, bool(*gt)(const EST_UItem *item1, const EST_UItem *item2), void(*exchange)(EST_UItem *item1, EST_UItem *item2))
Definition: EST_UList.cc:394
EST_Litem * insert_after(EST_Litem *ptr, const T &item)
Definition: EST_TList.h:205
EST_TRwIterator< EST_TList< T >, IPointer, T > RwEntries
Definition: EST_TList.h:268
static void sort_unique(EST_UList &l, bool(*eq)(const EST_UItem *item1, const EST_UItem *item2), bool(*gt)(const EST_UItem *item1, const EST_UItem *item2), void(*item_free)(EST_UItem *item))
Definition: EST_UList.cc:402
void exchange(int i, int j)
exchange 2
Definition: EST_TList.h:222
void wfree(void *p)
Definition: walloc.c:131
void exchange(EST_Litem *a, EST_Litem *b)
exchange 1
Definition: EST_TList.h:219
void point_to_first(IPointer &ip) const
Definition: EST_TList.h:257
void clear(void)
remove all items in list
Definition: EST_TList.h:244
EST_UItem * insert_after(EST_UItem *ptr, EST_UItem *new_item)
Definition: EST_UList.cc:124
int index(EST_UItem *item) const
Definition: EST_UList.cc:67
void prepend(const T &item)
add item onto start of list
Definition: EST_TList.h:199
static bool operator_eq(const EST_UList &a, const EST_UList &b, bool(*eq)(const EST_UItem *item1, const EST_UItem *item2))
Definition: EST_UList.cc:281