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

utils.h File Reference

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "config.h"

Include dependency graph for utils.h:

Include dependency graph

This graph shows which files directly or indirectly include this file:

Included by dependency graph

Go to the source code of this file.

Compounds

struct  AVL_tree_st
struct  hash_table_st
struct  ll_st
struct  ts_strtok_st

Defines

#define FALSE   0
#define TRUE   1

Typedefs

typedef timeval tv_time
typedef timespec ts_time
typedef ts_strtok_st ts_strtok_state
typedef ll_st ll_node
typedef ll_stll
typedef ll hash_el
typedef hash_table_st hash_table
typedef int(* hash_function )(void *element, int num_slots)
typedef int(* sc_comparator )(void *element1, void *element2)
typedef void(* deletor )(void *element)
typedef AVL_tree_stAVL_tree
typedef AVL_tree_st AVL_tree_element
typedef int(* AVL_comparator )(void *node, void *comparison)
typedef void(* AVL_deletor )(void *deleteMe)

Functions

void dump_buf (FILE *std, char *buf, int retlen)
tv_time tv_timeadd (tv_time t1, tv_time t2)
tv_time tv_timesub (tv_time t1, tv_time t2)
tv_time tv_timemul (tv_time t1, double mult)
int tv_timecmp (tv_time t1, tv_time t2)
ts_time ts_timeadd (ts_time t1, ts_time t2)
ts_time ts_timesub (ts_time t1, ts_time t2)
ts_time ts_timemul (ts_time t1, double mult)
int ts_timecmp (ts_time t1, ts_time t2)
ts_strtok_statets_strtok_init (char *string)
char * ts_strtok (char *matching, ts_strtok_state *state)
int ts_strtok_finish (ts_strtok_state *state)
char * dumb_strnstr (char *str, char *substr, int n)
const char * dumb_strcasestr (const char *string, const char *substr)
int correct_write (int s, char *data, int len)
int correct_read (int s, char *data, int len)
int ll_add_to_end (ll *addToMe, void *data)
int ll_add_to_start (ll *addToMe, void *data)
ll ll_find (ll *findInMe, void *data, int(*compare)(void *d1, void *d2))
int ll_sorted_insert (ll *insertme, void *data, int(*compare)(void *d1, void *d2))
int ll_del (ll *delFromMe, void *data, int(*compare)(void *d1, void *d2), void(*nukeElement)(void *nukeMe))
int ll_delfirst (ll *delFromMe, void(*nukeElement)(void *nukeMe))
int ll_destroy (ll *destroyMe, void(*nukeElement)(void *nukeMe))
int ll_build (ll *buildMe, char *buildFromMe)
int chained_hash_create (int num_slots, hash_table *rt)
int chained_hash_destroy (hash_table ht, deletor d)
int chained_hash_insert (hash_table ht, void *element, hash_function f)
void * chained_hash_search (hash_table ht, void *element, hash_function f, sc_comparator c)
int chained_hash_delete (hash_table ht, void *element, hash_function f, deletor d, sc_comparator c)
int Tree_Add (AVL_tree *addToMe, void *addMe, AVL_comparator theComp)
int Tree_Delete (AVL_tree *deleteFromMe, void *deleteMe, AVL_comparator theComp, AVL_deletor theDel)
int Tree_Destroy (AVL_tree *destroyMe, AVL_deletor theDel)
AVL_tree Tree_Search (AVL_tree searchMe, void *searchForMe, AVL_comparator theComp)
AVL_tree Tree_First (AVL_tree searchMe)
AVL_tree Tree_Last (AVL_tree searchMe)
AVL_tree Tree_Next (AVL_tree searchMe)


Define Documentation

#define FALSE   0
 

Definition at line 287 of file utils.h.

#define TRUE   1
 

Definition at line 290 of file utils.h.


Typedef Documentation

typedef int(* AVL_comparator)(void *node, void *comparison)
 

Definition at line 306 of file utils.h.

typedef void(* AVL_deletor)(void *deleteMe)
 

Definition at line 307 of file utils.h.

typedef struct AVL_tree_st * AVL_tree
 

typedef struct AVL_tree_st AVL_tree_element
 

typedef void(* deletor)(void *element)
 

Definition at line 232 of file utils.h.

typedef ll hash_el
 

Definition at line 225 of file utils.h.

Referenced by chained_hash_create(), and chained_hash_destroy().

typedef int(* hash_function)(void *element, int num_slots)
 

Definition at line 230 of file utils.h.

typedef struct hash_table_st hash_table
 

typedef struct ll_st * ll
 

Referenced by ARPTable::arpinput(), ARPTable::arprequest(), ARPTable::arpresolve(), SplayScheduler::deque(), SplayScheduler::head(), and ARPTable::Terminate().

typedef struct ll_st ll_node
 

typedef int(* sc_comparator)(void *element1, void *element2)
 

Definition at line 231 of file utils.h.

typedef struct ts_strtok_st ts_strtok_state
 

typedef struct timespec ts_time
 

Definition at line 70 of file utils.h.

