Wiselib
|
00001 /*************************************************************************** 00002 ** This file is part of the generic algorithm library Wiselib. ** 00003 ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project. ** 00004 ** ** 00005 ** The Wiselib is free software: you can redistribute it and/or modify ** 00006 ** it under the terms of the GNU Lesser General Public License as ** 00007 ** published by the Free Software Foundation, either version 3 of the ** 00008 ** License, or (at your option) any later version. ** 00009 ** ** 00010 ** The Wiselib is distributed in the hope that it will be useful, ** 00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** 00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** 00013 ** GNU Lesser General Public License for more details. ** 00014 ** ** 00015 ** You should have received a copy of the GNU Lesser General Public ** 00016 ** License along with the Wiselib. ** 00017 ** If not, see <http://www.gnu.org/licenses/>. ** 00018 ***************************************************************************/ 00019 #ifndef __INTERNAL_INTERFACE_ROUTING_TABLE_STATIC_ARRAY__ 00020 #define __INTERNAL_INTERFACE_ROUTING_TABLE_STATIC_ARRAY__ 00021 00022 #include "util/pstl/iterator.h" 00023 #include "util/pstl/vector_static.h" 00024 #include "util/pstl/pair.h" 00025 #include "util/serialization/pstl_pair.h" 00026 00027 namespace wiselib 00028 { 00029 00030 template<typename OsModel_P, 00031 typename Radio_P, 00032 unsigned int TABLE_SIZE, 00033 typename Value_P = typename Radio_P::node_id_t> 00034 class StaticArrayRoutingTable 00035 : public vector_static<OsModel_P, pair<typename Radio_P::node_id_t, Value_P>, TABLE_SIZE> 00036 { 00037 public: 00038 typedef OsModel_P OsModel; 00039 typedef Radio_P Radio; 00040 00041 typedef StaticArrayRoutingTable<OsModel, Radio, TABLE_SIZE, Value_P> map_type; 00042 typedef typename map_type::vector_type vector_type; 00043 00044 typedef typename map_type::iterator iterator; 00045 typedef typename map_type::size_type size_type; 00046 00047 typedef typename map_type::value_type value_type; 00048 typedef typename Radio::node_id_t key_type; 00049 typedef Value_P mapped_type; 00050 typedef typename map_type::pointer pointer; 00051 typedef typename map_type::reference reference; 00052 // -------------------------------------------------------------------- 00053 StaticArrayRoutingTable() 00054 : vector_type() 00055 {} 00056 // -------------------------------------------------------------------- 00057 StaticArrayRoutingTable(StaticArrayRoutingTable& rt) 00058 : vector_type( rt ) 00059 {} 00060 // -------------------------------------------------------------------- 00061 ~StaticArrayRoutingTable() 00062 {} 00063 // -------------------------------------------------------------------- 00064 StaticArrayRoutingTable& operator=(StaticArrayRoutingTable& rt) 00065 { 00066 *this = rt; 00067 } 00068 // -------------------------------------------------------------------- 00071 mapped_type& operator[]( const key_type& k ) 00072 { 00073 iterator it = find(k); 00074 if ( it != this->end() ) 00075 return it->second; 00076 00077 value_type val; 00078 val.first = k; 00079 this->push_back( val ); 00080 00081 it = find(k); 00082 if ( it != this->end() ) 00083 return it->second; 00084 00085 // return dummy value that can be written to; this dummy value is 00086 // *only* returned if the static vector is full and can not hold 00087 // new components 00088 // TODO: Print Error message since this case should not happen! 00089 return dummy_; 00090 } 00092 // -------------------------------------------------------------------- 00095 pair<iterator, bool> insert( const value_type& x ) 00096 { 00097 pair<iterator, bool> ret; 00098 ret.first = find(x.first); 00099 00100 if ( ret.first != this->end() ) 00101 { 00102 ret.second = false; 00103 return ret; 00104 } 00105 00106 ret.first = vector_type::insert( this->end(), x ); 00107 return ret; 00108 } 00109 // -------------------------------------------------------------------- 00110 //TODO template <class InputIterator> 00111 // void insert ( InputIterator first, InputIterator last ); 00113 // -------------------------------------------------------------------- 00116 iterator find( const key_type& k ) 00117 { 00118 for ( iterator it = this->begin(); it != this->end(); ++it ) 00119 { 00120 if ( it->first == k ) 00121 return it; 00122 } 00123 return this->end(); 00124 } 00126 00127 private: 00128 mapped_type dummy_; 00129 }; 00130 00131 } 00132 00133 #endif