tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
tm_hash_set.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 /* tm_hash_set.hpp
12  *
13  * Simple hash-based set.
14  * Element type must be the same size as unsigned long.
15  * Currently has only built-in hash function, which is designed to work
16  * well for pointers.
17  */
18 
19 #ifndef TM_HASH_SET_HPP__
20 #define TM_HASH_SET_HPP__
21 
22 #include "tm_set.hpp"
23 #include "tm_list_set.hpp"
24 
25 template<typename T>
26 class tm_hash_set : public tm_set<T>
27 {
30 
31  tm_hash_set(const tm_hash_set&);
32  // no implementation; forbids passing by value
34  // no implementation; forbids copying
35 
36  // Hash function that should work reasonably well for pointers.
37  // Basically amounts to cache line number.
38  //
39  TRANSACTION_SAFE unsigned long hash(T item)
40  {
41  // verbose attributing to avoid gcc error
42  union {
43  T from;
44  unsigned long to;
45  } cast = { item };
46  return (cast.to >> 6) % num_buckets;
47  }
48 
49  public:
50  TRANSACTION_SAFE virtual void insert(const T item)
51  {
52  int b = hash(item);
53  bucket[b]->insert(item);
54  }
55 
56  TRANSACTION_SAFE virtual void remove(const T item)
57  {
58  int b = hash(item);
59  bucket[b]->remove(item);
60  }
61 
62  virtual bool lookup(const T item)
63  {
64  return bucket[hash(item)]->lookup(item);
65  }
66 
67  virtual void apply_to_all(void (*f)(T item))
68  {
69  for (int i = 0; i < num_buckets; i++)
70  bucket[i]->apply_to_all(f);
71  }
72 
73  tm_hash_set(int capacity)
74  {
75  // get space for buckets (load factor 1.5)
76  num_buckets = capacity + (capacity >> 1);
77 
79  for (int i = 0; i < num_buckets; i++)
80  bucket[i] = new tm_list_set<T>();
81  }
82 
83  // Destruction not currently supported.
84  virtual ~tm_hash_set() { assert(false); }
85 
86  // for debugging (non-thread-safe):
87  void print_stats()
88  {
89  for (int b = 0; b < num_buckets; b++) {
90  if (b % 8 == 0)
91  cout << "\n";
92  cout << "\t" << bucket[b]->size();
93  }
94  if (num_buckets % 8 != 0)
95  cout << "\n";
96  }
97 };
98 
99 #endif // TM_HASH_SET_HPP__
tm_list_set< T > ** bucket
Definition: tm_hash_set.hpp:28
int num_buckets
Definition: tm_hash_set.hpp:29
virtual void apply_to_all(void(*f)(T item))
Definition: tm_hash_set.hpp:67
tm_hash_set & operator=(const tm_hash_set &)
Definition: tm_set.hpp:20
Definition: tm_hash_set.hpp:26
#define TRANSACTION_SAFE
Definition: common.hpp:87
tm_hash_set(const tm_hash_set &)
virtual TRANSACTION_SAFE void insert(const T item)
Definition: tm_hash_set.hpp:50
virtual ~tm_hash_set()
Definition: tm_hash_set.hpp:84
virtual bool lookup(const T item)
Definition: tm_hash_set.hpp:62
void print_stats()
Definition: tm_hash_set.hpp:87
tm_hash_set(int capacity)
Definition: tm_hash_set.hpp:73
Definition: tm_list_set.hpp:25
TRANSACTION_SAFE unsigned long hash(T item)
Definition: tm_hash_set.hpp:39