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 /* Template 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 #include "EST_TList.h"
44
make(const T & val)45 template<class T> EST_TItem<T> *EST_TItem<T>::make(const T &val)
46 {
47 EST_TItem<T> *it=NULL;
48 if (s_free!=NULL)
49 {
50 void *mem = s_free;
51 s_free=(EST_TItem<T> *)s_free->n;
52 s_nfree--;
53
54 // Create an item in the retrieved memory.
55 it=new (mem) EST_TItem<T>(val);
56 }
57 else
58 it = new EST_TItem<T>(val);
59
60 return it;
61 }
62
release(EST_TItem<T> * it)63 template<class T> void EST_TItem<T>::release(EST_TItem<T> *it)
64 {
65 if (0) // (s_nfree < s_maxFree)
66 {
67 // Destroy the value in case it holds resources.
68 it->EST_TItem<T>::~EST_TItem();
69
70 // I suppose it's a bit weird to use 'n' after calling the destructor.
71 it->n=s_free;
72 s_free=it;
73 s_nfree++;
74 }
75 else
76 {
77 delete it;
78 }
79 }
80
copy_items(const EST_TList<T> & l)81 template<class T> void EST_TList<T>::copy_items(const EST_TList<T> &l)
82 {
83 EST_UItem *p;
84 for (p = l.head(); p; p = p->next())
85 append(l.item(p));
86 }
87
free_item(EST_UItem * item)88 template<class T> void EST_TList<T>::free_item(EST_UItem *item)
89 { EST_TItem<T>::release((EST_TItem<T> *)item); }
90
91
EST_TList(const EST_TList<T> & l)92 template<class T> EST_TList<T>::EST_TList(const EST_TList<T> &l)
93 {
94 init();
95 copy_items(l);
96 }
97
exchange_contents(EST_Litem * a,EST_Litem * b)98 template<class T> void EST_TList<T>::exchange_contents(EST_Litem *a,EST_Litem *b)
99 {
100
101 if(a==b)
102 return;
103
104 T temp;
105
106 temp = ((EST_TItem<T> *)a)->val;
107 ((EST_TItem<T> *)a)->val = ((EST_TItem<T> *)b)->val;
108 ((EST_TItem<T> *)b)->val = temp;
109
110 }
111
112
operator =(const EST_TList<T> & a)113 template<class T> EST_TList<T> &EST_TList<T>::operator=(const EST_TList<T> &a)
114 {
115 clear(); // clear out all current items in list.
116 copy_items(a);
117 return *this;
118 }
119
120
operator +=(const EST_TList<T> & a)121 template<class T> EST_TList<T> &EST_TList<T>::operator+=(const EST_TList<T> &a)
122 {
123 if (this == &a)
124 {
125 cerr << "EST_TList: error: tried to add list to itself\n";
126 return *this;
127 }
128 copy_items(a);
129 return *this;
130 }
131
132
133
134