哈希表类模板的设计与实现

template <class T, class N>
struct HashTableNode
{
 T varValue;
 N varName;
 HashTableNode<T, N> *next;
 N Scope;
 N alias;   //will hold the full name of whatever it is pointing to.
}; 

template <class T,class N>
class hashtable
{
 private:
 N currentScope()
  {
   return Scope.Value();
  }

 int hash(N varName,int moder = 10)
  {
   int i = 0;
   for(int q = 0;( q < varName.len() + 1);q++)
    i = i + (q * varName[q]);
   i = i % moder;
   return i;
  }

 public:
  HashTableNode<T, N> *HTNode[10];
  
  hashtable()
  {
   for(int i = 0;i < 10;i++)
    HTNode[i] = NULL;
   Scope.Push("Global");
   ScopeID = "GlobalScope";
  }
  
  ~hashtable()
  {
   for (int i = 0;i< 10;i++)
   {
    HTNode[i] = NULL;
   }
  }

bool newScope(N ScopeName = "")
  {
   if (ScopeID == "")
   {
    ScopeID = "SystemScope";
    //return true;
   }
   if (ScopeName != "")
   {
    node<N> *temp;
    temp = Scope.First;
    while (temp != NULL)
    {
     if (temp->value == ScopeName) 
     {
      newScope (ScopeName + "X");
      return true;
     }
     temp = temp->next;
    }
    delete [] temp;
    Scope.Push(ScopeName);
    if (Debug) cout << "Starting" << ScopeName << endl;
    return true;
   }
   else
   {
    ScopeID += "X";
    Scope.Push(ScopeID);
    if (Debug) cout << "Starting" << ScopeID << endl;
    return true;
   }
  }
  
  bool endScope()
  {
   N temp;
   temp = currentScope();
   HashTableNode<T, N> *t;

   for (int i = 0;i < 10;i ++)
   {
    t = HTNode[i];
    if (t != NULL)
    {
     while ((t != NULL) && ( t->Scope == temp))
     {
      HTNode[i] = t->next;
      delete t;
      t = HTNode[i];
     }
    }
   } 
   temp = Scope.Pop();
   if (Debug) cout << "ending " <<  temp << endl;
   if (Scope.Value() != "") return true;
   return false;
  }
  
  bool addNode(N varName,T varValue,N iScope = "")
  {
   int i = 0;
   N S;
   S = currentScope();
   if (iScope != "") S = iScope;
   i = hash(varName);
   HashTableNode<T, N> *t;
   t = HTNode[i];
   while (t != NULL)
   {
    if ((t->varName == varName) && ( t->Scope == S )) return false;
    t = t->next;
   }

   t = new HashTableNode<T, N>;
   t->varValue = varValue;
   t->varName = varName;
   t->Scope = S;
   t->next = HTNode[i];
   t->alias = "";
   HTNode[i] = t;
   
   return true;
  }

  bool addAlias(N varName,N AliasName,N iScope = "")
  {   //varName is the pointer AliasName is where it points
   int i = 0;
   N S;
   S = currentScope();
   if (iScope != "") S = iScope;
   i = hash(varName);
   HashTableNode<T, N> *t;
   t = HTNode[i];
   while (t != NULL)
   {
    if ((t->varName == varName) && ( t->Scope == S )) return false;
    t = t->next;
   }

   t = new HashTableNode<T, N>;
   t->varName = varName;
   t->Scope = S;
   t->next = HTNode[i];
   t->alias = AliasName;
   HTNode[i] = t;
   
   return true;
  }

bool ChangeNode(N varName,T varValue)
  {
   N S("");
   if (varName.InStr(':') > 0)
   {
    S = Tokenize(varName,":");
    varName.LTrim(':');
   }
   int i = 0;
   i = hash(varName);
   HashTableNode<T, N> *t;
   t = HTNode[i];
   if (S != "")
    while (t != NULL)
    {
     if ((t->varName == varName) && (t->Scope == S))
     {
     if (t->alias == "")
      t->varValue = varValue;          
     else 
      ChangeNode(t->alias,varValue);
     return true;
     }
     t = t->next;
    }
   else
    while (t != NULL)
    {
     if (t->varName == varName)
     {
     if (t->alias == "")
      t->varValue = varValue;          
     else 
      ChangeNode(t->alias,varValue);
     return true;
     }
     t = t->next;
    }
   return false;
  }
  
  T Value(N varName)
  {
   N S("");
   if (varName.InStr(':') > 0)
   {
    S = Tokenize(varName,":");
    varName.LTrim(':');
   }
   int i = 0;
   i = hash(varName);
   HashTableNode<T, N> *t;
   t = HTNode[i];
   if (S != "")
    while (t != NULL)
    {
     if ((t->varName == varName) && (t->Scope == S))
     {
      if (t->alias == "") return t->varValue;
      else return Value(t->alias);
     }
     t = t->next;
    }
   else
    while (t != NULL)
    {
     if (t->varName == varName)  
     {
      if (t->alias == "") return t->varValue;
      else return Value(t->alias);
     }
     t = t->next;
    }
   return NULL;
  }

bool exists(N varName)
  {
   N S("");
   if (varName.InStr(':') > 0)
   {
    S = Tokenize(varName,":");
    varName.LTrim(':');
   }
   int i = 0;
   i = hash(varName);
   HashTableNode<T, N> *t;
   t = HTNode[i];
   while (t != NULL)
   {
    if (t->varName == varName)  return true;
    t = t->next;
   }
   return false;
  }

  bool existsInScope(N varName)
  {
   int i = 0;
   i = hash(varName);
   HashTableNode<T, N> *t;
   t = HTNode[i];
   while (t != NULL)
   {
    if ((t->varName == varName) && (t->Scope == currentScope())) return true;
    t = t->next;
   }
   return false;

  }

  T Val(N c)
  {
   if (exists(c))
    return (Value(c));
   else if (isNumber(c)) return c;
   else if (isStringVar(c)) return (c.Trim('\''));
   return cEmpty;
  }
  
  N getScope()
  {
   return Scope.front();
  }

  N FullVarName(N varName)
  {
   if (varName.InStr(':') > 0) return varName;
   int i = 0;
   i = hash(varName);
   HashTableNode<T, N> *t;
   t = HTNode[i];
   while (t != NULL)
   {
    if (t->varName == varName)  return t->Scope + ":" + t->varName ;
    t = t->next;
   }
   return cEmpty;
  }

 private:
  stack<N> Scope;
  N ScopeID ;
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值