home *** CD-ROM | disk | FTP | other *** search
/ Boldly Go Collection / version40.iso / TS / 17A / DRWIN101.ZIP / SYMUTIL.CPP < prev    next >
C/C++ Source or Header  |  1991-09-05  |  3KB  |  146 lines

  1. #include "symutil.hpp"
  2.  
  3.  
  4. SymbolTable::SymbolTable(int _size,CompareFunc _compare)
  5. {
  6.   if (_compare==NULL) compare=(CompareFunc)strcmp; else compare=_compare;
  7.   if (_size<1) size=SIZE; else size=_size;
  8.   base=NULL;
  9.   curr=NULL;
  10.   count=0;
  11. }   //SymbolTable::SymbolTable(int,CompareFunc)
  12.  
  13.  
  14. void* SymbolTable::Insert(void* sym)
  15. {
  16.   if (base==NULL) {
  17.     base=(SYMBOL_NODE*) new BYTE[size+sizeof(SYMBOL_NODE)];
  18.     if (base==NULL) return NULL;
  19.     count++;
  20.     base->top=NULL;
  21.     base->left=NULL;
  22.     base->right=NULL;
  23.     base->is_left=0;
  24.     base->is_right=0;
  25.     memcpy(base->data,sym,size);
  26.     return base->data;
  27.   }
  28.  
  29.   int cmp;
  30.   SYMBOL_NODE* node=base;
  31.   SYMBOL_NODE* last;
  32.  
  33.   while (node!=NULL) {
  34.     last=node;
  35.     cmp=compare(sym,node->data);
  36.     if (cmp==0) { curr=node; return node->data; }
  37.     if (cmp<0) node=node->left; else node=node->right;
  38.   }   //while
  39.  
  40.   node=(SYMBOL_NODE*) new BYTE[size+sizeof(SYMBOL_NODE)];
  41.   if (node==NULL) return NULL;
  42.   count++;
  43.   node->top=last;
  44.   node->left=NULL;
  45.   node->right=NULL;
  46.   node->is_left=0;
  47.   node->is_right=0;
  48.   memcpy(node->data,sym,size);
  49.   if (cmp<0) {
  50.     last->left=node;
  51.     node->is_left=1;
  52.   }
  53.   else {
  54.     last->right=node;
  55.     node->is_right=1;
  56.   }
  57.   curr=node;
  58.   return node->data;
  59. }   //SymbolTable::Insert(void*)
  60.  
  61.  
  62. void* SymbolTable::Find(void* sym)
  63. {
  64.   if (base==NULL) return NULL;
  65.   int cmp;
  66.   SYMBOL_NODE* node=base;
  67.   while (node!=NULL) {
  68.     cmp=compare(sym,node->data);
  69.     if (cmp==0) break;
  70.     if (cmp<0) node=node->left; else node=node->right;
  71.   }   //while
  72.   curr=node;
  73.   if (node==NULL) return NULL;
  74.   return node->data;
  75. }   //SymbolTable::Find(void*)
  76.  
  77.  
  78. void* SymbolTable::First(void)
  79. {
  80.   if (base==NULL) return NULL;
  81.   curr=base;
  82.   while (curr->left!=NULL) curr=curr->left;
  83.   return curr->data;
  84. }   //SymbolTable::First(void)
  85.  
  86.  
  87. void* SymbolTable::Next(void)
  88. {
  89.   if (curr==NULL) return NULL;
  90.   if (curr->right != NULL) {           //if a right node's available..
  91.     curr=curr->right;                  //..go right
  92.     while (curr->left != NULL) curr=curr->left;   //..then go far to left
  93.   }
  94.   else {
  95.     while (curr->is_right) curr=curr->top;
  96.     curr=curr->top;
  97.   }
  98.   if (curr==NULL) return NULL;
  99.   return curr->data;
  100. }    //SymbolTable::Next(void)
  101.  
  102.  
  103. void* SymbolTable::Last(void)
  104. {
  105.   if (base==NULL) return NULL;
  106.   curr=base;
  107.   while (curr->right!=NULL) curr=curr->right;
  108.   return curr->data;
  109. }   //SymbolTable::Last(void)
  110.  
  111.  
  112. void* SymbolTable::Prev(void)
  113. {
  114.   if (curr==NULL) return NULL;
  115.   if (curr->left != NULL) {            //if a left node's available..
  116.     curr=curr->left;                   //..go left
  117.     while (curr->right!= NULL) curr=curr->right;   //..then go far to right
  118.   }
  119.   else {
  120.     while (curr->is_left) curr=curr->top;
  121.     curr=curr->top;
  122.   }
  123.   if (curr==NULL) return NULL;
  124.   return curr->data;
  125. }    //SymbolTable::Prev(void)
  126.  
  127.  
  128. void SymbolTable::Purge(void)
  129. {
  130.   if (base==NULL) return;
  131.   while ((base->left!=NULL)||(base->right!=NULL)) {
  132.     curr=base;
  133.     while ((curr->left!=NULL) || (curr->right!=NULL))
  134.       if (curr->left !=NULL) curr=curr->left; else curr=curr->right;
  135.     if (curr->is_right)
  136.       curr->top->right=NULL;
  137.     else
  138.       curr->top->left=NULL;
  139.     delete curr;
  140.   }   //while
  141.   delete base;
  142.   count=0;
  143. }   //SymbolTable::Purge(void)
  144.  
  145.  
  146.