一個非常不錯的哈希表的模板類 .

 
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 ;
};

 


//stack.h

template <class T>
struct node
...{
    T value;
    node<T> *next;
};

template <class T>
class stack
...{
public:
    char Err[35];
    node<T> *First;
    int Count;

    stack()...{Count = 0;strcpy(Err,"");First = NULL;}

     T Pop()
    ...{
        node<T> *tempNode;
        T tempVal;

        if (First!=NULL)
        ...{
            tempVal = First->value;
            tempNode = First;
            First = First->next;
            delete(tempNode);
        }
        else return NULL;
        return tempVal;
    }

    T front()
    ...{
        return First->value;
    }

    T Value()
    ...{
        if (First!=NULL)
        ...{
            return First->value;
        }
        return "";
    }



    void Push(T v)
    ...{
        node<T> *tempNode;
        tempNode = new node<T>;

        tempNode->value = v;
        tempNode->next = First;
        First = tempNode;
    }

    bool Last()
    ...{
        if (First!=NULL)
            if (First->next==NULL)
                return true;
    return false;
    }

    void Empty()
    ...{
        T t;
        while (First != NULL)
            t = Pop();
    }

    bool isEmpty()
    ...{
        if (First != NULL)
            return false;
        return true;
    }    

};


template <class T>
void CopyStack(stack<T>& aStack, stack<T>& bStack)    //Copy b into a
    ...{
        aStack.Empty();
        stack<T> temp;
        T a;

        while (bStack.isEmpty() == false)
            temp.Push(bStack.Pop());

        while (temp.isEmpty() == false)
        ...{
            a = temp.Pop();
            bStack.Push(a);
            aStack.Push(a);
        }
    }



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章