File: SculptCache.cpp

package info (click to toggle)
pymol 1.8.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 42,248 kB
  • ctags: 24,095
  • sloc: cpp: 474,635; python: 75,034; ansic: 22,888; sh: 236; makefile: 78; csh: 21
file content (137 lines) | stat: -rw-r--r-- 3,380 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

/* 
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* copyright 1998-2000 by Warren Lyford Delano of DeLano Scientific. 
D* -------------------------------------------------------------------
E* It is unlawful to modify or remove this copyright notice.
F* -------------------------------------------------------------------
G* Please see the accompanying LICENSE file for further information. 
H* -------------------------------------------------------------------
I* Additional authors of this source file include:
-* 
-* 
-*
Z* -------------------------------------------------------------------
*/
#include"os_python.h"
#include"os_predef.h"
#include"os_std.h"
#include"os_gl.h"
#include"OOMac.h"
#include"Feedback.h"
#include"Util.h"
#include"SculptCache.h"

#define CACHE_HASH_SIZE 65536

#define cache_hash(d,e,f,g) \
((((d)      )&0x003F)|\
 (((e+g)<< 6)&0x0FC0)|\
 (((f-g)<<12)&0xF000))

static void SculptCacheCheck(PyMOLGlobals * G)
{
  CSculptCache *I = G->SculptCache;
  if(!I->Hash) {
    I->Hash = Calloc(int, CACHE_HASH_SIZE);
  }
}

int SculptCacheInit(PyMOLGlobals * G)
{
  CSculptCache *I = NULL;
  if((I = (G->SculptCache = Calloc(CSculptCache, 1)))) {
    I->Hash = NULL;             /* don't allocate until we need it */
    I->List = VLAlloc(SculptCacheEntry, 16);
    I->NCached = 1;
    return 1;
  } else {
    return 0;
  }
}

void SculptCachePurge(PyMOLGlobals * G)
{
  CSculptCache *I = G->SculptCache;
  if(I->Hash) {
    FreeP(I->Hash);
    I->Hash = NULL;
  }
  I->NCached = 1;
}

void SculptCacheFree(PyMOLGlobals * G)
{
  CSculptCache *I = G->SculptCache;
  FreeP(I->Hash);
  VLAFreeP(I->List);
  FreeP(G->SculptCache);
}

int SculptCacheQuery(PyMOLGlobals * G, int rest_type, int id0, int id1, int id2, int id3,
                     float *value)
{
  CSculptCache *I = G->SculptCache;
  int *v, i;
  SculptCacheEntry *e;
  int found = false;
  if(!I->Hash)
    SculptCacheCheck(G);
  if(I->Hash) {
    v = I->Hash + cache_hash(id0, id1, id2, id3);
    i = (*v);
    while(i) {
      e = I->List + i;
      if((e->rest_type == rest_type) && (e->id0 == id0) && (e->id1 == id1)
         && (e->id2 == id2) && (e->id3 == id3)) {
        found = true;
        *value = e->value;
        break;
      }
      i = e->next;
    }
  }
  return (found);
}

void SculptCacheStore(PyMOLGlobals * G, int rest_type, int id0, int id1, int id2, int id3,
                      float value)
{
  CSculptCache *I = G->SculptCache;
  int *v, i;
  SculptCacheEntry *e;
  int found = false;
  if(!I->Hash)
    SculptCacheCheck(G);
  if(I->Hash) {
    v = I->Hash + cache_hash(id0, id1, id2, id3);
    i = (*v);
    while(i) {
      e = I->List + i;;
      if((e->rest_type == rest_type) && (e->id0 == id0) && (e->id1 == id1)
         && (e->id2 == id2) && (e->id3 == id3)) {
        e->value = value;
        found = true;
        break;
      }
      i = e->next;
    }
    if(!found) {
      VLACheck(I->List, SculptCacheEntry, I->NCached);
      v = I->Hash + cache_hash(id0, id1, id2, id3);
      e = I->List + I->NCached;
      e->next = *v;
      *v = I->NCached;          /* become new head of list */

      e->rest_type = rest_type;
      e->id0 = id0;
      e->id1 = id1;
      e->id2 = id2;
      e->id3 = id3;
      e->value = value;

      I->NCached++;
    }
  }
}