32#ifndef MADNESS_WORLD_WORLDHASHMAP_H__INCLUDED
33#define MADNESS_WORLD_WORLDHASHMAP_H__INCLUDED
55 template <
class keyT,
class valueT,
class hashT>
class ConcurrentHashMap;
57 template <
class keyT,
class valueT,
class hashfunT>
58 class ConcurrentHashMap;
60 namespace Hash_private {
66 template <
typename keyT,
typename valueT>
69 typedef std::pair<const keyT, valueT>
datumT;
78 template <
class keyT,
class valueT>
82 typedef std::pair<const keyT, valueT>
datumT;
116 gotlock = result->try_lock(lockmode);
122 if (!gotlock) waiter.
wait();
136 result =
match(datum.first);
142 gotlock = result->try_lock(lockmode);
144 if (!gotlock) waiter.
wait();
148 return std::pair<entryT*,bool>(result,notfound);
151 bool del(
const keyT& key,
int lockmode) {
154 for (
entryT *t=
p,*prev=0; t; prev=t,t=t->next) {
155 if (t->datum.first == key) {
157 prev->next = t->next;
180 for (t=
p; t; t=t->next)
181 if (t->datum.first == key)
break;
190 typedef typename std::conditional<std::is_const<hashT>::value,
191 typename std::add_const<typename hashT::entryT>::type,
193 typedef typename std::conditional<std::is_const<hashT>::value,
194 typename std::add_const<typename hashT::datumT>::type,
207 template <
class otherHashT>
214 if ((
unsigned)
bin ==
h->nbins) {
246 template <
class otherHashT>
251 if (!
entry)
return *
this;
276 if (n==0 || !
entry)
return;
289 while (
unsigned(n) >=
h->bins[
bin].size()) {
290 n -=
h->bins[
bin].size();
292 if (
unsigned(
bin) ==
h->nbins) {
329 template <
class hashT,
int lockmode>
333 typedef typename std::conditional<std::is_const<hashT>::value,
334 typename std::add_const<typename hashT::entryT>::type,
336 typedef typename std::conditional<std::is_const<hashT>::value,
337 typename std::add_const<typename hashT::datumT>::type,
395 template <
class keyT,
class valueT,
class hashfunT = Hash<keyT> >
399 typedef std::pair<const keyT,valueT>
datumT;
420 static const int primes[] = {11, 23, 31, 41, 53, 61, 71, 83, 101,
421 131, 181, 239, 293, 359, 421, 557, 673, 821, 953, 1021, 1231,
422 1531, 1747, 2069, 2543, 3011, 4003, 5011, 6073, 7013, 8053,
423 9029, 9907, 17401, 27479, 37847, 48623, 59377, 70667, 81839,
424 93199, 104759, 224759, 350411, 479951, 611969, 746791, 882391,
425 1299743, 2750171, 4256257, 5800159, 7368811, 8960477, 10570871,
427 static const int nprimes =
sizeof(primes)/
sizeof(
int);
431 for (
int i=0; i<nprimes; ++i)
if (n<=primes[i])
return primes[i];
432 return primes[nprimes-1];
461 [[maybe_unused]]
auto&& [it, inserted] =
insert(*
p);
471 return std::pair<iterator,bool>(
iterator(
this,bin,result.first),result.second);
509 [[maybe_unused]]
auto erased =
try_erase(it->first);
527 if (!entry)
return end();
528 else return iterator(
this,bin,entry);
534 if (!entry)
return end();
542 bool foundit = entry;
543 if (foundit) result.
set(entry);
551 bool foundit = entry;
552 if (foundit) result.
set(entry);
560 [[nodiscard]]
size_t size()
const {
568 return it.first->second;
598 for (
unsigned int i=0; i<
nbins; ++i) {
599 if (i && (i%10)==0) printf(
"\n");
609 template <
typename hashT,
typename distT>
615 template <
typename hashT>
Disables default copy constructor and assignment operators.
Definition nodefaults.h:49
Definition worldhashmap.h:396
bool find(accessor &result, const keyT &key)
Definition worldhashmap.h:538
hashfunT hashfun
Definition worldhashmap.h:415
const size_t nbins
Definition worldhashmap.h:411
bool insert(accessor &result, const keyT &key)
Returns true if new pair was inserted; false if key is already in the map.
Definition worldhashmap.h:493
ConcurrentHashMap(const hashT &h)
Definition worldhashmap.h:445
Hash_private::HashAccessor< const hashT, entryT::READLOCK > const_accessor
Definition worldhashmap.h:405
void erase(const_accessor &item)
Definition worldhashmap.h:518
size_t size() const
Definition worldhashmap.h:560
ConcurrentHashMap< keyT, valueT, hashfunT > hashT
Definition worldhashmap.h:398
bool insert(const_accessor &result, const datumT &datum)
Returns true if new pair was inserted; false if key is already in the map and the datum was not inser...
Definition worldhashmap.h:484
hashT & operator=(const hashT &h)
Definition worldhashmap.h:456
const_iterator cbegin() const
Definition worldhashmap.h:579
Hash_private::HashAccessor< hashT, entryT::WRITELOCK > accessor
Definition worldhashmap.h:404
bool find(const_accessor &result, const keyT &key) const
Definition worldhashmap.h:547
void print_stats() const
Definition worldhashmap.h:597
const_iterator cend() const
Definition worldhashmap.h:591
static int nbins_prime(int n)
Definition worldhashmap.h:419
Hash_private::bin< keyT, valueT > binT
Definition worldhashmap.h:401
void erase(accessor &item)
Definition worldhashmap.h:513
Hash_private::HashIterator< const hashT > const_iterator
Definition worldhashmap.h:403
const_iterator begin() const
Definition worldhashmap.h:575
Hash_private::entry< keyT, valueT > entryT
Definition worldhashmap.h:400
const_iterator find(const keyT &key) const
Definition worldhashmap.h:531
iterator begin()
Definition worldhashmap.h:571
void erase(const iterator &it)
Definition worldhashmap.h:507
std::pair< iterator, bool > insert(const datumT &datum)
Definition worldhashmap.h:468
bool insert(accessor &result, const datumT &datum)
Returns true if new pair was inserted; false if key is already in the map and the datum was not inser...
Definition worldhashmap.h:475
const_iterator end() const
Definition worldhashmap.h:587
binT * bins
Definition worldhashmap.h:412
valueT & operator[](const keyT &key)
Definition worldhashmap.h:566
ConcurrentHashMap(int n=1021, const hashfunT &hf=hashfunT())
Definition worldhashmap.h:440
bool insert(const_accessor &result, const keyT &key)
Returns true if new pair was inserted; false if key is already in the map.
Definition worldhashmap.h:498
bool try_erase(const keyT &key)
Definition worldhashmap.h:502
iterator end()
Definition worldhashmap.h:583
hashfunT & get_hash() const
Definition worldhashmap.h:595
unsigned int hash_to_bin(const keyT &key) const
Definition worldhashmap.h:435
virtual ~ConcurrentHashMap()
Definition worldhashmap.h:452
Hash_private::HashIterator< hashT > iterator
Definition worldhashmap.h:402
iterator find(const keyT &key)
Definition worldhashmap.h:524
void clear()
Definition worldhashmap.h:556
std::pair< const keyT, valueT > datumT
Definition worldhashmap.h:399
Definition worldhashmap.h:330
datumT & operator*() const
Definition worldhashmap.h:370
datumT & reference
Definition worldhashmap.h:341
void convert_read_lock_to_write_lock()
Definition worldhashmap.h:360
void release()
Definition worldhashmap.h:380
void unset()
Used by Hash after having already released lock and deleted entry.
Definition worldhashmap.h:355
datumT value_type
Definition worldhashmap.h:339
void set(entryT *entry)
Used by Hash to set entry (assumed that it has the lock already)
Definition worldhashmap.h:348
~HashAccessor()
Definition worldhashmap.h:388
datumT * pointer
Definition worldhashmap.h:340
std::conditional< std::is_const< hashT >::value, typenamestd::add_const< typenamehashT::datumT >::type, typenamehashT::datumT >::type datumT
Definition worldhashmap.h:338
std::conditional< std::is_const< hashT >::value, typenamestd::add_const< typenamehashT::entryT >::type, typenamehashT::entryT >::type entryT
Definition worldhashmap.h:335
entryT * entry
Definition worldhashmap.h:344
datumT * operator->() const
Definition worldhashmap.h:375
HashAccessor()
Definition worldhashmap.h:366
HashAccessor(entryT *entry)
Definition worldhashmap.h:368
bool gotlock
Definition worldhashmap.h:345
iterator for hash
Definition worldhashmap.h:188
datumT * pointer
Definition worldhashmap.h:199
pointer operator->() const
Definition worldhashmap.h:322
HashIterator()
Makes invalid iterator.
Definition worldhashmap.h:226
bool operator!=(const HashIterator &a) const
Definition worldhashmap.h:312
int distance(const HashIterator &other) const
Difference between iterators only supported for this=start and other=end.
Definition worldhashmap.h:267
int bin
Definition worldhashmap.h:204
datumT value_type
Definition worldhashmap.h:197
std::conditional< std::is_const< hashT >::value, typenamestd::add_const< typenamehashT::entryT >::type, typenamehashT::entryT >::type entryT
Definition worldhashmap.h:192
HashIterator operator++(int)
Definition worldhashmap.h:257
HashIterator(const HashIterator< otherHashT > &other)
Implicit conversion of another hash type to this hash type.
Definition worldhashmap.h:247
HashIterator(const HashIterator &other)
Copy constructor.
Definition worldhashmap.h:239
reference operator*() const
Definition worldhashmap.h:316
void advance(int n)
Only positive increments are supported.
Definition worldhashmap.h:275
std::forward_iterator_tag iterator_category
Definition worldhashmap.h:196
entryT * entry
Definition worldhashmap.h:205
HashIterator & operator++()
Definition worldhashmap.h:250
bool operator==(const HashIterator &a) const
Definition worldhashmap.h:308
void next_non_null_entry()
If the entry is null (end of current bin) finds next non-empty bin.
Definition worldhashmap.h:211
std::conditional< std::is_const< hashT >::value, typenamestd::add_const< typenamehashT::datumT >::type, typenamehashT::datumT >::type datumT
Definition worldhashmap.h:195
HashIterator(hashT *h, int bin, entryT *entry)
Makes iterator to specific entry.
Definition worldhashmap.h:235
hashT * h
Definition worldhashmap.h:203
std::ptrdiff_t difference_type
Definition worldhashmap.h:198
datumT & reference
Definition worldhashmap.h:200
HashIterator(hashT *h, bool begin)
Makes begin/end iterator.
Definition worldhashmap.h:229
Definition worldhashmap.h:79
entryT * p
Definition worldhashmap.h:87
void clear()
Definition worldhashmap.h:96
entry< keyT, valueT > entryT
Definition worldhashmap.h:81
bool del(const keyT &key, int lockmode)
Definition worldhashmap.h:151
std::size_t size() const
Definition worldhashmap.h:173
entryT * find(const keyT &key, const int lockmode) const
Definition worldhashmap.h:108
std::pair< entryT *, bool > insert(const datumT &datum, int lockmode)
Definition worldhashmap.h:129
int ninbin
Definition worldhashmap.h:88
std::pair< const keyT, valueT > datumT
Definition worldhashmap.h:82
entryT * match(const keyT &key) const
Definition worldhashmap.h:178
~bin()
Definition worldhashmap.h:92
bin()
Definition worldhashmap.h:90
Definition worldhashmap.h:67
datumT datum
Definition worldhashmap.h:70
std::pair< const keyT, valueT > datumT
Definition worldhashmap.h:69
class entry< keyT, valueT > * next
Definition worldhashmap.h:72
entry(const datumT &datum, entry< keyT, valueT > *next)
Definition worldhashmap.h:74
Definition worldmutex.h:306
void convert_read_lock_to_write_lock() const
Converts read to write lock without releasing the read lock.
Definition worldmutex.h:388
static const int WRITELOCK
Definition worldmutex.h:312
static const int READLOCK
Definition worldmutex.h:311
void unlock(int lockmode) const
Definition worldmutex.h:379
static const int NOLOCK
Definition worldmutex.h:310
Definition worldmutex.h:109
void wait()
Definition worldmutex.cc:103
Spinlock using pthread spinlock operations.
Definition worldmutex.h:253
void lock() const
Acquire the spinlock waiting if necessary.
Definition worldmutex.h:277
void unlock() const
Free a spinlock owned by this thread.
Definition worldmutex.h:287
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition derivatives.cc:72
Defines madness::MadnessException for exception handling.
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition madness_exception.h:119
#define MADNESS_ASSERT(condition)
Assert a condition that should be free of side-effects since in release builds this might be a no-op.
Definition madness_exception.h:134
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
std::size_t hashT
The hash value type.
Definition worldhash.h:145
std::string type(const PairType &n)
Definition PNOParameters.h:18
void advance(madness::Hash_private::HashIterator< hashT > &it, const distT &dist)
Definition worldhashmap.h:610
int distance(const madness::Hash_private::HashIterator< hashT > &it, const madness::Hash_private::HashIterator< hashT > &jt)
Definition worldhashmap.h:616
static const double a
Definition nonlinschro.cc:118
AtomicInt sum
Definition test_atomicint.cc:46
std::pair< int, double > valueT
Definition test_binsorter.cc:6
double dist(const Vector< double, 3 > v1, const Vector< double, 3 > v2)
distance between v1 and v2
Definition test_localizer.cc:38
double h(const coord_1d &r)
Definition testgconv.cc:68
const char * status[2]
Definition testperiodic.cc:43
Defines hash functions for use in distributed containers.
Implements Mutex, MutexFair, Spinlock, ConditionVariable.