User Manual, Developers Guide and API Documentation

DynamicMatrix.hpp

Go to the documentation of this file.
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. 16, 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_DYNAMICMATRIX
00029 #define WNS_CONTAINER_DYNAMICMATRIX
00030 
00031 #include <WNS/Exception.hpp>
00032 
00033 #include <vector>
00034 #include <list>
00035 
00036 namespace wns { namespace container {
00037 
00050     template <typename T>
00051     class DynamicMatrix
00052     {
00053         typedef T ValueType;
00054 
00057         struct Cell
00058         {
00060             Cell() : value_() {}
00062             ValueType value_;
00063         };
00064 
00066         std::vector<DynamicMatrix*> subMatrices_;
00067 
00069         int dim_;
00071         Cell* cell_;
00072 
00073     public:
00079         DynamicMatrix(std::list<int> dimensionSizes) :
00080             subMatrices_(),
00081             dim_(dimensionSizes.size()),
00082             cell_(NULL)
00083             {
00084                 if (dim_ != 0)
00085                 {
00086                     int thisSize = dimensionSizes.front();
00087                     dimensionSizes.pop_front();
00088 
00089                     for (int ii = 0; ii < thisSize; ++ii)
00090                     {
00091                         subMatrices_.push_back( new DynamicMatrix(dimensionSizes) );
00092                     }
00093                 }
00094                 else if (dim_ == 0)
00095                 {
00096                     cell_ = new Cell;
00097                 }
00098             }
00099 
00101         ~DynamicMatrix()
00102             {
00103                 if (dim_ == 0)
00104                 {
00105                     delete cell_;
00106                 }
00107                 else
00108                 {
00109                     for (size_t ii = 0; ii < subMatrices_.size(); ++ii)
00110                     {
00111                         delete subMatrices_.at(ii);
00112                     }
00113                     subMatrices_.clear();
00114                 }
00115             }
00116 
00119         const ValueType&
00120         getValue(std::list<int> indices) const
00121             {
00122                 if (this->dim_ == 0)
00123                 {
00124                     if (! indices.empty())
00125                         throw(wns::Exception("Inconsistent Matrix!"));
00126                     if (! subMatrices_.empty())
00127                         throw(wns::Exception("Inconsistent Matrix!"));
00128 
00129                     return this->getValue();
00130                 }
00131 
00132                 int nextindex = indices.front();
00133                 indices.pop_front();
00134                 return subMatrices_.at(nextindex)->getValue(indices);
00135             }
00136 
00137 
00140         ValueType&
00141         getValue(std::list<int> indices)
00142             {
00143                 if (this->dim_ == 0)
00144                 {
00145                     if (! indices.empty())
00146                         throw(wns::Exception("Inconsistent Matrix!"));
00147                     if (! subMatrices_.empty())
00148                         throw(wns::Exception("Inconsistent Matrix!"));
00149 
00150                     return this->getValue();
00151                 }
00152 
00153                 int nextindex = indices.front();
00154                 indices.pop_front();
00155                 return subMatrices_.at(nextindex)->getValue(indices);
00156             }
00157 
00160         void
00161         setValue(std::list<int> indices, ValueType value)
00162             {
00163                 if (this->dim_ == 0)
00164                 {
00165                     if (! indices.empty())
00166                         throw(wns::Exception("Inconsistent Matrix!"));
00167                     if (! subMatrices_.empty())
00168                         throw(wns::Exception("Inconsistent Matrix!"));
00169 
00170                     this->setValue(value);
00171                     return;
00172                 }
00173 
00174                 int nextindex = indices.front();
00175                 indices.pop_front();
00176                 subMatrices_.at(nextindex)->setValue(indices, value);
00177             }
00178 
00179     private:
00181         ValueType&
00182         getValue()
00183             {
00184                 if (dim_ == 0)
00185                     return cell_->value_;
00186 
00187                 throw(wns::Exception("Inconsistent Matrix!"));
00188             }
00189 
00191         const ValueType&
00192         getValue() const
00193             {
00194                 if (dim_ == 0)
00195                     return cell_->value_;
00196 
00197                 throw(wns::Exception("Inconsistent Matrix!"));
00198             }
00199 
00201         void
00202         setValue(ValueType v)
00203             {
00204                 if (dim_ == 0)
00205                 {
00206                     cell_->value_ = v;
00207                     return;
00208                 }
00209 
00210                 throw(wns::Exception("Inconsistent Matrix!"));
00211             }
00212 
00213     };
00214 } // container
00215 } // wns
00216 
00217 #endif // not defined WNS_CONTAINER_DYNAMICMATRIX

Generated on Wed May 23 03:31:32 2012 for openWNS by  doxygen 1.5.5