atlas  0.6
hashtable.h
Go to the documentation of this file.
1 
5 /*
6  Copyright (C) 2006 Marc van Leeuwen
7  part of the Atlas of Lie Groups and Representations
8 
9  For license information see the LICENSE file
10 */
11 
12 
13 #ifndef HASHTABLE_H
14 #define HASHTABLE_H
15 
16 #include "hashtable_fwd.h"
17 #include <cstddef>
18 #include <vector>
19 
20 namespace atlas {
21 namespace hashtable {
22 
23  /* The HashTable template class has two type parameters:
24  the class Entry of values stored in the hash table, and the unsigned
25  integral type Number used for sequence numbers (whose size will give a
26  limit to the number of different Entry values that can be stored).
27 
28  The class Entry should at least have the following members
29 
30  typename Pooltype; // given by a typedef inside the class definition
31 
32  explicit Entry(Pooltype::const_reference); // |explicit| may be absent
33  size_t hashCode(size_t modulus) const;
34  bool operator!=(Pooltype::const_reference another) const;
35 
36  Here:
37 
38  Pooltype::const_reference is a type related to Entry that is returned
39  by Pooltype::operator[] (see below) and can be tested against an
40  Entry; it might be const Entry& (as it will be if Pooltype is
41  std::vector<Entry>) in which case the constructor
42  Entry(Pooltype::const_reference) will be the ordinary copy
43  constructor, or it might something more fancy in the case of
44  compacted storage (as will be used for KL polynomials) in which case
45  the mentioned constructor will need an explicit definition
46 
47  hashCode computes a hash code for the Entry in the range [0,modulus[,
48  where modulus is a power of 2
49 
50  operator!= tests inequality,
51 
52  Pooltype is a container class (for instance std::vector<Entry>),
53  such that
54 
55  typename const_reference; // is some typedef
56 
57  Pooltype(); // creates an empty store
58  void Pooltype::push_back(const Entry&); // adds entry to store
59  size_t size() const; // returns nr of entries
60  const_reference operator[] (Number i) const; // recalls entry i
61  void swap(Pooltype& other); // usual swap method
62  */
63 
64 template <class Entry, typename Number>
65 class HashTable
66 {
67  // data members
68  size_t d_mod; // hash modulus, the number of slots present
69  std::vector<Number> d_hash;
70  typename Entry::Pooltype& d_pool;
71 
72  // interface
73  public:
74 
75  // static constants
76  static const Number empty; // code for empty slot in d_hash
77  static const float fill_fraction; // (probably ought to be variable)
78 
79  // constructor
80  HashTable(typename Entry::Pooltype& pool); // caller supplies ref to pool
81 
82  // manipulator
83  Number match(const Entry&); // lookup entry and return its sequence number
84 
85  // accessors
86  Number find(const Entry&) const; // const variant of match; may return empty
87  typename Entry::Pooltype::const_reference operator[] (Number i) const
88  { return d_pool[i]; }
89  Number size() const { return Number(d_pool.size()); }
90  size_t capacity () const { return d_mod; }
91 
92  private: // auxiliary functions
93  void rehash(); // ensure d_hash is coherent with d_pool and d_mod
94  size_t max_fill() const // maximum number of stored entries before rehashing
95  { return static_cast<size_t>(fill_fraction*d_mod); }
96 
97  public: // methods to make HashTable behave like a container, in some cases
98 
99  void reconstruct(); // must call when |d_pool| has been changed by others
100 
101  typedef Number const_iterator; // type returned by begin() and end()
102  typedef const_iterator iterator; // iterators are not mutable
103 
104  const_iterator begin() const { return Number(0); }
105  const_iterator end() const { return size(); }
106 
107  void swap(HashTable& other)
108  {
109  std::swap(d_mod,other.d_mod);
110  d_hash.swap(other.d_hash);
111  d_pool.swap(other.d_pool);
112 
113  }
114 
115 }; // |class HashTable|
116 
117 } // |namespace hashtable|
118 } // |namespace atlas|
119 
120 #include "hashtable_def.h"
121 
122 #endif
Entry::Pooltype & d_pool
Definition: hashtable.h:70
size_t d_mod
Definition: hashtable.h:68
size_t capacity() const
Definition: hashtable.h:90
void swap(simple_list< T, Alloc > &x, simple_list< T, Alloc > &y)
Definition: sl_list.h:674
size_t max_fill() const
Definition: hashtable.h:94
const_iterator end() const
Definition: hashtable.h:105
void reconstruct()
Definition: hashtable_def.h:41
Number find(const Entry &) const
Definition: hashtable_def.h:51
Number const_iterator
Definition: hashtable.h:101
Number match(const Entry &)
Definition: hashtable_def.h:68
Entry::Pooltype::const_reference operator[](Number i) const
Definition: hashtable.h:87
const_iterator iterator
Definition: hashtable.h:102
const_iterator begin() const
Definition: hashtable.h:104
void swap(HashTable &other)
Definition: hashtable.h:107
Definition: Atlas.h:110
void rehash()
Definition: hashtable_def.h:23
Definition: Atlas.h:38
static const Number empty
Definition: hashtable.h:76
Number size() const
Definition: hashtable.h:89
static const float fill_fraction
Definition: hashtable.h:77
HashTable(typename Entry::Pooltype &pool)
Definition: hashtable_def.h:16
std::vector< Number > d_hash
Definition: hashtable.h:69