Referenced by ts_timeadd(), ts_timemul(), and ts_timesub().

typedef struct timeval tv_time
 

Definition at line 69 of file utils.h.

Referenced by tv_timeadd(), tv_timemul(), and tv_timesub().


Function Documentation

int chained_hash_create int  num_slots,
hash_table rt
 

Definition at line 603 of file utils.cc.

References hash_el, hash_table_st::num_elements, and hash_table_st::slot_array.

00604 {
00605    hash_table  retval;
00606    hash_el    *sArray;
00607    int i;
00608 
00609    /* Creating a chained hash table is as simple as allocating room
00610       for all of the slots. */
00611    
00612    sArray = retval.slot_array = (hash_el *) malloc(num_slots*sizeof(hash_el));
00613    if (retval.slot_array == NULL)
00614       return -1;
00615 
00616    retval.num_elements = num_slots;
00617    *rt = retval;
00618 
00619    /* Initialize all slots to have a null entry */
00620    for (i=0; i<num_slots; i++)
00621      sArray[i] = NULL;
00622 
00623    return 0;
00624 }

int chained_hash_delete hash_table  ht,
void *  element,
hash_function  f,
deletor  d,
sc_comparator  c
 

Definition at line 698 of file utils.cc.

References ll_del(), hash_table_st::num_elements, and hash_table_st::slot_array.

00700 {
00701    ll   *slotList;
00702    int   slotnum;
00703 
00704    slotnum = f(element, ht.num_elements);
00705    if (! ((slotnum >= 0) && (slotnum < ht.num_elements)) )
00706      return 0;  /* f is bad */
00707 
00708    slotList = &(ht.slot_array[slotnum]);
00709    if (slotList == NULL)
00710      return 0;  /* no list, no element */
00711 
00712    return ll_del(slotList, element, c, d);
00713 }

Here is the call graph for this function:

int chained_hash_destroy hash_table  ht,
deletor  d
 

Definition at line 626 of file utils.cc.

References hash_el, ll_destroy(), hash_table_st::num_elements, and hash_table_st::slot_array.

00627 {
00628    int      i, numEl;
00629    hash_el *sArray;
00630 
00631    /* Destroying a hash table first implies destroying all of the
00632       lists in the table, then freeing the table itself. */
00633 
00634    for (i=0, numEl=ht.num_elements, sArray=ht.slot_array; i<numEl; i++)
00635    {
00636       if (sArray[i] != NULL)
00637       {
00638          /* We must destroy the list in this slot */
00639          ll_destroy(&(sArray[i]), d);
00640          sArray[i] = NULL;
00641       }
00642    }
00643    free(sArray);
00644    return 0;
00645 }

Here is the call graph for this function:

int chained_hash_insert hash_table  ht,
void *  element,
hash_function  f
 

Definition at line 647 of file utils.cc.

References ll_add_to_start(), hash_table_st::num_elements, and hash_table_st::slot_array.

00648 {
00649    int  slotnum;
00650    ll  *slotList;
00651 
00652    slotnum = f(element, ht.num_elements);
00653    if (! ((slotnum >= 0) && (slotnum < ht.num_elements)) )
00654      return -1;   /* f is bad */
00655 
00656    slotList = &(ht.slot_array[slotnum]);
00657    if (ll_add_to_start(slotList, element) != 1)
00658      return -1;   /* list insert failed */
00659 
00660    return 0;
00661 }

Here is the call graph for this function:

void* chained_hash_search hash_table  ht,
void *  element,
hash_function  f,
sc_comparator  c
 

Definition at line 663 of file utils.cc.

References ll_st::data, ll_find(), hash_table_st::num_elements, and hash_table_st::slot_array.

00665 {
00666    ll   *slotList, foundEl;
00667    int   slotnum;
00668 
00669 #ifdef DEBUG
00670    printf("chained_hash_search: about to f element, numelements %d\n", ht.num_elements);
00671 #endif
00672 
00673    slotnum = f(element, ht.num_elements);
00674 
00675 #ifdef DEBUG
00676    printf("f() returned slotnum %d\n", slotnum);
00677 #endif
00678 
00679    if (! ((slotnum >= 0) && (slotnum < ht.num_elements)) )
00680      return NULL;  /* f is bad */
00681 
00682 #ifdef DEBUG
00683    printf("in chs: slotnum %d\n", slotnum);
00684 #endif
00685    slotList = &(ht.slot_array[slotnum]);
00686    if (slotList == NULL)
00687      return NULL;  /* no list, no element */
00688 
00689 #ifdef DEBUG
00690    printf("about to ll_find on slotList.\n");
00691 #endif
00692    foundEl = ll_find(slotList, element, c);
00693    if (foundEl == NULL)
00694      return NULL;
00695    return foundEl->data;
00696 }

Here is the call graph for this function:

int correct_read int  s,
char *  data,
int  len
 

Definition at line 332 of file utils.cc.

References len.

Referenced by lf_get_next_entry().

