Replace Progs/RNAalifold with x64 binary and add all other programs
[jabaws.git] / binaries / src / ViennaRNA / Kinfold / cache.c
1 /*
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
5   Kinfold: $Name:  $
6   $Id: cache.c,v 1.3 2006/10/04 12:45:12 xtof Exp $
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <math.h>
12 #include <string.h>
13 #include "utils.h"
14 #include "cache_util.h"
15
16 /*
17   modify cache_f(), cache_comp() and the typedef of cache_entry
18   in cache_utils.h to suit your application
19 */
20
21 /* PUBLIC FUNCTIONES */
22 cache_entry *lookup_cache (char *x);
23 int write_cache (cache_entry *x);
24 /*  void delete_cache (cache_entry *x); */
25 void kill_cache();
26 void initialize_cache();
27
28 /* PRIVATE FUNCTIONES */
29 /*  static int cache_comp(cache_entry *x, cache_entry *y); */
30 static unsigned cache_f (char *x);
31
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 */
38 /* next is default */
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 */
42
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;
46
47 /* stolen from perl source */
48 char coeff[] = {
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};
57
58 /* key must not be longer than 128 */
59 #pragma inline (cache_f)
60 unsigned cache_f(char *x) { 
61   register char *s;
62   register int i;
63   register unsigned cache;
64
65   s = x;
66
67   for (i=0,    cache = 0;
68        /* while */ *s;
69        s++,           i++ , cache *= 5 ) {
70     cache += *s * coeff[i];
71   }
72
73    /* divide through CACHESIZE for normalization */
74   return ((cache) & (CACHESIZE));
75 }
76
77
78 /* returns NULL unless x is in the cache */
79 cache_entry *lookup_cache (char *x) {
80   int cacheval;
81   cache_entry *c;
82
83   cacheval=cache_f(x);
84   if ((c=cachetab[cacheval]))
85     if (strcmp(c->structure,x)==0) return c;
86   
87   return NULL;
88 }
89
90 /* returns 1 if x already was in the cache */
91 int write_cache (cache_entry *x) {
92   int cacheval;
93   cache_entry *c;
94   
95   cacheval=cache_f(x->structure);
96   if ((c=cachetab[cacheval])) {
97     free(c->structure);
98     free(c->neighbors);
99     free(c->rates);
100     free(c->energies);
101     free(c);
102   }
103   cachetab[cacheval]=x;
104   return 0;
105 }
106
107 /**/
108 void initialize_cache () { }
109
110 /**/
111 void kill_cache () {
112   int i;
113   
114   for (i=0;i<CACHESIZE+1;i++) {
115     if ( cachetab[i] ) {
116       free (cachetab[i]->structure);
117       free (cachetab[i]->neighbors);
118       free (cachetab[i]->rates);
119       free (cachetab[i]->energies);
120       free (cachetab[i]);
121     }
122     cachetab[i]=NULL;
123   }
124 }
125
126 #if 0
127 /**/
128 static int cache_comp(cache_entry *x, cache_entry *y) {
129   return strcmp(((cache_entry *)x)->structure, ((cache_entry *)y)->structure);
130 }
131 #endif
132
133 /* End of file */