1  /************************************************************************/
2  /*                                                                      */
3  /*                Centre for Speech Technology Research                 */
4  /*                     University of Edinburgh, UK                      */
5  /*                       Copyright (c) 1996,1997                        */
6  /*                        All Rights Reserved.                          */
7  /*                                                                      */
8  /*  Permission is hereby granted, free of charge, to use and distribute */
9  /*  this software and its documentation without restriction, including  */
10  /*  without limitation the rights to use, copy, modify, merge, publish, */
11  /*  distribute, sublicense, and/or sell copies of this work, and to     */
12  /*  permit persons to whom this work is furnished to do so, subject to  */
13  /*  the following conditions:                                           */
14  /*   1. The code must retain the above copyright notice, this list of   */
15  /*      conditions and the following disclaimer.                        */
16  /*   2. Any modifications must be clearly marked as such.               */
17  /*   3. Original authors' names are not deleted.                        */
18  /*   4. The authors' names are not used to endorse or promote products  */
19  /*      derived from this software without specific prior written       */
20  /*      permission.                                                     */
21  /*                                                                      */
22  /*  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK       */
23  /*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING     */
24  /*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT  */
25  /*  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE    */
26  /*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   */
27  /*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  */
28  /*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,         */
29  /*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF      */
30  /*  THIS SOFTWARE.                                                      */
31  /*                                                                      */
32  /*************************************************************************/
33  /*                                                                       */
34  /*                 Author: Richard Caley (rjc@cstr.ed.ac.uk)             */
35  /*                   Date: Tue Aug 26 1997                               */
36  /* --------------------------------------------------------------------  */
37  /* Extending buffers, i.e. arrays which grow as needed. I got fed up     */
38  /* of writing this code all over the place.                              */
39  /*                                                                       */
40  /*************************************************************************/
41 
42 #include <cstdlib>
43 #include <cstdio>
44 #include <cstring>
45 #include "EST_unix.h"
46 #include "EST_TBuffer.h"
47 
48 template<class T>
EST_TBuffer(unsigned int size,int step)49 EST_TBuffer<T>::EST_TBuffer(unsigned int size, int step)
50 {
51   p_buffer = NULL;
52   init(size, step);
53 }
54 
55 template<class T>
~EST_TBuffer(void)56 EST_TBuffer<T>::~EST_TBuffer(void)
57 {
58   // save the buffer if we have a slot
59   for(int i=0; i<TBUFFER_N_OLD; i++)
60     if (EST_old_buffers[i].mem == NULL)
61       {
62 	EST_old_buffers[i].mem = p_buffer;
63 	EST_old_buffers[i].size = p_size*sizeof(T);
64 	p_buffer = NULL;
65 	p_size =0;
66 	break;
67       }
68 
69   if (p_buffer)
70     {
71       delete[] p_buffer;
72       p_buffer = NULL;
73       p_size = 0;
74     }
75 }
76 
77 template<class T>
init(unsigned int size,int step)78 void EST_TBuffer<T>::init(unsigned int size, int step)
79 {
80   for(int i=0; i<TBUFFER_N_OLD; i++)
81     if (EST_old_buffers[i].size/sizeof(T) >= size)
82       {
83 	p_buffer = (T *)EST_old_buffers[i].mem;
84 	p_size = EST_old_buffers[i].size/sizeof(T);
85 	EST_old_buffers[i].mem = NULL;
86 	EST_old_buffers[i].size = 0;
87 	break;
88       }
89 
90   if (p_buffer == NULL)
91     {
92     p_buffer = new T[size];
93     p_size = size;
94     }
95   p_step = step;
96 }
97 
98 template<class T>
expand_to(unsigned int req_size,bool copy)99 void EST_TBuffer<T>::expand_to(unsigned int req_size, bool copy)
100 {
101   if (req_size > p_size)
102     {
103       unsigned int new_size = p_size;
104 
105       while(new_size < req_size)
106 	if (p_step >0)
107 	  new_size += p_step;
108 	else
109 	  new_size = (int)(new_size*(float)(-p_step)/100.0);
110 
111       T * new_buffer = new T[new_size];
112 
113       if (copy)
114 	memcpy(new_buffer, p_buffer, p_size*sizeof(T));
115 
116       delete[] p_buffer;
117       p_buffer = new_buffer;
118       p_size = new_size;
119     }
120 }
121 
122 template<class T>
expand_to(unsigned int req_size,const T & set_to,int howmany)123 void EST_TBuffer<T>::expand_to(unsigned int req_size, const T &set_to, int howmany)
124 {
125   if (req_size > p_size)
126     {
127       unsigned int new_size = p_size;
128 
129       while(new_size < req_size)
130 	if (p_step >0)
131 	  new_size += p_step;
132 	else
133 	  new_size = (int)(new_size*(float)(-p_step)/100.0);
134 
135       T * new_buffer = new T[new_size];
136 
137       if (howmany<0)
138 	howmany=new_size;
139       for(int i=0; i<howmany; i++)
140 	new_buffer[i] = set_to;
141 
142       delete[] p_buffer;
143       p_buffer = new_buffer;
144       p_size = new_size;
145     }
146 }
147 
148 template<class T>
set(const T & set_to,int howmany)149 void EST_TBuffer<T>::set(const T &set_to, int howmany)
150 {
151   if (howmany < 0)
152     howmany = p_size;
153 
154   for(int i=0; i<howmany; i++)
155     p_buffer[i] = set_to;
156 }
157 
158