2011-09-21 20:06:48 +04:00
|
|
|
#ifndef UTIL_SIZED_ITERATOR__
|
|
|
|
#define UTIL_SIZED_ITERATOR__
|
|
|
|
|
|
|
|
#include "util/proxy_iterator.hh"
|
|
|
|
|
2013-05-19 18:12:06 +04:00
|
|
|
#include <algorithm>
|
2011-09-21 20:06:48 +04:00
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
|
2011-11-12 02:39:27 +04:00
|
|
|
#include <stdint.h>
|
2011-09-21 20:06:48 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
|
|
|
class SizedInnerIterator {
|
|
|
|
public:
|
|
|
|
SizedInnerIterator() {}
|
|
|
|
|
|
|
|
SizedInnerIterator(void *ptr, std::size_t size) : ptr_(static_cast<uint8_t*>(ptr)), size_(size) {}
|
|
|
|
|
|
|
|
bool operator==(const SizedInnerIterator &other) const {
|
|
|
|
return ptr_ == other.ptr_;
|
|
|
|
}
|
|
|
|
bool operator<(const SizedInnerIterator &other) const {
|
|
|
|
return ptr_ < other.ptr_;
|
|
|
|
}
|
|
|
|
SizedInnerIterator &operator+=(std::ptrdiff_t amount) {
|
|
|
|
ptr_ += amount * size_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
std::ptrdiff_t operator-(const SizedInnerIterator &other) const {
|
|
|
|
return (ptr_ - other.ptr_) / size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const void *Data() const { return ptr_; }
|
|
|
|
void *Data() { return ptr_; }
|
|
|
|
std::size_t EntrySize() const { return size_; }
|
|
|
|
|
2014-01-28 04:51:35 +04:00
|
|
|
friend void swap(SizedInnerIterator &first, SizedInnerIterator &second) {
|
2013-06-24 19:05:47 +04:00
|
|
|
std::swap(first.ptr_, second.ptr_);
|
|
|
|
std::swap(first.size_, second.size_);
|
|
|
|
}
|
|
|
|
|
2011-09-21 20:06:48 +04:00
|
|
|
private:
|
|
|
|
uint8_t *ptr_;
|
|
|
|
std::size_t size_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SizedProxy {
|
|
|
|
public:
|
|
|
|
SizedProxy() {}
|
|
|
|
|
|
|
|
SizedProxy(void *ptr, std::size_t size) : inner_(ptr, size) {}
|
|
|
|
|
|
|
|
operator std::string() const {
|
|
|
|
return std::string(reinterpret_cast<const char*>(inner_.Data()), inner_.EntrySize());
|
|
|
|
}
|
|
|
|
|
|
|
|
SizedProxy &operator=(const SizedProxy &from) {
|
|
|
|
memcpy(inner_.Data(), from.inner_.Data(), inner_.EntrySize());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SizedProxy &operator=(const std::string &from) {
|
|
|
|
memcpy(inner_.Data(), from.data(), inner_.EntrySize());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const void *Data() const { return inner_.Data(); }
|
|
|
|
void *Data() { return inner_.Data(); }
|
2013-06-24 19:05:47 +04:00
|
|
|
|
2014-01-28 04:51:35 +04:00
|
|
|
friend void swap(SizedProxy first, SizedProxy second) {
|
2013-05-17 05:04:47 +04:00
|
|
|
std::swap_ranges(
|
2013-06-24 19:05:47 +04:00
|
|
|
static_cast<char*>(first.inner_.Data()),
|
|
|
|
static_cast<char*>(first.inner_.Data()) + first.inner_.EntrySize(),
|
|
|
|
static_cast<char*>(second.inner_.Data()));
|
2013-05-17 05:04:47 +04:00
|
|
|
}
|
2011-09-21 20:06:48 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class util::ProxyIterator<SizedProxy>;
|
|
|
|
|
|
|
|
typedef std::string value_type;
|
|
|
|
|
|
|
|
typedef SizedInnerIterator InnerIterator;
|
|
|
|
|
|
|
|
InnerIterator &Inner() { return inner_; }
|
|
|
|
const InnerIterator &Inner() const { return inner_; }
|
|
|
|
InnerIterator inner_;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef ProxyIterator<SizedProxy> SizedIterator;
|
|
|
|
|
|
|
|
inline SizedIterator SizedIt(void *ptr, std::size_t size) { return SizedIterator(SizedProxy(ptr, size)); }
|
|
|
|
|
2013-06-24 19:05:47 +04:00
|
|
|
// Useful wrapper for a comparison function i.e. sort.
|
2011-09-21 20:06:48 +04:00
|
|
|
template <class Delegate, class Proxy = SizedProxy> class SizedCompare : public std::binary_function<const Proxy &, const Proxy &, bool> {
|
|
|
|
public:
|
|
|
|
explicit SizedCompare(const Delegate &delegate = Delegate()) : delegate_(delegate) {}
|
|
|
|
|
|
|
|
bool operator()(const Proxy &first, const Proxy &second) const {
|
|
|
|
return delegate_(first.Data(), second.Data());
|
|
|
|
}
|
|
|
|
bool operator()(const Proxy &first, const std::string &second) const {
|
|
|
|
return delegate_(first.Data(), second.data());
|
|
|
|
}
|
|
|
|
bool operator()(const std::string &first, const Proxy &second) const {
|
|
|
|
return delegate_(first.data(), second.Data());
|
|
|
|
}
|
|
|
|
bool operator()(const std::string &first, const std::string &second) const {
|
|
|
|
return delegate_(first.data(), second.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
const Delegate &GetDelegate() const { return delegate_; }
|
2013-06-24 19:05:47 +04:00
|
|
|
|
2011-09-21 20:06:48 +04:00
|
|
|
private:
|
|
|
|
const Delegate delegate_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace util
|
|
|
|
#endif // UTIL_SIZED_ITERATOR__
|