vectors.h
5.01 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// -*- C++ -*-
// Little library of vectors and sparse vectors
// Copyright (C) 2007- Leon Bottou
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 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 for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA
// $Id: vectors.h,v 1.13 2007/10/02 20:40:06 cvs Exp $
#ifndef VECTORS_H
#define VECTORS_H 1
#include <cstring>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cassert>
#include <vector>
#include <cmath>
#include "wrapper.h"
class FVector;
class SVector;
typedef float VFloat;
class FVector
{
private:
struct Rep
{
int refcount;
int size;
VFloat *data;
Rep() : size(0), data(0) {}
~Rep() { delete [] data; }
void resize(int n);
Rep *copy();
};
Wrapper<Rep> w;
Rep *rep() { return w.rep(); }
const Rep *rep() const { return w.rep(); }
void qset(int i, double v);
public:
FVector();
FVector(int n);
FVector(const SVector &v);
int size() const { return rep()->size; }
// these accessors are range-checked.
// get() returns 0 when i is out-of-range.
// set() expands the vector.
double get(int i) const;
double set(int i, double v);
// warning: you can write vector[i] but
// the subscripts are not range-checked!
// on the other hand, that's fast.
operator const VFloat* () const { return rep()->data; }
operator VFloat* () { w.detach(); return rep()->data; }
void clear();
void resize(int n);
void touch(int i);
FVector slice(int fi, int ti) const;
void add(double c1);
void add(const FVector &v2);
void add(const SVector &v2);
void add(const FVector &v2, double c2);
void add(const SVector &v2, double c2);
void add(const FVector &v2, double c2, const FVector &q2);
void add(const SVector &v2, double c2, const FVector &q2);
void scale(double c1);
void combine(double c1, const FVector &v2, double c2);
void combine(double c1, const SVector &v2, double c2);
friend std::ostream& operator<<(std::ostream &f, const FVector &v);
friend std::istream& operator>>(std::istream &f, FVector &v);
bool save(std::ostream &f) const;
bool load(std::istream &f);
};
class SVector
{
public:
struct Pair
{
int i;
VFloat v;
};
private:
struct Rep
{
int refcount;
int npairs;
int mpairs;
int size;
struct Pair *pairs;
Rep() : npairs(0), mpairs(-1), size(0), pairs(0) {}
~Rep() { delete [] pairs; }
void resize(int n);
double qset(int i, double v);
Rep *copy();
};
Wrapper<Rep> w;
Rep *rep() { return w.rep(); }
const Rep *rep() const { return w.rep(); }
public:
SVector();
SVector(const FVector &v);
int size() const { return rep()->size; }
// these accessors are range-checked.
// get() returns 0 when i is out-of-range.
// set() expands the vector.
double get(int i) const;
double set(int i, double v);
// to quickly iterate over the non-zero coefficients,
// do for(SVector::Pair *p = x; p->i>=0; p++) { ... }
int npairs() const { return rep()->npairs; }
operator const Pair* () const { return rep()->pairs; }
void clear();
void trim();
SVector slice(int fi, int ti) const;
std::pair<unsigned, double> extract_component(unsigned aFeatureOrderIndex);
int sparse_size();
void add(const SVector &v2);
void add(const SVector &v2, double c2);
void scale(double c1);
void combine(double c1, const SVector &v2, double c2);
void normalise();
std::vector< std::pair <int,double> > unpack();
friend std::ostream& operator<<(std::ostream &f, const SVector &v);
friend std::istream& operator>>(std::istream &f, SVector &v);
bool save(std::ostream &f) const;
bool load(std::istream &f);
friend SVector combine(const SVector &v1, double a1,
const SVector &v2, double a2);
};
double dot(const FVector &v1, const FVector &v2);
double dot(const FVector &v1, const SVector &v2);
double dot(const SVector &v1, const FVector &v2);
double dot(const SVector &v1, const SVector &v2);
SVector combine(const SVector &v1, double a1, const SVector &v2, double a2);
FVector combine(const FVector &v1, double a1, const SVector &v2, double a2);
FVector combine(const SVector &v1, double a1, const FVector &v2, double a2);
FVector combine(const FVector &v1, double a1, const FVector &v2, double a2);
#endif
/* -------------------------------------------------------------
Local Variables:
c++-font-lock-extra-types: ("\\sw+_t" "[A-Z]\\sw*[a-z]\\sw*" "std::\\sw+")
End:
------------------------------------------------------------- */