Wiselib
wiselib.testing/algorithms/coloring/imjudged/color_table.h
Go to the documentation of this file.
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_COLORING_COLORS_TABLE__
00020 #define __INTERNAL_INTERFACE_COLORING_COLORS_TABLE__
00021 
00022 #include "util/pstl/pair.h"
00023 
00024 #ifndef __DEBUG_SORT_COLOR__
00025 #define __DEBUG_SORT_COLOR__
00026 #endif
00027 
00028 
00029 namespace wiselib {
00030 
00031     template<typename OsModel_P,
00032              typename Value_P,
00033              uint16_t VECTOR_SIZE>
00034     class ColorsTable
00035     {
00036     public:
00037         typedef OsModel_P OsModel;
00038         typedef typename OsModel_P::Debug Debug;
00039         typedef typename OsModel_P::Radio Radio;
00040 
00041         typedef typename Radio::node_id_t node_id_t;
00042         typedef typename Radio::size_t size_t;
00043         typedef typename Radio::block_data_t block_data_t;
00044 
00045         typedef Value_P color_value_type;
00046         typedef color_value_type* color_pointer;
00047         typedef color_value_type& color_reference;
00048 
00049         typedef pair<color_value_type, uint16_t> value_type;
00050         typedef value_type* pointer;
00051         typedef value_type& reference;
00052         
00053         typedef typename OsModel_P::size_t size_type;
00054         
00055 
00056         ColorsTable()
00057         {
00058             start_ = &vec_[0];
00059             finish_ = start_;
00060             end_of_storage_ = start_ + VECTOR_SIZE;
00061         }
00062 
00063         //TODO: change the value type to data_block_t
00064         ColorsTable( const uint8_t *data, size_type len)
00065         {
00066              memcpy( vec_, data, len );
00067              start_ = &vec_[0];
00068              finish_ = start_ + (len/ sizeof( value_type ));
00069              end_of_storage_ = start_ + VECTOR_SIZE;
00070         }
00071 
00072         void parse_array( const uint8_t *data, size_type len)
00073         {
00074              memcpy( vec_, data, len );
00075              start_ = &vec_[0];
00076              finish_ = start_ + (len/ sizeof( value_type ));
00077              end_of_storage_ = start_ + VECTOR_SIZE;
00078         }
00079 
00080         ~ColorsTable()
00081         {
00082             
00083         }
00084 
00085         void
00086         remove(const color_value_type& color)
00087         {
00088             pointer tmp;
00089             tmp = start_;
00090 
00091             for ( ; tmp != finish_; tmp++ )
00092             {
00093                 if ( tmp->first == color )
00094                 {
00095                     (tmp->second)--;
00096                     break;
00097                 }
00098             }
00099 
00100             if ( tmp == finish_ )
00101             {
00102                 return;
00103             }
00104 
00105             for ( ; tmp!=finish_ ; tmp++ )
00106             {
00107                 if ( tmp->second < (tmp+1)->second )
00108                 {
00109                     swap(tmp, tmp+1);
00110                 }
00111                 else if ( tmp->second == 0 )
00112                 {
00113                     finish_--;
00114                     break;
00115                 }
00116             }
00117 
00118         }
00119 
00120         void
00121         insert(const color_value_type& color)
00122         {
00123 
00124             pointer tmp;
00125             tmp = start_;
00126 
00127             for ( ; tmp != finish_; tmp++ )
00128             {
00129                 if ( tmp->first == color )
00130                 {
00131                     (tmp->second)++;
00132                     break;
00133                 }
00134             }
00135 
00136             if ( tmp == finish_ )
00137             {
00138                 tmp->first = color;
00139                 tmp->second = 1;
00140                 finish_++;
00141             }
00142 
00143             for ( ; tmp!=start_ ; tmp-- )
00144             {
00145                 if ( tmp->second > (tmp-1)->second )
00146                 {
00147                     swap(tmp, tmp-1);
00148                 }
00149                 else
00150                 {
00151                     break;
00152                 }
00153             }
00154 
00155        }
00156 
00157       size_type bytes()
00158       { return size_type(finish_ - start_)*sizeof(value_type); }
00159 
00162       size_type size()
00163       { return size_type(finish_ - start_); }
00164       // --------------------------------------------------------------------
00165       size_type max_size()
00166       { return VECTOR_SIZE; }
00167       // --------------------------------------------------------------------
00168       size_type capacity()
00169       { return VECTOR_SIZE; }
00170       // --------------------------------------------------------------------
00171       bool empty()
00172       { return size() == 0; }
00173 
00174       void clear()
00175       { finish_ = start_; }
00176 
00178       // --------------------------------------------------------------------
00181       color_value_type operator[](size_type n)
00182       {
00183          return (*(this->start_ + n)).first;
00184       }
00185       // --------------------------------------------------------------------
00186       
00187       ColorsTable& operator=( ColorsTable& ct )
00188       {
00189           memcpy( vec_, ct.vec_, sizeof(vec_) );
00190           start_ = &vec_[0];
00191           finish_ = start_ + (ct.finish_ - ct.start_);
00192           end_of_storage_ = start_ + VECTOR_SIZE;
00193 
00194           return *this;
00195       }
00196       // --------------------------------------------------------------------
00197 
00198       uint16_t cardinality(color_value_type& color)
00199       {
00200           pointer tmp;
00201           tmp = start_;
00202 
00203           for ( ; tmp != finish_; tmp++ )
00204           {
00205               if ( tmp->first == color )
00206               {
00207                   return tmp->second;
00208               }
00209           }
00210 
00211           return -1;
00212       }
00213       // --------------------------------------------------------------------
00214       uint16_t cardinalityAt(size_type n)
00215       {
00216           return (*(this->start_ + n)).second;
00217       }
00218       // --------------------------------------------------------------------
00219       color_reference at(size_type n)
00220       {
00221          return (*this)[n];
00222       }
00223       // --------------------------------------------------------------------
00224       uint8_t*
00225       data()
00226       { return (uint8_t*)start_; }
00227       
00228       inline void swap(pointer color, pointer color1)
00229       {
00230           value_type tmp_data;
00231           pointer tmp = &tmp_data;
00232           
00233           tmp->first = color->first;
00234           tmp->second = color->second;
00235 
00236           color->first = color1->first;
00237           color->second = color1->second;
00238 
00239           color1->first = tmp->first;
00240           color1->second = tmp->second;
00241       }
00242 
00243         value_type vec_[VECTOR_SIZE];
00244 
00245     private:
00246         pointer start_, finish_, end_of_storage_;
00247     };
00248 
00249    
00250 }
00251 
00252 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines