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 "RowBufferedVMatrix.h"
00042
#include <plearn/math/TMat_maths.h>
00043
00044
namespace PLearn {
00045
using namespace std;
00046
00049
PLEARN_IMPLEMENT_ABSTRACT_OBJECT(RowBufferedVMatrix,
00050
"A base class for VMatrices that keep the last row(s) in a buffer for faster access.",
00051
"");
00052
00053 RowBufferedVMatrix::RowBufferedVMatrix(
int the_length,
int the_width)
00054 :
VMatrix(the_length, the_width),
00055
current_row_index(-1), current_row(the_width),
00056
other_row_index(-1), other_row(the_width)
00057 {}
00058
00059 RowBufferedVMatrix::RowBufferedVMatrix()
00060 :
current_row_index(-1),
other_row_index(-1)
00061 {}
00062
00064
00066 real RowBufferedVMatrix::get(
int i,
int j)
const
00067
{
00068
if(
current_row_index!=i)
00069 {
00070
current_row.
resize(width_);
00071
getNewRow(i,
current_row);
00072
current_row_index = i;
00073 }
00074
return current_row[j];
00075 }
00076
00078
00080 void RowBufferedVMatrix::getRow(
int i,
Vec v)
const {
00081
if (
current_row_index != i) {
00082
current_row.
resize(width_);
00083
getNewRow(i,
current_row);
00084
current_row_index = i;
00085 }
00086 v.
copyFrom(
current_row.
data(), width_);
00087 }
00088
00090
00092 void RowBufferedVMatrix::getSubRow(
int i,
int j,
Vec v)
const
00093
{
00094
if(
current_row_index!=i)
00095 {
00096
current_row.
resize(width_);
00097
getNewRow(i,
current_row);
00098
current_row_index = i;
00099 }
00100 v.
copyFrom(
current_row.
data()+j, v.
length());
00101 }
00102
00104
00106 real RowBufferedVMatrix::dot(
int i1,
int i2,
int inputsize)
const
00107
{
00108
int w = width_;
00109
current_row.
resize(w);
00110
other_row.
resize(w);
00111
00112
if(i1==
current_row_index)
00113 {
00114
if(i2==i1)
00115
return pownorm(
current_row.
subVec(0,inputsize));
00116
if(i2!=
other_row_index)
00117 {
00118
getNewRow(i2,
other_row);
00119
other_row_index = i2;
00120 }
00121 }
00122
else if(i1==
other_row_index)
00123 {
00124
if(i2==i1)
00125
return pownorm(
other_row.
subVec(0,inputsize));
00126
if(i2!=
current_row_index)
00127 {
00128
getNewRow(i2,
current_row);
00129
current_row_index = i2;
00130 }
00131 }
00132
else
00133 {
00134
if(i2==
current_row_index)
00135 {
00136
getNewRow(i1,
other_row);
00137
other_row_index = i1;
00138 }
00139
else if(i2==
other_row_index)
00140 {
00141
getNewRow(i1,
current_row);
00142
current_row_index = i1;
00143 }
00144
else
00145 {
00146
getNewRow(i1,
current_row);
00147
getNewRow(i2,
other_row);
00148
current_row_index = i1;
00149
other_row_index = i2;
00150 }
00151 }
00152
return PLearn::dot(
current_row.
subVec(0,inputsize),
other_row.
subVec(0,inputsize));
00153 }
00154
00156
00158 real RowBufferedVMatrix::dot(
int i,
const Vec& v)
const
00159
{
00160
if(i!=
current_row_index)
00161 {
00162
current_row.
resize(width_);
00163
getNewRow(i,
current_row);
00164 i =
current_row_index;
00165 }
00166
return PLearn::dot(
current_row.
subVec(0,v.
length()),v);
00167 }
00168
00170
00172 void RowBufferedVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) {
00173 inherited::makeDeepCopyFromShallowCopy(copies);
00174
deepCopyField(
current_row, copies);
00175
deepCopyField(
other_row, copies);
00176 }
00177
00178 }