1 #include "hypercache.h"
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;
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) {
17 *cache = new configcache(cacheid, nequi, nskip, datadir, configmem, configMemSize, cachemode, log);
19 for (vector<para>::iterator parit=delayedParaAdd.begin(); parit != delayedParaAdd.end(); ++parit)
20 (*cache)->addPara(parit->parid, parit->val);
22 for (vector<para>::iterator parit=delayedParaSet.begin(); parit != delayedParaSet.end(); ++parit)
23 (*cache)->setPara(parit->parid, parit->val);
26 void hypercache::addPara(const string& parid, const double& val) {
28 newpara.parid = parid;
30 delayedParaAdd.push_back(newpara);
33 C->addPara(parid, val);
35 for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
36 (*osit)->addPara(parid, val);
39 void hypercache::setPara(const string& parid, const double& val) {
41 newpara.parid = parid;
43 delayedParaSet.push_back(newpara);
46 C->setPara(parid, val);
48 for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
49 (*osit)->setPara(parid, val);
52 void hypercache::finalize() {
53 for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
59 string hypercache::fileOfPath(const string& dressedfile) {
60 return dressedfile.substr(dressedfile.find_last_of("\\/")+1);
63 void hypercache::readC(bool& readnewconfig, int& nequileft) {
64 C->readConfig(readnewconfig, nequileft, &parentConfigs);
67 activeCFile = fileOfPath(C->getInFileName());
72 void hypercache::writeC(int actnequi) {
73 C->writeConfig(actnequi);
74 activeCFile = fileOfPath( C->getOutFileName().substr( 0, C->getOutFileName().length()-4 ) );
77 void hypercache::writeO(int obsid, int actnequi) {
78 if ( activeCFile != "" ) {
79 unsigned long afilehash = configcache::hash(activeCFile);
80 Os[obsid]->writeHeader("concurrent_cfile", (char*)(&afilehash), sizeof(unsigned long), actnequi);
82 Os[obsid]->writeConfig(actnequi);
85 void hypercache::readO(int obsid, bool& readnewconfig, int& nequileft) {
86 Os[obsid]->readConfig(readnewconfig, nequileft);
88 if ( nequileft < 0 ) {
89 unsigned long *parentconfig = (unsigned long*)Os[obsid]->getHeader("concurrent_cfile");
90 if ( parentconfig != NULL )
91 addParentConfig(parentconfig);
95 void hypercache::addParentConfig(const unsigned long *parentconfig) {
96 for (vector<unsigned long>::iterator parit = parentConfigs.begin(); parit != parentConfigs.end(); ++parit)
97 if ( *parit == *parentconfig )
100 parentConfigs.push_back(*parentconfig);