home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 5.0 KB | 171 lines | [TEXT/MPS ] |
- // The C++ Booch Components (Version 2.1)
- // (C) Copyright 1990-1993 Grady Booch. All Rights Reserved.
- //
- // Restricted Rights Legend
- // Use, duplication, or disclosure is subject to restrictions as set forth
- // in subdivision (c)(1)(ii) of the Rights in Technical Data and Computer
- // Software clause at DFARS 252.227-7013.
- //
- // BCHashTa.cpp
- //
- // This file contains the definition for the open hash table class.
-
- #include "BCHashTa.h"
-
- template<class Item, class Value, BC_Index Buckets, class Container>
- BC_TTable<Item, Value, Buckets, Container>::
- BC_TTable(const BC_TTable<Item, Value, Buckets, Container>& t)
- : fValidCache(0),
- fSize(0),
- fHash(t.fHash)
- {
- operator=(t);
- }
-
- template<class Item, class Value, BC_Index Buckets, class Container>
- BC_TTable<Item, Value, Buckets, Container>&
- BC_TTable<Item, Value, Buckets, Container>::
- operator=(const BC_TTable<Item, Value, Buckets, Container>& t)
- {
- if (this == &t)
- return *this;
- else {
- Clear();
- for (BC_Index bucket_index = 0; (bucket_index < Buckets); bucket_index++)
- for (BC_Index index = 0; (index < t.fRep[bucket_index].Length()); index++) {
- BC_Index bucket = fHash(t.fRep[bucket_index][index].fItem) % Buckets;
- fRep[bucket].Append(t.fRep[bucket_index][index]);
- }
- fSize = t.fSize;
- return *this;
- }
- }
-
- template<class Item, class Value, BC_Index Buckets, class Container>
- BC_Boolean BC_TTable<Item, Value, Buckets, Container>::
- operator==(const BC_TTable<Item, Value, Buckets, Container>& t) const
- {
- if (this == &t)
- return 1;
- else {
- if (fSize == t.fSize) {
- for (BC_Index bucket = 0; (bucket < Buckets); bucket++)
- for (BC_Index index = 0; (index < fRep[bucket].Length()); index++) {
- const BC_TPair<Item, Value>* pair =
- &fRep[bucket].ItemAt(index);
- if (!t.IsBound(pair->fItem) ||
- (pair->fValue != *(t.ValueOf(pair->fItem))))
- return 0;
- }
- return 1;
- }
- return 0;
- }
- }
-
- template<class Item, class Value, BC_Index Buckets, class Container>
- void BC_TTable<Item, Value, Buckets, Container>::Clear()
- {
- for (BC_Index index = 0; (index < Buckets); index++)
- fRep[index].Clear();
- fValidCache = 0;
- fSize = 0;
- }
-
- template<class Item, class Value, BC_Index Buckets, class Container>
- BC_Boolean BC_TTable<Item, Value, Buckets, Container>::
- Bind(const Item& item, const Value& value)
- {
- if (fValidCache && (fCache.fItem == item))
- return 0;
- else {
- BC_Index bucket = fHash(item) % Buckets;
- BC_ExtendedIndex index =
- fRep[bucket].Location((BC_TPair<Item, Value>)item);
- if (index != -1)
- return 0;
- else {
- fCache = BC_TPair<Item, Value>(item, value);
- fValidCache = 1;
- fRep[bucket].Insert(fCache);
- fSize++;
- }
- return 1;
- }
- }
-
- template<class Item, class Value, BC_Index Buckets, class Container>
- BC_Boolean BC_TTable<Item, Value, Buckets, Container>::
- Rebind(const Item& item, const Value& value)
- {
- BC_Index bucket = fHash(item) % Buckets;
- BC_ExtendedIndex index =
- fRep[bucket].Location((BC_TPair<Item, Value>)item);
- if (index != -1) {
- fCache = BC_TPair<Item, Value>(item, value);
- fValidCache = 1;
- BC_TPair<Item, Value>* pair = &fRep[bucket].ItemAt(index);
- pair->fValue = value;
- return 1;
- }
- return 0;
- }
-
- template<class Item, class Value, BC_Index Buckets, class Container>
- BC_Boolean BC_TTable<Item, Value, Buckets, Container>::Unbind(const Item& item)
- {
- BC_Index bucket = fHash(item) % Buckets;
- BC_ExtendedIndex index = fRep[bucket].Location(item);
- if (index != -1) {
- if (fValidCache && (fCache.fItem == item))
- fValidCache = 0;
- fRep[bucket].Remove(index);
- fSize--;
- return 1;
- }
- return 0;
- }
-
- template<class Item, class Value, BC_Index Buckets, class Container>
- BC_Boolean BC_TTable<Item, Value, Buckets, Container>::
- IsBound(const Item& item) const
- {
- if (fValidCache && (fCache.fItem == item))
- return 1;
- else {
- BC_Index bucket =
- ((BC_TTable<Item, Value, Buckets, Container>&)*this).
- fHash(item) % Buckets;
- BC_ExtendedIndex index = fRep[bucket].Location(item);
- if (index != -1) {
- ((BC_TTable<Item, Value, Buckets, Container>&)*this).fCache =
- fRep[bucket].ItemAt(index);
- ((BC_TTable<Item, Value, Buckets, Container>&)*this).
- fValidCache = 1;
- return 1;
- }
- return 0;
- }
- }
-
- template<class Item, class Value, BC_Index Buckets, class Container>
- const Value* BC_TTable<Item, Value,Buckets, Container>::ValueOf(const Item& item) const
- {
- if (fValidCache && (fCache.fItem == item))
- return &fCache.fValue;
- else {
- BC_Index bucket =
- ((BC_TTable<Item, Value, Buckets, Container>&)*this).
- fHash(item) % Buckets;
- BC_ExtendedIndex index = fRep[bucket].Location(item);
- if (index != -1) {
- ((BC_TTable<Item, Value, Buckets, Container>&)*this).fCache =
- fRep[bucket].ItemAt(index);
- ((BC_TTable<Item, Value, Buckets, Container>&)*this).
- fValidCache = 1;
- return &fRep[bucket][index].fValue;
- }
- return 0;
- }
- }
-