137b74a387bb3993387029859c2d9d051c41c724eStephen Hines//===- HashTable.h --------------------------------------------------------===// 25460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// 35460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// The MCLinker Project 45460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// 55460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// This file is distributed under the University of Illinois Open Source 65460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// License. See LICENSE.TXT for details. 75460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// 85460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===----------------------------------------------------------------------===// 937b74a387bb3993387029859c2d9d051c41c724eStephen Hines#ifndef MCLD_ADT_HASHTABLE_H_ 1037b74a387bb3993387029859c2d9d051c41c724eStephen Hines#define MCLD_ADT_HASHTABLE_H_ 1137b74a387bb3993387029859c2d9d051c41c724eStephen Hines 1237b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/ADT/HashBase.h" 1337b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/ADT/HashEntryFactory.h" 1437b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/ADT/HashIterator.h" 1537b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/ADT/TypeTraits.h" 1637b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Support/Allocators.h" 1737b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Support/Compiler.h" 1837b74a387bb3993387029859c2d9d051c41c724eStephen Hines 195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <utility> 205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 2122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaonamespace mcld { 225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/** \class HashTable 245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * \brief HashTable is a hash table which follows boost::unordered_map, but it 255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * is open addressing and can linear probing. 265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * 275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * mcld::HashTable is a linear probing hash table. It does not allocate 285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * the memory space of the entries by itself. Instead, entries are allocated 295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * outside and then emplaced into the hash table. 305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */ 3137b74a387bb3993387029859c2d9d051c41c724eStephen Hinestemplate <typename HashEntryTy, 3237b74a387bb3993387029859c2d9d051c41c724eStephen Hines typename HashFunctionTy, 3337b74a387bb3993387029859c2d9d051c41c724eStephen Hines typename EntryFactoryTy = HashEntryFactory<HashEntryTy> > 3437b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass HashTable : public HashTableImpl<HashEntryTy, HashFunctionTy> { 3537b74a387bb3993387029859c2d9d051c41c724eStephen Hines private: 365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao typedef HashTableImpl<HashEntryTy, HashFunctionTy> BaseTy; 375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 3837b74a387bb3993387029859c2d9d051c41c724eStephen Hines public: 395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao typedef size_t size_type; 405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao typedef HashFunctionTy hasher; 415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao typedef HashEntryTy entry_type; 425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao typedef typename BaseTy::bucket_type bucket_type; 435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao typedef typename HashEntryTy::key_type key_type; 445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 4537b74a387bb3993387029859c2d9d051c41c724eStephen Hines typedef HashIterator<ChainIteratorBase<BaseTy>, NonConstTraits<HashEntryTy> > 4637b74a387bb3993387029859c2d9d051c41c724eStephen Hines chain_iterator; 475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao typedef HashIterator<ChainIteratorBase<const BaseTy>, 4837b74a387bb3993387029859c2d9d051c41c724eStephen Hines ConstTraits<HashEntryTy> > const_chain_iterator; 495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 5037b74a387bb3993387029859c2d9d051c41c724eStephen Hines typedef HashIterator<EntryIteratorBase<BaseTy>, NonConstTraits<HashEntryTy> > 5137b74a387bb3993387029859c2d9d051c41c724eStephen Hines entry_iterator; 525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao typedef HashIterator<EntryIteratorBase<const BaseTy>, 5337b74a387bb3993387029859c2d9d051c41c724eStephen Hines ConstTraits<HashEntryTy> > const_entry_iterator; 545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 5537b74a387bb3993387029859c2d9d051c41c724eStephen Hines typedef entry_iterator iterator; 5637b74a387bb3993387029859c2d9d051c41c724eStephen Hines typedef const_entry_iterator const_iterator; 575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 5837b74a387bb3993387029859c2d9d051c41c724eStephen Hines public: 595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // ----- constructor ----- // 6037b74a387bb3993387029859c2d9d051c41c724eStephen Hines explicit HashTable(size_type pSize = 3); 615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao ~HashTable(); 62551ae4ebd3e9d137ea668fb83ae4a55b8cfba451Stephen Hines 6337b74a387bb3993387029859c2d9d051c41c724eStephen Hines EntryFactoryTy& getEntryFactory() { return m_EntryFactory; } 645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // ----- modifiers ----- // 665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao void clear(); 675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao /// insert - insert a new element to the container. The element is 695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // constructed in-place, i.e. no copy or move operations are performed. 705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // If the element already exists, return the element, and set pExist true. 715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao entry_type* insert(const key_type& pKey, bool& pExist); 725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao /// erase - remove the element with the same key 745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao size_type erase(const key_type& pKey); 755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // ----- lookups ----- // 775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao /// find - finds an element with key pKey 785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // If the element does not exist, return end() 795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao iterator find(const key_type& pKey); 805460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao /// find - finds an element with key pKey, constant version 825460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // If the element does not exist, return end() 835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao const_iterator find(const key_type& pKey) const; 845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 855460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao size_type count(const key_type& pKey) const; 86551ae4ebd3e9d137ea668fb83ae4a55b8cfba451Stephen Hines 875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // ----- hash policy ----- // 885460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao float load_factor() const; 895460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 905460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao /// rehash - if the load factor is larger than 75%, or the empty buckets is 915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // less than 12.5%, the rehash the hash table 925460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao void rehash(); 935460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 945460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao /// rehash - immediately re-new the hash table to the size pCount, and 955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // rehash all elements. 965460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao void rehash(size_type pCount); 975460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 985460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // ----- iterators ----- // 995460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao iterator begin(); 1005460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao iterator end(); 1015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 1025460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao const_entry_iterator begin() const; 1035460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao const_entry_iterator end() const; 1045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 1055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao chain_iterator begin(const key_type& pKey); 1065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao chain_iterator end(const key_type& pKey); 1075460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao const_chain_iterator begin(const key_type& pKey) const; 1085460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao const_chain_iterator end(const key_type& pKey) const; 1095460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 11037b74a387bb3993387029859c2d9d051c41c724eStephen Hines private: 1115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao EntryFactoryTy m_EntryFactory; 1125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 11337b74a387bb3993387029859c2d9d051c41c724eStephen Hines private: 11437b74a387bb3993387029859c2d9d051c41c724eStephen Hines DISALLOW_COPY_AND_ASSIGN(HashTable); 1155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}; 1165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 1175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include "HashTable.tcc" 1185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 11937b74a387bb3993387029859c2d9d051c41c724eStephen Hines} // namespace mcld 120affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 12137b74a387bb3993387029859c2d9d051c41c724eStephen Hines#endif // MCLD_ADT_HASHTABLE_H_ 122