]> git.treefish.org Git - phys/latlib.git/blob - hypercache.cpp
...
[phys/latlib.git] / hypercache.cpp
1 #include "hypercache.h"
2
3 configcache *hypercache::C = NULL;
4 vector<hypercache::para> hypercache::delayedParaAdd;
5 vector<hypercache::para> hypercache::delayedParaSet;
6 string hypercache::activeCFile = "";
7 vector<unsigned long> hypercache::parentConfigs;
8 ostream* hypercache::log;
9 vector<configcache*> hypercache::Os;
10
11 void hypercache::initCache(configcache **cache,
12                            const string& cacheid, const int& nequi, const int& nskip, const string& datadir, 
13                            char **configmem, const int& configMemSize, const int& cachemode, ostream* _log) {
14   if ( _log != NULL )
15     log = _log;
16   
17   *cache = new configcache(cacheid, nequi, nskip, datadir, configmem, configMemSize, cachemode, log);
18   
19   for (vector<para>::iterator parit=delayedParaAdd.begin(); parit != delayedParaAdd.end(); ++parit)
20     (*cache)->addPara(parit->parid, parit->val);
21
22   for (vector<para>::iterator parit=delayedParaSet.begin(); parit != delayedParaSet.end(); ++parit)
23     (*cache)->setPara(parit->parid, parit->val);
24 }
25
26 void hypercache::addPara(const string& parid, const double& val) {
27   para newpara; 
28   newpara.parid = parid;
29   newpara.val = val;
30   delayedParaAdd.push_back(newpara);
31
32   if (C != NULL)
33     C->addPara(parid, val);
34
35   for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
36     (*osit)->addPara(parid, val);
37 }
38
39 void hypercache::setPara(const string& parid, const double& val) {
40   para newpara; 
41   newpara.parid = parid;
42   newpara.val = val;
43   delayedParaSet.push_back(newpara);
44
45   if (C != NULL)
46     C->setPara(parid, val);
47   
48   for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
49     (*osit)->setPara(parid, val);
50 }
51
52 void hypercache::finalize() {
53   for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
54     delete *osit;
55   
56   delete C;
57 }
58
59 string hypercache::fileOfPath(const string& dressedfile) {
60   return dressedfile.substr(dressedfile.find_last_of("\\/")+1);
61 }
62
63 int hypercache::readC() { 
64   int readret;
65
66   readret = C->readConfig(&parentConfigs);
67
68   if ( readret == -1 )
69     activeCFile = fileOfPath(C->getInFileName());
70   else
71     activeCFile = "";
72
73   return readret; 
74 }
75
76 void hypercache::writeC() {
77   C->writeConfig();
78   activeCFile = fileOfPath( C->getOutFileName().substr( 0, C->getOutFileName().length()-4 ) );
79 }
80
81 void hypercache::writeO(int obsid) {
82   if ( activeCFile != "" ) {
83     unsigned long afilehash = configcache::hash(activeCFile);
84     Os[obsid]->writeHeader("concurrent_cfile", (char*)(&afilehash), sizeof(unsigned long));
85   }
86   Os[obsid]->writeConfig(); 
87 }
88
89 int hypercache::readO(int obsid) {
90   int readret;
91
92   readret = Os[obsid]->readConfig();
93
94   if ( readret == -1 ) {
95     unsigned long *parentconfig = (unsigned long*)Os[obsid]->getHeader("concurrent_cfile");
96     if ( parentconfig != NULL )
97       addParentConfig(parentconfig);
98   }
99   
100   return readret;
101 }
102
103 void hypercache::addParentConfig(const unsigned long *parentconfig) {
104   for (vector<unsigned long>::iterator parit = parentConfigs.begin(); parit != parentConfigs.end(); ++parit)
105     if ( *parit == *parentconfig )
106       return;
107
108   parentConfigs.push_back(*parentconfig);
109 }