00001 /* 00002 * Copyright (C) 1997-2004 The CDG Team <cdg@nats.informatik.uni-hamburg.de> 00003 * 00004 * This file is free software; as a special exception the author gives 00005 * unlimited permission to copy and/or distribute it, with or without 00006 * modifications, as long as this notice is preserved. 00007 * 00008 * This program is distributed in the hope that it will be useful, but 00009 * WITHOUT ANY WARRANTY, to the extent permitted by law; without even the 00010 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00011 * 00012 * $Id: scorematrix.c,v 1.20 2004/09/27 17:07:05 micha Exp $ 00013 */ 00014 00015 /* ------------------------------------------------------------------------- 00016 * @addtogroup Scorematrix Scorematrix - The matrix of scores appearing in each ConstraintEdge 00017 * 00018 * @author Ingo Schroeder 00019 * @date 6/3/97 00020 * 00021 * 00022 * \b Note: 00023 * The functions for accessing these matrices check whether the matrix is 00024 * non- \b NULL, but they do not check whether the index is 00025 * meaningful. Hence the behaviour of these functions is undefined if an 00026 * invalid array index is passed to them. 00027 * 00028 * @{ 00029 */ 00030 00031 /* ---------------------------------------------------------------------- */ 00032 #include <config.h> 00033 00034 #include <stdio.h> 00035 #include "scorematrix.h" 00036 #include "cdg.h" 00037 #include "hook.h" 00038 00039 /* ---------------------------------------------------------------------- */ 00040 00041 /* ---------------------------------------------------------------------- 00042 * Entry in the matrix. 00043 * All entries in the matrix are pairs of a score and a flag indicating 00044 * whether this pair is valid or not. 00045 */ 00046 struct SMEntryStruct { 00047 double score; /**< score of this entry*/ 00048 Boolean flag; /**< indicating if this is a valid pari */ 00049 }; 00050 00051 typedef struct SMEntryStruct SMEntryStruct; 00052 /**< type of matrix entry structure */ 00053 typedef SMEntryStruct *SMEntry; 00054 /**< type of matrix entry pointer */ 00055 00056 /* ---------------------------------------------------------------------- 00057 * The score matrix 00058 * 00059 * Since not all matrices in the edges of a constraint net are of equal 00060 * size, each matrix administrates the number of its rows and columns 00061 * explicitly in addition to the array itself. The element <i>(r,c)</i> in 00062 * a score matrix is always referenced by 00063 * 00064 * <b> sm->entries[c * sm->rows + r] </b> 00065 * 00066 * The last element of a matrix always has the index 00067 * 00068 * <b> sm->rows * sm->cols -1 </b> 00069 * 00070 * The type \b ScoreMatrix is only used in a \b ConstraintEdge. A 00071 * similar function is fulfilled by the structure \b ScoreCache 00072 * defined by the module \ref Scache, which has the advantage of 00073 * being usable even without constraint edges. 00074 */ 00075 struct ScoreMatrixStruct { 00076 int rows; /**< number of rows in the matrix */ 00077 int cols; /**< number of columns in the matrix */ 00078 SMEntry *entries; /**< entries in the matrix */ 00079 }; 00080 00081 typedef struct ScoreMatrixStruct ScoreMatrixStruct; 00082 /**< type of score matrix structure */ 00083 00084 /* ---------------------------------------------------------------------- */ 00085 /* ---------------------------------------------------------------------- */ 00086 00087 /* ---------------------------------------------------------------------- 00088 * creates and returns a new score matrix 00089 * 00090 * This function allocates a new \b ScoreMatrix and returns a pointer 00091 * to it. It allocates an array of size <b> r * c </b>. All elements 00092 * are initialized to pairs of the form <b> (0.0, FALSE) </b>. 00093 */ 00094 ScoreMatrix smNew(int r, int c) 00095 { 00096 ScoreMatrix sm; 00097 int i; 00098 int total = r * c; 00099 00100 sm = (ScoreMatrix)memMalloc( sizeof(ScoreMatrixStruct) ); 00101 sm->rows = r; 00102 sm->cols = c; 00103 00104 sm->entries = (SMEntry *)memMalloc( sizeof(SMEntry) * total ); 00105 00106 for ( i = 0; i < total; i++ ) { 00107 sm->entries[i] = (SMEntry)memMalloc( sizeof(SMEntryStruct) ); 00108 sm->entries[i]->score = 0.0; 00109 sm->entries[i]->flag = FALSE; 00110 } 00111 00112 return( sm ); 00113 } 00114 00115 /* ---------------------------------------------------------------------- 00116 * deletes score matrix 00117 * 00118 * This function deallocates a \b ScoreMatrix. It first deallocates 00119 * all elements of <b> sm->entries[] </b> and then the array itself as well 00120 * as the \b ScoreMatrixStruct. 00121 */ 00122 void smDelete(ScoreMatrix sm) 00123 { 00124 int i; 00125 00126 if ( sm == NULL ) { 00127 cdgPrintf(CDG_ERROR, "smDelete: argument must not be NULL\n"); 00128 abort(); 00129 } 00130 00131 for ( i = 0; i < sm->rows * sm->cols; i++ ) 00132 memFree( sm->entries[i] ); 00133 00134 memFree( sm->entries ); 00135 memFree( sm ); 00136 } 00137 00138 /* ---------------------------------------------------------------------- 00139 * sets a score matrix element to a new value, returns old value 00140 */ 00141 double smSetScore(ScoreMatrix sm, double score, int r, int c) 00142 { 00143 double old; 00144 00145 if ( sm == NULL ) { 00146 cdgPrintf(CDG_ERROR, "smSet: argument must not be NULL\n"); 00147 abort(); 00148 } 00149 00150 old = sm->entries[ c * sm->rows + r ]->score; 00151 sm->entries[ c * sm->rows + r ]->score = score; 00152 00153 return( old ); 00154 } 00155 00156 /* ---------------------------------------------------------------------- 00157 * sets a flag matrix element to a new value, returns old value 00158 */ 00159 Boolean smSetFlag(ScoreMatrix sm, Boolean flag, int r, int c) 00160 { 00161 Boolean old; 00162 00163 if ( sm == NULL ) { 00164 cdgPrintf(CDG_ERROR, "smSet: argument must not be NULL\n"); 00165 abort(); 00166 } 00167 00168 old = sm->entries[ c * sm->rows + r ]->flag; 00169 sm->entries[ c * sm->rows + r ]->flag = flag; 00170 00171 return( old ); 00172 } 00173 00174 /* ---------------------------------------------------------------------- 00175 * sets flags of all matrix elements to a new value 00176 * 00177 * This function sets the \b flag of all elements of 00178 * \b sm->entries[] to \b flag. 00179 */ 00180 void smSetAllFlags(ScoreMatrix sm, Boolean flag) 00181 { 00182 int i; 00183 00184 if ( sm == NULL ) { 00185 cdgPrintf(CDG_ERROR, "smSet: argument must not be NULL\n"); 00186 abort(); 00187 } 00188 00189 for ( i = 0; i < sm->rows * sm->cols; i++ ) 00190 sm->entries[ i ]->flag = flag; 00191 00192 } 00193 00194 /* ---------------------------------------------------------------------- 00195 * retrieves a score matrix element score 00196 */ 00197 double smGetScore(ScoreMatrix sm, int r, int c) 00198 { 00199 if ( sm == NULL ) { 00200 cdgPrintf(CDG_ERROR, "smGet: argument must not be NULL\n"); 00201 abort(); 00202 } 00203 00204 return( sm->entries[ c * sm->rows + r ]->score ); 00205 } 00206 00207 /* ---------------------------------------------------------------------- 00208 * retrieves a score matrix element flag 00209 * @returns the \b flag of the specified element. 00210 */ 00211 Boolean smGetFlag(ScoreMatrix sm, int r, int c) 00212 { 00213 if ( sm == NULL ) { 00214 cdgPrintf(CDG_ERROR, "smGet: argument must not be NULL\n"); 00215 abort(); 00216 } 00217 00218 return( sm->entries[ c * sm->rows + r ]->flag ); 00219 } 00220 00221 /* ---------------------------------------------------------------------- */ 00222 /** @} */