00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
#include <plearn/math/VecCompressor.h>
00042
#include "CompressedVMatrix.h"
00043
00044
namespace PLearn {
00045
using namespace std;
00046
00047
00048
00049
00050
00051
PLEARN_IMPLEMENT_OBJECT(CompressedVMatrix,
"ONE LINE DESCR",
"ONE LINE HELP");
00052
00053 CompressedVMatrix::CompressedVMatrix()
00054 : data(0), rowstarts(0), dataend(0), curpos(0)
00055 {
00056 }
00057
00058 CompressedVMatrix::CompressedVMatrix(
int the_max_length,
int the_width, size_t memory_alloc)
00059 {
init(the_max_length, the_width,
00060 memory_alloc!=0 ?memory_alloc :the_max_length*VecCompressor::worstCaseSize(the_width)); }
00061
00062 void CompressedVMatrix::init(
int the_max_length,
int the_width, size_t memory_alloc)
00063 {
00064 length_ = 0;
00065 width_ = the_width;
00066
max_length = the_max_length;
00067
data =
new signed char[memory_alloc];
00068
dataend =
data+memory_alloc;
00069
curpos =
data;
00070
rowstarts =
new signed char*[
max_length];
00071 }
00072
00073 CompressedVMatrix::CompressedVMatrix(
VMat m, size_t memory_alloc)
00074 {
00075
if(memory_alloc==0)
00076
init(m.
length(), m.
width(), m.
length()*VecCompressor::worstCaseSize(m.
width()));
00077
Vec v(m.
width());
00078
for(
int i=0; i<m.
length(); i++)
00079 {
00080 m->getRow(i,v);
00081
appendRow(v);
00082 }
00083 }
00084
00085 void CompressedVMatrix::getNewRow(
int i,
const Vec& v)
const
00086
{
00087
#ifdef BOUNDCHECK
00088
if(v.
length() != width_)
00089
PLERROR(
"In CompressedVMatrix::getNewRow length of v and width of matrix do not match");
00090
if(i<0 || i>=length_)
00091
PLERROR(
"In CompressedVMatrix::getNewRow OUT OF BOUNDS row index");
00092
#endif
00093
VecCompressor::uncompressVec(
rowstarts[i],v);
00094 }
00095
00096 void CompressedVMatrix::appendRow(
Vec v)
00097 {
00098
if(length_>=
max_length)
00099
PLERROR(
"In CompressedVMatrix::appendRow, max_length exceeded");
00100
rowstarts[length_] =
curpos;
00101 curpos = VecCompressor::compressVec(v,curpos);
00102
if(curpos>
dataend)
00103
PLERROR(
"In CompressedVMatrix::appendRow not enough space reserved for data");
00104 ++length_;
00105 }
00106
00107 void CompressedVMatrix::compacify()
00108 {
00109 size_t datasize =
curpos-
data;
00110
signed char* old_data = data;
00111
signed char** old_rowstarts =
rowstarts;
00112 data =
new signed char[datasize];
00113
dataend = data+datasize;
00114
curpos =
dataend;
00115 rowstarts =
new signed char*[length_];
00116 memcpy(data, old_data, datasize);
00117
00118
for(
int i=0; i<length_; i++)
00119 rowstarts[i] = data + (old_rowstarts[i]-old_data);
00120
max_length = length_;
00121
delete[] old_data;
00122
delete[] old_rowstarts;
00123 }
00124
00125 CompressedVMatrix::~CompressedVMatrix()
00126 {
00127
if(
data)
00128
delete[]
data;
00129
if(
rowstarts)
00130
delete[]
rowstarts;
00131 }
00132
00133 }