Wiselib
|
00001 /* 00002 * File: aggregationmsg.h 00003 * Author: Koninis 00004 * 00005 * Created on January 22, 2011, 1:16 PM 00006 */ 00007 00008 #ifndef GREEDY_PARTITION_H 00009 #define GREEDY_PARTITION_H 00010 00011 #include "util/pstl/vector_static.h" 00012 00013 #define MAX_ITEMS 65 00014 00015 namespace wiselib { 00016 00022 template 00023 <typename OsModel_P, typename AggregateValue_P> 00024 class greedy_partition { 00025 public: 00026 typedef OsModel_P OsModel; 00027 typedef typename OsModel::block_data_t block_data_t; 00028 typedef typename OsModel::Radio::node_id_t node_id_t; 00029 typedef typename OsModel::Radio::size_t size_t; 00030 00031 typedef AggregateValue_P value_t; 00032 typedef greedy_partition<OsModel,AggregateValue_P> self_t; 00033 00034 struct item { 00035 value_t value; 00036 uint32_t timeStampMillis; 00037 }; 00038 00039 typedef wiselib::vector_static<OsModel, struct item, MAX_ITEMS> items_vector_t; 00040 typedef typename items_vector_t::iterator items_iterator_t; 00041 00042 00043 greedy_partition() { 00044 Avalue = 0; 00045 timeMillis = 0; 00046 source = 0; 00047 k = 10; 00048 n = 60; 00049 // value = 0; 00050 } 00051 00052 greedy_partition(uint32_t K,uint32_t N) : 00053 Avalue(0), 00054 timeMillis(0), 00055 source(0), 00056 k(K), 00057 n(N) 00058 {} 00059 00060 void clear() { 00061 items.clear(); 00062 } 00063 00064 greedy_partition(block_data_t * buffer) { 00065 Avalue = read<OsModel, block_data_t, value_t>(buffer); 00066 } 00067 00068 void writeTo(uint8_t *buffer) { 00069 write<OsModel, block_data_t, value_t> (buffer, Avalue); 00070 } 00071 00072 void set_value (value_t v) { 00073 Avalue = v; 00074 } 00075 00076 value_t get() { 00077 value_t result = 0; 00078 for ( items_iterator_t 00079 it = items.begin(); 00080 it != items.end(); 00081 it++) { 00082 if ( result < it->value ) { 00083 result = it->value; 00084 } 00085 } 00086 00087 return result; 00088 } 00089 00090 void put(value_t value, uint32_t timeStamp) { 00091 uint32_t index = ((timeStamp*1000)/n)%k; 00092 00093 if (items.size() < k) { 00094 struct item new_item; 00095 new_item.value = value; 00096 new_item.timeStampMillis = timeStamp; 00097 // printf("[%d x %d] ",value,timeStamp); 00098 items.push_back(new_item); 00099 return; 00100 } 00101 00102 if ( timeStamp == 163 || timeStamp == 193) { 00103 // printf("[%d x %d] \n",value,timeStamp); 00104 // printf("[%d x %d y %d] \n",index,items.at(index).timeStampMillis,items.at(index).value); 00105 00106 } 00107 if ( (timeStamp - items.at(index).timeStampMillis) >= n 00108 || value >= items.at(index).value) { 00109 items.at(index).timeStampMillis = timeStamp; 00110 items.at(index).value = value; 00111 } 00112 } 00113 00114 // aggregate_base& operator+(aggregate_base &rhs) { 00115 // aggregate_base result; 00116 00117 // result.set_value((this->get() > rhs.get()) ? this->get() : rhs.get()); 00118 00119 // result.set_value(value + rhs.get()); 00120 // return self_t(); 00121 // } 00122 00123 self_t combine(self_t &rhs) { 00124 self_t result; 00125 result.set_value((this->get() < rhs.get()) ? this->get() : rhs.get()); 00126 00127 // result.set_value(this->get() + rhs.get()); 00128 return result; 00129 } 00130 00131 size_t size() { 00132 return sizeof(value_t); 00133 } 00134 00135 void set(uint32_t kk, uint32_t nn) { 00136 k = kk; 00137 n = nn; 00138 } 00139 00140 private: 00141 value_t Avalue; 00142 node_id_t source; 00143 uint32_t timeMillis; 00144 00145 items_vector_t items; 00146 //algorithms parameters 00147 uint32_t k; 00148 uint32_t n; 00149 00150 }; 00151 00152 } 00153 00154 #endif