#
source:
foam/trunk/vision/src/Vector.h
@
85

Revision 85, 3.9 KB checked in by dave, 11 years ago (diff) |
---|

Line | |
---|---|

1 | // Copyright (C) 2009 foam |

2 | // |

3 | // This program is free software; you can redistribute it and/or modify |

4 | // it under the terms of the GNU General Public License as published by |

5 | // the Free Software Foundation; either version 2 of the License, or |

6 | // (at your option) any later version. |

7 | // |

8 | // This program is distributed in the hope that it will be useful, |

9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |

10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |

11 | // GNU General Public License for more details. |

12 | // |

13 | // You should have received a copy of the GNU General Public License |

14 | // along with this program; if not, write to the Free Software |

15 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |

16 | |

17 | #include <assert.h> |

18 | #include <iostream> |

19 | |

20 | #ifndef FOAM_VECTOR |

21 | #define FOAM_VECTOR |

22 | |

23 | template<class T> |

24 | class Vector |

25 | { |

26 | public: |

27 | Vector(unsigned int s); |

28 | ~Vector(); |

29 | Vector(const Vector &other); |

30 | |

31 | T &operator[](unsigned int i) |

32 | { |

33 | assert(i<m_Size); |

34 | return m_Data[i]; |

35 | } |

36 | |

37 | unsigned int Size() const { return m_Size; } |

38 | T *GetRawData() { return m_Data; } |

39 | const T *GetRawDataConst() const { return m_Data; } |

40 | |

41 | void Print() const; |

42 | void SetAll(T s); |

43 | |

44 | Vector &operator=(const Vector &other); |

45 | Vector operator+(const Vector &other) const; |

46 | Vector operator-(const Vector &other) const; |

47 | Vector operator*(T v) const; |

48 | Vector operator/(T v) const; |

49 | Vector &operator+=(const Vector &other); |

50 | Vector &operator-=(const Vector &other); |

51 | Vector &operator*=(T v); |

52 | Vector &operator/=(T v); |

53 | |

54 | static void RunTests(); |

55 | |

56 | private: |

57 | |

58 | unsigned int m_Size; |

59 | T *m_Data; |

60 | }; |

61 | |

62 | template<class T> |

63 | Vector<T>::Vector(unsigned int s) : |

64 | m_Size(s) |

65 | { |

66 | m_Data=new T[s]; |

67 | } |

68 | |

69 | template<class T> |

70 | Vector<T>::~Vector() |

71 | { |

72 | delete[] m_Data; |

73 | } |

74 | |

75 | template<class T> |

76 | Vector<T>::Vector(const Vector &other) |

77 | { |

78 | m_Size = other.m_Size; |

79 | m_Data=new T[m_Size]; |

80 | memcpy(m_Data,other.m_Data,m_Size*sizeof(T)); |

81 | } |

82 | |

83 | template<class T> |

84 | Vector<T> &Vector<T>::operator=(const Vector &other) |

85 | { |

86 | if (m_Data!=NULL) |

87 | { |

88 | delete[] m_Data; |

89 | } |

90 | |

91 | m_Size = other.m_Size; |

92 | m_Data=new T[m_Size]; |

93 | memcpy(m_Data,other.m_Data,m_Size*sizeof(T)); |

94 | |

95 | return *this; |

96 | } |

97 | |

98 | template<class T> |

99 | void Vector<T>::Print() const |

100 | { |

101 | for (unsigned int i=0; i<m_Size; i++) |

102 | { |

103 | std::cerr<<(*this)[i]<<" "; |

104 | } |

105 | std::cerr<<std::endl; |

106 | } |

107 | |

108 | template<class T> |

109 | void Vector<T>::SetAll(T s) |

110 | { |

111 | for (unsigned int i=0; i<m_Size; i++) |

112 | { |

113 | (*this)[i]=s; |

114 | } |

115 | } |

116 | |

117 | template<class T> |

118 | Vector<T> Vector<T>::operator+(const Vector &other) const |

119 | { |

120 | assert(m_Size==other.m_Size); |

121 | |

122 | Vector<T> ret(m_Size); |

123 | for (unsigned int i=0; i<m_Size; i++) |

124 | { |

125 | ret[i]=(*this)[i]+other[i]; |

126 | } |

127 | return ret; |

128 | } |

129 | |

130 | template<class T> |

131 | Vector<T> Vector<T>::operator-(const Vector &other) const |

132 | { |

133 | assert(m_Size==other.m_Size); |

134 | |

135 | Vector<T> ret(m_Size); |

136 | for (unsigned int i=0; i<m_Size; i++) |

137 | { |

138 | ret[i]=(*this)[i]-other[i]; |

139 | } |

140 | return ret; |

141 | } |

142 | |

143 | template<class T> |

144 | Vector<T> Vector<T>::operator*(T v) const |

145 | { |

146 | Vector<T> ret(m_Size); |

147 | for (unsigned int i=0; i<m_Size; i++) |

148 | { |

149 | ret[i]=(*this)[i]*v; |

150 | } |

151 | return ret; |

152 | } |

153 | |

154 | template<class T> |

155 | Vector<T> Vector<T>::operator/(T v) const |

156 | { |

157 | Vector<T> ret(m_Size); |

158 | for (unsigned int i=0; i<m_Size; i++) |

159 | { |

160 | ret[i]=(*this)[i]/v; |

161 | } |

162 | return ret; |

163 | } |

164 | |

165 | template<class T> |

166 | Vector<T> &Vector<T>::operator+=(const Vector &other) |

167 | { |

168 | for (unsigned int i=0; i<m_Size; i++) |

169 | { |

170 | (*this)[i]+=other[i]; |

171 | } |

172 | return *this; |

173 | } |

174 | |

175 | template<class T> |

176 | Vector<T> &Vector<T>::operator-=(const Vector &other) |

177 | { |

178 | for (unsigned int i=0; i<m_Size; i++) |

179 | { |

180 | (*this)[i]-=other[i]; |

181 | } |

182 | return *this; |

183 | } |

184 | |

185 | template<class T> |

186 | Vector<T> &Vector<T>::operator*=(T v) |

187 | { |

188 | for (unsigned int i=0; i<m_Size; i++) |

189 | { |

190 | (*this)[i]*=v; |

191 | } |

192 | return *this; |

193 | } |

194 | |

195 | template<class T> |

196 | Vector<T> &Vector<T>::operator/=(T v) |

197 | { |

198 | for (unsigned int i=0; i<m_Size; i++) |

199 | { |

200 | (*this)[i]/=v; |

201 | } |

202 | return *this; |

203 | } |

204 | |

205 | |

206 | template<class T> |

207 | void Vector<T>::RunTests() |

208 | { |

209 | Vector<T> m(10); |

210 | m.SetAll(0); |

211 | assert(m[0]==0); |

212 | m[5]=0.5; |

213 | assert(m[5]==0.5); |

214 | Vector<T> om(m); |

215 | assert(om[5]==0.5); |

216 | } |

217 | |

218 | #endif |

**Note:**See TracBrowser for help on using the repository browser.