📄 hashassociation.h
字号:
/* $Id: HashAssociation.h,v 1.2 1997/02/02 01:31:04 matt Exp $ Hashed association. ** This class is obsolete since the hash table class started using (key, value) pairs. (c) Aug 95 Matt Phillips. */#ifndef _HASSOC_H#define _HASSOC_H#include "Association.h"#include "AssociationItem.h"#include "ClosedHash.h"// NOTE:// 1. this class uses technically illegal upcasting from class AK// to A class in order to generate temporary "A" objects that do not// destruct as they go out of scope. this technique is used by the// add, get and remove members where only the key will be used, since// the data member will be unitialised.template <class K, class T, class A, class AK, class HASH, int SIZE>class HashAssociationImp : public Association<K, T>{ // K = key type, T = type to be associated with key // A = assoc record type, AK = key subclass of Apublic: // translate an A * object into a K * object for HASH function // NOTE: this function needs to be defined BEFORE any function that // refers to HashAssociationImpIter because the iterator references // hash(). class Hash { public: static int hash (const A &v) { return HASH::hash (v.refKey ()); } }; typedef HashAssociationImpIter<K, T, A, AK, HASH, SIZE> Iterator; int nItems () const {return hashTable.nItems ();} int isFull () const {return hashTable.isFull ();} int isEmpty () const {return hashTable.isEmpty ();} void clear () {hashTable.clear ();} virtual T &add (K &key, T &i) { A &a = hashTable.add ((A &)AK (key)); // naughty upcast! a.set (i); return a.ref (); } virtual int remove (const K &key) {return hashTable.remove ((A &)AK (key));} // naughty upcast! virtual T *get (const K &key) const { A *a = hashTable.get ((A &)AK (key)); // naughty upcast! return a ? &(a->ref ()) : 0; } Iterator *makeIter () const {return new Iterator (*this);}protected: typedef TypeDClosedHash (A, Hash, SIZE) HashTable; HashTable hashTable; friend Iterator;};template <class K, class T, class A, class AK, class HASH, int SIZE>class HashAssociationImpIter : public AssociationIter<K, T>{public: HashAssociationImpIter (const HashAssociationImp<K, T, A, AK, HASH, SIZE> &a) : iter (a.hashTable) {} virtual void reset () {iter.reset ();} virtual operator int () const {return int (iter);} virtual void operator ++ (int) {iter++;} virtual T value () const {return iter.value ().ref ();} virtual T &ref () const {return iter.ref ().ref ();} virtual const K &getKey () const {return iter.ref ().refKey ();}protected: ClosedHashImpIter<A, HashAssociationImp<K, T, A, AK, HASH, SIZE>::Hash, SIZE, DLinkedItem<A> > iter;};#define TypeDHashAssociation(K, T, HASH, SIZE) \HashAssociationImp<K, T, DAssocItem<K, T>, DAssocKey<K>, HASH, SIZE>#define TypeIHashAssociation(K, T, HASH, SIZE, ownsK, ownsT) \HashAssociationImp<K, T, IAssocItem<K, T, ownsK, ownsT>, \IAssocKey<K>, HASH, SIZE>#define TypeIKHashAssociation(K, T, HASH, SIZE, ownsK) \HashAssociationImp<K, T, IKAssocItem<K, T, ownsK>, IAssocKey<K>, \HASH, SIZE>#define TypeITHashAssociation(K, T, HASH, SIZE, ownsT) \HashAssociationImp<K, T, ITAssocItem<K, T, ownsT>, DAssocKey<K>, \HASH, SIZE>#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -