2004-06-24 16:32:45 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
|
|
|
* Copyright(C) 2004 \/)\/ *
|
|
|
|
* Visual Computing Lab /\/| *
|
|
|
|
* ISTI - Italian National Research Council | *
|
|
|
|
* \ *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
|
|
|
* 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 *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
|
|
|
* for more details. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
|
|
History
|
|
|
|
|
|
|
|
$Log: not supported by cvs2svn $
|
2004-10-19 18:50:27 +02:00
|
|
|
Revision 1.13 2004/10/08 15:12:04 ponchio
|
|
|
|
Working version (maybe)
|
|
|
|
|
2004-10-08 17:12:04 +02:00
|
|
|
Revision 1.12 2004/10/04 16:49:54 ponchio
|
|
|
|
Daily backup. Preparing for compression.
|
|
|
|
|
2004-10-04 18:49:54 +02:00
|
|
|
Revision 1.11 2004/10/01 16:00:12 ponchio
|
|
|
|
Added include <assert.h>
|
|
|
|
|
2004-10-01 18:00:12 +02:00
|
|
|
Revision 1.10 2004/09/30 23:56:33 ponchio
|
|
|
|
Backup (added strips and normals)
|
|
|
|
|
2004-10-01 01:56:33 +02:00
|
|
|
Revision 1.9 2004/07/20 14:04:32 ponchio
|
|
|
|
Improved efficience in operator[]
|
|
|
|
|
2004-07-20 16:04:32 +02:00
|
|
|
Revision 1.8 2004/07/15 14:32:49 ponchio
|
|
|
|
Debug.
|
|
|
|
|
2004-07-15 16:32:49 +02:00
|
|
|
Revision 1.7 2004/07/05 15:49:39 ponchio
|
|
|
|
Windows (DevCpp, mingw) port.
|
|
|
|
|
2004-07-05 17:49:39 +02:00
|
|
|
Revision 1.6 2004/07/04 15:23:48 ponchio
|
|
|
|
Debug
|
|
|
|
|
2004-07-04 17:23:48 +02:00
|
|
|
Revision 1.5 2004/07/02 17:41:37 ponchio
|
|
|
|
Debug.
|
|
|
|
|
2004-07-02 19:42:43 +02:00
|
|
|
Revision 1.4 2004/07/02 13:02:39 ponchio
|
|
|
|
Added GetRegion, Read and Write
|
|
|
|
|
2004-07-02 15:02:39 +02:00
|
|
|
Revision 1.3 2004/07/01 21:36:54 ponchio
|
|
|
|
*** empty log message ***
|
|
|
|
|
2004-07-01 23:36:54 +02:00
|
|
|
Revision 1.2 2004/06/25 16:47:13 ponchio
|
|
|
|
Various debug
|
|
|
|
|
2004-06-25 18:47:13 +02:00
|
|
|
Revision 1.1 2004/06/24 14:32:45 ponchio
|
|
|
|
Moved from wrap/nexus
|
|
|
|
|
2004-06-24 16:32:45 +02:00
|
|
|
Revision 1.3 2004/06/22 15:32:09 ponchio
|
|
|
|
Tested
|
|
|
|
|
|
|
|
Revision 1.2 2004/06/22 10:27:16 ponchio
|
|
|
|
*** empty log message ***
|
|
|
|
|
|
|
|
Revision 1.1 2004/06/22 00:39:56 ponchio
|
|
|
|
Created
|
|
|
|
|
|
|
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef VFILE_H
|
|
|
|
#define VFILE_H
|
|
|
|
|
2004-10-04 18:49:54 +02:00
|
|
|
#include "file.h"
|
2004-07-05 17:49:39 +02:00
|
|
|
|
2004-10-01 18:00:12 +02:00
|
|
|
#include <assert.h>
|
2004-06-24 16:32:45 +02:00
|
|
|
#include <map>
|
|
|
|
#include <list>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
/**Vector structure on file with simulated mmapping.
|
|
|
|
* a priority queue of buffers is used
|
|
|
|
* TODO: port to over 4Gb usable space
|
|
|
|
* some mechanism to report errors?
|
|
|
|
* use an Iterator?
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace nxs {
|
|
|
|
|
2004-10-04 18:49:54 +02:00
|
|
|
template <class T> class VFile: public File {
|
2004-06-24 16:32:45 +02:00
|
|
|
public:
|
|
|
|
|
|
|
|
struct Buffer {
|
|
|
|
unsigned int key;
|
2004-07-02 19:42:43 +02:00
|
|
|
unsigned int size; //in number of elements
|
2004-06-24 16:32:45 +02:00
|
|
|
T *data;
|
|
|
|
};
|
|
|
|
|
2004-10-04 18:49:54 +02:00
|
|
|
protected:
|
|
|
|
unsigned int n_elements;
|
2004-06-24 16:32:45 +02:00
|
|
|
std::list<Buffer> buffers;
|
2004-06-25 18:47:13 +02:00
|
|
|
typedef typename std::list<Buffer>::iterator list_iterator;
|
2004-06-24 16:32:45 +02:00
|
|
|
|
2004-06-25 18:47:13 +02:00
|
|
|
std::map<unsigned int, list_iterator> index; //TODO move to hash_map
|
2004-07-20 16:04:32 +02:00
|
|
|
Buffer *last_buffer;
|
2004-06-24 16:32:45 +02:00
|
|
|
|
|
|
|
unsigned int chunk_size; //default buffer size (expressed in number of T)
|
|
|
|
unsigned int queue_size;
|
|
|
|
|
|
|
|
public:
|
2004-06-25 18:47:13 +02:00
|
|
|
class iterator {
|
|
|
|
public:
|
|
|
|
iterator(unsigned int p = 0, VFile *b = 0): n(p), buffer(b) {}
|
|
|
|
T &operator*() { return (*buffer)[n]; }
|
|
|
|
void operator++() { n++; }
|
|
|
|
bool operator!=(const iterator &i) { return i.n != n; }
|
|
|
|
private:
|
|
|
|
unsigned int n;
|
|
|
|
VFile *buffer;
|
|
|
|
};
|
2004-06-24 16:32:45 +02:00
|
|
|
|
2004-10-04 18:49:54 +02:00
|
|
|
VFile(): last_buffer(NULL) {}
|
|
|
|
~VFile() { Close(); }
|
2004-06-24 16:32:45 +02:00
|
|
|
bool Create(const std::string &filename,
|
|
|
|
unsigned int _chunk_size = 4096/sizeof(T),
|
|
|
|
unsigned int _queue_size = 1000) {
|
|
|
|
|
|
|
|
assert(_chunk_size > 0);
|
2004-10-04 18:49:54 +02:00
|
|
|
n_elements = 0;
|
2004-07-20 16:04:32 +02:00
|
|
|
last_buffer = NULL;
|
2004-06-24 16:32:45 +02:00
|
|
|
chunk_size = _chunk_size;
|
|
|
|
queue_size = _queue_size;
|
2004-07-05 17:49:39 +02:00
|
|
|
|
2004-10-04 18:49:54 +02:00
|
|
|
return File::Create(filename);
|
2004-06-24 16:32:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Load(const std:: string &filename,
|
|
|
|
unsigned int _chunk_size = 4096/sizeof(T),
|
|
|
|
unsigned int _queue_size = 1000) {
|
|
|
|
|
|
|
|
assert(_chunk_size > 0);
|
2004-07-20 16:04:32 +02:00
|
|
|
last_buffer = NULL;
|
2004-06-24 16:32:45 +02:00
|
|
|
chunk_size = _chunk_size;
|
|
|
|
queue_size = _queue_size;
|
2004-07-05 17:49:39 +02:00
|
|
|
|
2004-10-04 18:49:54 +02:00
|
|
|
if(!File::Load(filename)) return false;
|
|
|
|
n_elements = size/sizeof(T);
|
2004-06-24 16:32:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Close() {
|
2004-07-02 19:42:43 +02:00
|
|
|
Flush();
|
2004-06-24 16:32:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Flush() {
|
2004-06-25 18:47:13 +02:00
|
|
|
list_iterator i;
|
2004-06-24 16:32:45 +02:00
|
|
|
for(i = buffers.begin(); i != buffers.end(); i++)
|
|
|
|
FlushBuffer(*i);
|
|
|
|
buffers.clear();
|
|
|
|
index.clear();
|
2004-07-20 16:04:32 +02:00
|
|
|
last_buffer = NULL;
|
2004-06-24 16:32:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FlushBuffer(Buffer buffer) {
|
2004-10-04 18:49:54 +02:00
|
|
|
SetPosition(buffer.key * chunk_size * sizeof(T));
|
|
|
|
WriteBuffer((char *)(buffer.data), buffer.size * sizeof(T));
|
2004-06-24 16:32:45 +02:00
|
|
|
delete []buffer.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resize(unsigned int elem) {
|
2004-07-02 19:42:43 +02:00
|
|
|
Flush();
|
2004-10-08 17:12:04 +02:00
|
|
|
File::Redim(elem * sizeof(T));
|
2004-06-24 16:32:45 +02:00
|
|
|
n_elements = elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Remember that T is a valid pointer only until next call of
|
|
|
|
* getElement or setElement
|
|
|
|
*/
|
|
|
|
T &operator[](unsigned int n) {
|
|
|
|
|
2004-07-02 15:02:39 +02:00
|
|
|
assert(n < n_elements);
|
2004-06-24 16:32:45 +02:00
|
|
|
|
|
|
|
unsigned int chunk = n/chunk_size;
|
|
|
|
unsigned int offset = n - chunk*chunk_size;
|
2004-07-02 19:42:43 +02:00
|
|
|
assert(offset < chunk_size * sizeof(T));
|
2004-06-24 16:32:45 +02:00
|
|
|
|
2004-07-20 16:04:32 +02:00
|
|
|
if(last_buffer && last_buffer->key == chunk)
|
|
|
|
return *(last_buffer->data + offset);
|
|
|
|
|
|
|
|
if(index.count(chunk)) {
|
|
|
|
last_buffer = &*index[chunk];
|
2004-06-24 16:32:45 +02:00
|
|
|
return *((*(index[chunk])).data + offset);
|
2004-07-20 16:04:32 +02:00
|
|
|
}
|
2004-06-24 16:32:45 +02:00
|
|
|
|
|
|
|
if(buffers.size() > queue_size) {
|
|
|
|
Buffer &buffer= buffers.back();
|
2004-07-02 19:42:43 +02:00
|
|
|
assert(buffer.key != chunk);
|
2004-06-24 16:32:45 +02:00
|
|
|
FlushBuffer(buffer);
|
|
|
|
index.erase(buffer.key);
|
|
|
|
buffers.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer buffer;
|
|
|
|
buffer.key = chunk;
|
2004-07-02 19:42:43 +02:00
|
|
|
buffer.size = chunk_size;
|
2004-07-15 16:32:49 +02:00
|
|
|
|
2004-07-02 19:42:43 +02:00
|
|
|
if(buffer.size + chunk * chunk_size > n_elements)
|
2004-07-15 16:32:49 +02:00
|
|
|
buffer.size = n_elements - chunk * chunk_size;
|
|
|
|
|
|
|
|
buffer.data = new T[buffer.size];
|
2004-07-02 19:42:43 +02:00
|
|
|
|
|
|
|
buffers.push_front(buffer);
|
|
|
|
index[buffer.key] = buffers.begin();
|
2004-07-20 16:04:32 +02:00
|
|
|
last_buffer = &*buffers.begin();
|
|
|
|
|
2004-10-04 18:49:54 +02:00
|
|
|
SetPosition(chunk * chunk_size * sizeof(T));
|
|
|
|
ReadBuffer((char *)(buffer.data), buffer.size * sizeof(T));
|
2004-06-24 16:32:45 +02:00
|
|
|
|
|
|
|
return *(buffer.data + offset);
|
|
|
|
}
|
2004-07-04 17:23:48 +02:00
|
|
|
|
2004-07-02 15:02:39 +02:00
|
|
|
/** you can get a region instead of an element but:
|
|
|
|
1)region must be Chunk aligned.
|
|
|
|
2)you get impredictable results if regions overlap or mix with operator[]
|
|
|
|
*/
|
2004-10-01 01:56:33 +02:00
|
|
|
T *GetRegion(unsigned int start, unsigned int size, bool flush = true) {
|
2004-07-02 15:02:39 +02:00
|
|
|
assert(start + size <= n_elements);
|
2004-07-15 16:32:49 +02:00
|
|
|
assert((size % chunk_size) == 0);
|
|
|
|
assert((start % chunk_size) == 0);
|
|
|
|
if(size == 0) return NULL;
|
|
|
|
|
2004-07-02 15:02:39 +02:00
|
|
|
unsigned int chunk = start/chunk_size;
|
|
|
|
|
|
|
|
if(index.count(chunk))
|
|
|
|
return ((*(index[chunk])).data);
|
|
|
|
|
2004-10-01 01:56:33 +02:00
|
|
|
while(flush && buffers.size() > queue_size) {
|
2004-07-02 15:02:39 +02:00
|
|
|
Buffer &buffer= buffers.back();
|
|
|
|
FlushBuffer(buffer);
|
|
|
|
index.erase(buffer.key);
|
|
|
|
buffers.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer buffer;
|
|
|
|
buffer.key = chunk;
|
2004-07-15 16:32:49 +02:00
|
|
|
buffer.size = size;
|
|
|
|
buffer.data = new T[buffer.size];
|
2004-07-02 19:42:43 +02:00
|
|
|
|
|
|
|
buffers.push_front(buffer);
|
|
|
|
index[chunk] = buffers.begin();
|
|
|
|
|
2004-10-04 18:49:54 +02:00
|
|
|
SetPosition(chunk * chunk_size * sizeof(T));
|
|
|
|
ReadBuffer((char *)(buffer.data), buffer.size * sizeof(T));
|
2004-07-02 15:02:39 +02:00
|
|
|
return buffer.data;
|
2004-10-19 18:50:27 +02:00
|
|
|
}
|
|
|
|
//non buffered read only acces.
|
|
|
|
T read(unsigned int element) {
|
|
|
|
SetPosition(element * sizeof(T));
|
|
|
|
T t;
|
|
|
|
ReadBuffer(&t, sizeof(T));
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
void write(unsigned int element, T &t) {
|
|
|
|
SetPosition(element * sizeof(T));
|
|
|
|
WriteBuffer(&t, sizeof(T));
|
|
|
|
}
|
2004-06-24 16:32:45 +02:00
|
|
|
|
2004-07-15 16:32:49 +02:00
|
|
|
void PushBack(const T &t) {
|
|
|
|
Resize(n_elements+1);
|
|
|
|
operator[](n_elements-1) = t;
|
|
|
|
}
|
|
|
|
|
2004-06-24 16:32:45 +02:00
|
|
|
unsigned int Size() { return n_elements; }
|
|
|
|
unsigned int ChunkSize() { return chunk_size; }
|
|
|
|
unsigned int QueueSize() { return queue_size; }
|
2004-06-25 18:47:13 +02:00
|
|
|
iterator Begin() { return iterator(0, this); }
|
|
|
|
iterator End() { return iterator(Size(), this); }
|
2004-06-24 16:32:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}//namespace
|
|
|
|
|
|
|
|
#endif
|