1 #include "hypercache.h"
3 configcache *hypercache::O = NULL;
4 configcache *hypercache::C = NULL;
5 vector<hypercache::para> hypercache::delayedParaAdd;
6 vector<hypercache::para> hypercache::delayedParaSet;
7 string hypercache::activeCFile = "";
8 vector<unsigned long> hypercache::parentConfigs;
9 writeout *hypercache::out = NULL;
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, writeout *out_a) {
15 bool allInitBefore = true;
17 if (O == NULL || C == NULL)
18 allInitBefore = false;
23 *cache = new configcache(cacheid, nequi, nskip, datadir, configmem, configMemSize, cachemode, out_a);
25 if (O != NULL && C != NULL && !allInitBefore) {
26 for (vector<para>::iterator parit=delayedParaAdd.begin(); parit != delayedParaAdd.end(); ++parit) {
27 O->addPara(parit->parid, parit->val);
28 C->addPara(parit->parid, parit->val);
30 for (vector<para>::iterator parit=delayedParaSet.begin(); parit != delayedParaSet.end(); ++parit) {
31 O->setPara(parit->parid, parit->val);
32 C->setPara(parit->parid, parit->val);
37 void hypercache::addPara(const string& parid, const double& val) {
38 if (O == NULL || C == NULL) {
40 newpara.parid = parid;
42 delayedParaAdd.push_back(newpara);
45 O->addPara(parid, val);
46 C->addPara(parid, val);
50 void hypercache::setPara(const string& parid, const double& val) {
51 if (O == NULL || C == NULL) {
53 newpara.parid = parid;
55 delayedParaSet.push_back(newpara);
58 O->setPara(parid, val);
59 C->setPara(parid, val);
61 parentConfigs.clear();
65 void hypercache::finalize() {
70 string hypercache::fileOfPath(const string& dressedfile) {
71 return dressedfile.substr(dressedfile.find_last_of("\\/")+1);
74 bool hypercache::readC() {
77 if ( readret = C->readConfig(&parentConfigs) )
78 activeCFile = fileOfPath(C->getInFileName());
85 void hypercache::writeC() {
87 activeCFile = fileOfPath( C->getOutFileName().substr( 0, C->getOutFileName().length()-4 ) );
90 void hypercache::writeO() {
91 if ( activeCFile != "" ) {
92 unsigned long afilehash = configcache::hash(activeCFile);
93 O->writeHeader("concurrent_cfile", (char*)(&afilehash), sizeof(unsigned long));
99 bool hypercache::readO() {
102 if ( readret = O->readConfig() ) {
103 unsigned long *parentconfig = (unsigned long*)O->getHeader("concurrent_cfile");
104 if ( parentconfig != NULL )
105 addParentConfig(parentconfig);
111 void hypercache::addParentConfig(const unsigned long *parentconfig) {
112 for (vector<unsigned long>::iterator parit = parentConfigs.begin(); parit != parentConfigs.end(); ++parit)
113 if ( *parit == *parentconfig )
116 parentConfigs.push_back(*parentconfig);