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 int hypercache::readC() {
77 readret = C->readConfig(&parentConfigs);
80 activeCFile = fileOfPath(C->getInFileName());
87 void hypercache::writeC() {
89 activeCFile = fileOfPath( C->getOutFileName().substr( 0, C->getOutFileName().length()-4 ) );
92 void hypercache::writeO() {
93 if ( activeCFile != "" ) {
94 unsigned long afilehash = configcache::hash(activeCFile);
95 O->writeHeader("concurrent_cfile", (char*)(&afilehash), sizeof(unsigned long));
101 int hypercache::readO() {
104 readret = O->readConfig();
106 if ( readret == -1 ) {
107 unsigned long *parentconfig = (unsigned long*)O->getHeader("concurrent_cfile");
108 if ( parentconfig != NULL )
109 addParentConfig(parentconfig);
115 void hypercache::addParentConfig(const unsigned long *parentconfig) {
116 for (vector<unsigned long>::iterator parit = parentConfigs.begin(); parit != parentConfigs.end(); ++parit)
117 if ( *parit == *parentconfig )
120 parentConfigs.push_back(*parentconfig);