[map] Add 5 methods

This commit is contained in:
Behdad Esfahbod 2021-11-01 22:29:14 -06:00
parent 38debbab7e
commit c30f6834d0
6 changed files with 92 additions and 1 deletions

View File

@ -346,6 +346,7 @@ COMPILED_TESTS = \
test-algs \
test-array \
test-iter \
test-map \
test-meta \
test-number \
test-ot-tag \
@ -381,6 +382,10 @@ test_iter_SOURCES = test-iter.cc hb-static.cc
test_iter_CPPFLAGS = $(COMPILED_TESTS_CPPFLAGS)
test_iter_LDADD = $(COMPILED_TESTS_LDADD)
test_map_SOURCES = test-map.cc hb-static.cc
test_map_CPPFLAGS = $(COMPILED_TESTS_CPPFLAGS)
test_map_LDADD = $(COMPILED_TESTS_LDADD)
test_meta_SOURCES = test-meta.cc hb-static.cc
test_meta_CPPFLAGS = $(COMPILED_TESTS_CPPFLAGS)
test_meta_LDADD = $(COMPILED_TESTS_LDADD)

View File

@ -39,10 +39,14 @@ template <typename K, typename V,
V vINVALID = hb_is_pointer (V) ? 0 : hb_is_signed (V) ? hb_int_min (V) : (V) -1>
struct hb_hashmap_t
{
HB_DELETE_COPY_ASSIGN (hb_hashmap_t);
hb_hashmap_t () { init (); }
~hb_hashmap_t () { fini (); }
hb_hashmap_t (const hb_hashmap_t& o) : hb_hashmap_t () { hb_copy (o, *this); }
hb_hashmap_t (hb_hashmap_t&& o) : hb_hashmap_t () { hb_swap (*this, o); }
hb_hashmap_t& operator= (const hb_hashmap_t&& o) { hb_copy (o, *this); return *this; }
hb_hashmap_t& operator= (hb_hashmap_t&& o) { hb_swap (*this, o); return *this; }
static_assert (hb_is_integral (K) || hb_is_pointer (K), "");
static_assert (hb_is_integral (V) || hb_is_pointer (V), "");
@ -70,6 +74,17 @@ struct hb_hashmap_t
unsigned int prime;
item_t *items;
friend void swap (hb_hashmap_t& a, hb_hashmap_t& b)
{
if (unlikely (!a.successful || !b.successful))
return;
hb_swap (a.successful, b.successful);
hb_swap (a.population, b.population);
hb_swap (a.occupancy, b.occupancy);
hb_swap (a.mask, b.mask);
hb_swap (a.prime, b.prime);
hb_swap (a.items, b.items);
}
void init_shallow ()
{
successful = true;

View File

@ -488,6 +488,7 @@ if get_option('tests').enabled()
'test-repacker': ['test-repacker.cc', 'hb-static.cc'],
'test-priority-queue': ['test-priority-queue.cc', 'hb-static.cc'],
'test-iter': ['test-iter.cc', 'hb-static.cc'],
'test-map': ['test-map.cc', 'hb-static.cc'],
'test-meta': ['test-meta.cc', 'hb-static.cc'],
'test-number': ['test-number.cc', 'hb-number.cc'],
'test-ot-tag': ['hb-ot-tag.cc'],

68
src/test-map.cc Normal file
View File

@ -0,0 +1,68 @@
/*
* Copyright © 2021 Behdad Esfahbod
*
* 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.
*
*/
#include "hb.hh"
#include "hb-map.hh"
int
main (int argc, char **argv)
{
/* Test copy constructor. */
{
hb_map_t v1;
v1.set (1, 2);
hb_map_t v2 {v1};
assert (v1.get_population () == 1);
assert (v2.get_population () == 1);
assert (v1[1] == 2);
assert (v2[1] == 2);
}
/* Test copy assignment. */
{
hb_map_t v1;
v1.set (1, 2);
hb_map_t v2 = v1;
assert (v1.get_population () == 1);
assert (v2.get_population () == 1);
assert (v1[1] == 2);
assert (v2[1] == 2);
}
/* Test move constructor. */
{
hb_map_t v {hb_map_t {}};
}
/* Test move assignment. */
{
hb_map_t v;
v = hb_map_t {};
}
return 0;
}

View File

@ -59,6 +59,7 @@ main (int argc, char **argv)
v = hb_set_t {1, 2};
assert (v.get_population () == 2);
}
/* Test initializing set from iterable. */
{
hb_set_t s;

View File

@ -77,6 +77,7 @@ main (int argc, char **argv)
assert (v[0] == 1);
assert (v[1] == 2);
}
/* Test initializing from iterable. */
{
hb_set_t s;