![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /******************************************************************************* 00002 * This file is part of openWNS (open Wireless Network Simulator) 00003 * _____________________________________________________________________________ 00004 * 00005 * Copyright (C) 2004-2007 00006 * Chair of Communication Networks (ComNets) 00007 * Kopernikusstr. 5, D-52074 Aachen, Germany 00008 * phone: ++49-241-80-27910, 00009 * fax: ++49-241-80-22242 00010 * email: info@openwns.org 00011 * www: http://www.openwns.org 00012 * _____________________________________________________________________________ 00013 * 00014 * openWNS is free software; you can redistribute it and/or modify it under the 00015 * terms of the GNU Lesser General Public License version 2 as published by the 00016 * Free Software Foundation; 00017 * 00018 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY 00019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00020 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00021 * details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public License 00024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00025 * 00026 ******************************************************************************/ 00027 00028 #ifndef WNS_CONTAINER_MATRIX_HPP 00029 #define WNS_CONTAINER_MATRIX_HPP 00030 00031 #include <WNS/container/MultiAccessible.hpp> 00032 #include <WNS/Assure.hpp> 00033 00034 #include <vector> 00035 #include <sstream> 00036 00037 namespace wns { namespace container { 00038 00042 template<typename T, MultiAccessibleDefs::SizeType N, typename R = const T&> 00043 class Matrix : 00044 public MultiReadWriteAccessible<T, N, R> 00045 { 00046 typedef MultiReadWriteAccessible<T, N, R> SuperType; 00047 public: 00048 typedef typename SuperType::ValueType ValueType; 00049 typedef typename SuperType::IndexType IndexType; 00050 typedef MultiAccessibleDefs::SizeType SizeType; 00051 00052 typedef Matrix<T, N - 1, R> HyperplaneType; 00053 typedef HyperplaneType& Reference; 00054 typedef const HyperplaneType& ConstReference; 00055 00056 Matrix() : 00057 SuperType(), 00058 v() 00059 {} 00060 00061 Matrix(const SizeType sizes[N], 00062 const ValueType defaultValue = ValueType()) : 00063 SuperType(), 00064 v(sizes[0], HyperplaneType(&sizes[1], defaultValue)) 00065 {} 00066 00067 virtual 00068 ~Matrix() 00069 {} 00070 00071 virtual Reference 00072 operator[](const IndexType& index) 00073 { 00074 assure(!isEmpty(), "Matrix is empty"); 00075 assure(index < v.size(), "out of range: index " << index << " >= " << v.size()); 00076 return v[index]; 00077 } 00078 00079 virtual ConstReference 00080 operator[](const IndexType& index) const 00081 { 00082 assure(!isEmpty(), "Matrix is empty"); 00083 assure(index < v.size(), "out of range: index " << index << " >= " << v.size()); 00084 return v[index]; 00085 } 00086 00087 virtual bool 00088 operator==(const Matrix& other) const 00089 { 00090 for (size_t xx = 0; xx < v.size(); ++xx) 00091 { 00092 if (other[xx] != v[xx]) 00093 { 00094 return false; 00095 } 00096 } 00097 return true; 00098 } 00099 00100 virtual bool 00101 operator!=(const Matrix& other) const 00102 { 00103 return !(*this == other); 00104 } 00105 00106 // this is for backward compatibility 00107 virtual SizeType 00108 getSize(const SizeType& dim) const 00109 { 00110 return dimSize(dim); 00111 } 00112 00113 virtual SizeType 00114 dimSize(const SizeType& dim) const 00115 { 00116 if (dim == 0) 00117 { 00118 return v.size(); 00119 } 00120 else 00121 { 00122 if (isEmpty()) 00123 { 00124 return 0; 00125 } 00126 else 00127 { 00128 return v[0].dimSize(dim - 1); 00129 } 00130 } 00131 } 00132 00134 virtual bool 00135 isEmpty() const 00136 { 00137 return v.empty(); 00138 } 00139 00140 private: 00141 std::vector<HyperplaneType> v; 00142 }; 00143 00147 template<typename T, 00148 typename R> 00149 class Matrix<T, 1, R> : 00150 public MultiReadWriteAccessible<T, 1, R> 00151 { 00152 typedef MultiReadWriteAccessible<T, 1, R> SuperType; 00153 public: 00154 typedef typename SuperType::ValueType ValueType; 00155 typedef typename SuperType::IndexType IndexType; 00156 typedef MultiAccessibleDefs::SizeType SizeType; 00157 00158 typedef ValueType& Reference; 00159 typedef const ValueType& ConstReference; 00160 00161 Matrix() : 00162 SuperType(), 00163 v() 00164 {} 00165 00166 Matrix(const SizeType sizes[1], 00167 const ValueType defaultValue = ValueType()) : 00168 SuperType(), 00169 v(sizes[0], defaultValue) 00170 {} 00171 00172 virtual 00173 ~Matrix() 00174 {} 00175 00176 virtual bool 00177 operator==(const Matrix& other) const 00178 { 00179 for(size_t xx = 0; xx < v.size(); ++xx) 00180 { 00181 if (other[xx] != v[xx]) 00182 { 00183 return false; 00184 } 00185 } 00186 return true; 00187 } 00188 00189 virtual bool 00190 operator!=(const Matrix& other) const 00191 { 00192 return !(*this == other); 00193 } 00194 00195 virtual Reference 00196 operator[](const IndexType& index) 00197 { 00198 assure(!isEmpty(), "Matrix is empty"); 00199 assure(index < v.size(), "out of range: index " << index << " >= " << v.size()); 00200 return v[index]; 00201 } 00202 00203 virtual typename SuperType::ReturnType 00204 operator[](const IndexType& index) const 00205 { 00206 assure(!isEmpty(), "Matrix is empty"); 00207 assure(index < v.size(), "out of range: index " << index << " >= " << v.size()); 00208 return v[index]; 00209 } 00210 00211 virtual SizeType 00212 getSize(const SizeType& dim) const 00213 { 00214 return dimSize(dim); 00215 } 00216 00217 #ifndef WNS_NDEBUG 00218 virtual SizeType 00219 dimSize(const SizeType& dim) const 00220 #else 00221 virtual SizeType 00222 dimSize(const SizeType& /*dim*/) const 00223 #endif 00224 { 00225 assure(dim == 0, "Dimension must be zero"); 00226 return v.size(); 00227 } 00228 00230 virtual bool 00231 isEmpty() const 00232 { 00233 return v.empty(); 00234 } 00235 00236 private: 00237 std::vector<ValueType> v; 00238 }; 00239 00243 typedef wns::container::Matrix<double, 2u> MatrixDouble; 00244 00248 inline 00249 std::ostream& 00250 operator <<(std::ostream& os, const wns::container::MatrixDouble& matrix) 00251 { 00252 unsigned int rows=matrix.getSize(0); 00253 unsigned int cols=matrix.getSize(1); 00254 os << "("; 00255 for(unsigned int row=0; row<rows; row++) { 00256 os << "("; 00257 for(unsigned int col=0; col<cols; col++) { 00258 os << matrix[row][col]; 00259 if (col<cols-1) os << ", "; 00260 } 00261 os << ")"; 00262 if (row<rows-1) os << "," << std::endl; 00263 } 00264 os << ")"; 00265 return os; 00266 } 00267 } 00268 } 00269 00270 00271 #endif // WNS_CONTAINER_MATRIX_HPP 00272
1.5.5