バナーをクリックすれば目次に戻ります
Copyright 1999 Rogue Wave Software
Copyright 1999 Sun Microsystems, Inc.
RWHashTable
RWHashTable RWCollection
RWCollectable
#include <rw/hashtab.h> RWHashTable h ;
このクラスによって格納されるオブジェクトは、仮想関数 hash() および isEqual() を適切に定義した、抽象基底クラス RWCollectable から継承しなければなりません (RWCollectable を参照してください)。
キーと一致するオブジェクトを見つけるには、キーの仮想関数 hash() を最初に呼び出して、オブジェクトの存在するバケットを判定しなければなりません。次に、各オブジェクトに対し、キーを引数として仮想関数 isEqual() を呼び出し、そのバケットを線形探索します。TRUE を返した最初のオブジェクトを返します。
ハッシュテーブルの初期バケット数はコンストラクタで設定します。これには、デフォルト値があります。コレクション内の項目数がバケット数よりもずっと多い場合は、各バケットを線形探索するため、効率は下がります。バケット数は、メンバー関数 resize() を呼び出して変更できます。変更するためには、すべてのオブジェクトを再ハッシュする必要があります。
このクラスの反復子は、RWHashTableIterator です。
#include <rw/hashtab.h> #include <rw/colldate.h> #include <rw/rstream.h> main(){ RWHashTable table; RWCollectableDate *july = new RWCollectableDate(7, "July", 1990); RWCollectableDate *may = new RWCollectableDate (1, "May", 1977); RWCollectableDate *feb = new RWCollectableDate (22, "Feb", 1983); RWCollectableDate *aug = new RWCollectableDate (2, "Aug", 1966); table.insert(july); table.insert(may); table.insert(feb); table.insert(aug); cout << "Table contains " << table.entries() << " entries.\n"; RWCollectableDate key(22, "Feb", 1983); cout << "It does "; if (!table.contains(&key)) cout << "not "; cout << "contain the key " << key << endl; delete july; delete may; delete feb; delete aug; return 0; } |
Table contains 4 entries. It does contain the key February 22, 1983 |
RWHashTable(size_t N = RWCollection::DEFAULT_CAPACITY);
RWHashTable(const RWHashTable& t);
void operator=(const RWHashTable& t);
RWBoolean operator==(const RWHashTable& t) const;
RWBoolean operator<=(const RWHashTable& t) const;
RWBoolean operator!=(const RWHashTable&) const;
virtual void apply(RWapplyCollectable ap, void*);
virtual RWspace binaryStoreSize() const;
virtual void clear();
virtual void clearAndDestroy();
virtual int compareTo(const RWCollectable*) const;
virtual RWBoolean contains(const RWCollectable*) const;
virtual size_t entries() const;
virtual RWCollectable* find(const RWCollectable*) const;
virtual unsigned hash() const;
virtual RWCollectable* insert(RWCollectable* a);
virtual RWClassID isA() const;
virtual RWBoolean isEmpty() const;
virtual RWBoolean isEqual(const RWCollectable*) const;
virtual RWCollectable* newSpecies() const;
virtual size_t occurrencesOf(const RWCollectable*) const;
virtual RWCollectable* remove(const RWCollectable*);
virtual void removeAndDestroy(const RWCollectable*);
virtual void resize(size_t n = 0);
virtual void restoreGuts(RWvistream&); virtual void restoreGuts(RWFile&); virtual void saveGuts(RWvostream&) const; virtual void saveGuts(RWFile&) const;
RWStringID stringID();