2 //////////////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation. //
7 // This program is distributed in the hope that it will be useful, but //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
10 // General Public License for more details. //
12 // You should have received a copy of the GNU General Public License //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
15 // Written by Francois Fleuret //
16 // (C) Ecole Polytechnique Federale de Lausanne //
17 // Contact <pom@epfl.ch> for comments & bug reports //
18 //////////////////////////////////////////////////////////////////////////////////
37 inline void resize(int s) {
40 content = new T[size];
43 inline int length() const { return size; }
45 inline Vector(std::istream &is) {
46 is.read((char *) &size, sizeof(size));
47 content = new T[size];
48 is.read((char *) content, sizeof(T)*size);
51 inline Vector() : size(0), content(0) { }
52 inline Vector(int s) : size(s), content(new T[size]) {}
53 inline Vector(const Vector &v) : size(v.size), content(new T[size]) {
54 if(size > 0) memcpy(content, v.content, size * sizeof(T));
63 inline void load(std::istream &is) {
64 is.read((char *) &size, sizeof(size));
66 is.read((char *) content, sizeof(T) * size);
69 inline void save(std::ostream &os) const {
70 os.write((char *) &size, sizeof(size));
71 os.write((char *) content, sizeof(T) * size);
74 // inline void fill(const T &t) {
76 // for(int i = 0; i < size; i++) *(s++) = t;
79 inline Vector &operator = (const Vector &v) {
84 content = new T[size];
86 if(size > 0) memcpy(content, v.content, size * sizeof(T));
91 inline bool operator == (const Vector &v) {
93 if(v.size != size) return false;
94 return memcmp(content, v.content, size * sizeof(T)) == 0;
98 inline bool operator != (const Vector &v) {
100 if(v.size != size) return true;
101 return memcmp(content, v.content, size * sizeof(T)) != 0;
105 inline Vector & clear() {
106 if(size > 0) memset(content, 0, size * sizeof(T));
110 inline T &operator [] (int k) {
111 ASSERT(k >= 0 && k < size, "Index out of bound in Vector::operator []");
115 inline T operator [] (int k) const {
116 ASSERT(k >= 0 && k < size, "Index out of bound in Vector::operator [] const");
120 inline T norme() const {
122 for(int i = 0; i<size; i++) s += content[i] * content[i];
126 inline void print(std::ostream &os) const {
127 for(int i = 0; i < size; i++) os << content[i] << ((i < size - 1) ? " " : "\n");
130 inline void print_for_gnuplot(std::ostream &os) const {
131 for(int i = 0; i < size; i++) os << content[i] << "\n";
137 std::ostream &operator << (std::ostream &os, const Vector<T> &v) { v.print(os); return os; }