2020-08-11 11:10:23 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2019 Adobe Inc.
|
|
|
|
*
|
|
|
|
* This is part of HarfBuzz, a text shaping library.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, without written agreement and without
|
|
|
|
* license or royalty fees, to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose, provided that the
|
|
|
|
* above copyright notice and the following two paragraphs appear in
|
|
|
|
* all copies of this software.
|
|
|
|
*
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
|
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
|
|
|
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
|
|
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
|
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*
|
|
|
|
* Adobe Author(s): Michiharu Ariza
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef HB_BIMAP_HH
|
|
|
|
#define HB_BIMAP_HH
|
|
|
|
|
|
|
|
#include "hb.hh"
|
|
|
|
#include "hb-map.hh"
|
|
|
|
|
|
|
|
/* Bi-directional map */
|
|
|
|
struct hb_bimap_t
|
|
|
|
{
|
|
|
|
void reset ()
|
|
|
|
{
|
|
|
|
forw_map.reset ();
|
|
|
|
back_map.reset ();
|
|
|
|
}
|
|
|
|
|
2023-07-09 23:51:41 +02:00
|
|
|
void alloc (unsigned pop)
|
2022-05-23 12:18:11 +02:00
|
|
|
{
|
2023-07-09 23:51:41 +02:00
|
|
|
forw_map.alloc (pop);
|
|
|
|
back_map.alloc (pop);
|
2022-05-23 12:18:11 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 11:10:23 +02:00
|
|
|
bool in_error () const { return forw_map.in_error () || back_map.in_error (); }
|
|
|
|
|
|
|
|
void set (hb_codepoint_t lhs, hb_codepoint_t rhs)
|
|
|
|
{
|
2021-09-19 22:31:36 +02:00
|
|
|
if (in_error ()) return;
|
2020-08-11 11:10:23 +02:00
|
|
|
if (unlikely (lhs == HB_MAP_VALUE_INVALID)) return;
|
|
|
|
if (unlikely (rhs == HB_MAP_VALUE_INVALID)) { del (lhs); return; }
|
2021-09-19 22:31:36 +02:00
|
|
|
|
2020-08-11 11:10:23 +02:00
|
|
|
forw_map.set (lhs, rhs);
|
2022-07-04 08:11:15 +02:00
|
|
|
if (unlikely (in_error ())) return;
|
2021-09-19 22:31:36 +02:00
|
|
|
|
2020-08-11 11:10:23 +02:00
|
|
|
back_map.set (rhs, lhs);
|
2022-07-04 08:11:15 +02:00
|
|
|
if (unlikely (in_error ())) forw_map.del (lhs);
|
2020-08-11 11:10:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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); }
|
2022-07-04 08:11:15 +02:00
|
|
|
bool has (hb_codepoint_t lhs) const { return forw_map.has (lhs); }
|
|
|
|
|
2020-08-11 11:10:23 +02:00
|
|
|
|
|
|
|
void del (hb_codepoint_t lhs)
|
|
|
|
{
|
|
|
|
back_map.del (get (lhs));
|
|
|
|
forw_map.del (lhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear ()
|
|
|
|
{
|
|
|
|
forw_map.clear ();
|
|
|
|
back_map.clear ();
|
|
|
|
}
|
|
|
|
|
2022-07-04 08:11:15 +02:00
|
|
|
bool is_empty () const { return forw_map.is_empty (); }
|
2020-08-11 11:10:23 +02:00
|
|
|
|
|
|
|
unsigned int get_population () const { return forw_map.get_population (); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
hb_map_t forw_map;
|
|
|
|
hb_map_t back_map;
|
2023-05-23 02:05:01 +02:00
|
|
|
|
|
|
|
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())
|
2020-08-11 11:10:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inremental bimap: only lhs is given, rhs is incrementally assigned */
|
2023-07-09 23:51:41 +02:00
|
|
|
struct hb_inc_bimap_t
|
2020-08-11 11:10:23 +02:00
|
|
|
{
|
2023-07-09 23:51:41 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:10:23 +02:00
|
|
|
/* Add a mapping from lhs to rhs with a unique value if lhs is unknown.
|
|
|
|
* Return the rhs value as the result.
|
|
|
|
*/
|
|
|
|
hb_codepoint_t add (hb_codepoint_t lhs)
|
|
|
|
{
|
|
|
|
hb_codepoint_t rhs = forw_map[lhs];
|
|
|
|
if (rhs == HB_MAP_VALUE_INVALID)
|
|
|
|
{
|
2023-07-09 23:51:41 +02:00
|
|
|
rhs = back_map.length;
|
|
|
|
forw_map.set (lhs, rhs);
|
|
|
|
back_map.push (lhs);
|
2020-08-11 11:10:23 +02:00
|
|
|
}
|
|
|
|
return rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
hb_codepoint_t skip ()
|
2023-07-09 23:51:41 +02:00
|
|
|
{
|
|
|
|
hb_codepoint_t start = back_map.length;
|
|
|
|
back_map.push (HB_MAP_VALUE_INVALID);
|
|
|
|
return start;
|
|
|
|
}
|
2020-08-11 11:10:23 +02:00
|
|
|
|
2023-05-23 02:05:01 +02:00
|
|
|
hb_codepoint_t skip (unsigned count)
|
2023-07-09 23:51:41 +02:00
|
|
|
{
|
|
|
|
hb_codepoint_t start = back_map.length;
|
|
|
|
for (unsigned i = 0; i < count; i++)
|
|
|
|
back_map.push (HB_MAP_VALUE_INVALID);
|
|
|
|
return start;
|
|
|
|
}
|
2023-05-23 02:05:01 +02:00
|
|
|
|
2020-08-11 11:10:23 +02:00
|
|
|
hb_codepoint_t get_next_value () const
|
2023-07-09 23:51:41 +02:00
|
|
|
{ return back_map.length; }
|
2020-08-11 11:10:23 +02:00
|
|
|
|
|
|
|
void add_set (const hb_set_t *set)
|
|
|
|
{
|
2023-07-09 23:51:41 +02:00
|
|
|
for (auto i : *set) add (i);
|
2020-08-11 11:10:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create an identity map. */
|
|
|
|
bool identity (unsigned int size)
|
|
|
|
{
|
|
|
|
clear ();
|
2023-07-09 23:51:41 +02:00
|
|
|
for (hb_codepoint_t i = 0; i < size; i++) add (i);
|
2020-08-11 11:10:23 +02:00
|
|
|
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:
|
|
|
|
/* Optional: after finished adding all mappings in a random order,
|
|
|
|
* reassign rhs to lhs so that they are in the same order. */
|
|
|
|
void sort ()
|
|
|
|
{
|
|
|
|
hb_codepoint_t count = get_population ();
|
|
|
|
hb_vector_t <hb_codepoint_t> work;
|
2023-07-09 23:51:41 +02:00
|
|
|
if (unlikely (!work.resize (count, false))) return;
|
2020-08-11 11:10:23 +02:00
|
|
|
|
|
|
|
for (hb_codepoint_t rhs = 0; rhs < count; rhs++)
|
2023-07-09 23:51:41 +02:00
|
|
|
work.arrayZ[rhs] = back_map[rhs];
|
2020-08-11 11:10:23 +02:00
|
|
|
|
|
|
|
work.qsort (cmp_id);
|
|
|
|
|
|
|
|
clear ();
|
|
|
|
for (hb_codepoint_t rhs = 0; rhs < count; rhs++)
|
2023-07-09 23:51:41 +02:00
|
|
|
add (work.arrayZ[rhs]);
|
2020-08-11 11:10:23 +02:00
|
|
|
}
|
|
|
|
|
2023-07-09 23:51:41 +02:00
|
|
|
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); }
|
|
|
|
|
2020-08-11 11:10:23 +02:00
|
|
|
protected:
|
2023-07-09 23:51:41 +02:00
|
|
|
hb_map_t forw_map;
|
|
|
|
hb_vector_t<hb_codepoint_t> back_map;
|
|
|
|
|
|
|
|
public:
|
|
|
|
auto keys () const HB_AUTO_RETURN (+ back_map.iter())
|
2020-08-11 11:10:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* HB_BIMAP_HH */
|