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 $
|
2005-01-14 16:25:29 +01:00
|
|
|
Revision 1.19 2004/12/03 01:20:56 ponchio
|
|
|
|
Debug
|
|
|
|
|
2004-12-03 02:20:56 +01:00
|
|
|
Revision 1.18 2004/12/01 03:24:32 ponchio
|
|
|
|
Level 2.
|
|
|
|
|
2004-12-01 04:24:32 +01:00
|
|
|
Revision 1.17 2004/11/30 22:49:39 ponchio
|
|
|
|
Level 0.
|
|
|
|
|
2004-11-30 23:50:30 +01:00
|
|
|
Revision 1.16 2004/11/28 04:12:04 ponchio
|
|
|
|
winsockapi include problem
|
|
|
|
|
2004-11-28 05:12:04 +01:00
|
|
|
Revision 1.15 2004/11/18 18:30:14 ponchio
|
|
|
|
Using baricenters... lotsa changes.
|
|
|
|
|
2004-11-18 19:30:15 +01:00
|
|
|
Revision 1.14 2004/10/19 16:50:27 ponchio
|
|
|
|
Added row file access ....
|
|
|
|
|
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-11-18 19:30:15 +01:00
|
|
|
#include "mfile.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
|
2004-12-03 02:20:56 +01:00
|
|
|
* add interface for readonly reads even when file is readwrite...
|
|
|
|
* instead of queue size use ramsize!!!!
|
2004-06-24 16:32:45 +02:00
|
|
|
* some mechanism to report errors?
|
|
|
|
* use an Iterator?
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace nxs {
|
|
|
|
|
2004-11-18 19:30:15 +01:00
|
|
|
template <class T> class VFile: public MFile {
|
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-11-18 19:30:15 +01:00
|
|
|
return MFile::Create(filename);
|
2004-06-24 16:32:45 +02:00
|
|
|
}
|
|
|
|
|
2004-11-30 23:50:30 +01:00
|
|
|
bool Load(const std:: string &filename, bool rdonly = false,
|
2004-06-24 16:32:45 +02:00
|
|
|
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-11-30 23:50:30 +01:00
|
|
|
if(!MFile::Load(filename, rdonly)) return false;
|
2005-01-14 16:25:29 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-11-18 19:30:15 +01:00
|
|
|
void Delete() {
|
|
|
|
Flush();
|
|
|
|
MFile::Delete();
|
|
|
|
}
|
|
|
|
|
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-12-01 04:24:32 +01:00
|
|
|
if(!readonly) {
|
|
|
|
SetPosition((int64)buffer.key * (int64)chunk_size * (int64)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-12-03 02:20:56 +01:00
|
|
|
//TODO do i really need to flush?
|
2004-07-02 19:42:43 +02:00
|
|
|
Flush();
|
2004-11-28 05:12:04 +01:00
|
|
|
MFile::Redim((int64)elem * (int64)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-11-28 05:12:04 +01:00
|
|
|
SetPosition((int64)chunk * (int64)chunk_size * (int64)sizeof(T));
|
2004-10-04 18:49:54 +02:00
|
|
|
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-12-03 02:20:56 +01: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-11-28 05:12:04 +01:00
|
|
|
SetPosition((int64)chunk * (int64)chunk_size * (int64)sizeof(T));
|
2004-10-04 18:49:54 +02:00
|
|
|
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) {
|
2004-11-28 05:12:04 +01:00
|
|
|
SetPosition((int64)element * (int64)sizeof(T));
|
2004-10-19 18:50:27 +02:00
|
|
|
T t;
|
|
|
|
ReadBuffer(&t, sizeof(T));
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
void write(unsigned int element, T &t) {
|
2004-11-28 05:12:04 +01:00
|
|
|
SetPosition((int64)element * (int64)sizeof(T));
|
2004-10-19 18:50:27 +02:00
|
|
|
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
|