#pragma once
#include <wtf/IndexKeyType.h>
#include <wtf/Vector.h>
namespace WTF {
template<typename Key, typename Value>
class IndexMap {
WTF_MAKE_FAST_ALLOCATED;
public:
IndexMap() = default;
IndexMap(IndexMap&&) = default;
IndexMap& operator=(IndexMap&&) = default;
IndexMap(const IndexMap&) = default;
IndexMap& operator=(const IndexMap&) = default;
template<typename... Args>
explicit IndexMap(size_t size, Args&&... args)
{
m_vector.fill(Value(std::forward<Args>(args)...), size);
}
template<typename... Args>
void resize(size_t size, Args&&... args)
{
m_vector.fill(Value(std::forward<Args>(args)...), size);
}
template<typename... Args>
void clear(Args&&... args)
{
m_vector.fill(Value(std::forward<Args>(args)...), m_vector.size());
}
size_t size() const { return m_vector.size(); }
Value& at(const Key& key)
{
return m_vector[IndexKeyType<Key>::index(key)];
}
const Value& at(const Key& key) const
{
return m_vector[IndexKeyType<Key>::index(key)];
}
Value& at(size_t index)
{
return m_vector[index];
}
const Value& at(size_t index) const
{
return m_vector[index];
}
Value& operator[](size_t index) { return at(index); }
const Value& operator[](size_t index) const { return at(index); }
Value& operator[](const Key& key) { return at(key); }
const Value& operator[](const Key& key) const { return at(key); }
template<typename PassedValue>
void append(const Key& key, PassedValue&& value)
{
RELEASE_ASSERT(IndexKeyType<Key>::index(key) == m_vector.size());
m_vector.append(std::forward<PassedValue>(value));
}
private:
Vector<Value, 0, UnsafeVectorOverflow> m_vector;
};
}
using WTF::IndexMap;