00333 {
00334   int sofar, ret;
00335   char *tmp;
00336 
00337   sofar = 0;
00338   while(sofar != len) {
00339     tmp = data + (unsigned long) sofar;
00340     ret = read(s, tmp, len-sofar);
00341     if (ret <= 0) {
00342       if (! ((ret == -1) && ((errno == EAGAIN) || (errno == EINTR))) )
00343         /* BUG:: What if another thread generates an error and trounces errno? */
00344         return ret;
00345     } else
00346       sofar += ret;
00347   }
00348   return len;
00349 }

int correct_write int  s,
char *  data,
int  len
 

Definition at line 310 of file utils.cc.

References len.

00311 {
00312   int sofar, ret;
00313   char *tmp;
00314 
00315   if (len == -1) 
00316     len = strlen(data);
00317   
00318   sofar = 0;
00319   while (sofar != len) {
00320     tmp = data + (unsigned long) sofar;
00321     ret = write(s, tmp, len-sofar);
00322     if (ret <= 0) {
00323       if (! ((ret == -1) && ((errno == EAGAIN) || (errno == EINTR))) )
00324         /* BUG:: What if another thread generates an error and trounces errno? */
00325         return ret;
00326     } else
00327       sofar += ret;
00328   }
00329   return len;
00330 }

const char* dumb_strcasestr const char *  string,
const char *  substr
 

Definition at line 64 of file utils.cc.

00065 {
00066   int str_len, substr_len, cmplen, i;
00067   const char *ptr;
00068 
00069   str_len = strlen(string);
00070   substr_len = strlen(substr);
00071   cmplen = str_len - substr_len + 1;
00072 
00073   for (ptr = string, i=0; i<cmplen; i++, ptr++) {
00074     if (strncasecmp(ptr, substr, substr_len) == 0)
00075       return ptr;
00076   }
00077   return NULL;
00078 }

char* dumb_strnstr char *  str,
char *  substr,
int  n
 

Definition at line 292 of file utils.cc.

00293 {
00294   int i, substrlen, ml;
00295   char *tmp;
00296 
00297   substrlen = strlen(substr);
00298 
00299   for (i=0,tmp=str; i<=n-substrlen; i++, tmp++) {
00300     if (*tmp == '\0')
00301       return NULL;
00302     if ( (ml = memcmp(tmp, substr, substrlen)) == 0)
00303       return tmp;
00304   }
00305   return NULL;
00306 }

void dump_buf FILE *  std,
char *  buf,
int  retlen
 

Definition at line 84 of file utils.cc.

00085 {
00086   int i, j;
00087 
00088   for (i=0; i<retlen/5; i++) {
00089     for (j=0; j<5; j++) {
00090       fprintf(std, "%.02x ", *(buf + (unsigned) 5*i + j));
00091     }
00092     fprintf(std, "     ");
00093     for (j=0; j<5; j++) {
00094       fprintf(std, "%c ", *(buf + (unsigned) 5*i + j));
00095     }
00096     fprintf(std, "\n");
00097   }
00098   if (retlen % 5 != 0) {
00099     for (j=0; j < retlen % 5; j++) {
00100       fprintf(std, "%.02x ", *(buf + (unsigned) 5*(retlen/5) + j));
00101     }
00102     fprintf(std, "     ");
00103     for (j=0; j<5; j++) {
00104       fprintf(std, "%c ", *(buf + (unsigned) 5*i + j));
00105     }
00106     fprintf(std, "\n");
00107   }
00108 }

int ll_add_to_end ll addToMe,
void *  data
 

Definition at line 360 of file utils.cc.

References Allocate_ll(), ll_st::data, and ll_st::next.

Referenced by ll_build(), and ll_sorted_insert().

00361 {
00362    ll temp;
00363 
00364    temp = *addToMe;
00365    if (temp == NULL)
00366    {
00367       *addToMe = temp = Allocate_ll();
00368       temp->data = data;
00369       temp->prev = temp->next = NULL;
00370       return 1;
00371    }
00372 
00373    while (temp->next != NULL)
00374      temp = temp->next;
00375 
00376    temp->next = Allocate_ll();
00377    (temp->next)->prev = temp;
00378    (temp->next)->next = NULL;
00379    (temp->next)->data = data;
00380    return 1;
00381 }

Here is the call graph for this function:

int ll_add_to_start ll addToMe,
void *  data
 

Definition at line 383 of file utils.cc.

References Allocate_ll(), ll_st::data, and ll_st::prev.

Referenced by chained_hash_insert().

00384 {
00385    ll temp;
00386 
00387    temp = *addToMe;
00388    if (temp == NULL)
00389    {
00390       *addToMe = temp = Allocate_ll();
00391       temp->data = data;
00392       temp->prev = temp->next = NULL; 
00393       return 1;
00394    }
00395 
00396    temp->prev = Allocate_ll();
00397    (temp->prev)->next = temp;
00398    (temp->prev)->prev = NULL;
00399    (temp->prev)->data = data;
00400    *addToMe = temp->prev;
00401 
00402    return 1;
00403 
00404 }

Here is the call graph for this function:

int ll_build ll buildMe,
char *  buildFromMe
 

