-
Notifications
You must be signed in to change notification settings - Fork 15
/
CompressedVMatrix.cc
146 lines (126 loc) · 4.68 KB
/
CompressedVMatrix.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// -*- C++ -*-
// PLearn (A C++ Machine Learning Library)
// Copyright (C) 1998 Pascal Vincent
// Copyright (C) 1999-2001 Pascal Vincent, Yoshua Bengio, Rejean Ducharme and University of Montreal
// Copyright (C) 2002 Pascal Vincent, Julien Keable, Xavier Saint-Mleux
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. The name of the authors may not be used to endorse or promote
// products derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
// NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// This file is part of the PLearn library. For more information on the PLearn
// library, go to the PLearn Web site at www.plearn.org
/* *******************************************************
* $Id$
******************************************************* */
#include "CompressedVMatrix.h"
#include <plearn/math/VecCompressor.h>
namespace PLearn {
using namespace std;
// ************************
// ** CompressedVMatrix **
PLEARN_IMPLEMENT_OBJECT(CompressedVMatrix, "ONE LINE DESCR", "ONE LINE HELP");
CompressedVMatrix::CompressedVMatrix()
: data(0), rowstarts(0), dataend(0), curpos(0)
{
}
CompressedVMatrix::CompressedVMatrix(int the_max_length, int the_width, size_t memory_alloc)
{ init(the_max_length, the_width,
memory_alloc!=0 ?memory_alloc :the_max_length*VecCompressor::worstCaseSize(the_width)); }
void CompressedVMatrix::init(int the_max_length, int the_width, size_t memory_alloc)
{
length_ = 0;
width_ = the_width;
max_length = the_max_length;
data = new signed char[memory_alloc];
dataend = data+memory_alloc;
curpos = data;
rowstarts = new signed char*[max_length];
}
CompressedVMatrix::CompressedVMatrix(VMat m, size_t memory_alloc)
{
if(memory_alloc==0)
init(m.length(), m.width(), m.length()*VecCompressor::worstCaseSize(m.width()));
Vec v(m.width());
for(int i=0; i<m.length(); i++)
{
m->getRow(i,v);
appendRow(v);
}
}
void CompressedVMatrix::getNewRow(int i, const Vec& v) const
{
#ifdef BOUNDCHECK
if(v.length() != width_)
PLERROR("In CompressedVMatrix::getNewRow length of v and width of matrix do not match");
if(i<0 || i>=length_)
PLERROR("In CompressedVMatrix::getNewRow OUT OF BOUNDS row index");
#endif
VecCompressor::uncompressVec(rowstarts[i],v);
}
void CompressedVMatrix::appendRow(Vec v)
{
if(length_>=max_length)
PLERROR("In CompressedVMatrix::appendRow, max_length exceeded");
rowstarts[length_] = curpos;
curpos = VecCompressor::compressVec(v,curpos);
if(curpos>dataend)
PLERROR("In CompressedVMatrix::appendRow not enough space reserved for data");
++length_;
}
void CompressedVMatrix::compacify()
{
size_t datasize = curpos-data;
signed char* old_data = data;
signed char** old_rowstarts = rowstarts;
data = new signed char[datasize];
dataend = data+datasize;
curpos = dataend;
rowstarts = new signed char*[length_];
memcpy(data, old_data, datasize);
for(int i=0; i<length_; i++)
rowstarts[i] = data + (old_rowstarts[i]-old_data);
max_length = length_;
delete[] old_data;
delete[] old_rowstarts;
}
CompressedVMatrix::~CompressedVMatrix()
{
if(data)
delete[] data;
if(rowstarts)
delete[] rowstarts;
}
} // end of namespace PLearn
/*
Local Variables:
mode:c++
c-basic-offset:4
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0))
indent-tabs-mode:nil
fill-column:79
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :