1 #ifndef _FOLLY_MAP_TEST_H
2 #define _FOLLY_MAP_TEST_H
4 #include <folly/concurrency/ConcurrentHashMap.h>
5 #include <folly/AtomicHashMap.h>
6 #include <folly/AtomicUnorderedMap.h>
8 #include <cds_test/stress_test.h>
9 #include <cds_test/stress_test_util.h>
17 namespace folly_test {
19 typedef folly::ConcurrentHashMap<size_t, size_t> ConcurrentHashMap;
20 typedef folly::AtomicHashMap<size_t, size_t> AtomicHashMap;
21 typedef folly::AtomicUnorderedInsertMap64<size_t, size_t>
22 AtomicUnorderedInsertMap;
24 // AtomicUnorderedInsertMap
25 template <typename Key, typename Value>
26 bool map_insert(AtomicUnorderedInsertMap* map, Key key, Value value) {
27 auto iter = map->find(key);
28 if (iter == map->cend() || iter->second != value) {
29 // Insert/update the <key,value> pair
30 map->emplace(key, value);
38 template <typename Key, typename Value>
39 bool map_insert(AtomicHashMap* map, Key key, Value value) {
40 auto iter = map->find(key);
41 if (iter == map->end() || iter->second != value) {
42 // Insert/update the <key,value> pair
43 map->insert({key, value});
51 template <typename Key, typename Value>
52 bool map_insert(ConcurrentHashMap * map, Key key, Value value) {
53 auto iter = map->find(key);
54 if (iter == map->cend() || iter->second != value) {
55 // Insert/update the <key,value> pair
56 map->insert({key, value});
63 template <typename Map, typename Key>
64 bool map_find(const Map* map, Key key) {
65 return map->find(key) != map->cend();
68 // Specialization for AtomicHashMap
69 template <typename Key>
70 bool map_find(const AtomicHashMap* map, Key key) {
71 return map->find(key) != map->end();
74 // Doesn't erase for AtomicUnorderedInsertMap & AtomicHashMap, but returns
75 // whether the key exists in the map.
76 template <typename Map, typename Key>
77 bool map_delete(Map* map, Key key) {
78 return map_find(map, key);
81 // Specialization for ConcurrentHashMap
82 template <typename Key>
83 bool map_delete(ConcurrentHashMap* map, Key key) {
84 if (map_find(map, key)) {
92 } // namespace folly_test