Definition at line 549 of file utils.cc.

References ll_add_to_end().

00550 {
00551  /* Takes string of form [a, b, c, d, e] and builds linked list with
00552     elements from the string.  Malloc's space for new linked list element
00553     strings. */
00554 
00555     char *end, *start, *data, *temp;
00556 
00557     data = (char *) malloc(sizeof(char) * (strlen(buildFromMe)+1));
00558     if (data == NULL)
00559     {
00560       fprintf(stderr, "Out of memory in ll_build!\n");
00561       return 0;
00562     }
00563     strcpy(data, buildFromMe);
00564     start = end = data + 1;
00565     
00566     while ((*end != ']') && (*end != '\0'))
00567     {
00568       while (*start == ' ')
00569       {
00570         start++;  end++;
00571       }
00572 
00573       if (*end == ',')
00574       {
00575           *end = '\0';
00576           temp = (char *) (malloc(sizeof(char) * (strlen(start)+1)));
00577           if (temp == NULL)
00578           {
00579              fprintf(stderr, "Out of memory in ll_build!\n");
00580              return 0;
00581           }
00582           strcpy(temp, start);
00583           ll_add_to_end(buildMe, (void *) temp);
00584           start = end + 1;
00585       }
00586       end++;
00587     }
00588     *end = '\0';
00589     temp = (char *) (malloc(sizeof(char) * (strlen(start)+1)));
00590     if (temp == NULL)
00591     {
00592        fprintf(stderr, "Out of memory in ll_build!\n");
00593        return 0;
00594     }
00595     strcpy(temp, start);
00596     ll_add_to_end(buildMe, (void *) temp);
00597     free(data);
00598     return 1;
00599 }

Here is the call graph for this function:

int ll_del ll delFromMe,
void *  data,
int(*  compare)(void *d1, void *d2),
void(*  nukeElement)(void *nukeMe)
 

Definition at line 446 of file utils.cc.

References compare(), ll_st::data, Free_ll(), ll_st::next, and ll_st::prev.

Referenced by chained_hash_delete().

00448 {
00449    ll temp = *delFromMe;
00450 
00451    while (temp != NULL)
00452    {
00453       if (compare(data, temp->data) == 0)  /* ie the same */
00454       {
00455          if (temp->prev != NULL)
00456            (temp->prev)->next = temp->next;
00457 
00458          if (temp->next != NULL)
00459            (temp->next)->prev = temp->prev;
00460 
00461          if (temp == *delFromMe)
00462             *delFromMe = temp->next;
00463 
00464          if (nukeElement != NULL)
00465             nukeElement(temp->data);
00466          Free_ll(temp);
00467          return 1;
00468       }
00469       temp = temp->next;
00470    }
00471    return 0;
00472 }

Here is the call graph for this function:

int ll_delfirst ll delFromMe,
void(*  nukeElement)(void *nukeMe)
 

Definition at line 474 of file utils.cc.

References ll_st::data, Free_ll(), ll_st::next, and ll_st::prev.

00475 {
00476    ll temp = *delFromMe;
00477 
00478    if (temp == NULL)
00479       return 0;
00480 
00481    if (temp->prev != NULL)
00482      (temp->prev)->next = temp->next;
00483 
00484    if (temp->next != NULL)
00485      (temp->next)->prev = temp->prev;
00486 
00487    if (temp == *delFromMe)
00488      *delFromMe = temp->next;
00489 
00490    if (nukeElement != NULL)
00491      nukeElement(temp->data);
00492    Free_ll(temp);
00493    return 1;
00494 }

Here is the call graph for this function:

int ll_destroy ll destroyMe,
void(*  nukeElement)(void *nukeMe)
 

Definition at line 496 of file utils.cc.

References ll_st::data, Free_ll(), and ll_st::next.

Referenced by chained_hash_destroy().

00497 {
00498    ll temp = *destroyMe, next;
00499 
00500    while (temp != NULL)
00501    {
00502       next = temp->next;
00503       if (nukeElement != NULL)
00504          nukeElement(temp->data);
00505       Free_ll(temp);
00506       temp = next;
00507    }
00508    *destroyMe = NULL;
00509    return 1;
00510 }

Here is the call graph for this function:

ll ll_find ll findInMe,
void *  data,
int(*  compare)(void *d1, void *d2)
 

Definition at line 406 of file utils.cc.

References compare(), ll_st::data, and ll_st::next.

Referenced by chained_hash_search().

00407 {
00408    ll temp = *findInMe;
00409 
00410    while (temp != NULL)
00411    {
00412      if (compare(data, temp->data) == 0)  /* ie the same */
00413         return temp;
00414      
00415      temp = temp->next;
00416    }
00417    return temp;
00418 }

Here is the call graph for this function:

int ll_sorted_insert ll insertme,
void *  data,
int(*  compare)(void *d1, void *d2)
 

Definition at line 420 of file utils.cc.

References Allocate_ll(), compare(), ll_st::data, ll_add_to_end(), ll_st::next, and ll_st::prev.

