#ifndef TOOLS_GN_UNIQUE_VECTOR_H_
#define TOOLS_GN_UNIQUE_VECTOR_H_
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <functional>
#include <vector>
#include "hash_table_base.h"
struct UniqueVectorNode {
uint32_t hash32;
uint32_t index_plus1;
size_t hash_value() const { return hash32; }
bool is_valid() const { return !is_null(); }
bool is_null() const { return index_plus1 == 0; }
static constexpr bool is_tombstone() { return false; }
size_t index() const { return index_plus1 - 1u; }
static uint32_t ToHash32(size_t hash) { return static_cast<uint32_t>(hash); }
static UniqueVectorNode Make(size_t hash, size_t index) {
return {ToHash32(hash), static_cast<uint32_t>(index + 1u)};
}
};
using UniqueVectorHashTableBase = HashTableBase<UniqueVectorNode>;
class UniqueVectorHashSet : public UniqueVectorHashTableBase {
public:
using BaseType = UniqueVectorHashTableBase;
using Node = BaseType::Node;
template <typename T, typename EqualTo = std::equal_to<T>>
Node* Lookup(size_t hash, const T& item, const std::vector<T>& vector) const {
uint32_t hash32 = Node::ToHash32(hash);
return BaseType::NodeLookup(hash32, [&](const Node* node) {
return hash32 == node->hash32 && EqualTo()(vector[node->index()], item);
});
}
void Insert(Node* node, size_t hash, size_t index) {
*node = Node::Make(hash, index);
BaseType::UpdateAfterInsert(false);
}
void Clear() { NodeClear(); }
};
template <typename T,
typename Hash = std::hash<T>,
typename EqualTo = std::equal_to<T>>
class UniqueVector {
public:
using Vector = std::vector<T>;
using iterator = typename Vector::iterator;
using const_iterator = typename Vector::const_iterator;
const Vector& vector() const { return vector_; }
size_t size() const { return vector_.size(); }
bool empty() const { return vector_.empty(); }
void clear() {
vector_.clear();
set_.Clear();
}
void reserve(size_t s) { vector_.reserve(s); }
const T& operator[](size_t index) const { return vector_[index]; }
const_iterator begin() const { return vector_.begin(); }
const_iterator end() const { return vector_.end(); }
Vector release() {
Vector result = std::move(vector_);
clear();
return result;
}
bool push_back(const T& t) {
size_t hash;
auto* node = Lookup(t, &hash);
if (node->is_valid()) {
return false;
}
vector_.push_back(t);
set_.Insert(node, hash, vector_.size() - 1);
return true;
}
bool push_back(T&& t) {
size_t hash = Hash()(t);
auto* node = Lookup(t, &hash);
if (node->is_valid()) {
return false;
}
vector_.push_back(std::move(t));
set_.Insert(node, hash, vector_.size() - 1);
return true;
}
template <typename... ARGS>
bool emplace_back(ARGS... args) {
return push_back(T{std::forward<ARGS>(args)...});
}
std::pair<bool, size_t> PushBackWithIndex(const T& t) {
size_t hash;
auto* node = Lookup(t, &hash);
if (node->is_valid()) {
return {false, node->index()};
}
size_t result = vector_.size();
vector_.push_back(t);
set_.Insert(node, hash, result);
return {true, result};
}
std::pair<bool, size_t> PushBackWithIndex(T&& t) {
size_t hash;
auto* node = Lookup(t, &hash);
if (node->is_valid()) {
return {false, node->index()};
}
size_t result = vector_.size();
vector_.push_back(std::move(t));
set_.Insert(node, hash, result);
return {true, result};
}
template <typename... ARGS>
std::pair<bool, size_t> EmplaceBackWithIndex(ARGS... args) {
return PushBackWithIndex(T{std::forward<ARGS>(args)...});
}
template <typename iter>
void Append(const iter& begin, const iter& end) {
for (iter i = begin; i != end; ++i)
push_back(*i);
}
template <typename C,
typename = std::void_t<
decltype(static_cast<const T>(*std::declval<C>().begin())),
decltype(static_cast<const T>(*std::declval<C>().end()))>>
void Append(const C& other) {
Append(other.begin(), other.end());
}
template <typename C,
typename = std::void_t<
decltype(static_cast<T>(*std::declval<C>().begin())),
decltype(static_cast<T>(*std::declval<C>().end()))>>
void Append(C&& other) {
for (auto it = other.begin(); it != other.end(); ++it)
push_back(std::move(*it));
}
bool Contains(const T& t) const {
size_t hash;
return Lookup(t, &hash)->is_valid();
}
size_t IndexOf(const T& t) const {
size_t hash;
return Lookup(t, &hash)->index();
}
static constexpr size_t kIndexNone = 0xffffffffu;
private:
UniqueVectorNode* Lookup(const T& t, size_t* hash) const {
*hash = Hash()(t);
return set_.Lookup<T, EqualTo>(*hash, t, vector_);
}
Vector vector_;
UniqueVectorHashSet set_;
};
#endif