/*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <boost/iterator/iterator_facade.hpp>
#include <boost/noncopyable.hpp>
#include <boost/type_traits/is_convertible.hpp>
-#include <glog/logging.h>
#include <stdexcept>
#include <functional>
#include <atomic>
-#include "folly/AtomicHashArray.h"
-#include "folly/Foreach.h"
-#include "folly/Hash.h"
-#include "folly/Likely.h"
-#include "folly/ThreadCachedInt.h"
+#include <folly/AtomicHashArray.h>
+#include <folly/Foreach.h>
+#include <folly/Hash.h>
+#include <folly/Likely.h>
+#include <folly/ThreadCachedInt.h>
namespace folly {
* make_pair everywhere), and providing both can lead to some gross
* template error messages.
*
- * - Not Allocator-aware.
+ * - The Allocator must not be stateful (a new instance will be spun up for
+ * each allocation), and its allocate() method must take a raw number of
+ * bytes.
*
* - KeyT must be a 32 bit or 64 bit atomic integer type, and you must
* define special 'locked' and 'empty' key values in the ctor
* - We don't take the Hash function object as an instance in the
* constructor.
*
- * - We don't take a Compare template parameter (since our keys must
- * be integers, and the underlying hash array here uses atomic
- * compare-and-swap instructions, we only allow normal integer
- * comparisons).
*/
// Thrown when insertion fails due to running out of space for
{}
};
-template<class KeyT, class ValueT, class HashFcn>
+template<class KeyT, class ValueT,
+ class HashFcn, class EqualFcn, class Allocator>
class AtomicHashMap : boost::noncopyable {
- typedef AtomicHashArray<KeyT, ValueT, HashFcn> SubMap;
+ typedef AtomicHashArray<KeyT, ValueT, HashFcn, EqualFcn, Allocator> SubMap;
public:
typedef KeyT key_type;
typedef ValueT mapped_type;
typedef std::pair<const KeyT, ValueT> value_type;
typedef HashFcn hasher;
- typedef std::equal_to<KeyT> key_equal;
+ typedef EqualFcn key_equal;
typedef value_type* pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
}
}
- key_equal key_eq() const { return key_eq(); }
+ key_equal key_eq() const { return key_equal(); }
hasher hash_function() const { return hasher(); }
// TODO: emplace() support would be nice.
}
iterator begin() {
- return iterator(this, 0,
+ iterator it(this, 0,
subMaps_[0].load(std::memory_order_relaxed)->begin());
- }
-
- iterator end() {
- return iterator();
+ it.checkAdvanceToNextSubmap();
+ return it;
}
const_iterator begin() const {
- return const_iterator(this, 0,
+ const_iterator it(this, 0,
subMaps_[0].load(std::memory_order_relaxed)->begin());
+ it.checkAdvanceToNextSubmap();
+ return it;
+ }
+
+ iterator end() {
+ return iterator();
}
const_iterator end() const {
struct SimpleRetT { uint32_t i; size_t j; bool success;
SimpleRetT(uint32_t ii, size_t jj, bool s) : i(ii), j(jj), success(s) {}
- SimpleRetT() {}
+ SimpleRetT() = default;
};
template <class T>
SimpleRetT findInternal(const KeyT k) const;
- SimpleRetT findAtInternal(const uint32_t idx) const;
+ SimpleRetT findAtInternal(uint32_t idx) const;
std::atomic<SubMap*> subMaps_[kNumSubMaps_];
std::atomic<uint32_t> numMapsAllocated_;
} // namespace folly
-#include "AtomicHashMap-inl.h"
+#include <folly/AtomicHashMap-inl.h>
#endif // FOLLY_ATOMICHASHMAP_H_