#ifndef BASE_CONTAINERS_SMALL_MAP_H_
#define BASE_CONTAINERS_SMALL_MAP_H_
#include <stddef.h>
#include <limits>
#include <map>
#include <new>
#include <utility>
#include "base/check.h"
#include "base/check_op.h"
#include "base/memory/raw_ptr.h"
inline constexpr size_t kUsingFullMapSentinel =
std::numeric_limits<size_t>::max();
namespace base {
namespace internal {
template <typename NormalMap>
class small_map_default_init {
public:
void operator()(NormalMap* map) const { new (map) NormalMap(); }
};
template <typename M>
struct has_key_equal {
typedef char sml;
typedef struct { char dummy[2]; } big;
template <typename U> static big test(typename U::key_equal*);
template <typename> static sml test(...);
static const bool value = (sizeof(test<M>(0)) == sizeof(big));
};
template <typename M> const bool has_key_equal<M>::value;
template <typename M, bool has_key_equal_value>
struct select_equal_key {
struct equal_key {
bool operator()(const typename M::key_type& left,
const typename M::key_type& right) {
typename M::key_compare comp;
return !comp(left, right) && !comp(right, left);
}
};
};
template <typename M>
struct select_equal_key<M, true> {
typedef typename M::key_equal equal_key;
};
}
template <typename NormalMap,
size_t kArraySize = 4,
typename EqualKey = typename internal::select_equal_key<
NormalMap,
internal::has_key_equal<NormalMap>::value>::equal_key,
typename MapInit = internal::small_map_default_init<NormalMap>>
class small_map {
static_assert(kArraySize > 0, "Initial size must be greater than 0");
static_assert(kArraySize != kUsingFullMapSentinel,
"Initial size out of range");
public:
typedef typename NormalMap::key_type key_type;
typedef typename NormalMap::mapped_type data_type;
typedef typename NormalMap::mapped_type mapped_type;
typedef typename NormalMap::value_type value_type;
typedef EqualKey key_equal;
small_map() : size_(0), functor_(MapInit()) {}
explicit small_map(const MapInit& functor) : size_(0), functor_(functor) {}
small_map(const small_map& src) {
InitFrom(src);
}
void operator=(const small_map& src) {
if (&src == this) return;
Destroy();
InitFrom(src);
}
~small_map() { Destroy(); }
class const_iterator;
class iterator {
public:
typedef typename NormalMap::iterator::iterator_category iterator_category;
typedef typename NormalMap::iterator::value_type value_type;
typedef typename NormalMap::iterator::difference_type difference_type;
typedef typename NormalMap::iterator::pointer pointer;
typedef typename NormalMap::iterator::reference reference;
inline iterator() : array_iter_(nullptr) {}
inline iterator& operator++() {
if (array_iter_ != nullptr) {
++array_iter_;
} else {
++map_iter_;
}
return *this;
}
inline iterator operator++(int ) {
iterator result(*this);
++(*this);
return result;
}
inline iterator& operator--() {
if (array_iter_ != nullptr) {
--array_iter_;
} else {
--map_iter_;
}
return *this;
}
inline iterator operator--(int ) {
iterator result(*this);
--(*this);
return result;
}
inline value_type* operator->() const {
return array_iter_ ? array_iter_.get() : map_iter_.operator->();
}
inline value_type& operator*() const {
return array_iter_ ? *array_iter_ : *map_iter_;
}
inline bool operator==(const iterator& other) const {
if (array_iter_ != nullptr) {
return array_iter_ == other.array_iter_;
} else {
return other.array_iter_ == nullptr && map_iter_ == other.map_iter_;
}
}
inline bool operator!=(const iterator& other) const {
return !(*this == other);
}
private:
friend class small_map;
friend class const_iterator;
inline explicit iterator(value_type* init) : array_iter_(init) {}
inline explicit iterator(const typename NormalMap::iterator& init)
: array_iter_(nullptr), map_iter_(init) {}
raw_ptr<value_type, AllowPtrArithmetic> array_iter_;
typename NormalMap::iterator map_iter_;
};
class const_iterator {
public:
typedef typename NormalMap::const_iterator::iterator_category
iterator_category;
typedef typename NormalMap::const_iterator::value_type value_type;
typedef typename NormalMap::const_iterator::difference_type difference_type;
typedef typename NormalMap::const_iterator::pointer pointer;
typedef typename NormalMap::const_iterator::reference reference;
inline const_iterator() : array_iter_(nullptr) {}
inline const_iterator(const iterator& other)
: array_iter_(other.array_iter_), map_iter_(other.map_iter_) {}
inline const_iterator& operator++() {
if (array_iter_ != nullptr) {
++array_iter_;
} else {
++map_iter_;
}
return *this;
}
inline const_iterator operator++(int ) {
const_iterator result(*this);
++(*this);
return result;
}
inline const_iterator& operator--() {
if (array_iter_ != nullptr) {
--array_iter_;
} else {
--map_iter_;
}
return *this;
}
inline const_iterator operator--(int ) {
const_iterator result(*this);
--(*this);
return result;
}
inline const value_type* operator->() const {
return array_iter_ ? array_iter_.get() : map_iter_.operator->();
}
inline const value_type& operator*() const {
return array_iter_ ? *array_iter_ : *map_iter_;
}
inline bool operator==(const const_iterator& other) const {
if (array_iter_ != nullptr) {
return array_iter_ == other.array_iter_;
}
return other.array_iter_ == nullptr && map_iter_ == other.map_iter_;
}
inline bool operator!=(const const_iterator& other) const {
return !(*this == other);
}
private:
friend class small_map;
inline explicit const_iterator(const value_type* init)
: array_iter_(init) {}
inline explicit const_iterator(
const typename NormalMap::const_iterator& init)
: array_iter_(nullptr), map_iter_(init) {}
raw_ptr<const value_type, AllowPtrArithmetic> array_iter_;
typename NormalMap::const_iterator map_iter_;
};
iterator find(const key_type& key) {
key_equal compare;
if (UsingFullMap()) {
return iterator(map()->find(key));
}
for (size_t i = 0; i < size_; ++i) {
if (compare(array_[i].first, key)) {
return iterator(array_ + i);
}
}
return iterator(array_ + size_);
}
const_iterator find(const key_type& key) const {
key_equal compare;
if (UsingFullMap()) {
return const_iterator(map()->find(key));
}
for (size_t i = 0; i < size_; ++i) {
if (compare(array_[i].first, key)) {
return const_iterator(array_ + i);
}
}
return const_iterator(array_ + size_);
}
data_type& operator[](const key_type& key) {
key_equal compare;
if (UsingFullMap()) {
return map_[key];
}
for (size_t i = size_; i > 0; --i) {
const size_t index = i - 1;
if (compare(array_[index].first, key)) {
return array_[index].second;
}
}
if (size_ == kArraySize) {
ConvertToRealMap();
return map_[key];
}
DCHECK(size_ < kArraySize);
new (&array_[size_]) value_type(key, data_type());
return array_[size_++].second;
}
std::pair<iterator, bool> insert(const value_type& x) {
key_equal compare;
if (UsingFullMap()) {
std::pair<typename NormalMap::iterator, bool> ret = map_.insert(x);
return std::make_pair(iterator(ret.first), ret.second);
}
for (size_t i = 0; i < size_; ++i) {
if (compare(array_[i].first, x.first)) {
return std::make_pair(iterator(array_ + i), false);
}
}
if (size_ == kArraySize) {
ConvertToRealMap();
std::pair<typename NormalMap::iterator, bool> ret = map_.insert(x);
return std::make_pair(iterator(ret.first), ret.second);
}
DCHECK(size_ < kArraySize);
new (&array_[size_]) value_type(x);
return std::make_pair(iterator(array_ + size_++), true);
}
template <class InputIterator>
void insert(InputIterator f, InputIterator l) {
while (f != l) {
insert(*f);
++f;
}
}
template <typename... Args>
std::pair<iterator, bool> emplace(Args&&... args) {
key_equal compare;
if (UsingFullMap()) {
std::pair<typename NormalMap::iterator, bool> ret =
map_.emplace(std::forward<Args>(args)...);
return std::make_pair(iterator(ret.first), ret.second);
}
value_type x(std::forward<Args>(args)...);
for (size_t i = 0; i < size_; ++i) {
if (compare(array_[i].first, x.first)) {
return std::make_pair(iterator(array_ + i), false);
}
}
if (size_ == kArraySize) {
ConvertToRealMap();
std::pair<typename NormalMap::iterator, bool> ret =
map_.emplace(std::move(x));
return std::make_pair(iterator(ret.first), ret.second);
}
DCHECK(size_ < kArraySize);
new (&array_[size_]) value_type(std::move(x));
return std::make_pair(iterator(array_ + size_++), true);
}
iterator begin() {
return UsingFullMap() ? iterator(map_.begin()) : iterator(array_);
}
const_iterator begin() const {
return UsingFullMap() ? const_iterator(map_.begin())
: const_iterator(array_);
}
iterator end() {
return UsingFullMap() ? iterator(map_.end()) : iterator(array_ + size_);
}
const_iterator end() const {
return UsingFullMap() ? const_iterator(map_.end())
: const_iterator(array_ + size_);
}
void clear() {
if (UsingFullMap()) {
map_.~NormalMap();
} else {
for (size_t i = 0; i < size_; ++i) {
array_[i].~value_type();
}
}
size_ = 0;
}
iterator erase(const iterator& position) {
if (UsingFullMap()) {
return iterator(map_.erase(position.map_iter_));
}
size_t i = static_cast<size_t>(position.array_iter_ - array_);
CHECK_LE(i, size_);
array_[i].~value_type();
--size_;
if (i != size_) {
new (&array_[i]) value_type(std::move(array_[size_]));
array_[size_].~value_type();
return iterator(array_ + i);
}
return end();
}
size_t erase(const key_type& key) {
iterator iter = find(key);
if (iter == end()) {
return 0;
}
erase(iter);
return 1;
}
size_t count(const key_type& key) const {
return (find(key) == end()) ? 0 : 1;
}
size_t size() const { return UsingFullMap() ? map_.size() : size_; }
bool empty() const { return UsingFullMap() ? map_.empty() : size_ == 0; }
bool UsingFullMap() const { return size_ == kUsingFullMapSentinel; }
inline NormalMap* map() {
CHECK(UsingFullMap());
return &map_;
}
inline const NormalMap* map() const {
CHECK(UsingFullMap());
return &map_;
}
private:
size_t size_;
MapInit functor_;
union {
value_type array_[kArraySize];
NormalMap map_;
};
void ConvertToRealMap() {
union Storage {
Storage() {}
~Storage() {}
value_type array[kArraySize];
} temp;
for (size_t i = 0; i < kArraySize; ++i) {
new (&temp.array[i]) value_type(std::move(array_[i]));
array_[i].~value_type();
}
size_ = kUsingFullMapSentinel;
functor_(&map_);
for (size_t i = 0; i < kArraySize; ++i) {
map_.insert(std::move(temp.array[i]));
temp.array[i].~value_type();
}
}
void InitFrom(const small_map& src) {
functor_ = src.functor_;
size_ = src.size_;
if (src.UsingFullMap()) {
functor_(&map_);
map_ = src.map_;
} else {
for (size_t i = 0; i < size_; ++i) {
new (&array_[i]) value_type(src.array_[i]);
}
}
}
void Destroy() {
if (UsingFullMap()) {
map_.~NormalMap();
} else {
for (size_t i = 0; i < size_; ++i) {
array_[i].~value_type();
}
}
}
};
}
#endif