Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

iflist.cc

Go to the documentation of this file.
00001 // Copyright (c) 2000 by the University of Southern California
00002 // All rights reserved.
00003 //
00004 // Permission to use, copy, modify, and distribute this software and its
00005 // documentation in source and binary forms for non-commercial purposes
00006 // and without fee is hereby granted, provided that the above copyright
00007 // notice appear in all copies and that both the copyright notice and
00008 // this permission notice appear in supporting documentation. and that
00009 // any documentation, advertising materials, and other materials related
00010 // to such distribution and use acknowledge that the software was
00011 // developed by the University of Southern California, Information
00012 // Sciences Institute.  The name of the University may not be used to
00013 // endorse or promote products derived from this software without
00014 // specific prior written permission.
00015 //
00016 // THE UNIVERSITY OF SOUTHERN CALIFORNIA makes no representations about
00017 // the suitability of this software for any purpose.  THIS SOFTWARE IS
00018 // PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
00019 // INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
00020 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 //
00022 // Other copyrights might apply to parts of this software and are so
00023 // noted when applicable.
00024 //
00025 
00026 /***********************************************************/
00027 /* iflist.cc : Chalermek Intanagonwiwat (USC/ISI) 06/25/99 */
00028 /***********************************************************/
00029 
00030 
00031 #include <stdio.h>
00032 #include "config.h"
00033 #include "random.h"
00034 #include "iflist.h"
00035 
00036 
00037 void Agent_List::InsertFront(Agent_List **start, Agent_List *cur)
00038 {
00039   if (cur == NULL)
00040      return;
00041 
00042   cur->next = *start;
00043   *start = cur;
00044 }
00045 
00046 
00047 void Agent_List::Remove(Agent_List **prev, Agent_List *cur)
00048 {
00049   if (cur == NULL)
00050     return;
00051 
00052   *prev = cur->next;  
00053 }
00054 
00055 
00056 void Agent_List::FreeAll(Agent_List **prev)
00057 {
00058   Agent_List *temp;
00059   Agent_List *next;
00060 
00061   for (temp = *prev; temp != NULL; temp=next) {
00062     next = temp->next;
00063     delete temp;
00064   }
00065 
00066   *prev = NULL;
00067 }
00068 
00069 
00070 PrvCurPtr Agent_List::Find(Agent_List **start, ns_addr_t addr)
00071 {
00072   PrvCurPtr RetVal;
00073   Agent_List **prv, *cur;
00074 
00075   for (prv= start, cur=*start; cur != NULL; prv= &(cur->next), cur=cur->next) {
00076     if ( NODE_ADDR(cur) == addr.addr_ && PORT(cur)==addr.port_) 
00077       break;
00078   }
00079  
00080   RetVal.prv = prv;
00081   RetVal.cur = cur;
00082 
00083   return RetVal;
00084 }
00085 
00086 
00087 // Copy list1's elements not already in *result, and insert in *result 
00088 
00089 void Agent_List::Union(Agent_List **result, Agent_List *list1)
00090 {
00091   PrvCurPtr RetVal;
00092   Agent_List *dupAgent;
00093   
00094   for (Agent_List *cur=list1; cur != NULL; cur=AGENT_NEXT(cur)) {
00095     RetVal = Find(result, AGT_ADDR(cur));
00096     if (RetVal.cur == NULL) {
00097       dupAgent = new Agent_List;
00098       *dupAgent = *cur;
00099       InsertFront(result, dupAgent);
00100     }
00101   }
00102 }
00103 
00104 void Agent_List::print()
00105 {
00106   Agent_List *cur;
00107 
00108   for (cur=this; cur!=NULL; cur=cur->next) {
00109     printf("(%d,%d)\n", cur->agent_addr.addr_, cur->agent_addr.port_);
00110   }
00111 }
00112 
00113 
00114 In_List *In_List::FindMaxIn()
00115 {
00116   In_List *cur_in, *max_in;
00117   int     max_diff;
00118 
00119   max_in = NULL;
00120   max_diff = 0;
00121 
00122   for (cur_in = this; cur_in != NULL;
00123        cur_in = IN_NEXT(cur_in) ) {
00124     if ( TOTAL_RECV(cur_in) - PREV_RECV(cur_in) > max_diff) {
00125       max_diff = TOTAL_RECV(cur_in) - PREV_RECV(cur_in);
00126       max_in = cur_in;
00127     }
00128   }
00129 
00130   return max_in;
00131 }
00132 
00133 
00134 Out_List *Out_List::WhereToGo()
00135 {
00136   Out_List *cur_out;
00137   double slot = Random::uniform();
00138 
00139   for (cur_out = this; cur_out!=NULL; cur_out = OUT_NEXT(cur_out) ) {
00140     if (slot >= FROM_SLOT(cur_out) && slot < TO_SLOT(cur_out) )
00141       return cur_out;
00142   }
00143 
00144   if (cur_out == NULL && this != NULL)
00145     printf("Something must be wrong! \n");
00146 
00147   return cur_out;
00148 }
00149 
00150 
00151 void Out_List::CalRange()
00152 {
00153   Out_List *cur_out=this;
00154   double cur_slot=0.0;
00155 
00156   for (cur_out = this; cur_out != NULL; cur_out = OUT_NEXT(cur_out) ) {
00157 
00158     if ( GRADIENT(cur_out) <= 0.0 ) {
00159       FROM_SLOT(cur_out) = -1.0;
00160       TO_SLOT(cur_out) = -1.0;
00161       continue;
00162     }
00163 
00164     FROM_SLOT(cur_out) = cur_slot;
00165     cur_slot = cur_slot + GRADIENT(cur_out);
00166     TO_SLOT(cur_out) = cur_slot;
00167   }   
00168 
00169   return;
00170 }
00171 
00172 
00173 void Out_List::NormalizeGradient()
00174 {
00175   Out_List *cur;
00176   float sum=0.0;
00177   int   num=0; 
00178 
00179   for (cur=this; cur!=NULL; cur=OUT_NEXT(cur)) {
00180     sum = sum + GRADIENT(cur);
00181     num++;
00182   }
00183 
00184   if (num == 0) return;
00185 
00186   if (sum == 0.0) {
00187     float share= 1.0/num;
00188 
00189     for (cur=this; cur != NULL; cur = OUT_NEXT(cur)) {
00190       GRADIENT(cur) = share;
00191     }
00192     
00193     return;
00194   }
00195 
00196   for (cur=this; cur != NULL; cur = OUT_NEXT(cur)) {
00197     GRADIENT(cur) = GRADIENT(cur)/sum;
00198   }
00199 }
00200 
00201 
00202 
00203 
00204 
00205 
00206 

Generated on Tue Apr 20 12:14:20 2004 for NS2.26SourcesOriginal by doxygen 1.3.3