Skip to content

Commit

Permalink
use associative semantics for maps
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed Jan 12, 2024
1 parent 919b155 commit 15ae799
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 6 additions & 5 deletions lib/HarfBuzz/Map.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ unit class HarfBuzz::Map;

use HarfBuzz::Set;
use HarfBuzz::Raw;
use HarfBuzz::Raw::Defs :types;
use HarfBuzz::Raw::Defs :types, :hb-set-value;

has hb_map:D $!raw handles<elems> = hb_map::create();
method raw { $!raw }
Expand All @@ -21,12 +21,13 @@ method values {
}
}

method exists(UInt:D $i --> Int) {
$!raw.exists($i).so;
method exists(UInt:D() $k --> Int) {
$!raw.exists($k).so;
}

method AT-POS(UInt:D $i --> Int) {
$i < $.elems ?? $!raw.get($i) !! Int
method AT-KEY(UInt:D() $k --> Int) {
my $v := $!raw.get($k);
$v == HB_SET_VALUE_INVALID ?? Int !! $v;
}

submethod DESTROY { .destroy with $!raw }
4 changes: 3 additions & 1 deletion t/face.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ subtest 'unicode-set', {
subtest 'unicode-to-gid-map', {
my HarfBuzz::Map $unicode-map = $face.unicode-to-gid-map;
ok $unicode-map.exists(42), 'exists';
nok $unicode-map.exists(31), '!exists';
my HarfBuzz::Set $keys = $unicode-map.keys;
my HarfBuzz::Set $values = $unicode-map.values;
is $values.array[10], 11, 'values';
is $unicode-map.elems, 854, 'elems';
is $unicode-map[42], 11;
is $unicode-map{42}, 11;
is-deeply $unicode-map{31}, Int;
}

0 comments on commit 15ae799

Please sign in to comment.