tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Hash.hpp
Go to the documentation of this file.
1 /**
2  * Copyright (C) 2011
3  * University of Rochester Department of Computer Science
4  * and
5  * Lehigh University Department of Computer Science and Engineering
6  *
7  * License: Modified BSD
8  * Please see the file LICENSE.RSTM for licensing information
9  */
10 
11 #ifndef HASH_HPP__
12 #define HASH_HPP__
13 
14 #include "List.hpp"
15 
16 // the Hash class is an array of N_BUCKETS LinkedLists
17 class HashTable
18 {
19  static const int N_BUCKETS = 256;
20 
21  /**
22  * during a sanity check, we want to make sure that every element in a
23  * bucket actually hashes to that bucket; we do it by passing this
24  * method to the extendedSanityCheck for the bucket.
25  */
26  static bool verify_hash_function(uint32_t val, uint32_t bucket)
27  {
28  return ((val % N_BUCKETS) == bucket);
29  }
30 
31  public:
32  /**
33  * Templated type defines what kind of list we'll use at each bucket.
34  */
36 
38  void insert(int val TM_ARG)
39  {
40  bucket[val % N_BUCKETS].insert(val TM_PARAM);
41  }
42 
44  bool lookup(int val TM_ARG) const
45  {
46  return bucket[val % N_BUCKETS].lookup(val TM_PARAM);
47  }
48 
50  void remove(int val TM_ARG)
51  {
52  bucket[val % N_BUCKETS].remove(val TM_PARAM);
53  }
54 
55  bool isSane() const
56  {
57  for (int i = 0; i < N_BUCKETS; i++)
58  if (!bucket[i].extendedSanityCheck(verify_hash_function, i))
59  return false;
60  return true;
61  }
62 };
63 
64 #endif // HASH_HPP__
#define TM_CALLABLE
Definition: cxxtm.hpp:32
TM_CALLABLE void remove(int val TM_ARG)
Definition: List.hpp:179
#define TM_ARG
Definition: cxxtm.hpp:40
TM_CALLABLE bool lookup(int val TM_ARG) const
Definition: List.hpp:136
List bucket[N_BUCKETS]
Definition: Hash.hpp:35
Definition: List.hpp:20
bool isSane() const
Definition: Hash.hpp:55
#define TM_PARAM
Definition: cxxtm.hpp:42
static bool verify_hash_function(uint32_t val, uint32_t bucket)
Definition: Hash.hpp:26
TM_CALLABLE bool lookup(int val TM_ARG) const
Definition: Hash.hpp:44
TM_CALLABLE void insert(int val TM_ARG)
Definition: Hash.hpp:38
TM_CALLABLE void insert(int val TM_ARG)
Definition: List.hpp:109
static const int N_BUCKETS
Definition: Hash.hpp:19
Definition: Hash.hpp:17