2 Last changed Time-stamp: <2006-10-03 16:31:54 xtof>
3 c Christoph Flamm and Ivo L Hofacker
4 {xtof,ivo}@tbi.univie.ac.at
6 $Id: cache.c,v 1.3 2006/10/04 12:45:12 xtof Exp $
14 #include "cache_util.h"
17 modify cache_f(), cache_comp() and the typedef of cache_entry
18 in cache_utils.h to suit your application
21 /* PUBLIC FUNCTIONES */
22 cache_entry *lookup_cache (char *x);
23 int write_cache (cache_entry *x);
24 /* void delete_cache (cache_entry *x); */
26 void initialize_cache();
28 /* PRIVATE FUNCTIONES */
29 /* static int cache_comp(cache_entry *x, cache_entry *y); */
30 static unsigned cache_f (char *x);
32 /* #define CACHESIZE 67108864 -1 */ /* 2^26 -1 must be power of 2 -1 */
33 /* #define CACHESIZE 33554432 -1 */ /* 2^25 -1 must be power of 2 -1 */
34 /* #define CACHESIZE 16777216 -1 */ /* 2^24 -1 must be power of 2 -1 */
35 /* #define CACHESIZE 4194304 -1 */ /* 2^22 -1 must be power of 2 -1 */
36 #define CACHESIZE 1048576 -1 /* 2^20 -1 must be power of 2 -1 */
37 /* #define CACHESIZE 262144 -1 */ /* 2^18 -1 must be power of 2 -1 */
39 /* #define CACHESIZE 65536 -1 */ /* 2^16 -1 must be power of 2 -1 */
40 /* #define CACHESIZE 16384 -1 */ /* 2^14 -1 must be power of 2 -1 */
41 /* #define CACHESIZE 4096 -1 */ /* 2^12 -1 must be power of 2 -1 */
43 static cache_entry *cachetab[CACHESIZE+1];
44 static char UNUSED rcsid[] ="$Id: cache.c,v 1.3 2006/10/04 12:45:12 xtof Exp $";
45 unsigned long collisions=0;
47 /* stolen from perl source */
49 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
50 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
51 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
52 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
53 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
54 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
55 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
56 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1};
58 /* key must not be longer than 128 */
59 #pragma inline (cache_f)
60 unsigned cache_f(char *x) {
63 register unsigned cache;
69 s++, i++ , cache *= 5 ) {
70 cache += *s * coeff[i];
73 /* divide through CACHESIZE for normalization */
74 return ((cache) & (CACHESIZE));
78 /* returns NULL unless x is in the cache */
79 cache_entry *lookup_cache (char *x) {
84 if ((c=cachetab[cacheval]))
85 if (strcmp(c->structure,x)==0) return c;
90 /* returns 1 if x already was in the cache */
91 int write_cache (cache_entry *x) {
95 cacheval=cache_f(x->structure);
96 if ((c=cachetab[cacheval])) {
103 cachetab[cacheval]=x;
108 void initialize_cache () { }
114 for (i=0;i<CACHESIZE+1;i++) {
116 free (cachetab[i]->structure);
117 free (cachetab[i]->neighbors);
118 free (cachetab[i]->rates);
119 free (cachetab[i]->energies);
128 static int cache_comp(cache_entry *x, cache_entry *y) {
129 return strcmp(((cache_entry *)x)->structure, ((cache_entry *)y)->structure);