metric.cc

00001 /*
00002  * Copyright (c) 2007 Regents of the SIGNET lab, University of Padova.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University of Padova (SIGNET lab) nor the 
00014  *    names of its contributors may be used to endorse or promote products 
00015  *    derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00018  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
00019  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00020  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00021  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00022  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00023  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00024  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00025  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00026  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00027  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include "metric.h"
00031 
00032 /*       _______________________________________
00033  *      /                                       \
00034  *      |               MetricList              |
00035  *      \_______________________________________/
00036  */
00037 
00038 MetricList::MetricList() : list_(0), listLength_(0), metricCount_(0)
00039 {
00040 }
00041 
00042 MetricList::~MetricList()
00043 {
00044         if(listLength_ > 0)
00045                 delete [] list_;
00046 }
00047 
00048 void MetricList::addMetric(Metric *m)
00049 {
00050         int i = find(m->getName());
00051         if(i == metricCount_ || strcmp(list_[i]->getName(), m->getName()) != 0)
00052                 addNewMetric(m);
00053         else
00054                 list_[i] = m;
00055 }
00056 
00057 void MetricList::addNewMetric(Metric *m)
00058 {
00059         if(metricCount_ < listLength_)
00060         {
00061                 int i = find(m->getName());
00062                 for(int j = metricCount_; j >= i ; j--)
00063                 {
00064                         list_[j + 1] = list_[j];
00065                 }
00066                 list_[i] = m;
00067         }
00068         else
00069         {
00070                 Metric **tmp = new Metric*[listLength_ + METRIC_ALLOC_PARAM_PER_TIME];
00071                 int offset = 0;
00072                 for(int i = 0; i < listLength_; i++)
00073                 {
00074                         if(!offset)
00075                         {
00076                                 if(strcmp(list_[i]->getName(), m->getName()) > 0)
00077                                 {
00078                                         offset = 1;
00079                                         tmp[i] = m;
00080                                 }
00081                         }
00082                         tmp[i + offset] = list_[i];
00083                 }
00084                 for(int i = listLength_; i < listLength_ + METRIC_ALLOC_PARAM_PER_TIME; i++)
00085                 {
00086                         tmp[i] = 0;
00087                 }
00088                 if(!offset)
00089                         tmp[listLength_] = m;
00090                 if(listLength_ > 0)
00091                         delete [] list_;
00092                 list_ = tmp;
00093                 listLength_ += METRIC_ALLOC_PARAM_PER_TIME;
00094         }
00095         metricCount_++;
00096 }
00097 
00098 int MetricList::find(const char *name)
00099 {
00100         if(metricCount_ == 0)
00101                 return 0;
00102         if(metricCount_ == 1)
00103         {
00104                 if(strcmp(list_[0]->getName(), name) >= 0)
00105                         return 0;
00106                 else
00107                         return 1;
00108         }
00109         int max = metricCount_ - 1;
00110         int min = 0;
00111         while(max - min > 0)
00112         {
00113                 int i = min + (max - min) / 2;
00114                 int c1 = -1;
00115                 int c2 = strcmp(list_[i]->getName(), name);
00116                 if(i > 0)
00117                         c1 = strcmp(list_[i-1]->getName(), name);
00118                 
00119                 if(i > 0 && c2 >= 0 && c1 < 0)
00120                 {
00121                         return i;
00122                 }
00123                 else if(i == 0 && c2 >= 0)
00124                 {
00125                         return 0;
00126                 }
00127                 else if(i == metricCount_ - 1 && c2 < 0)
00128                 {
00129                         return i + 1;
00130                 }
00131                 else if(c2 < 0)
00132                 {
00133                         min = i + 1;
00134                 }
00135                 else
00136                 {
00137                         max = i - 1;
00138                 }
00139 
00140         }
00141         int c1 = -1;
00142         int c2 = strcmp(list_[min]->getName(), name);
00143         if(min > 0)
00144                 c1 = strcmp(list_[min-1]->getName(), name);
00145         
00146         if(min > 0 && c2 >= 0 && c1 < 0)
00147         {
00148                 return min;
00149         }
00150         else if(min == 0 && c2 >= 0)
00151         {
00152                 return 0;
00153         }
00154         else if(min == metricCount_ - 1 && c2 < 0)
00155         {
00156                 return min + 1;
00157         }
00158 }
00159 
00160 int MetricList::length()
00161 {
00162         return metricCount_;
00163 }
00164 
00165 Metric *MetricList::getMetric(int i)
00166 {
00167         if(i < 0 || i >= metricCount_)
00168                 return 0;
00169         return list_[i];
00170 }
00171 
00172 Metric *MetricList::getMetric(const char *name)
00173 {
00174         int i = find(name);
00175         if(i == metricCount_ || strcmp(list_[i]->getName(), name) != 0)
00176                 return 0;
00177         return list_[i];
00178 }
00179 
00180 void MetricList::empty()
00181 {
00182         for(int i = 0; i < metricCount_; i++)
00183                 list_[i] = 0;
00184         metricCount_ = 0;
00185 }
00186 
00187 /*       _______________________________________
00188  *      /                                       \
00189  *      |               Metric                  |
00190  *      \_______________________________________/
00191  */
00192 
00193 Metric::Metric(const char *name) : name_(name), list_()
00194 {
00195 }
00196 
00197 Metric::~Metric()
00198 {
00199 }
00200 
00201 const char*Metric::getName()
00202 {
00203         return name_;
00204 }
00205 
00206 int Metric::command(int argc, const char *const* argv)
00207 {
00208         Tcl &tcl = Tcl::instance();
00209         if(argc == 2)
00210         {
00211                 if(strcasecmp(argv[1], "type"))
00212                 {
00213                         tcl.resultf("%s", name_);
00214                         return TCL_OK;
00215                 }
00216         }
00217 }
00218 
00219 void Metric::insertParam(MetricList *m)
00220 {
00221         for(int i = 0; i < m->length(); i++)
00222                 list_.addMetric(m->getMetric(i));
00223 }
00224 
00225 void Metric::clearParam()
00226 {
00227         list_.empty();
00228 }
00229 
00230 /*       _______________________________________
00231  *      /                                       \
00232  *      |               RouteInfo               |
00233  *      \_______________________________________/
00234  */
00235 
00236 RouteInfo::RouteInfo() : nChild_(0), father_(0), list_(), lengthChild_(0)
00237 {
00238         reachability_ = ROUTEINFO_DEFAULT_REACHABILITY;
00239 }
00240 
00241 RouteInfo::~RouteInfo()
00242 {
00243         if(lengthChild_ > 0)
00244                 delete [] child_;
00245 }
00246 
00247 void RouteInfo::addChild(RouteInfo *c)
00248 {
00249         if(lengthChild_ <= nChild_)
00250         {
00251                 RouteInfo **tmp = new RouteInfo*[lengthChild_ + ROUTEINFO_ALLOC_CHILD_PER_TIME];
00252                 for(int i = 0; i < lengthChild_; i++)
00253                         tmp[i] = child_[i];
00254                 if(lengthChild_ > 0)
00255                         delete [] child_;
00256                 child_ = tmp;
00257                 lengthChild_ += ROUTEINFO_ALLOC_CHILD_PER_TIME;
00258         }
00259         child_[nChild_++] = c;
00260 }
00261 
00262 int RouteInfo::getNChild()
00263 {
00264         return nChild_;
00265 }
00266 
00267 RouteInfo *RouteInfo::getChild(int i)
00268 {
00269         if(i < 0 || i >= nChild_)
00270                 return 0;
00271         
00272         return child_[i];
00273 }
00274 
00275 void RouteInfo::addFather(RouteInfo *f)
00276 {
00277         father_ = f;
00278 }
00279 
00280 RouteInfo *RouteInfo::getFather()
00281 {
00282         return father_;
00283 }
00284 
00285 void RouteInfo::setModuleId(int id)
00286 {
00287         moduleId_ = id;
00288 }
00289 
00290 int RouteInfo::getModuleId()
00291 {
00292         return moduleId_;
00293 }
00294 
00295 void RouteInfo::addMetric(Metric *m)
00296 {
00297         list_.addMetric(m);
00298 }
00299 
00300 Metric *RouteInfo::getMetric(const char *m)
00301 {
00302         return list_.getMetric(m);
00303 }
00304 
00305 Metric *RouteInfo::getMetric(int i)
00306 {
00307         return list_.getMetric(i);
00308 }
00309 
00310 int RouteInfo::getNMetric()
00311 {
00312         return list_.length();
00313 }
00314 
00315 Reachability RouteInfo::reachability()
00316 {
00317         return reachability_;
00318 }
00319 
00320 void RouteInfo::setReachability(Reachability r)
00321 {
00322         reachability_ = r;
00323 }
00324 

Generated on Wed Nov 26 15:47:28 2008 for NS-MIRACLE library by  doxygen 1.5.2