00422 {
00423   ll temp = *insertme, addEl;
00424 
00425   while (temp != NULL) {
00426     if (compare(data, temp->data) <= 0) {/* ie. the same or smaller */
00427       /* insert before temp */
00428       addEl = Allocate_ll();
00429       addEl->data = data;
00430       addEl->prev = temp->prev;
00431       addEl->next = temp;
00432       temp->prev = addEl;
00433       if (addEl->prev == NULL)
00434         *insertme = addEl;
00435       else
00436         (addEl->prev)->next = addEl;
00437       return 1;
00438     }
00439     temp = temp->next;
00440   }
00441   
00442   /* We hit the end, so add to the end */
00443   return ll_add_to_end(insertme, data);
00444 }

Here is the call graph for this function:

int Tree_Add AVL_tree addToMe,
void *  addMe,
AVL_comparator  theComp
 

Definition at line 730 of file utils.cc.

References Allocate_AVL_tree(), AVL_tree_st::bal, FALSE, AVL_tree_st::left, AVL_tree_st::parent, AVL_tree_st::right, and TRUE.

00731 {
00732     static int       checkBalance = FALSE;
00733     static AVL_tree      parent = NULL;
00734     AVL_tree        *addTree = addToMe;
00735 
00736     if (*addTree == NULL)  /* Add in a new leaf... */
00737     {
00738         *addTree = Allocate_AVL_tree();
00739         (*addTree)->data = addMe;
00740         (*addTree)->bal = 0;
00741         (*addTree)->left = (*addTree)->right = NULL;
00742         (*addTree)->parent = parent;
00743         checkBalance = TRUE; /* whether balance should be checked */
00744         parent = NULL;
00745         return 0;
00746     }
00747     else
00748         if ( theComp( (*addTree)->data, addMe) > 0 )   // ie. (*addTree)->data > addMe
00749         {   
00750             parent = *addTree;
00751             if ( Tree_Add(&((*addTree)->left), addMe, theComp) != 0 )
00752             {   checkBalance = FALSE;    return -1;  }
00753             if (checkBalance == TRUE)  /* CHECK FOR REBALANCING -
00754                                           LEFT INSERTION */
00755             {
00756                 switch( (*addTree)->bal) {
00757                     case 1 :  (*addTree)->bal = 0;
00758                               checkBalance = FALSE;  return 0;
00759                     case 0 :  (*addTree)->bal = -1;
00760                               checkBalance = TRUE;  return 0;
00761                     case -1:
00762                     {  AVL_tree p = (*addTree)->left;
00763                        switch (p->bal) {
00764                            case -1: {  /* LL ROTATION */
00765                                 AVL_tree q = *addTree;
00766                                 AVL_tree r = p->right;
00767                                 p->parent = q->parent;
00768                                 p->right = q;
00769                                 q->parent = p; q->left = r;
00770                                 if (r != NULL)
00771                                     r->parent = q;
00772                                 *addTree = p;  (*addTree)->bal = 0;
00773                                 q->bal = 0;  checkBalance = FALSE;
00774                                 return 0;  }
00775                            case 1: {  /* LR ROTATION */
00776                                 AVL_tree m = *addTree;
00777                                 AVL_tree q = p->right;
00778                                 AVL_tree s = q->left;
00779                                 AVL_tree u = q->right;
00780                                 q->parent = m->parent;
00781                                 m->parent = q;
00782                                 p->parent = q;
00783                                 if (s != NULL)
00784                                    s->parent = p;
00785                                 if (u != NULL)
00786                                    u->parent = m;
00787                                 p->right = s;  q->left = p;
00788                                 q->right = m;  m->left = u;
00789                                 *addTree = q;
00790                                 if (q->bal == 0)
00791                                 {
00792                                    q->bal = 0;
00793                                    p->bal = 0;
00794                                    m->bal = 0;
00795                                 }
00796                                 else if (q->bal == -1)
00797                                 {
00798                                    q->bal = 0;
00799                                    p->bal = 0;
00800                                    m->bal = 1;
00801                                    
00802                                 }
00803                                 else if (q->bal == 1)
00804                                 {
00805                                    q->bal = 0;
00806                                    p->bal = -1;
00807                                    m->bal = 0;
00808                                 }
00809                                 checkBalance = FALSE;  return 0; }
00810                            default: exit(1);
00811                      } } }
00812             }
00813             else
00814             {   checkBalance = FALSE;  return 0; }
00815         }
00816         else
00817         if ( theComp( (*addTree)->data, addMe) < 0 ) /* ie. (*addTree)->data < addMe */
00818         {
00819             parent = *addTree;
00820             if ( Tree_Add(&((*addTree)->right), addMe, theComp) != 0 )
00821             {  checkBalance = FALSE;  return -1; }
00822             if (checkBalance == TRUE)
00823             {    /* Check for rebalancing - right insertion */
00824                 switch ( (*addTree)->bal) {
00825                     case -1:  (*addTree)->bal = 0;
00826                               checkBalance = FALSE;  return 0;
00827                     case 0 :  (*addTree)->bal = 1;
00828                               checkBalance = TRUE;  return 0;
00829                     case 1 :
00830                     {  AVL_tree p = (*addTree)->right;
00831                        switch (p->bal) {
00832                            case -1: {  /*i RL ROTATION */
00833                              AVL_tree m = *addTree;
00834                              AVL_tree q = p->left;
00835                              AVL_tree r = q->right;
00836                              AVL_tree s = q->left;
00837                              q->parent = m->parent;
00838                              m->parent = q;
00839                              p->parent = q;
00840                              if (s != NULL)
00841                                 s->parent = m;
00842                              if (r != NULL)
00843                                 r->parent = p;
00844                              m->right = s;  q->left = m;
00845                              q->right = p;  p->left = r;
00846                              (*addTree) = q;
00847                                 if (q->bal == 0)
00848                                 {
00849                                    q->bal = 0;
00850                                    p->bal = 0;
00851                                    m->bal = 0;
00852                                 }
00853                                 else if (q->bal == -1)
00854                                 {
00855                                    q->bal = 0;
00856                                    p->bal = 1;
00857                                    m->bal = 0;
00858                                    
00859                                 }
00860                                 else if (q->bal == 1)
00861                                 {
00862                                    q->bal = 0;
00863                                    p->bal = 0;
00864                                    m->bal = -1;
00865                                 }
00866                              checkBalance = FALSE;
00867                              return 0; }
00868                            case 1: {  /* RR ROTATION */
00869                              AVL_tree q = (*addTree);
00870                              AVL_tree r = p->left;
00871                              p->parent = q->parent;
00872                              q->parent = p;
00873                              if (r != NULL)
00874                                 r->parent = q;
00875                              p->left = q;  q->right = r;
00876                              (*addTree) = p;  (*addTree)->bal = 0;
00877                              q->bal = 0;  checkBalance = FALSE;
00878                              return 0; }
00879                        }
00880                     }
00881                 }
00882             }
00883             else
00884             {  checkBalance = FALSE;  return 0; }
00885         }
00886         else    /* key already exists - bomb out */
00887         {  checkBalance = FALSE;  return -1;  }
00888 
00889     return 0;
00890 }

