Interface Documentation
Version: invalid
hash_table.hh
1 
2 /*
3  @@@@@@@@ @@ @@@@@@ @@@@@@@@ @@
4  /@@///// /@@ @@////@@ @@////// /@@
5  /@@ /@@ @@@@@ @@ // /@@ /@@
6  /@@@@@@@ /@@ @@///@@/@@ /@@@@@@@@@/@@
7  /@@//// /@@/@@@@@@@/@@ ////////@@/@@
8  /@@ /@@/@@//// //@@ @@ /@@/@@
9  /@@ @@@//@@@@@@ //@@@@@@ @@@@@@@@ /@@
10  // /// ////// ////// //////// //
11  Copyright (c) 2016, Los Alamos National Security, LLC
12  All rights reserved.
13  */
14 
15 #pragma omp
16 
17 #if !defined(__FLECSI_PRIVATE__)
18 #error Do not include this file directly!
19 #endif
20 
21 #include "flecsi/run/context.hh"
23 
24 namespace flecsi {
25 namespace topo {
26 
27 //----------------------------------------------------------------------------//
30 //----------------------------------------------------------------------------//
31 
32 template<class KEY, class TYPE>
33 struct hash_table {
34 
35  using id_t = util::id_t;
36  using key_t = KEY;
37  using type_t = TYPE;
38 
39  // The number of nits used for the hash table
40  static constexpr size_t hash_bits_ = 22;
41  // The capacity of the hash table
42  static constexpr size_t hash_capacity_ = 1 << hash_bits_;
43  // The simple mask used in this function
44  static constexpr size_t hash_mask_ = (1 << hash_bits_) - 1;
45  static constexpr size_t modulo = 5;
46 
47  static size_t collision; // Collision counter
48 
53  template<typename S>
54  static type_t * find(S & index_space, key_t key) {
55  size_t h = hash(key);
56  type_t * ptr = index_space.storage()->begin() + h;
57  while(ptr->key() != key && ptr->key() != key_t::null()) {
58  h += modulo;
59  h = h >= hash_capacity_ ? h % hash_capacity_ : h;
60  ptr = index_space.storage()->begin() + h;
61  }
62  if(ptr->key() != key) {
63  return nullptr;
64  }
65  return ptr;
66  }
67 
73  template<typename S, class... ARGS>
74  static type_t * insert(S & index_space, const key_t & key, ARGS &&... args) {
75  size_t h = hash(key);
76  type_t * ptr = index_space.storage()->begin() + h;
77  while(ptr->key() != key && ptr->key() != key_t::null()) {
78  h += modulo;
79  h = h >= hash_capacity_ ? h % hash_capacity_ : h;
80  ptr = index_space.storage()->begin() + h;
81  ++collision;
82  }
83  auto b = new(ptr) type_t(key, std::forward<ARGS>(args)...);
84  return ptr;
85  }
86 
91  static size_t hash(const key_t & key) {
92  return key & hash_mask_;
93  }
94 }; // class hash_table
95 
96 template<class K, class T>
98 
99 } // namespace topo
100 } // namespace flecsi
Definition: id.hh:36
static size_t hash(const key_t &key)
the Hash function transforming a key in position in the hash table.
Definition: hash_table.hh:91
static type_t * find(S &index_space, key_t key)
Find a value in the hashtable While the value or a null key is not found we keep looping.
Definition: hash_table.hh:54
static type_t * insert(S &index_space, const key_t &key, ARGS &&... args)
Insert an object in the hash map at a defined position This function tries to find the first availabl...
Definition: hash_table.hh:74
Definition: hash_table.hh:33
Definition: index_space.hh:85
Definition: control.hh:31