Added the images for test.
[pom.git] / vector.h
1
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.                                //
6 //                                                                              //
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.                                     //
11 //                                                                              //
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/>.         //
14 //                                                                              //
15 // Written by Francois Fleuret                                                  //
16 // (C) Ecole Polytechnique Federale de Lausanne                                 //
17 // Contact <pom@epfl.ch> for comments & bug reports                             //
18 //////////////////////////////////////////////////////////////////////////////////
19
20 #ifndef VECTOR_H
21 #define VECTOR_H
22
23 #include <iostream>
24 #include <string.h>
25 #include <stdlib.h>
26
27 using namespace std;
28
29 #include "misc.h"
30
31 template<class T>
32 class Vector {
33 protected:
34   int size;
35   T *content;
36 public:
37   inline void resize(int s) {
38     delete[] content;
39     size = s;
40     content = new T[size];
41   }
42
43   inline int length() const { return size; }
44
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);
49   }
50
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));
55   }
56   inline ~Vector() {
57     delete[] content;
58 #ifdef DEBUG
59     content = 0;
60 #endif
61   }
62
63   inline void load(std::istream &is) {
64     is.read((char *) &size, sizeof(size));
65     resize(size);
66     is.read((char *) content, sizeof(T) * size);
67   }
68
69   inline void save(std::ostream &os) const {
70     os.write((char *) &size, sizeof(size));
71     os.write((char *) content, sizeof(T) * size);
72   }
73
74 //   inline void fill(const T &t) {
75 //     T *s = content;
76 //     for(int i = 0; i < size; i++) *(s++) = t;
77 //   }
78
79   inline Vector &operator = (const Vector &v) {
80     if(this != &v) {
81       if(v.size != size) {
82         delete[] content;
83         size = v.size;
84         content = new T[size];
85       }
86       if(size > 0) memcpy(content, v.content, size * sizeof(T));
87     }
88     return *this;
89   }
90
91   inline bool operator == (const Vector &v) {
92     if(this != &v) {
93       if(v.size != size) return false;
94       return memcmp(content, v.content, size * sizeof(T)) == 0;
95     } else return true;
96   }
97
98   inline bool operator != (const Vector &v) {
99     if(this != &v) {
100       if(v.size != size) return true;
101       return memcmp(content, v.content, size * sizeof(T)) != 0;
102     } else return false;
103   }
104
105   inline Vector & clear() {
106     if(size > 0) memset(content, 0, size * sizeof(T));
107     return *this;
108   }
109
110   inline T &operator [] (int k) {
111     ASSERT(k >= 0 && k < size, "Index out of bound in Vector::operator []");
112     return content[k];
113   }
114
115   inline T operator [] (int k) const {
116     ASSERT(k >= 0 && k < size, "Index out of bound in Vector::operator [] const");
117     return content[k];
118   }
119
120   inline void print(std::ostream &os) const {
121     for(int i = 0; i < size; i++) os << content[i] << ((i < size - 1) ? " " : "\n");
122   }
123
124   inline void print_for_gnuplot(std::ostream &os) const {
125     for(int i = 0; i < size; i++) os << content[i] << "\n";
126   }
127
128 };
129
130 template<class T>
131 std::ostream &operator << (std::ostream &os, const Vector<T> &v) { v.print(os); return os; }
132
133 #endif