#ifndef HB_BIMAP_HH
#define HB_BIMAP_HH
#include "hb.hh"
#include "hb-map.hh"
struct hb_bimap_t
{
void reset ()
{
forw_map.reset ();
back_map.reset ();
}
void alloc (unsigned pop)
{
forw_map.alloc (pop);
back_map.alloc (pop);
}
bool in_error () const { return forw_map.in_error () || back_map.in_error (); }
void set (hb_codepoint_t lhs, hb_codepoint_t rhs)
{
if (in_error ()) return;
if (unlikely (lhs == HB_MAP_VALUE_INVALID)) return;
if (unlikely (rhs == HB_MAP_VALUE_INVALID)) { del (lhs); return; }
forw_map.set (lhs, rhs);
if (unlikely (in_error ())) return;
back_map.set (rhs, lhs);
if (unlikely (in_error ())) forw_map.del (lhs);
}
hb_codepoint_t get (hb_codepoint_t lhs) const { return forw_map.get (lhs); }
hb_codepoint_t backward (hb_codepoint_t rhs) const { return back_map.get (rhs); }
hb_codepoint_t operator [] (hb_codepoint_t lhs) const { return get (lhs); }
bool has (hb_codepoint_t lhs) const { return forw_map.has (lhs); }
void del (hb_codepoint_t lhs)
{
back_map.del (get (lhs));
forw_map.del (lhs);
}
void clear ()
{
forw_map.clear ();
back_map.clear ();
}
bool is_empty () const { return forw_map.is_empty (); }
unsigned int get_population () const { return forw_map.get_population (); }
protected:
hb_map_t forw_map;
hb_map_t back_map;
public:
auto keys () const HB_AUTO_RETURN (+ forw_map.keys())
auto values () const HB_AUTO_RETURN (+ forw_map.values())
auto iter () const HB_AUTO_RETURN (+ forw_map.iter())
};
struct hb_inc_bimap_t
{
bool in_error () const { return forw_map.in_error () || back_map.in_error (); }
unsigned int get_population () const { return forw_map.get_population (); }
void reset ()
{
forw_map.reset ();
back_map.reset ();
}
void alloc (unsigned pop)
{
forw_map.alloc (pop);
back_map.alloc (pop);
}
void clear ()
{
forw_map.clear ();
back_map.resize (0);
}
hb_codepoint_t add (hb_codepoint_t lhs)
{
hb_codepoint_t rhs = forw_map[lhs];
if (rhs == HB_MAP_VALUE_INVALID)
{
rhs = back_map.length;
forw_map.set (lhs, rhs);
back_map.push (lhs);
}
return rhs;
}
hb_codepoint_t skip ()
{
hb_codepoint_t start = back_map.length;
back_map.push (HB_MAP_VALUE_INVALID);
return start;
}
hb_codepoint_t skip (unsigned count)
{
hb_codepoint_t start = back_map.length;
back_map.alloc (back_map.length + count);
for (unsigned i = 0; i < count; i++)
back_map.push (HB_MAP_VALUE_INVALID);
return start;
}
hb_codepoint_t get_next_value () const
{ return back_map.length; }
void add_set (const hb_set_t *set)
{
for (auto i : *set) add (i);
}
bool identity (unsigned int size)
{
clear ();
for (hb_codepoint_t i = 0; i < size; i++) add (i);
return !in_error ();
}
protected:
static int cmp_id (const void* a, const void* b)
{ return (int)*(const hb_codepoint_t *)a - (int)*(const hb_codepoint_t *)b; }
public:
void sort ()
{
hb_codepoint_t count = get_population ();
hb_vector_t <hb_codepoint_t> work;
if (unlikely (!work.resize (count, false))) return;
for (hb_codepoint_t rhs = 0; rhs < count; rhs++)
work.arrayZ[rhs] = back_map[rhs];
work.qsort (cmp_id);
clear ();
for (hb_codepoint_t rhs = 0; rhs < count; rhs++)
add (work.arrayZ[rhs]);
}
hb_codepoint_t get (hb_codepoint_t lhs) const { return forw_map.get (lhs); }
hb_codepoint_t backward (hb_codepoint_t rhs) const { return back_map[rhs]; }
hb_codepoint_t operator [] (hb_codepoint_t lhs) const { return get (lhs); }
bool has (hb_codepoint_t lhs) const { return forw_map.has (lhs); }
protected:
hb_map_t forw_map;
hb_vector_t<hb_codepoint_t> back_map;
public:
auto keys () const HB_AUTO_RETURN (+ back_map.iter())
};
#endif