Here is the call graph for this function:

int Tree_Delete AVL_tree deleteFromMe,
void *  deleteMe,
AVL_comparator  theComp,
AVL_deletor  theDel
 

Definition at line 892 of file utils.cc.

References AVL_tree_st::data, Free_AVL_tree(), AVL_tree_st::left, AVL_tree_st::parent, AVL_tree_st::right, and Tree_Next().

00894 {
00895    /* A simplified version of an AVL_delete algorithm.  This procedure will
00896       instead do a regular binary tree deletion  */
00897 
00898     AVL_tree         tempTree, tempTreeX, Z;
00899     static AVL_tree *root = NULL;
00900 
00901     Z = *deleteFromMe;
00902 
00903     if (*deleteFromMe == NULL)  /* hit a leaf.  bomb out */
00904     {    root = NULL;
00905          return -1;
00906     }
00907 
00908     if (root == NULL)
00909         root = deleteFromMe;
00910 
00911     if ( theComp((*deleteFromMe)->data, deleteMe) > 0 ) /* ie. (*deleteFromMe)->data > deleteMe */
00912        return Tree_Delete(&((*deleteFromMe)->left), deleteMe, theComp, theDel);
00913     else
00914     {
00915         if ( theComp((*deleteFromMe)->data, deleteMe) < 0) /* ie. (*deleteFromMe)->data < deleteMe */ 
00916            return Tree_Delete(&((*deleteFromMe)->right), deleteMe, theComp, theDel);
00917         else
00918         {
00919             theDel((*deleteFromMe)->data);   /* Call the deletor function */
00920             
00921             /* we've found the node to delete - three cases */
00922             if (  (((*deleteFromMe)->left) == NULL) ||
00923                   (((*deleteFromMe)->right) == NULL) )
00924                 tempTree = *deleteFromMe;
00925             else
00926                 tempTree = Tree_Next(*deleteFromMe);
00927 
00928             if ( tempTree->left != NULL)
00929                 tempTreeX = tempTree->left;
00930             else
00931                 tempTreeX = tempTree->right;
00932 
00933             if (tempTreeX != NULL)
00934                 tempTreeX->parent = tempTree->parent;
00935 
00936             if (tempTree->parent == NULL)   /* ie root, replace all */
00937                 *root = tempTreeX;
00938             else
00939             {
00940                 if ( tempTree == ((tempTree->parent)->left) )
00941                     ((tempTree->parent)->left) = tempTreeX;
00942                 else
00943                     ((tempTree->parent)->right) = tempTreeX;
00944             }
00945 
00946             if (tempTree != Z)
00947             {
00948               /* splice successor back in */
00949               (*deleteFromMe)->data = tempTree->data;
00950             }
00951 
00952             Free_AVL_tree(tempTree);
00953             root = NULL;
00954             return 0;
00955 
00956         }
00957     }
00958 }

Here is the call graph for this function:

int Tree_Destroy AVL_tree destroyMe,
AVL_deletor  theDel
 

