00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
#include <config.h>
00031
00032
#include <stdio.h>
00033
#include "scache.h"
00034
#include "hook.h"
00035
#include "set.h"
00036
00037
00038
00039 #define indexOfPair(x,y) ((x<y) ? (((y*(y-1))>>1)+x) : (((x*(x-1))>>1)+y))
00040
00041 #define DEBUG_SCORECACHE 0
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 Boolean
scUseCache=
FALSE;
00054
00055
00056
00057
00058
00059
00060
00061
00062 ScoreCache scNew(
int noValues)
00063 {
00064
ScoreCache cache;
00065
int capacity =
indexOfPair(noValues, noValues - 1);
00066
00067 cache = (
ScoreCache) memMalloc(
sizeof (
ScoreCacheStruct));
00068 cache->
count = 0;
00069 cache->
size = 0;
00070 cache->
capacity = capacity;
00071 cache->
hits = 0;
00072
00073 cache->
data = (Number *) memMalloc(
sizeof (Number) * capacity);
00074
#if 0
00075
for (i = 0; i < capacity; i++) {
00076 cache->
data[i] = -1.0;
00077 }
00078
#endif
00079
00080
return cache;
00081 }
00082
00083
00084
00085
00086
00087
00088 void scDelete(
ScoreCache cache)
00089 {
00090
if(!cache) {
return; }
00091
00092 memFree(cache->
data);
00093 memFree(cache);
00094 }
00095
00096
00097
00098
00099
00100
00101
00102 Number
scGetScore(
ScoreCache cache, LevelValue a, LevelValue b)
00103 {
00104 Number score;
00105
int index;
00106
00107
if (!cache || a->no < 0 || b->no < 0
00108 || a->indexWRTNet == -1 || a->indexWRTNet == -1) {
00109
return -1.0;
00110 }
00111
00112 index =
indexOfPair(a->no, b->no);
00113
if (index >= cache->
size) {
00114
return -1.0;
00115 }
00116
00117 score = cache->
data[index];
00118
if (score != -1.0) {
00119 cache->
hits++;
00120 }
00121
00122
return score;
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 void scSetScore(
ScoreCache cache, LevelValue a, LevelValue b, Number score)
00138 {
00139
int index;
00140
int i;
00141
00142
if (!cache
00143 || a->indexWRTNet == -1 || b->indexWRTNet == -1) {
00144
return;
00145 }
00146
00147
if (a->no < 0) {
00148 a->no = cache->
count++;
00149 }
00150
if (b->no < 0) {
00151 b->no = cache->
count++;
00152 }
00153
00154 index =
indexOfPair(a->no, b->no);
00155
00156
00157
00158
00159
00160
00161
if (index >= cache->
capacity) {
00162
int old;
00163
00164 old = cache->
capacity;
00165
while (index >= cache->
capacity) {
00166 cache->
capacity *= 2;
00167 }
00168
00169
#if DEBUG_SCORECACHE
00170
cdgPrintf(
CDG_DEBUG,
00171
"DEBUG: resizing score-cache from %d to %d "
00172
"because of levelvalues %d, %d (index %d)\n",
00173 old, cache->
capacity, a->no, b->no, index);
00174
#endif
00175
00176 cache->
data = (Number *) memRealloc(cache->
data,
00177 sizeof (Number) * cache->
capacity);
00178
00179
#if 0
00180
for (i = old; i < cache->
capacity; i++) {
00181 cache->
data[i] = -1.0;
00182 }
00183
#endif
00184
}
00185
00186
00187
00188
00189
if (cache->
size < index) {
00190
for (i = cache->
size; i < index; i++) {
00191 cache->
data[i] = -1.0;
00192 }
00193 cache->
size = index;
00194 }
00195
00196
#if DEBUG_SCORECACHE
00197
else {
00198 Number s;
00199
00200 s = cache->
data[index];
00201
if (s != -1.0 && s != score) {
00202
cdgPrintf(
CDG_ERROR,
00203
"ERROR: overwriting binary score %f with %f\n", s, score);
00204
cdgPrintf(
CDG_ERROR,
" a=%d b=%d index=%d\n", a->no, b->no, index);
00205 }
00206 }
00207
#endif
00208
00209 cache->
data[index] = score;
00210 }
00211
00212
00213
00214
00215
00216 void scInitialize()
00217 {
00218 setRegister(
"cache", SET_BOOL, &
scUseCache,
NULL,
NULL,
NULL,
NULL);
00219 }
00220