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 ;
};
哈希表类模板的设计与实现
最新推荐文章于 2023-12-23 12:14:32 发布