Skip to content

Instantly share code, notes, and snippets.

@eugeneko
Created March 20, 2019 19:46
Show Gist options
  • Select an option

  • Save eugeneko/2f5f000c13ec71ef6c445bcaa36cd574 to your computer and use it in GitHub Desktop.

Select an option

Save eugeneko/2f5f000c13ec71ef6c445bcaa36cd574 to your computer and use it in GitHub Desktop.
Prototype of container
template <class T, unsigned NBoolsValue = 4, unsigned NIntsValue = 4, unsigned NVariantsValue = 1>
class CompactFlatVariantMap
{
public:
enum class PoolType { Bool, Int, Variant };
using ElementIndex = unsigned;
static const unsigned PoolTypeBits = 2;
static const unsigned PoolTypeMask = (1 << (PoolTypeBits + 1)) - 1;
static const unsigned NBools = NBoolsValue;
static const unsigned NInts = NIntsValue;
static const unsigned NVariants = NVariantsValue;
static const unsigned NElements = NBools + NInts + NVariants;
ElementIndex Find(const T& key) const
{
const unsigned index = std::find(keys_.begin(), keys_.end(), key) - keys_.begin();
return index < keys_.end() ? indexes_[index] : M_MAX_UNSIGNED;
}
bool Has(const T& key) const { return Find(key) != M_MAX_UNSIGNED; }
void SetBool(const T& key, bool value)
{
}
void Insert(const T& key, const Variant& value)
{
switch (value.GetType())
{
case VAR_INT:
default:
break;
}
}
private:
static ElementIndex EncodeIndex(unsigned index, PoolType pool) { return (index << PoolTypeBits) | static_cast<unsigned>(pool) & PoolTypeMask; }
static Pair<unsigned, PoolType> DecodeIndex(ElementIndex index) { return MakePair(index >> PoolTypeBits, static_cast<PoolType>(index & PoolTypeMask)); }
fixed_vector<T, NElements> keys_;
fixed_vector<ElementIndex, NElements> indexes_;
fixed_vector<bool, NBools> poolBools_;
fixed_vector<int, NInts> poolInts_;
fixed_vector<Variant, NVariants> poolVariants_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment