| 1 | package Games::Mahjongg::Solver::deBondt::FFI; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | $VERSION = '0.0.3'; | |||||||||
| 4 | 1 | my $pm_file = __FILE__; | |||||||||
| 5 | 1 | ▶ | use File::Basename qw(dirname); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | my $pm_dir = dirname $pm_file; | |||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use FFI::Platypus 1.00; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use FFI::Platypus::Memory qw( malloc free ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use FFI::CheckLib; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | my $ffi = FFI::Platypus->new( api => 1 ); | |||||||||
| 13 | 1 | $ffi->lang('CPP'); | |||||||||
| 14 | 1 | my @libs = find_lib_or_die( lib => "Games-Mahjongg-Solver-deBondt-FFI", libpath => [ | |||||||||
| 15 | "$pm_dir/../../../../blib/lib/auto/share/dist/Games-Mahjongg-Solver-deBondt-FFI/lib", | ||||||||||
| 16 | "$pm_dir/../../../../auto/share/dist/Games-Mahjongg-Solver-deBondt-FFI/lib", | ||||||||||
| 17 | ]); | ||||||||||
| 18 | # warn "FYI: we found libs @libs"; | ||||||||||
| 19 | 1 | ▶ | my $lib = @libs ? $libs[0] : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 1 | ▶ | $lib or die "Found no lib, should never happen, giving up, cannot continue"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 1 | $ffi->lib($lib); | |||||||||
| 22 | |||||||||||
| 23 | $ffi->custom_type( Foo => { | ||||||||||
| 24 | native_type => 'opaque', | ||||||||||
| 25 | 2333 | ▶ | perl_to_native => sub { ${ $_[0] } }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 0 | ▶ | native_to_perl => sub { bless \$_[0], 'Games::Mahjongg::Solver::deBondt::FFI' }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 27 | 1 | }); | |||||||||
| 28 | |||||||||||
| 29 | 1 | $ffi->attach( [ 'Foo::Foo()' => '_new' ] => ['Foo'] => 'void' ); | |||||||||
| 30 | 1 | $ffi->attach( [ 'Foo::~Foo()' => '_DESTROY' ] => ['Foo'] => 'void' ); | |||||||||
| 31 | 1 | $ffi->attach( [ 'Foo::get_r1()' => 'get_r1' ] => ['Foo'] => 'double' ); | |||||||||
| 32 | 1 | $ffi->attach( [ 'Foo::get_r2()' => 'get_r2' ] => ['Foo'] => 'double' ); | |||||||||
| 33 | 1 | $ffi->attach( [ 'Foo::get_g()' => 'opaque_get_g' ] => ['Foo'] => 'opaque' ); | |||||||||
| 34 | 1 | $ffi->attach( [ 'Foo::set_g(char*)' | |||||||||
| 35 | => 'set_g' ] => ['Foo','string'] | ||||||||||
| 36 | => 'void' ); | ||||||||||
| 37 | 1 | $ffi->attach( [ 'Foo::set_r1(double)' | |||||||||
| 38 | => 'set_r1' ] => ['Foo','double'] | ||||||||||
| 39 | => 'void' ); | ||||||||||
| 40 | 1 | $ffi->attach( [ 'Foo::set_r2(double)' | |||||||||
| 41 | => 'set_r2' ] => ['Foo','double'] | ||||||||||
| 42 | => 'void' ); | ||||||||||
| 43 | 1 | $ffi->attach( [ 'Foo::foo_mjsolve()' | |||||||||
| 44 | => 'mjsolve' ] => ['Foo'] => 'double' ); | ||||||||||
| 45 | # $ffi->attach( [ 'Foo::free_string(char*)' | ||||||||||
| 46 | # => 'free_string' ] => ['opaque'] => 'void' ); | ||||||||||
| 47 | |||||||||||
| 48 | 1 | my $size = $ffi->function('Foo::_size()' => [] => 'int')->call; | |||||||||
| 49 | |||||||||||
| 50 | sub new | ||||||||||
| 51 | 1 | ▶ | { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 52 | 1 | my($class) = @_; | |||||||||
| 53 | 1 | my $ptr = malloc $size; | |||||||||
| 54 | 1 | my $self = bless \$ptr, $class; | |||||||||
| 55 | 1 | _new($self); | |||||||||
| 56 | 1 | $self; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | sub DESTROY | ||||||||||
| 60 | 1 | ▶ | { | ||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||
| 61 | 1 | my($self) = @_; | |||||||||
| 62 | 1 | _DESTROY($self); | |||||||||
| 63 | 1 | free($$self); | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 258 | ▶ | sub get_g { | ||||||||
|
Subroutine
get_g: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 67 | 258 | my($self) = @_; | |||||||||
| 68 | 258 | my $ptr = $self->opaque_get_g; | |||||||||
| 69 | 258 | my $g = $ffi->cast( 'opaque' => 'string', $ptr ); # copies the string | |||||||||
| 70 | # $self->free_string($ptr); | ||||||||||
| 71 | 258 | return $g; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 1 | 1; | |||||||||
| 75 | |||||||||||
| 76 | =head1 NAME | ||||||||||
| 77 | |||||||||||
| 78 | Games::Mahjongg::Solver::deBondt::FFI - Perl/FFI bindings to Mahjongg Solitaire Solver (after peeking) by Michiel de Bondt | ||||||||||
| 79 | |||||||||||
| 80 | =head1 SYNOPSIS | ||||||||||
| 81 | |||||||||||
| 82 | my $ffi = Games::Mahjongg::Solver::deBondt::FFI->new(); | ||||||||||
| 83 | |||||||||||
| 84 | $ffi->set_g(' 10 14 02 00 ... 08 10 00 35'); | ||||||||||
| 85 | $ffi->set_r1(0); | ||||||||||
| 86 | $ffi->set_r2(0); | ||||||||||
| 87 | my ($remain) = $ffi->mjsolve(); | ||||||||||
| 88 | |||||||||||
| 89 | =head1 DESCRIPTION | ||||||||||
| 90 | |||||||||||
| 91 | The following is from the README of the DLL: | ||||||||||
| 92 | |||||||||||
| 93 | The exported routine of the dll is: | ||||||||||
| 94 | |||||||||||
| 95 | extern "C" __declspec (dllexport) | ||||||||||
| 96 | double mjsolve (char* g, double r1, double r2) | ||||||||||
| 97 | |||||||||||
| 98 | g is the string containing the positions of the (remaining) tiles, as decimal | ||||||||||
| 99 | numbers separated by whitespace, as follows: | ||||||||||
| 100 | |||||||||||
| 101 | <row_1> <column_1> <level_1> <value_1> | ||||||||||
| 102 | <row_2> <column_2> <level_2> <value_2> | ||||||||||
| 103 | : | ||||||||||
| 104 | : | ||||||||||
| 105 | <row_n> <column_n> <level_n> <value_n> | ||||||||||
| 106 | |||||||||||
| 107 | row_i and column_i must be in the interval [00..39]. level_i must be in the | ||||||||||
| 108 | interval [00..09] and value_i must be in the interval [00..99], although you | ||||||||||
| 109 | would need only [01..36] for value_i. Each value must appear an even number of | ||||||||||
| 110 | times and may appear at most four times. | ||||||||||
| 111 | |||||||||||
| 112 | The program searches partial solutions that reduce the number of tiles to | ||||||||||
| 113 | max(r1,r2) at least, and stops searching when a solution is found that reduce | ||||||||||
| 114 | the number of tiles to min(r1,r2). If a solution is found, the best solution | ||||||||||
| 115 | found solution is written to g in the very same format as the input in g, but | ||||||||||
| 116 | the tiles are in the take-away order. The length of g must be 12n at least. | ||||||||||
| 117 | |||||||||||
| 118 | The return value is max(r1,r2) + 2 if no solutions are found, and the number of | ||||||||||
| 119 | tiles remaining for the returned solution otherwise. | ||||||||||
| 120 | |||||||||||
| 121 | |||||||||||
| 122 | =head1 BUGS | ||||||||||
| 123 | |||||||||||
| 124 | Exception handling is underdeveloped. Be careful what you can do with | ||||||||||
| 125 | $g, it is an opaque string that must match what the library expects | ||||||||||
| 126 | to get. | ||||||||||
| 127 | |||||||||||
| 128 | =head1 AUTHOR | ||||||||||
| 129 | |||||||||||
| 130 | Binding written by Andreas Koenig C<< <andk@cpan.org> >> | ||||||||||
| 131 | |||||||||||
| 132 | =head1 LICENSE | ||||||||||
| 133 | |||||||||||
| 134 | See the file COPYRIGHT that comes with this package | ||||||||||
| 135 | |||||||||||
| 136 | =head1 SEE ALSO | ||||||||||
| 137 | |||||||||||
| 138 | - home page of the library: https://www.math.ru.nl/~debondt/mjsolver.html | ||||||||||
| 139 | |||||||||||
| 140 | - link to the zip file with the library: https://www.math.ru.nl/~debondt/mjsolver_dll.zip | ||||||||||
| 141 | |||||||||||
| 142 | - thanks to Pedro Gimeno Fortea for his article about the solver at | ||||||||||
| 143 | http://www.formauri.es/personal/pgimeno/mj-preview/MjSolver.php | ||||||||||
| 144 | |||||||||||
| 145 | - Kmahjongg Homepage: https://apps.kde.org/kmahjongg/ | ||||||||||
| 146 | |||||||||||
| 147 | =cut | ||||||||||