00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef VECTORS_H
00024 #define VECTORS_H
00025
00026 #include <cstring>
00027 #include <string>
00028 #include <sstream>
00029 #include <cassert>
00030 #include <iostream>
00031 #include "wrapper.h"
00032
00033
00034 class FVector;
00035 class SVector;
00036
00037 typedef double VFloat;
00038
00039 class FVector
00040 {
00041 private:
00042 struct Rep
00043 {
00044 int refcount;
00045 int size;
00046 VFloat *data;
00047 Rep() : size(0), data(0) {}
00048 ~Rep() { delete [] data; }
00049 void resize(int n);
00050 Rep *copy();
00051 };
00052
00053 Wrapper<Rep> w;
00054 Rep *rep() { return w.rep(); }
00055 const Rep *rep() const { return w.rep(); }
00056 void qset(int i, double v);
00057
00058 public:
00059 FVector();
00060 FVector(int n);
00061 FVector(const SVector &v);
00062 int size() const { return rep()->size; }
00063
00064
00065
00066
00067 double get(int i) const;
00068 double set(int i, double v);
00069
00070
00071
00072
00073 operator const VFloat* () const { return rep()->data; }
00074 operator VFloat* () { w.detach(); return rep()->data; }
00075
00076 void clear();
00077 void resize(int n);
00078 void touch(int i);
00079 FVector slice(int fi, int ti) const;
00080
00081 void add(double c1);
00082 void add(const FVector &v2);
00083 void add(const SVector &v2);
00084 void add(const FVector &v2, double c2);
00085 void add(const SVector &v2, double c2);
00086 void add(const FVector &v2, double c2, const FVector &q2);
00087 void add(const SVector &v2, double c2, const FVector &q2);
00088 void scale(double c1);
00089 void combine(double c1, const FVector &v2, double c2);
00090 void combine(double c1, const SVector &v2, double c2);
00091
00092
00093 friend std::ostream& operator<<(std::ostream &f, const FVector &v);
00094 friend std::istream& operator>>(std::istream &f, FVector &v);
00095 bool save(std::ostream &f) const;
00096 bool load(std::istream &f);
00097 void loadString(std::string str);
00103 int fromPSQL(std::string str);
00108 double getSpaceUsed();
00109
00115 void *write_bin(size_t &total_size);
00122 int read_bin(void *data, size_t length);
00123 };
00124
00125
00126
00127 class SVector
00128 {
00129 public:
00130 struct Pair
00131 {
00132 int i;
00133 VFloat v;
00134 };
00135 private:
00136 struct Rep
00137 {
00138 int refcount;
00139 int npairs;
00140 int mpairs;
00141 int size;
00142 struct Pair *pairs;
00143
00144 Rep() : npairs(0), mpairs(-1), size(0), pairs(0) {}
00145 ~Rep() { delete [] pairs; }
00146 void resize(int n);
00147 double qset(int i, double v);
00148 Rep *copy();
00149 };
00150
00151 Wrapper<Rep> w;
00152 Rep *rep() { return w.rep(); }
00153 const Rep *rep() const { return w.rep(); }
00154
00155 public:
00156 SVector();
00157 SVector(const FVector &v);
00158 int size() const { return rep()->size; }
00159
00160
00161
00162
00163 double get(int i) const;
00164 double set(int i, double v);
00165
00166
00167
00168 int npairs() const { return rep()->npairs; }
00169 operator const Pair* () const { return rep()->pairs; }
00170
00171 void clear();
00172 void trim();
00173 SVector slice(int fi, int ti) const;
00174
00175 void add(const SVector &v2);
00176 void add(const SVector &v2, double c2);
00177 void scale(double c1);
00178 void combine(double c1, const SVector &v2, double c2);
00179
00180 friend std::ostream& operator<<(std::ostream &f, const SVector &v);
00181 friend std::istream& operator>>(std::istream &f, SVector &v);
00182 bool save(std::ostream &f) const;
00183 bool load(std::istream &f);
00184 double loadString(std::string str);
00185 void loadFromString(std::string str);
00186 void loadFromString(std::string str1, std::string str2);
00187
00193 int fromPSQL(std::string str);
00198 double getSpaceUsed();
00199 bool loadStrStream(std::istream&);
00200
00201 friend SVector combine(const SVector &v1, double a1,
00202 const SVector &v2, double a2);
00203
00204
00210 void *write_bin(size_t &total_size);
00217 int read_bin(void *data, size_t length);
00218 };
00219
00220 double dot(const FVector &v1, const FVector &v2);
00221 double dot(const FVector &v1, const SVector &v2);
00222 double dot(const SVector &v1, const FVector &v2);
00223 double dot(const SVector &v1, const SVector &v2);
00224
00225 SVector combine(const SVector &v1, double a1, const SVector &v2, double a2);
00226 FVector combine(const FVector &v1, double a1, const SVector &v2, double a2);
00227 FVector combine(const SVector &v1, double a1, const FVector &v2, double a2);
00228 FVector combine(const FVector &v1, double a1, const FVector &v2, double a2);
00229
00230 template<class T>
00231 void parse_labeled_vector(std::string ex, int &label, T &f);
00232
00233 #endif
00234
00235
00236
00237
00238
00239