Definition at line 960 of file utils.cc.

References Free_AVL_tree().

00961 {
00962    if (*destroyMe == NULL)
00963       return 0;
00964    
00965    Tree_Destroy( &((*destroyMe)->left), theDel);
00966    Tree_Destroy( &((*destroyMe)->right), theDel);
00967    
00968    theDel( (*destroyMe)->data );
00969    Free_AVL_tree(*destroyMe);
00970    *destroyMe = NULL;
00971    return 0;
00972 }

Here is the call graph for this function:

AVL_tree Tree_First AVL_tree  searchMe  ) 
 

Definition at line 989 of file utils.cc.

References AVL_tree_st::left.

00990 {
00991    AVL_tree retTree = searchMe;
00992    
00993    if (retTree == NULL)
00994       return NULL;
00995    
00996    while (retTree->left != NULL)
00997       retTree = retTree->left;
00998    
00999    return retTree;
01000 }

AVL_tree Tree_Last AVL_tree  searchMe  ) 
 

Definition at line 1002 of file utils.cc.

References AVL_tree_st::right.

01003 {
01004    AVL_tree retTree = searchMe;
01005    
01006    if (retTree == NULL)
01007       return NULL;
01008    
01009    while (retTree->right != NULL)
01010       retTree = retTree->right;
01011    
01012    return retTree;
01013 }

AVL_tree Tree_Next AVL_tree  searchMe  ) 
 

Definition at line 1015 of file utils.cc.

References AVL_tree_st::left, AVL_tree_st::parent, and AVL_tree_st::right.

Referenced by Tree_Delete().

01016 {
01017     AVL_tree    tempTree;
01018 
01019     if ((searchMe->right) != NULL) /* aha! swoop down to min  of right subtree */
01020     {
01021         tempTree = searchMe->right;
01022         while ((tempTree->left) != NULL)
01023            tempTree = tempTree->left;
01024         return tempTree;
01025     }
01026    
01027     /* right is null - find lowest ancestor whose left child is also ancestor of x */
01028     tempTree = searchMe->parent;
01029     while ( (tempTree != NULL) && (searchMe == tempTree->right) )
01030     {
01031        searchMe = tempTree;
01032        tempTree = tempTree->parent;
01033     }
01034     return tempTree;
01035 }

AVL_tree Tree_Search AVL_tree  searchMe,
void *  searchForMe,
AVL_comparator  theComp
 

Definition at line 974 of file utils.cc.

References AVL_tree_st::data, AVL_tree_st::left, and AVL_tree_st::right.

00975 {
00976     if (searchMe == NULL)  /* didn't find it! */
00977        return NULL;
00978 
00979     if (theComp(searchMe->data, searchForMe) > 0 ) /* ie. searchMe->data > searchForMe */
00980        return Tree_Search(searchMe->left, searchForMe, theComp);
00981 
00982     if (theComp(searchMe->data, searchForMe) < 0 ) /* ie. searchMe->data < searchForMe */
00983        return Tree_Search(searchMe->right, searchForMe, theComp);
00984 
00985     /* aha! found correct key */
00986     return searchMe;
00987 }

char* ts_strtok char *  matching,
ts_strtok_state state
 

Definition at line 257 of file utils.cc.

References ts_strtok_st::chars_remaining, and ts_strtok_st::nxt_ptr.

00258 {
00259   int len1, len2;
00260   char *tmpo;
00261 
00262   if ((matching == NULL) || (state == NULL) || (state->chars_remaining <= 0))
00263     return NULL;
00264 
00265   len1 = strspn(state->nxt_ptr, matching);
00266   if (len1 > state->chars_remaining) {
00267     return NULL;
00268   }
00269 
00270   tmpo = state->nxt_ptr + (unsigned) len1;
00271   len2 = strcspn(tmpo, matching);
00272   if (len2+len1 > state->chars_remaining) {
00273     return NULL;
00274   }
00275   *(tmpo+len2) = '\0';
00276   state->nxt_ptr = tmpo + len2 + 1;       /* +1 for the null */
00277   state->chars_remaining -= len1+len2+1;  /* +1 for the null */
00278   return tmpo;
00279 }

int ts_strtok_finish ts_strtok_state state  ) 
 

Definition at line 281 of file utils.cc.

References ts_strtok_st::string_dupe.

00282 {
00283   if (state) {
00284     if (state->string_dupe)
00285       free(state->string_dupe);
00286     free(state);
00287   }
00288   return 1;
00289 }

ts_strtok_state* ts_strtok_init char *  string  ) 
 

Definition at line 235 of file utils.cc.

References ts_strtok_st::chars_remaining, ts_strtok_st::nxt_ptr, and ts_strtok_st::string_dupe.

00236 {
00237   ts_strtok_state *retval;
00238 
00239   if (string == NULL)
00240     return NULL;
00241 
00242   retval = (ts_strtok_state *) malloc(sizeof(ts_strtok_state));
00243   if (retval == NULL)
00244     return NULL;
00245 
00246   retval->string_dupe = strdup(string);
00247   if (retval->string_dupe == NULL) {
00248     free(retval);
00249     return NULL;
00250   }
00251   retval->nxt_ptr = retval->string_dupe;
00252   retval->chars_remaining = strlen(retval->string_dupe);
00253 
00254   return retval;
00255 }

