This commit is contained in:
Federico Ponchio 2004-06-22 15:32:09 +00:00
parent f33b962262
commit 098b2acac0
1 changed files with 203 additions and 201 deletions

View File

@ -2,207 +2,209 @@
* VCGLib o o * * VCGLib o o *
* Visual and Computer Graphics Library o o * * Visual and Computer Graphics Library o o *
* _ O _ * * _ O _ *
* Copyright(C) 2004 \/)\/ * * Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| * * Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | * * ISTI - Italian National Research Council | *
* \ * * \ *
* All rights reserved. * * All rights reserved. *
* * * *
* This program is free software; you can redistribute it and/or modify * * This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * * it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or * * the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. * * (at your option) any later version. *
* * * *
* This program is distributed in the hope that it will be useful, * * This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of * * but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. * * for more details. *
* * * *
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.2 2004/06/22 10:27:16 ponchio
*** empty log message ***
Revision 1.1 2004/06/22 00:39:56 ponchio Revision 1.1 2004/06/22 00:39:56 ponchio
Created Created
****************************************************************************/ ****************************************************************************/
#ifndef VFILE_H #ifndef VFILE_H
#define VFILE_H #define VFILE_H
#include <errno.h> #include <unistd.h>
//#include <hash_map> #include <errno.h>
#include <map> //#include <hash_map>
#include <list> #include <map>
#include <string> #include <list>
#include <iostream> #include <string>
/**Vector structure on file with simulated mmapping. /**Vector structure on file with simulated mmapping.
* a priority queue of buffers is used * a priority queue of buffers is used
* TODO: port to over 4Gb usable space * TODO: port to over 4Gb usable space
* some mechanism to report errors? * some mechanism to report errors?
* use an Iterator? * use an Iterator?
*/ */
template <class T> class VFile { namespace nxs {
public:
template <class T> class VFile {
struct Buffer { public:
unsigned int key;
unsigned int size; struct Buffer {
T *data; unsigned int key;
}; T *data;
};
typedef std::list<Buffer>::iterator iterator;
private:
private:
FILE *fp;
FILE *fp; std::list<Buffer> buffers;
std::map<unsigned int, iterator> index; //TODO move to hash_map typedef typename std::list<Buffer>::iterator iterator;
std::list<Buffer> buffers;
unsigned int chunk_size; //default buffer size std::map<unsigned int, iterator> index; //TODO move to hash_map
unsigned int chunk_bits; //log2(chunk_size);
unsigned int queue_size; unsigned int chunk_size; //default buffer size (expressed in number of T)
unsigned int n_elements; //size of the vector unsigned int queue_size;
unsigned int n_elements; //size of the vector
public:
public:
VFile(): fp(NULL) {}
~VFile() { if(fp) close(); } VFile(): fp(NULL) {}
bool create(const std::string &filename, ~VFile() { if(fp) Close(); }
unsigned int _chunk_bits = 12, bool Create(const std::string &filename,
unsigned int _queue_size = 1000) { unsigned int _chunk_size = 4096/sizeof(T),
unsigned int _queue_size = 1000) {
fp = fopen(filename.c_str(), "wb+");
if(!fp) assert(_chunk_size > 0);
return false; chunk_size = _chunk_size;
chunk_bits = _chunk_bits; queue_size = _queue_size;
chunk_size = 1<<_chunk_bits; n_elements = 0;
queue_size = _queue_size;
n_elements = 0; fp = fopen(filename.c_str(), "wb+");
return true; if(!fp) return false;
}
return true;
bool load(const std:: string &filename, }
unsigned int _chunk_bits = 12,
unsigned int _queue_size = 1000) { bool Load(const std:: string &filename,
unsigned int _chunk_size = 4096/sizeof(T),
fp = fopen(filename.c_str(), "rb+"); unsigned int _queue_size = 1000) {
if(!fp) return false;
assert(_chunk_size > 0);
//troviamone la lunghezza chunk_size = _chunk_size;
fseek(fp, -1, SEEK_END); queue_size = _queue_size;
chunk_bits = _chunk_bits;
chunk_size = 1<<_chunk_bits; fp = fopen(filename.c_str(), "rb+");
queue_size = _queue_size; if(!fp) return false;
n_elements = ftell(fp)/ sizeof(T); fseek(fp, 0, SEEK_END);
assert(n_elements >= chunk_size); n_elements = ftell(fp)/ sizeof(T);
return true; return true;
} }
void close() { void Close() {
flush(); Flush();
fclose(fp); fclose(fp);
fp = 0; fp = 0;
} }
void flush() { void Flush() {
iterator i; iterator i;
for(i = buffers.begin(); i != buffers.end(); i++) for(i = buffers.begin(); i != buffers.end(); i++)
flushBuffer(*i); FlushBuffer(*i);
buffers.clear(); buffers.clear();
index.clear(); index.clear();
} }
void flushBuffer(Buffer buffer) { void FlushBuffer(Buffer buffer) {
fseek(fp, buffer.key * sizeof(T), SEEK_SET); fseek(fp, buffer.key * chunk_size * sizeof(T), SEEK_SET);
if(buffer.size != fwrite(buffer.data, sizeof(T), buffer.size, fp)) { if(chunk_size != fwrite(buffer.data, sizeof(T), chunk_size, fp)) {
cerr << "Could not write!" << endl; assert(0 && "Could not write");
exit(0); }
} delete []buffer.data;
delete []buffer.data; }
}
void Resize(unsigned int elem) {
void resize(unsigned int elem) { if(elem > n_elements) {
if(elem < n_elements) if(-1 == fseek(fp, elem*sizeof(T) -1, SEEK_SET)) {
return; assert(0 && "Could not resize");
if(n_elements < chunk_size) }
n_elements = chunk_size; unsigned char a;
while(elem > n_elements && n_elements < 256000000) fwrite(&a, sizeof(unsigned char), 1, fp);
n_elements *= 2; } else {
while(elem > n_elements) //TODO optimize: we do not need flush for buffers over elem.
n_elements += 256000000; Flush();
if(-1 == fseek(fp, n_elements * sizeof(T), SEEK_SET)) { int fd = fileno(fp);
cerr << "Could not resize!" << endl; ftruncate(fd, elem*sizeof(T));
exit(-1); }
} n_elements = elem;
fwrite(&elem, sizeof(unsigned int), 1, fp); }
}
/** Remember that T is a valid pointer only until next call of
/** Remember that T is a valid pointer only until next call of * getElement or setElement
* getElement or setElement */
*/ T &operator[](unsigned int n) {
T *getElement(unsigned int n) {
assert(n <= n_elements);
if(n > n_elements) {
cerr << "Overflow!" << endl; unsigned int chunk = n/chunk_size;
return NULL; unsigned int offset = n - chunk*chunk_size;
} assert(offset < chunk_size*sizeof(T));
unsigned int chunk = (n >> chunk_bits) << chunk_bits;
unsigned int offset = n - chunk; if(index.count(chunk))
return *((*(index[chunk])).data + offset);
if(index.count(chunk))
return (*(index[chunk])).data + offset; if(buffers.size() > queue_size) {
Buffer &buffer= buffers.back();
if(buffers.size() > queue_size) { FlushBuffer(buffer);
Buffer &buffer= buffers.back(); index.erase(buffer.key);
flushBuffer(buffer); buffers.pop_back();
index.erase(buffer.key); }
buffers.pop_back();
} Buffer buffer;
buffer.key = chunk;
Buffer buffer; buffer.data = new T[chunk_size * sizeof(T)];
buffer.key = chunk; if(fseek(fp, chunk * chunk_size * sizeof(T), SEEK_SET)) {
buffer.data = new T[chunk_size * chunks]; assert(0 && "failed to fseek");
buffer.size = chunks * chunk_size; return *(buffer.data);
if(fseek(fp, chunk * sizeof(T), SEEK_SET)) { }
cerr << "failed to fseek" << endl;
return NULL; unsigned int data_size = chunk_size;
} if(data_size + chunk * chunk_size > n_elements)
data_size = -chunk * chunk_size + n_elements;
if(buffer.size != fread(buffer.data, sizeof(T), buffer.size, fp)) {
if(!ferror(fp)) { if(data_size != fread(buffer.data, sizeof(T), data_size, fp)) {
cerr << "end of file" << endl; if(feof(fp)) {
} else { assert(0 && "end of file");
cerr << "failed reading!: " << errno << endl; } else {
} assert(0 && "failed reading!");
return NULL; }
} return (*buffer.data);
}
buffers.push_front(buffer);
index[chunk] = buffers.begin(); buffers.push_front(buffer);
return buffer.data + offset; index[chunk] = buffers.begin();
} return *(buffer.data + offset);
}
/**use this for directly writing on the file...
* be careful to flush (unless you never readed or flushed) /**use this for directly writing on the file...
*/ * be careful to flush (unless you never readed or flushed)
*/
void setElement(unsigned int i, T &t) { unsigned int Size() { return n_elements; }
*getElement(i) = t; unsigned int ChunkSize() { return chunk_size; }
} unsigned int QueueSize() { return queue_size; }
void setPosition(unsigned int chunk) { protected:
fseek(fp, chunk * sizeof(T), SEEK_SET); void SetPosition(unsigned int chunk) {
} fseek(fp, chunk * chunk_size * sizeof(T), SEEK_SET);
}
unsigned int size() { return n_elements; } };
unsigned int chunkSize() { return chunk_size; }
unsigned int queueSize() { return queue_size; } }//namespace
};
#endif
#endif