2010-09-15 01:33:11 +04:00
|
|
|
#ifndef UTIL_SORTED_UNIFORM__
|
|
|
|
#define UTIL_SORTED_UNIFORM__
|
2010-09-10 04:36:07 +04:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstddef>
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
#include <assert.h>
|
2010-09-10 04:36:07 +04:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
2011-06-27 02:21:44 +04:00
|
|
|
template <class T> class IdentityAccessor {
|
|
|
|
public:
|
|
|
|
typedef T Key;
|
|
|
|
T operator()(const uint64_t *in) const { return *in; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Pivot64 {
|
|
|
|
static inline std::size_t Calc(uint64_t off, uint64_t range, std::size_t width) {
|
|
|
|
std::size_t ret = static_cast<std::size_t>(static_cast<float>(off) / static_cast<float>(range) * static_cast<float>(width));
|
|
|
|
// Cap for floating point rounding
|
|
|
|
return (ret < width) ? ret : width - 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Use when off * width is <2^64. This is guaranteed when each of them is actually a 32-bit value.
|
|
|
|
struct Pivot32 {
|
|
|
|
static inline std::size_t Calc(uint64_t off, uint64_t range, uint64_t width) {
|
|
|
|
return static_cast<std::size_t>((off * width) / (range + 1));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Usage: PivotSelect<sizeof(DataType)>::T
|
|
|
|
template <unsigned> struct PivotSelect;
|
|
|
|
template <> struct PivotSelect<8> { typedef Pivot64 T; };
|
|
|
|
template <> struct PivotSelect<4> { typedef Pivot32 T; };
|
|
|
|
template <> struct PivotSelect<2> { typedef Pivot32 T; };
|
|
|
|
|
|
|
|
/* Binary search. */
|
|
|
|
template <class Iterator, class Accessor> bool BinaryFind(
|
|
|
|
const Accessor &accessor,
|
|
|
|
Iterator begin,
|
|
|
|
Iterator end,
|
|
|
|
const typename Accessor::Key key, Iterator &out) {
|
|
|
|
while (end > begin) {
|
|
|
|
Iterator pivot(begin + (end - begin) / 2);
|
|
|
|
typename Accessor::Key mid(accessor(pivot));
|
|
|
|
if (mid < key) {
|
|
|
|
begin = pivot + 1;
|
|
|
|
} else if (mid > key) {
|
|
|
|
end = pivot;
|
|
|
|
} else {
|
|
|
|
out = pivot;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2010-09-10 04:36:07 +04:00
|
|
|
}
|
2010-09-15 01:33:11 +04:00
|
|
|
|
2011-05-23 06:23:01 +04:00
|
|
|
// Search the range [before_it + 1, after_it - 1] for key.
|
|
|
|
// Preconditions:
|
|
|
|
// before_v <= key <= after_v
|
|
|
|
// before_v <= all values in the range [before_it + 1, after_it - 1] <= after_v
|
|
|
|
// range is sorted.
|
2011-06-27 02:21:44 +04:00
|
|
|
template <class Iterator, class Accessor, class Pivot> bool BoundedSortedUniformFind(
|
|
|
|
const Accessor &accessor,
|
|
|
|
Iterator before_it, typename Accessor::Key before_v,
|
|
|
|
Iterator after_it, typename Accessor::Key after_v,
|
|
|
|
const typename Accessor::Key key, Iterator &out) {
|
2011-06-28 01:20:42 +04:00
|
|
|
while (after_it > before_it + 1) {
|
2011-06-27 02:21:44 +04:00
|
|
|
Iterator pivot(before_it + (1 + Pivot::Calc(key - before_v, after_v - before_v, after_it - before_it - 1)));
|
|
|
|
typename Accessor::Key mid(accessor(pivot));
|
2011-05-23 06:23:01 +04:00
|
|
|
if (mid < key) {
|
|
|
|
before_it = pivot;
|
|
|
|
before_v = mid;
|
|
|
|
} else if (mid > key) {
|
|
|
|
after_it = pivot;
|
|
|
|
after_v = mid;
|
|
|
|
} else {
|
|
|
|
out = pivot;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-27 02:21:44 +04:00
|
|
|
template <class Iterator, class Accessor, class Pivot> bool SortedUniformFind(const Accessor &accessor, Iterator begin, Iterator end, const typename Accessor::Key key, Iterator &out) {
|
2010-09-15 01:33:11 +04:00
|
|
|
if (begin == end) return false;
|
2011-06-27 02:21:44 +04:00
|
|
|
typename Accessor::Key below(accessor(begin));
|
2010-09-15 01:33:11 +04:00
|
|
|
if (key <= below) {
|
|
|
|
if (key == below) { out = begin; return true; }
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Make the range [begin, end].
|
|
|
|
--end;
|
2011-06-27 02:21:44 +04:00
|
|
|
typename Accessor::Key above(accessor(end));
|
2010-09-15 01:33:11 +04:00
|
|
|
if (key >= above) {
|
|
|
|
if (key == above) { out = end; return true; }
|
|
|
|
return false;
|
|
|
|
}
|
2011-06-27 02:21:44 +04:00
|
|
|
return BoundedSortedUniformFind<Iterator, Accessor, Pivot>(accessor, begin, below, end, above, key, out);
|
2010-09-10 04:36:07 +04:00
|
|
|
}
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
// To use this template, you need to define a Pivot function to match Key.
|
|
|
|
template <class PackingT> class SortedUniformMap {
|
2010-09-10 04:36:07 +04:00
|
|
|
public:
|
2010-09-15 01:33:11 +04:00
|
|
|
typedef PackingT Packing;
|
|
|
|
typedef typename Packing::ConstIterator ConstIterator;
|
2011-01-25 22:11:48 +03:00
|
|
|
typedef typename Packing::MutableIterator MutableIterator;
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2011-06-27 02:21:44 +04:00
|
|
|
struct Accessor {
|
|
|
|
public:
|
|
|
|
typedef typename Packing::Key Key;
|
|
|
|
const Key &operator()(const ConstIterator &i) const { return i->GetKey(); }
|
|
|
|
Key &operator()(const MutableIterator &i) const { return i->GetKey(); }
|
|
|
|
};
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
// Offer consistent API with probing hash.
|
2010-09-27 04:57:11 +04:00
|
|
|
static std::size_t Size(std::size_t entries, float /*ignore*/ = 0.0) {
|
2010-09-15 01:33:11 +04:00
|
|
|
return sizeof(uint64_t) + entries * Packing::kBytes;
|
2010-09-10 04:36:07 +04:00
|
|
|
}
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
SortedUniformMap()
|
|
|
|
#ifdef DEBUG
|
|
|
|
: initialized_(false), loaded_(false)
|
|
|
|
#endif
|
|
|
|
{}
|
|
|
|
|
2010-10-27 21:50:40 +04:00
|
|
|
SortedUniformMap(void *start, std::size_t /*allocated*/) :
|
2010-09-15 01:33:11 +04:00
|
|
|
begin_(Packing::FromVoid(reinterpret_cast<uint64_t*>(start) + 1)),
|
|
|
|
end_(begin_), size_ptr_(reinterpret_cast<uint64_t*>(start))
|
|
|
|
#ifdef DEBUG
|
|
|
|
, initialized_(true), loaded_(false)
|
|
|
|
#endif
|
|
|
|
{}
|
|
|
|
|
|
|
|
void LoadedBinary() {
|
|
|
|
#ifdef DEBUG
|
|
|
|
assert(initialized_);
|
|
|
|
assert(!loaded_);
|
|
|
|
loaded_ = true;
|
|
|
|
#endif
|
|
|
|
// Restore the size.
|
|
|
|
end_ = begin_ + *size_ptr_;
|
|
|
|
}
|
2010-09-10 04:36:07 +04:00
|
|
|
|
|
|
|
// Caller responsible for not exceeding specified size. Do not call after FinishedInserting.
|
2010-09-15 01:33:11 +04:00
|
|
|
template <class T> void Insert(const T &t) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
assert(initialized_);
|
|
|
|
assert(!loaded_);
|
|
|
|
#endif
|
|
|
|
*end_ = t;
|
2010-09-10 04:36:07 +04:00
|
|
|
++end_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FinishedInserting() {
|
2010-09-15 01:33:11 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
assert(initialized_);
|
|
|
|
assert(!loaded_);
|
|
|
|
loaded_ = true;
|
|
|
|
#endif
|
|
|
|
std::sort(begin_, end_);
|
|
|
|
*size_ptr_ = (end_ - begin_);
|
2010-09-10 04:36:07 +04:00
|
|
|
}
|
|
|
|
|
2011-01-25 22:11:48 +03:00
|
|
|
// Don't use this to change the key.
|
|
|
|
template <class Key> bool UnsafeMutableFind(const Key key, MutableIterator &out) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
assert(initialized_);
|
|
|
|
assert(loaded_);
|
|
|
|
#endif
|
2011-06-27 02:21:44 +04:00
|
|
|
return SortedUniformFind<MutableIterator, Accessor, Pivot64>(begin_, end_, key, out);
|
2011-01-25 22:11:48 +03:00
|
|
|
}
|
|
|
|
|
2010-09-10 04:36:07 +04:00
|
|
|
// Do not call before FinishedInserting.
|
2010-09-15 01:33:11 +04:00
|
|
|
template <class Key> bool Find(const Key key, ConstIterator &out) const {
|
|
|
|
#ifdef DEBUG
|
|
|
|
assert(initialized_);
|
|
|
|
assert(loaded_);
|
|
|
|
#endif
|
2011-06-27 02:21:44 +04:00
|
|
|
return SortedUniformFind<ConstIterator, Accessor, Pivot64>(Accessor(), ConstIterator(begin_), ConstIterator(end_), key, out);
|
2010-09-10 04:36:07 +04:00
|
|
|
}
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
ConstIterator begin() const { return begin_; }
|
|
|
|
ConstIterator end() const { return end_; }
|
|
|
|
|
2010-09-10 04:36:07 +04:00
|
|
|
private:
|
2010-09-15 01:33:11 +04:00
|
|
|
typename Packing::MutableIterator begin_, end_;
|
|
|
|
uint64_t *size_ptr_;
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool initialized_;
|
|
|
|
bool loaded_;
|
|
|
|
#endif
|
2010-09-10 04:36:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace util
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
#endif // UTIL_SORTED_UNIFORM__
|