ts_time ts_timeadd ts_time  t1,
ts_time  t2
 

Definition at line 173 of file utils.cc.

References t1, and ts_time.

00174 {
00175   ts_time ret;
00176 
00177   ret.tv_sec = t1.tv_sec + t2.tv_sec;
00178   ret.tv_nsec = t1.tv_nsec + t2.tv_nsec;
00179   if (ret.tv_nsec > 1000000000L) {
00180     ret.tv_sec += 1;
00181     ret.tv_nsec -= 1000000000L;
00182   }
00183   return ret;
00184 }

int ts_timecmp ts_time  t1,
ts_time  t2
 

Definition at line 218 of file utils.cc.

References t1.

00219 {
00220  if (t1.tv_sec > t2.tv_sec)
00221      return 1;
00222   if (t1.tv_sec < t2.tv_sec)
00223      return -1;
00224   if (t1.tv_nsec > t2.tv_nsec)
00225      return 1;
00226   if (t1.tv_nsec < t2.tv_nsec)
00227      return -1;
00228   return 0;
00229 }

ts_time ts_timemul ts_time  t1,
double  mult
 

Definition at line 198 of file utils.cc.

References t1, and ts_time.

00199 {
00200   ts_time ret;
00201   double  rsec;
00202   double  rnsec;
00203 
00204   rsec = mult * ((double) t1.tv_sec);
00205   rnsec = mult * ((double) t1.tv_nsec);
00206 
00207   ret.tv_sec = (long)rsec;
00208   ret.tv_nsec = (long)rnsec;
00209 
00210   ret.tv_nsec += (long)((double) 1000000000.0 * (rsec - (double) ret.tv_sec));
00211   while (ret.tv_nsec > 1000000000) {
00212     ret.tv_nsec -= 1000000000;
00213     ret.tv_sec += 1;
00214   }
00215   return ret;
00216 }

ts_time ts_timesub ts_time  t1,
ts_time  t2
 

Definition at line 185 of file utils.cc.

References t1, and ts_time.

00186 {
00187   ts_time ret;
00188 
00189   if (t2.tv_nsec > t1.tv_nsec) {
00190     t1.tv_nsec += 1000000000;
00191     t2.tv_sec += 1;
00192   }
00193   ret.tv_sec = t1.tv_sec - t2.tv_sec;
00194   ret.tv_nsec = t1.tv_nsec - t2.tv_nsec;
00195   return ret;
00196 }

tv_time tv_timeadd tv_time  t1,
tv_time  t2
 

Definition at line 114 of file utils.cc.

References t1, and tv_time.

00115 {
00116   tv_time ret;
00117 
00118   ret.tv_sec = t1.tv_sec + t2.tv_sec;
00119   ret.tv_usec = t1.tv_usec + t2.tv_usec;
00120   if (ret.tv_usec > 1000000L) {
00121     ret.tv_sec += 1;
00122     ret.tv_usec -= 1000000L;
00123   }
00124   return ret;
00125 }

int tv_timecmp tv_time  t1,
tv_time  t2
 

Definition at line 160 of file utils.cc.

References t1.

00161 {
00162   if (t1.tv_sec > t2.tv_sec)
00163      return 1;
00164   if (t1.tv_sec < t2.tv_sec)
00165      return -1;
00166   if (t1.tv_usec > t2.tv_usec)
00167      return 1;
00168   if (t1.tv_usec < t2.tv_usec)
00169      return -1;
00170   return 0;
00171 }

tv_time tv_timemul tv_time  t1,
double  mult
 

Definition at line 140 of file utils.cc.

References t1, and tv_time.

00141 {
00142   tv_time ret;
00143   double  rsec;
00144   double  rusec;
00145 
00146   rsec = mult * ((double) t1.tv_sec);
00147   rusec = mult * ((double) t1.tv_usec);
00148 
00149   ret.tv_sec = (long)rsec;
00150   ret.tv_usec = (long)rusec;
00151 
00152   ret.tv_usec += (long)((double) 1000000.0 * (rsec - (double) ret.tv_sec));
00153   while (ret.tv_usec > 1000000) {
00154     ret.tv_usec -= 1000000;
00155     ret.tv_sec += 1;
00156   }
00157   return ret;
00158 }

tv_time tv_timesub tv_time  t1,
tv_time  t2
 

Definition at line 127 of file utils.cc.

References t1, and tv_time.

00128 {
00129   tv_time ret;
00130 
00131   if (t2.tv_usec > t1.tv_usec) {
00132     t1.tv_usec += 1000000;
00133     t2.tv_sec += 1;
00134   }
00135   ret.tv_sec = t1.tv_sec - t2.tv_sec;
00136   ret.tv_usec = t1.tv_usec - t2.tv_usec;
00137   return ret;
00138 }


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