1 /*************************************************************************/
3 /* Centre for Speech Technology Research */
4 /* (University of Edinburgh, UK) and */
6 /* Copyright (c) 2004 */
7 /* All Rights Reserved. */
9 /* Permission is hereby granted, free of charge, to use and distribute */
10 /* this software and its documentation without restriction, including */
11 /* without limitation the rights to use, copy, modify, merge, publish, */
12 /* distribute, sublicense, and/or sell copies of this work, and to */
13 /* permit persons to whom this work is furnished to do so, subject to */
14 /* the following conditions: */
16 /* 1. The code must retain the above copyright notice, this list of */
17 /* conditions and the following disclaimer. */
18 /* 2. Any modifications must be clearly marked as such. */
19 /* 3. Original authors' names are not deleted. */
20 /* 4. The authors' names are not used to endorse or promote products */
21 /* derived from this software without specific prior written */
24 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
25 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
26 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT */
27 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
28 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
29 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
30 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
31 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
34 /*************************************************************************/
36 /* Author: Korin Richmond */
37 /* Date: January 2004 */
39 /*************************************************************************/
41 #include "EST_JoinCostCache.h"
42 #include "EST_JoinCost.h"
43 #include "EST_error.h"
48 EST_JoinCostCache::EST_JoinCostCache( unsigned int id )
58 EST_JoinCostCache::EST_JoinCostCache( unsigned int id, unsigned int n )
62 deleteMemoryOnDeath(true)
64 cache = new unsigned char [cachelen];
68 EST_JoinCostCache::EST_JoinCostCache( unsigned int id, unsigned char *mem, unsigned int n, bool del )
73 deleteMemoryOnDeath(del)
78 EST_JoinCostCache::~EST_JoinCostCache()
81 if( deleteMemoryOnDeath )
85 unsigned char EST_JoinCostCache::val( unsigned int a, unsigned int b ) const
87 if( a>numInstances || b>numInstances )
88 EST_error( "Requested index greater than cache size" );
93 return cache[(b*(b-1)>>1)+a];
95 return cache[(a*(a-1)>>1)+b];
101 bool EST_JoinCostCache::setval( unsigned int a, unsigned int b, unsigned char v )
103 if( a>numInstances || b>numInstances )
104 EST_error( "Requested index greater than cache size" );
110 cache[(b*(b-1)>>1)+a] = v;
114 cache[(a*(a-1)>>1)+b] = v;
122 ostream& EST_JoinCostCache::write( ostream &os ) const
125 // os.write( cache, cachelen );
129 bool EST_JoinCostCache::computeAndCache( const EST_TList<EST_Item*> &list,
130 const EST_JoinCost &jc,
133 unsigned char qcost; // quantized cost
135 unsigned int qleveln = maxVal-minVal;
137 float ulimit = 1.0-1/(float)(qleveln);
138 float llimit = 0.0+1/(float)(qleveln);
141 EST_warning("EST_JoinCostCache::computeAndCache");
142 for( EST_Litem *it=list.head(); it; it=it->next(), ++i ){
145 for( EST_Litem *jt=it->next(); jt; jt=jt->next(), ++j ){
146 float cost = jc( list(it), list(jt) );
150 else if( cost <= llimit )
153 qcost = static_cast<unsigned char>(rint(cost*(float)qleveln));
155 setval( i, j, qcost );
158 // yet to be convinced this is the best place for this...
159 list(it)->set( "jccid", (int)this->id() );
160 list(it)->set( "jccindex", (int) i );