#include #include #include "Boolean.h" #include "ImpactStore.h" ImpactFile::ImpactFile() { // Initilise everything to null file = NULL; fb = NULL; } ImpactFile::~ImpactFile() { // Delete array if (fb!=NULL) delete fb; // Close file if (file!=NULL) fclose(file); } int ImpactFile::initialise(const int maximumid) { int i; // Open the file file = tmpfile(); if (file==NULL) return 0; // Alloc first block array fb = new long[maximumid]; if (fb==NULL) { fclose(file); file=NULL; return 0; } // Initialise first bloxk array for(i=0;iiterateStart(); while (ids[i]->iterateIsNext()) { // Check if we have to alloc a new block if (numonid[i] % impactperblock == 0) { // New block allocation if (cb == maxfilesz) { // New file cf++; pf[cf].initialise(maxid); cb=0; } // cf and cb now contains the current file and the current position in this file. // Write link in previous block. if (lf[i] !=-1) { // Write next block pointer // Seek to correct position fseek(pf[lf[i]].file, lb[i] + (impactperblock*sizeofimpact), SEEK_SET); if (cf!=lf[i]) { // We change file write file number inv = -cf; fwrite((void*)&inv,sizeof(long),1,pf[lf[i]].file); } else fwrite((void*)&cb,sizeof(long),1,pf[lf[i]].file); } // Update first block for file field if we changed file if (cf != lf[i]) pf[cf].fb[i] = cb; lb[i] = cb; lf[i] = cf; // Update current block cb += bsz; // Put the file pointer at the start of the block fseek(pf[lf[i]].file, lb[i], SEEK_SET); } // Save the impact ((Impact*)ids[i]->iterate())->write(pf[lf[i]].file); numonid[i]++; numinmemory--; } ids[i]->freeAllElements(); } numinmemory = 0; } int ImpactStore::recordImpact(const int id,const Impact& impact) { // Copy the impact buffer[numinmemory] = impact; // Add link to the impact if (!ids[id]->addElement(&buffer[numinmemory])) return FALSE; // If too much impacts on memory, flush on disk numinmemory++; if (numinmemory == buffersz) flush(); return TRUE; } int ImpactStore::getNumberOfImpacts(const int id) { return numonid[id]; } ImpactStoreWindow* ImpactStore::newImpactStoreWindow(const int id) { ImpactStoreWindow *isw; isw = new ImpactStoreWindow(); isw->is = this; isw->id = id; isw->curfile = 0; isw->curblock = pf[0].fb[id]; isw->curnum = 0; isw->maxnum = numonid[id]; return isw; } int ImpactStoreWindow::readNext(Impact &p) { if (curnum>=maxnum) return 0; while (curblock<0) { curfile++; curblock = is->pf[curfile].fb[id]; } fseek(is->pf[curfile].file,curblock + (curnum % is->impactperblock)*is->sizeofimpact,SEEK_SET); p.read(is->pf[curfile].file); curnum++; if ((curnum % is->impactperblock) == 0) { fread((void*)&curblock,sizeof(long),1,is->pf[curfile].file); } return 1; }