Build.PL100644001750001750 301514227505500 15632 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06# ========================================================================= # THIS FILE IS AUTOMATICALLY GENERATED BY MINILLA. # DO NOT EDIT DIRECTLY. # ========================================================================= use 5.008_001; use strict; use warnings; use utf8; BEGIN { push @INC, '.' } use builder::MyBuilder; use File::Basename; use File::Spec; my %args = ( license => 'perl_5', dynamic_config => 0, configure_requires => { 'Module::Build' => '0.4005', }, requires => { 'XSLoader' => '0.02', 'parent' => '0', 'perl' => '5.008001', }, recommends => { }, suggests => { }, build_requires => { 'Test::More' => '0.88', }, test_requires => { }, name => 'List-UtilsBy-XS', module_name => 'List::UtilsBy::XS', allow_pureperl => 0, script_files => [glob('script/*'), glob('bin/*')], PL_files => {}, test_files => ((-d '.git' || $ENV{RELEASE_TESTING}) && -d 'xt') ? 't/ xt/' : 't/', recursive_test_files => 1, ); if (-d 'share') { $args{share_dir} = 'share'; } my $builder = builder::MyBuilder->subclass( class => 'MyBuilder', code => q{ sub ACTION_distmeta { die "Do not run distmeta. Install Minilla and `minil install` instead.\n"; } sub ACTION_installdeps { die "Do not run installdeps. Run `cpanm --installdeps .` instead.\n"; } } )->new(%args); $builder->create_build_script(); Changes100644001750001750 73214227505500 15614 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06Revision history for Perl extension List::UtilsBy::XS 0.06 2022-04-19T10:13:08Z - Fix stack overrun issue of `extract_by` 0.05 2017-02-22T06:57:22Z - Fix build issue on newer Perl(5.25.9 or higher versions) 0.04 2014-04-17T15:29:06Z - Fix floating numbers sort issue(#2) 0.03 2013-06-25T02:27:07Z - Add CFLAGS(-DPERL_EXT) for older Perl 0.02 2013-06-24T05:35:29Z - Convert to Minilla 0.01 2011-09-26 - original version LICENSE100644001750001750 4375114227505500 15376 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06This software is copyright (c) 2013 by Syohei YOSHIDA . This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2013 by Syohei YOSHIDA . This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Suite 500, Boston, MA 02110-1335 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2013 by Syohei YOSHIDA . This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End META.json100644001750001750 361314227505500 15763 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06{ "abstract" : "XS implementation of List::UtilsBy", "author" : [ "Syohei YOSHIDA " ], "dynamic_config" : 0, "generated_by" : "Minilla/v3.1.16", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "List-UtilsBy-XS", "no_index" : { "directory" : [ "t", "xt", "inc", "share", "eg", "examples", "author", "benchmark", "builder" ] }, "prereqs" : { "build" : { "requires" : { "Test::More" : "0.88" } }, "configure" : { "requires" : { "Module::Build" : "0.4005", "Module::Build::XSUtil" : "0.02" } }, "develop" : { "requires" : { "Test::CPAN::Meta" : "0", "Test::MinimumVersion::Fast" : "0.04", "Test::PAUSE::Permissions" : "0.07", "Test::Pod" : "1.41", "Test::Spellunker" : "v0.2.7" } }, "runtime" : { "requires" : { "XSLoader" : "0.02", "parent" : "0", "perl" : "5.008001" } } }, "provides" : { "List::UtilsBy::XS" : { "file" : "lib/List/UtilsBy/XS.pm", "version" : "0.06" } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/syohex/p5-List-Utils-By/issues" }, "homepage" : "https://github.com/syohex/p5-List-Utils-By", "repository" : { "type" : "git", "url" : "https://github.com/syohex/p5-List-Utils-By.git", "web" : "https://github.com/syohex/p5-List-Utils-By" } }, "version" : "0.06", "x_serialization_backend" : "JSON::PP version 4.07", "x_static_install" : 0 } README.md100644001750001750 374314227505500 15625 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06[![Actions Status](https://github.com/syohex/p5-List-Utils-By/workflows/CI/badge.svg)](https://github.com/syohex/p5-List-Utils-By/actions) [![MetaCPAN Release](https://badge.fury.io/pl/List-UtilsBy-XS.svg)](https://metacpan.org/release/List-UtilsBy-XS) # NAME List::UtilsBy::XS - XS implementation of List::UtilsBy # SYNOPSIS use List::UtilsBy::XS qw(sort_by); sort_by { $_->{foo} } @hash_ref_list You can use those functions same as List::UtilsBy ones, but some functions have limitation. See [LIMITATION](https://metacpan.org/pod/LIMITATION) section. # DESCRIPTION List::UtilsBy::XS is XS implementation of List::UtilsBy. Functions are more fast than original ones. # FUNCTIONS Same as [List::UtilsBy](https://metacpan.org/pod/List%3A%3AUtilsBy) List::UtilsBy::XS implements following functions. - sort\_by - nsort\_by - rev\_sort\_by - rev\_nsort\_by - max\_by (alias nmax\_by) - min\_by (alias nmin\_by) - uniq\_by - partition\_by - count\_by - zip\_by - unzip\_by - extract\_by - weighted\_shuffle\_by - bundle\_by # LIMITATIONS Some functions are implemented by lightweight callback API. `sort_by`, `rev_sort_by`, `nsort_by`, `rev_nsort_by`, `min_by`, `max_by`, `nmin_by`, `nmax_by`, `uniq_by`, `partion_by`, `count_by`, `extract_by`, `weighted_shuffle_by` are limited some features. Limitations are: ## Don't change argument `$_` in code block [List::UtilsBy](https://metacpan.org/pod/List%3A%3AUtilsBy) localizes `$_` in the code block, but List::UtilsBy::XS doesn't localize it and it is only alias same as `map`, `grep`. So you should not modify `$_` in callback subroutine. ## Can't access to arguments as `@_` in code block You can access argument as only `$_` and cannot access as `@_`, `$_[n]`. # AUTHOR Syohei YOSHIDA # COPYRIGHT Copyright 2013- Syohei YOSHIDA # LICENSE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. # SEE ALSO [List::UtilsBy](https://metacpan.org/pod/List%3A%3AUtilsBy) bench.pl100644001750001750 653714227505500 17720 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/benchmark#!/usr/bin/env perl use strict; use warnings; use List::UtilsBy; use List::UtilsBy::XS; use Benchmark qw(cmpthese); use String::Random; my $generator = String::Random->new; my @array; for my $i (1..100) { push @array, { number => int(rand(100)), name => $generator->randregex('[A-Za-z0-9]{8}'), }; } print "Benchmarking List::Util and List::UtilsBy::XS\n"; # sort_by print "Bench: sort_by\n"; cmpthese(-1, { pp => sub { List::UtilsBy::sort_by { $_->{name} } @array }, xs => sub { List::UtilsBy::XS::sort_by { $_->{name} } @array }, }); print "\n"; # nsort_by print "Bench: nsort_by\n"; cmpthese(-1, { pp => sub { List::UtilsBy::nsort_by { $_->{number} } @array }, xs => sub { List::UtilsBy::XS::nsort_by { $_->{number} } @array }, }); print "\n"; # rev_sort_by print "Bench: rev_sort_by\n"; cmpthese(-1, { pp => sub { List::UtilsBy::rev_sort_by { $_->{name} } @array }, xs => sub { List::UtilsBy::XS::rev_sort_by { $_->{name} } @array }, }); print "\n"; # rev_nsort_by print "Bench: rev_nsort_by\n"; cmpthese(-1, { pp => sub { List::UtilsBy::rev_nsort_by { $_->{number} } @array }, xs => sub { List::UtilsBy::XS::rev_nsort_by { $_->{number} } @array }, }); print "\n"; # max_by print "Bench: max_by\n"; cmpthese(-1, { pp => sub { List::UtilsBy::max_by { $_->{number} } @array }, xs => sub { List::UtilsBy::XS::max_by { $_->{number} } @array }, }); print "\n"; # min_by print "Bench: min_by\n"; cmpthese(-1, { pp => sub { List::UtilsBy::min_by { $_->{number} } @array }, xs => sub { List::UtilsBy::XS::min_by { $_->{number} } @array }, }); print "\n"; # uniq_by print "Bench: uniq_by\n"; cmpthese(-1, { pp => sub { List::UtilsBy::uniq_by { $_->{name} } @array }, xs => sub { List::UtilsBy::XS::uniq_by { $_->{name} } @array }, }); print "\n"; # partition_by print "Bench: partition_by\n"; cmpthese(-1, { pp => sub { List::UtilsBy::partition_by { $_->{name} } @array }, xs => sub { List::UtilsBy::XS::partition_by { $_->{name} } @array }, }); print "\n"; # count_by print "Bench: count_by\n"; cmpthese(-1, { pp => sub { List::UtilsBy::count_by { $_->{name} } @array }, xs => sub { List::UtilsBy::XS::count_by { $_->{name} } @array }, }); print "\n"; # zip_by print "Bench: zip_by\n"; cmpthese(-1, { pp => sub { List::UtilsBy::zip_by { $_[0] + $_[1] } [0..100], [100..200] }, xs => sub { List::UtilsBy::XS::zip_by { $_[0] + $_[1] } [0..100], [100..200] }, }); print "\n"; # unzip_by print "Bench: unzip_by\n"; cmpthese(-1, { pp => sub { List::UtilsBy::unzip_by { ($_, $_) } (0..100) }, xs => sub { List::UtilsBy::XS::unzip_by { ($_, $_) } (0..100) }, }); print "\n"; # extract_by print "Bench: zip_by\n"; cmpthese(-1, { pp => sub { my @array = 1..10; List::UtilsBy::extract_by { $_ % 3 } @array; }, xs => sub { my @array = 1..10; List::UtilsBy::XS::extract_by { $_ % 3 } @array; }, }); print "\n"; # weighted_shuffle_by print "Bench: weighted_shuffle_by\n"; cmpthese(-1, { pp => sub { List::UtilsBy::weighted_shuffle_by { 1 } 1..100 }, xs => sub { List::UtilsBy::XS::weighted_shuffle_by { 1 } 1..100 }, }); print "\n"; # bundle_by print "Bench: bundle_by\n"; cmpthese(-1, { pp => sub { List::UtilsBy::bundle_by { $_[0] + $_[1] } 2, 1..100 }, xs => sub { List::UtilsBy::XS::bundle_by { $_[0] + $_[1] } 2, 1..100 }, }); print "\n"; MyBuilder.pm100644001750001750 75514227505500 20206 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/builderpackage builder::MyBuilder; use strict; use warnings; use 5.008001; use base 'Module::Build::XSUtil'; sub new { my ( $class, %args ) = @_; my $self = $class->SUPER::new( %args, c_source => 'xs-src', xs_files => { './xs-src/UtilsBy.xs' => './lib/List/UtilsBy/XS.xs', }, generate_ppport_h => 'lib/List/UtilsBy/ppport.h', extra_compiler_flags => ['-DPERL_EXT'], needs_compiler_c99 => 1, ); return $self; } 1; cpanfile100644001750001750 32314227505500 16021 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06requires 'XSLoader', '0.02'; requires 'parent'; requires 'perl', '5.008001'; on build => sub { requires 'Test::More', '0.88'; }; on configure => sub { requires 'Module::Build::XSUtil' => '>=0.02'; }; XS.pm100644001750001750 432314227505500 20305 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/lib/List/UtilsBypackage List::UtilsBy::XS; use 5.008_001; use strict; use warnings; use XSLoader; use parent qw/Exporter/; our $VERSION = '0.06'; our @EXPORT_OK = qw( sort_by rev_sort_by nsort_by rev_nsort_by max_by nmax_by min_by nmin_by uniq_by partition_by count_by zip_by unzip_by extract_by weighted_shuffle_by bundle_by ); our %EXPORT_TAGS = ( all => \@EXPORT_OK ); XSLoader::load __PACKAGE__, $VERSION; 1; __END__ =encoding utf-8 =for stopwords =head1 NAME List::UtilsBy::XS - XS implementation of List::UtilsBy =head1 SYNOPSIS use List::UtilsBy::XS qw(sort_by); sort_by { $_->{foo} } @hash_ref_list You can use those functions same as List::UtilsBy ones, but some functions have limitation. See L section. =head1 DESCRIPTION List::UtilsBy::XS is XS implementation of List::UtilsBy. Functions are more fast than original ones. =head1 FUNCTIONS Same as L List::UtilsBy::XS implements following functions. =over =item sort_by =item nsort_by =item rev_sort_by =item rev_nsort_by =item max_by (alias nmax_by) =item min_by (alias nmin_by) =item uniq_by =item partition_by =item count_by =item zip_by =item unzip_by =item extract_by =item weighted_shuffle_by =item bundle_by =back =head1 LIMITATIONS Some functions are implemented by lightweight callback API. C, C, C, C, C, C, C, C, C, C, C, C, C are limited some features. Limitations are: =head2 Don't change argument C<$_> in code block L localizes C<$_> in the code block, but List::UtilsBy::XS doesn't localize it and it is only alias same as C, C. So you should not modify C<$_> in callback subroutine. =head2 Can't access to arguments as C<@_> in code block You can access argument as only C<$_> and cannot access as C<@_>, C<$_[n]>. =head1 AUTHOR Syohei YOSHIDA Esyohex@gmail.comE =head1 COPYRIGHT Copyright 2013- Syohei YOSHIDA =head1 LICENSE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO L =cut minil.toml100644001750001750 36414227505500 16327 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06name = "List-UtilsBy-XS" badges = ['github-actions/CI', 'metacpan'] module_maker="ModuleBuild" [build] build_class = "builder::MyBuilder" [no_index] directory = ['t', 'xt', 'inc', 'share', 'eg', 'examples', 'author', 'benchmark', 'builder'] 00_compile.t100644001750001750 11514227505500 16673 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/tuse strict; use Test::More tests => 1; BEGIN { use_ok 'List::UtilsBy::XS' } 01_sort_by.t100644001750001750 231514227505500 16751 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/tuse strict; use warnings; use Test::More; use List::UtilsBy::XS qw( sort_by rev_sort_by ); { my $expected; my @gots; my @array; is_deeply( [ sort_by { } ], [], 'sort_by with empty list' ); is_deeply( [ sort_by { $_ } "a" ], [ "a" ], 'unit list' ); my $obj = { foo => 'bar' }; $expected = [ { foo => 'bar' } ]; @gots = sort_by { $_->{bar} } $obj; is_deeply(\@gots, $expected, "unit list by hash key"); is_deeply( [ sort_by { $_ } "a", "b" ], [ "a", "b" ], 'identity function no-op' ); is_deeply( [ sort_by { $_ } "b", "a" ], [ "a", "b" ], 'identity function on $_' ); is_deeply( [ sort_by { reverse $_ } "az", "by" ], [ "by", "az" ], 'reverse function' ); } { my $expected; my @gots; my @array; is_deeply( [ sort_by { } ], [], 'rev_sort_by with empty list' ); $expected = ["b", "a"]; @gots = rev_sort_by { $_ } "a", "b"; is_deeply(\@gots, $expected, 'reverse sort identity function'); push @array, { foo => 'aaa' }; push @array, { foo => 'bbb' }; @gots = rev_sort_by { $_->{foo} } @array; $expected = [ { foo => "bbb" }, { foo => "aaa" } ]; is_deeply(\@gots, $expected, "reverse sort by hash key 'foo'"); } done_testing; 02_nsort_by.t100644001750001750 347514227505500 17140 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/tuse strict; use warnings; use Test::More; use List::UtilsBy::XS qw( nsort_by rev_nsort_by ); { my $expected; my @gots; my @array; is_deeply( [ nsort_by { } ], [], 'nsort_by empty list' ); is_deeply( [ nsort_by { $_ } 1 ], [ 1 ], 'nsort_by unit list' ); @gots = nsort_by { $_ } 20, 25; is_deeply(\@gots, [ 20, 25 ], 'identity function no-op' ); @gots = nsort_by { $_ } 25, 20; is_deeply(\@gots, [ 20, 25 ], 'identity function on $_' ); @gots = nsort_by { $_ } 1.0, 2.1, 1.5; is_deeply(\@gots, [ 1.0, 1.5, 2.1 ], 'floating numbers sort' ); is_deeply( [ nsort_by { length $_ } "a", "bbb", "cc" ], [ "a", "cc", "bbb" ], 'length function' ); # List context would yield the matches and fail, scalar context would yield # the count and be correct @gots = nsort_by { () = m/(a)/g } "apple", "hello", "armageddon"; $expected = [ qw/hello apple armageddon/ ]; is_deeply(\@gots, $expected, 'scalar context' ); } { my $expected; my @gots; my @array; is_deeply( [ rev_nsort_by { } ], [], 'rev_nsort_by empty list' ); is_deeply( [ rev_nsort_by { $_ } 1 ], [ 1 ], 'rev_nsort_by unit list' ); @gots = rev_nsort_by { $_ } 20, 25; is_deeply(\@gots, [ 25, 20 ], 'identity function no-op' ); @gots = rev_nsort_by { $_ } 25, 20; is_deeply(\@gots, [ 25, 20 ], 'identity function on $_' ); @gots = rev_nsort_by { $_ } 1.0, 2.0, 1.5; is_deeply(\@gots, [ 2.0, 1.5, 1.0 ], 'floating numbers sort' ); @gots = rev_nsort_by { length $_ } "a", "bbb", "cc"; $expected = [ qw/bbb cc a/ ]; is_deeply(\@gots, $expected, 'reverse sort length function' ); @gots = rev_nsort_by { () = m/(a)/g } "apple", "hello", "armageddon"; $expected = [ qw/armageddon apple hello/ ]; is_deeply(\@gots, $expected, 'scalar context' ); } done_testing; 03_max_by.t100644001750001750 206214227505500 16550 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/tuse strict; use warnings; use Test::More; use List::UtilsBy::XS qw(max_by nmax_by); my $expected; my $got; my @gots; my @array; is_deeply( [ max_by {} ], [], 'empty list yields empty' ); $got = scalar (max_by { $_ } 10); is($got, 10, 'unit list yields value in scalar context'); is_deeply( [ max_by { $_ } 10 ], [ 10 ], 'unit list yields unit list value' ); is_deeply( ( scalar max_by { $_ } 10, 20 ), 20, 'identity function on $_' ); $got = scalar(max_by { length $_ } "a", "ccc", "bb"); is($got, 'ccc', "length function in scalar context"); @gots = max_by { length $_ } "a", "ccc", "bb"; is_deeply(\@gots, [ 'ccc' ], "length function in list context"); $got = scalar(max_by { length $_ } "a", "ccc", "bb", "ddd"); is($got, 'ccc', "first max element"); @gots = max_by { length $_ } "a", "ccc", "bb", "ddd"; $expected = [ qw/ccc ddd/ ]; is_deeply(\@gots, $expected, 'ties yield all maximal in list context'); is_deeply( ( scalar nmax_by { $_ } 10, 20 ), 20, 'nmax_by alias'); @gots = max_by { $_ } '0.1', '0.5', '0.2', '0.4'; is $gots[0], '0.5'; done_testing; 04_min_by.t100644001750001750 174614227505500 16557 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/tuse strict; use warnings; use Test::More; use List::UtilsBy::XS qw(min_by nmin_by); my $expected; my $got; my @gots; my @array; is_deeply( [ min_by {} ], [], 'empty list yields empty' ); $got = scalar (min_by { $_ } 10); ok($got == 10, 'unit list yields value in scalar context'); is_deeply( [ min_by { $_ } 10 ], [ 10 ], 'unit list yields unit list value' ); is_deeply( ( scalar min_by { $_ } 10, 20 ), 10, 'identity function on $_' ); $got = scalar(min_by { length $_ } "a", "ccc", "bb"); ok($got eq 'a', "length function in scalar context"); @gots = min_by { length $_ } "a", "ccc", "bb"; is_deeply(\@gots, [ 'a' ], "length function in list context"); $got = scalar(min_by { length $_ } "a", "ccc", "bb", "ddd"); ok($got eq 'a', "first max element"); @gots = min_by { length $_ } "a", "ccc", "b", "ddd"; $expected = [ qw/a b/ ]; is_deeply(\@gots, $expected, 'ties yield all maximal in list context'); is_deeply( ( scalar nmin_by { $_ } 10, 20 ), 10, 'nmin_by alias' ); done_testing; 05_uniq_by.t100644001750001750 104514227505500 16741 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/tuse strict; use warnings; use Test::More; use List::UtilsBy::XS qw( uniq_by ); my $expected; my @gots; my @array; is_deeply( [ uniq_by { } ], [], 'empty list' ); is_deeply( [ uniq_by { $_ } "a" ], [ "a" ], 'unit list' ); @gots = uniq_by { $_ } "a", "b"; is_deeply(\@gots, [ "a", "b" ],'identity function no-op'); @gots = uniq_by { $_ } "b", "a"; is_deeply(\@gots, [ "b", "a" ], 'identity function on $_' ); @gots = uniq_by { length $_ } "a", "b", "cc", "dd", "eee"; is_deeply(\@gots, [ "a", "cc", "eee" ], 'length function' ); done_testing; 06_partition_by.t100644001750001750 134414227505500 20001 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/tuse strict; use warnings; use Test::More; use List::UtilsBy::XS qw(partition_by); my $expected; my %gots; my @array; is_deeply( { partition_by { } }, {}, 'empty list' ); is_deeply({ partition_by { $_ } "a" }, { a => [ "a" ] }, 'unit list' ); %gots = partition_by { "all" } "a", "b"; $expected = { all => [ "a", "b" ] }; is_deeply(\%gots, $expected, 'constant function preserves order' ); %gots = partition_by { "all" } "b", "a"; $expected = { all => [ "b", "a" ] }; is_deeply(\%gots, $expected, 'constant function preserves order' ); %gots = partition_by { length $_ } "a", "b", "cc", "dd", "eee"; $expected = { 1 => [ "a", "b" ], 2 => [ "cc", "dd" ], 3 => [ "eee" ] }; is_deeply(\%gots, $expected, 'length function'); done_testing; 07_count_by.t100644001750001750 73514227505500 17104 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/tuse strict; use warnings; use Test::More; use List::UtilsBy::XS qw(count_by); my $expected; my %gots; my @array; is_deeply( { count_by { } }, {}, 'empty list' ); is_deeply( { count_by { $_ } "a" }, { a => 1 }, 'unit list' ); is_deeply( { count_by { "all" } "a", "b" }, { all => 2 }, 'constant function' ); %gots = count_by { length $_ } "a", "b", "cc", "dd", "eee"; $expected = { 1 => 2, 2 => 2, 3 => 1 }; is_deeply(\%gots, $expected, 'length function' ); done_testing; 08_zip_by.t100644001750001750 202014227505500 16564 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/tuse strict; use warnings; use Test::More; use List::UtilsBy::XS qw(zip_by); my @gots; my $expected; is_deeply( [ zip_by { } ], [], 'empty list' ); is_deeply( [ zip_by { [ @_ ] } [ "a" ], [ "b" ], [ "c" ] ], [ [ "a", "b", "c" ] ], 'singleton lists' ); @gots = zip_by { [ @_ ] } [ "a", "b", "c" ]; $expected = [ [ "a" ], [ "b" ], [ "c" ] ]; is_deeply(\@gots, $expected, 'narrow lists' ); @gots = zip_by { [ @_ ] } [ "a1", "a2" ], [ "b1", "b2" ]; $expected = [ [ "a1", "b1" ], [ "a2", "b2" ] ]; is_deeply(\@gots, $expected, 'zip with []' ); @gots = zip_by { join ",", @_ } [ "a1", "a2" ], [ "b1", "b2" ]; $expected = [ "a1,b1", "a2,b2" ]; is_deeply(\@gots, $expected, 'zip with join()' ); @gots = zip_by { [ @_ ] } [ 1 .. 3 ], [ 1 .. 2 ]; $expected = [ [ 1, 1 ], [ 2, 2 ], [ 3, undef ] ]; is_deeply(\@gots, $expected, 'non-rectangular adds undef' ); @gots = zip_by { @_ } [qw( one two three )], [ 1, 2, 3 ]; $expected = { one => 1, two => 2, three => 3 }; is_deeply({ @gots }, $expected, 'itemfunc can return lists' ); done_testing; 09_extract_by.t100644001750001750 341414227505500 17445 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/tuse strict; use warnings; use Test::More; use Scalar::Util qw( weaken isweak ); use List::UtilsBy::XS qw(extract_by); # We'll need a real array to work on my @numbers = ( 1 .. 10 ); my @gots; my $expected; @gots = extract_by { 0 } @numbers; is_deeply(\@gots , [], 'extract false returns none' ); is_deeply( \@numbers, [ 1 .. 10 ], 'extract false leaves array unchanged'); @gots = extract_by { $_ % 3 == 0 } @numbers; $expected = [ 3, 6, 9 ]; is_deeply(\@gots, $expected, 'extract div3 returns values'); $expected = [ 1, 2, 4, 5, 7, 8, 10 ]; is_deeply( \@numbers, $expected, 'extract div3 removes from array'); @gots = extract_by { 1 } @numbers; $expected = [ 1, 2, 4, 5, 7, 8, 10 ]; is_deeply(\@gots, $expected, 'extract true returns all') || diag explain \@gots; is_deeply( \@numbers, [], 'extract true leaves nothing' ); subtest 'scalar context' => sub { my @input = qw(a b c d e f); my $g = extract_by { 1 } @input; is $g, 6, "Return list length in scalar context"; my @input2 = qw(a b c d e f); my $g2 = extract_by { m/\A[bd]\z/ } @input2; is $g2, 2; }; TODO: { todo_skip "not implemented", 6; my @refs = map { {} } 1 .. 3; weaken $_ for my @weakrefs = @refs; @gots = extract_by { !defined $_ } @weakrefs; is_deeply(\@gots, [], 'extract undef refs returns nothing yet'); is( scalar @weakrefs, 3, 'extract undef refs leaves array unchanged'); ok(isweak $weakrefs[0], "extract_by doesn't break weakrefs"); undef $refs[0]; is_deeply( [ extract_by { !defined $_ } @weakrefs ], [ undef ], 'extract undef refs yields an undef' ); is( scalar @weakrefs, 2, 'extract undef refs removes from array' ); ok( isweak $weakrefs[0], "extract_by still doesn't break weakrefs" ); } done_testing; 10_weighted_shuffle_by.t100644001750001750 75114227505500 21260 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/tuse strict; use warnings; use Test::More; use List::UtilsBy::XS qw(weighted_shuffle_by); is_deeply( [ weighted_shuffle_by { } ], [], 'empty list' ); is_deeply( [ weighted_shuffle_by { 1 } "a" ], [ "a" ], 'unit list' ); my @vals = weighted_shuffle_by { 1 } "a", "b", "c"; is_deeply( [ sort @vals ], [ "a", "b", "c" ], 'set of return values' ); @vals = weighted_shuffle_by { ord $_ } "a", "b", "c"; is_deeply( [ sort @vals ], [ "a", "b", "c" ], 'set of return values' ); done_testing; 11_bundle_by.t100644001750001750 124414227505500 17234 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/tuse strict; use warnings; use Test::More; use List::UtilsBy::XS qw(bundle_by); my @gots; my $expected; @gots = bundle_by { $_[0] } 1, (1, 2, 3); is_deeply(\@gots, [ 1, 2, 3 ], 'bundle_by 1'); @gots = bundle_by { $_[0] } 2, (1, 2, 3, 4); is_deeply(\@gots, [ 1, 3 ], 'bundle_by 2 first'); @gots = bundle_by { @_ } 2, (1, 2, 3, 4); is_deeply(\@gots, [ 1, 2, 3, 4 ], 'bundle_by 2 all') || diag explain \@gots; @gots = bundle_by { [ @_ ] } 2, (1, 2, 3, 4); is_deeply(\@gots, [ [ 1, 2 ], [ 3, 4 ] ], 'bundle_by 2 [all]'); my %gots = bundle_by { uc $_[1] => $_[0] } 2, qw( a b c d ); is_deeply(\%gots, { B => "a", D => "c" }, 'bundle_by 2 constructing hash'); done_testing; 12_unzip_by.t100644001750001750 145114227505500 17131 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/tuse strict; use warnings; use Test::More; use List::UtilsBy::XS qw( unzip_by ); my @expected; is_deeply( [ unzip_by { } ], [], 'empty list' ); is_deeply( [ unzip_by { $_ } qw/a b c/ ], [ [ qw/a b c/ ] ], 'identity function' ); @expected = ([ qw/a b c/ ], [ qw/a b c/ ]); is_deeply( [ unzip_by { $_, $_ } qw/a b c/ ], \@expected, 'clone function' ); @expected = ([ qw/a b c/ ], [ 1, 2, 3 ]); is_deeply( [ unzip_by { m/(.)/g } qw/a1 b2 c3/ ], \@expected, 'regexp match function' ); @expected = ([ qw/a b c/ ], [ undef, 2, undef ]); is_deeply( [ unzip_by { m/(.)/g } qw/a b2 c/ ], \@expected, 'non-rectangular adds undef' ); @expected = ([ qw/a b c/ ], [ 'A', 2, undef ], [ 1, undef, undef ]); is_deeply( [ unzip_by { m/(.)/g } qw/aA1 b2 c/ ], \@expected, 'non-rectangular adds undef2' ); done_testing; 99_leaktrace.t100644001750001750 213214227505500 17241 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/t#!perl use strict; use warnings; BEGIN { use Test::More; eval "use Test::LeakTrace"; plan skip_all => "Test::LeakTrace required for memory leak testing" if $@; use List::UtilsBy::XS qw(:all); }; no_leaks_ok { my @a = sort_by { $_ } 1 .. 10; } 'sort_by'; no_leaks_ok { my @a = nsort_by { $_ } 1 .. 10; } 'nsort_by'; no_leaks_ok { my @a = max_by { $_ } 1 .. 10; } 'max_by'; no_leaks_ok { my @a = uniq_by { $_ } 1 .. 10; } 'uniq_by'; no_leaks_ok { my %a = partition_by { $_ } 1 .. 10; } 'partition_by'; no_leaks_ok { my %a = count_by { $_ } 1 .. 10; } 'count_by'; no_leaks_ok { my @a = zip_by { $_ } [1..10], [1..10]; } 'zip_by'; no_leaks_ok { my @a = unzip_by { $_ } 1..10; } 'unzip_by'; my @array = (1..10); no_leaks_ok { my @a = extract_by { $_ } @array; } 'extract_by'; my @array2 = (1..10); no_leaks_ok { my $a = extract_by { $_ } @array2; } 'extract_by with scalar context'; no_leaks_ok { my @a = weighted_shuffle_by { $_ } 1..10; } 'weighted_shuffle_by'; no_leaks_ok { my @a = bundle_by { $_ } 1..10; } 'bundle_by'; done_testing; UtilsBy.xs100644001750001750 4113514227505500 17551 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/xs-src#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" struct sort_elem { SV *key; SV *orig; }; static I32 sv_cmp_str_asc(pTHX_ SV *sv1, SV *sv2) { struct sort_elem *se1, *se2; se1 = (struct sort_elem*)SvIV(sv1); se2 = (struct sort_elem*)SvIV(sv2); return sv_cmp_locale(se1->key, se2->key); } static I32 sv_cmp_str_desc(pTHX_ SV *sv1, SV *sv2) { struct sort_elem *se1, *se2; se1 = (struct sort_elem*)SvIV(sv1); se2 = (struct sort_elem*)SvIV(sv2); return sv_cmp_locale(se2->key, se1->key); } static I32 sv_cmp_number_asc(pTHX_ SV *sv1, SV *sv2) { struct sort_elem *se1, *se2; NV key1, key2; se1 = (struct sort_elem*)SvIV(sv1); se2 = (struct sort_elem*)SvIV(sv2); key1 = SvNV(se1->key); key2 = SvNV(se2->key); return (key1 > key2) ? 1 : (key1 == key2) ? 0 : -1; } static I32 sv_cmp_number_desc(pTHX_ SV *sv1, SV *sv2) { struct sort_elem *se1, *se2; NV key1, key2; se1 = (struct sort_elem*)SvIV(sv1); se2 = (struct sort_elem*)SvIV(sv2); key1 = SvNV(se2->key); key2 = SvNV(se1->key); return (key1 > key2) ? 1 : (key1 == key2) ? 0 : -1; } MODULE = List::UtilsBy::XS PACKAGE = List::UtilsBy::XS void sort_by (code, ...) SV *code PROTOTYPE: &@ ALIAS: sort_by = 0 rev_sort_by = 1 CODE: { dMULTICALL; GV *gv; HV *stash; I32 gimme = G_SCALAR; SV **args = &PL_stack_base[ax]; int i; AV *tmps; struct sort_elem *elems; if (items <= 1) { XSRETURN_EMPTY; } tmps = (AV *)sv_2mortal((SV *)newAV()); cv = sv_2cv(code, &stash, &gv, 0); if (cv == Nullcv) { croak("Not a subroutine reference"); } PUSH_MULTICALL(cv); SAVESPTR(GvSV(PL_defgv)); Newx(elems, items - 1, struct sort_elem); for (i = 1; i < items; i++) { struct sort_elem *elem = &elems[i - 1]; GvSV(PL_defgv) = args[i]; MULTICALL; elem->key = newSVsv(*PL_stack_sp); elem->orig = newSVsv(args[i]); av_push(tmps, newSViv((IV)elem)); } POP_MULTICALL; if (ix) { sortsv(AvARRAY(tmps), av_len(tmps) + 1, sv_cmp_str_desc); } else { sortsv(AvARRAY(tmps), av_len(tmps) + 1, sv_cmp_str_asc); } for (i = 1; i < items; i++) { struct sort_elem *elem; elem = (struct sort_elem *)SvIV(*av_fetch(tmps, i-1, 0)); ST(i-1) = sv_2mortal(elem->orig); (void)sv_2mortal(elem->key); } Safefree(elems); XSRETURN(items - 1); } void nsort_by (code, ...) SV *code PROTOTYPE: &@ ALIAS: nsort_by = 0 rev_nsort_by = 1 CODE: { dMULTICALL; GV *gv; HV *stash; I32 gimme = G_SCALAR; SV **args = &PL_stack_base[ax]; int i; AV *tmps; struct sort_elem *elems; if (items <= 1) { XSRETURN_EMPTY; } tmps = (AV *)sv_2mortal((SV *)newAV()); cv = sv_2cv(code, &stash, &gv, 0); if (cv == Nullcv) { croak("Not a subroutine reference"); } PUSH_MULTICALL(cv); SAVESPTR(GvSV(PL_defgv)); Newx(elems, items - 1, struct sort_elem); for (i = 1; i < items; i++) { struct sort_elem *elem = &elems[i - 1]; GvSV(PL_defgv) = args[i]; MULTICALL; elem->key = newSVsv(*PL_stack_sp); elem->orig = newSVsv(args[i]); av_push(tmps, newSViv((IV)elem)); } POP_MULTICALL; if (ix) { sortsv(AvARRAY(tmps), av_len(tmps) + 1, sv_cmp_number_desc); } else { sortsv(AvARRAY(tmps), av_len(tmps) + 1, sv_cmp_number_asc); } for (i = 1; i < items; i++) { struct sort_elem *elem; elem = (struct sort_elem *)SvIV(*av_fetch(tmps, i-1, 0)); ST(i-1) = sv_2mortal(elem->orig); (void)sv_2mortal(elem->key); } Safefree(elems); XSRETURN(items - 1); } void min_by (code, ...) SV *code PROTOTYPE: &@ ALIAS: min_by = 0 max_by = 1 nmin_by = 2 nmax_by = 3 CODE: { dMULTICALL; GV *gv; HV *stash; I32 gimme = G_SCALAR; SV **args = &ST(1); I32 const len = items - 1; int i; AV *tmps; NV max; IV ret_count = 0; struct sort_elem *elems, *first; if (len < 1) { XSRETURN_EMPTY; } tmps = (AV *)sv_2mortal((SV *)newAV()); cv = sv_2cv(code, &stash, &gv, 0); if (cv == Nullcv) { croak("Not a subroutine reference"); } PUSH_MULTICALL(cv); SAVESPTR(GvSV(PL_defgv)); Newx(elems, items - 1, struct sort_elem); for (i = 0; i < len; i++) { struct sort_elem *elem = &elems[i]; GvSV(PL_defgv) = args[i]; MULTICALL; elem->key = newSVsv(*PL_stack_sp); elem->orig = newSVsv(args[i]); av_push(tmps, newSViv((IV)elem)); } POP_MULTICALL; if (ix & 0x1) { sortsv(AvARRAY(tmps), len, sv_cmp_number_desc); } else { sortsv(AvARRAY(tmps), len, sv_cmp_number_asc); } for(i = 0; i < len; i++) { struct sort_elem* elem = (struct sort_elem*)SvIVx(*av_fetch(tmps, i, TRUE)); sv_2mortal(elem->key); sv_2mortal(elem->orig); } first = (struct sort_elem *)SvIV(*av_fetch(tmps, 0, 0)); max = SvNV(first->key); ST(0) = first->orig; ret_count++; if (GIMME_V != G_ARRAY) { goto ret; } for (i = 2; i < items; i++) { struct sort_elem *elem; elem = (struct sort_elem *)SvIV(*av_fetch(tmps, i-1, 0)); if (max == SvNV(elem->key)) { ST(ret_count) = elem->orig; ret_count++; } else { goto ret; } } ret: Safefree(elems); XSRETURN(ret_count); } void uniq_by (code, ...) SV *code PROTOTYPE: &@ CODE: { dMULTICALL; GV *gv; HV *stash; I32 gimme = G_SCALAR; SV **args = &PL_stack_base[ax]; int i; AV *tmps; HV *rh; if (items <= 1) { XSRETURN_EMPTY; } tmps = (AV *)sv_2mortal((SV *)newAV()); rh = (HV *)sv_2mortal((SV *)newHV()); cv = sv_2cv(code, &stash, &gv, 0); if (cv == Nullcv) { croak("Not a subroutine reference"); } PUSH_MULTICALL(cv); SAVESPTR(GvSV(PL_defgv)); for (i = 1; i < items; i++) { STRLEN len; char *str; GvSV(PL_defgv) = args[i]; MULTICALL; str = SvPV(*PL_stack_sp, len); if (!hv_exists(rh, str, len)) { av_push(tmps, newSVsv(args[i])); (void)hv_store(rh, str, len, newSViv(1), 0); } } POP_MULTICALL; for (i = 0; i <= av_len(tmps); i++) { ST(i) = *av_fetch(tmps, i, 0); } XSRETURN(av_len(tmps) + 1); } void partition_by (code, ...) SV *code PROTOTYPE: &@ CODE: { dMULTICALL; GV *gv; HV *stash; I32 gimme = G_SCALAR; SV **args = &PL_stack_base[ax]; int i; HV *rh; HE *iter = NULL; if (items <= 1) { XSRETURN_EMPTY; } rh = (HV *)sv_2mortal((SV *)newHV()); cv = sv_2cv(code, &stash, &gv, 0); if (cv == Nullcv) { croak("Not a subroutine reference"); } PUSH_MULTICALL(cv); SAVESPTR(GvSV(PL_defgv)); for (i = 1; i < items; i++) { STRLEN len; char *str; GvSV(PL_defgv) = args[i]; MULTICALL; str = SvPV(*PL_stack_sp, len); if (!hv_exists(rh, str, len)) { AV* av = (AV *)sv_2mortal((SV *)newAV()); av_push(av, newSVsv(args[i])); (void)hv_store(rh, str, len, newRV_inc((SV *)av), 0); } else { AV *av = (AV *)SvRV(*hv_fetch(rh, str, len, 0)); av_push(av, newSVsv(args[i])); } } POP_MULTICALL; hv_iterinit(rh); i = 0; while ( (iter = hv_iternext( rh )) != NULL ) { ST(i) = hv_iterkeysv(iter); i++; ST(i) = hv_iterval(rh, iter); i++; } XSRETURN(i); } void count_by (code, ...) SV *code PROTOTYPE: &@ CODE: { dMULTICALL; GV *gv; HV *stash; I32 gimme = G_SCALAR; SV **args = &PL_stack_base[ax]; int i; HV *rh; HE *iter = NULL; if (items <= 1) { XSRETURN_EMPTY; } rh = (HV *)sv_2mortal((SV *)newHV()); cv = sv_2cv(code, &stash, &gv, 0); if (cv == Nullcv) { croak("Not a subroutine reference"); } PUSH_MULTICALL(cv); SAVESPTR(GvSV(PL_defgv)); for (i = 1; i < items; i++) { STRLEN len; char *str; GvSV(PL_defgv) = args[i]; MULTICALL; str = SvPV(*PL_stack_sp, len); if (!hv_exists(rh, str, len)) { SV* count = newSViv(1); (void)hv_store(rh, str, len, count, 0); } else { SV **count = hv_fetch(rh, str, len, 0); sv_inc(*count); } } POP_MULTICALL; hv_iterinit(rh); i = 0; while ( (iter = hv_iternext( rh )) != NULL ) { ST(i) = hv_iterkeysv(iter); i++; ST(i) = hv_iterval(rh, iter); i++; } XSRETURN(i); } void zip_by (code, ...) SV *code PROTOTYPE: &@ CODE: { dSP; SV **args = &PL_stack_base[ax]; AV *tmps, *retvals; I32 i, j, count; I32 len, max_length = -1; if (items <= 1) { XSRETURN_EMPTY; } tmps = (AV *)sv_2mortal((SV *)newAV()); retvals = (AV *)sv_2mortal((SV *)newAV()); for (i = 1; i < items; i++) { if (!SvROK(args[i]) || (SvTYPE(SvRV(args[i])) != SVt_PVAV)) { croak("arguments should be ArrayRef"); } len = av_len((AV*)SvRV(args[i])); if (len > max_length) { max_length = len; } av_push(tmps, newSVsv(args[i])); } SAVESPTR(GvSV(PL_defgv)); for (i = 0; i <= max_length; i++) { ENTER; SAVETMPS; PUSHMARK(sp); for (j = 1; j < items; j++) { AV *av = (AV*)SvRV( *av_fetch(tmps, j-1, 0) ); if (av_exists(av, i)) { SV *elem = *av_fetch(av, i, 0); XPUSHs(sv_2mortal(newSVsv(elem))); } else { XPUSHs(&PL_sv_undef); } } PUTBACK; count = call_sv(code, G_ARRAY); SPAGAIN; len = av_len(retvals); for (j = 0; j < count; j++) { av_store(retvals, len + (count - j), newSVsv(POPs)); } PUTBACK; FREETMPS; LEAVE; } len = av_len(retvals) + 1; for (i = 0; i < len; i++) { ST(i) = *av_fetch(retvals, i, 0); } XSRETURN(len); } void unzip_by (code, ...) SV *code PROTOTYPE: &@ CODE: { dSP; SV **args = &PL_stack_base[ax]; AV *retvals; I32 i, j, count; I32 len, max_len = 0; if (items <= 1) { XSRETURN_EMPTY; } retvals = (AV *)sv_2mortal((SV *)newAV()); SAVESPTR(GvSV(PL_defgv)); for (i = 1; i < items; i++) { ENTER; SAVETMPS; PUSHMARK(sp); XPUSHs(sv_2mortal(newSVsv(args[i]))); PUTBACK; GvSV(PL_defgv) = args[i]; count = call_sv(code, G_ARRAY); SPAGAIN; for (j = max_len; j < count; j++) { AV *tmp = (AV *)sv_2mortal((SV *)newAV()); av_store(retvals, j, newRV((SV*)tmp)); } if (max_len < count) { max_len = count; } for (j = count - 1; j >= 0; j--) { SV *ret = newSVsv(POPs); AV *tmp = (AV *)SvRV((SV*)*av_fetch(retvals, j, 0)); av_store(tmp, i - 1, ret); } PUTBACK; FREETMPS; LEAVE; } len = av_len(retvals) + 1; for (i = 0; i < len; i++) { AV *tmp = (AV *)SvRV((SV*)*av_fetch(retvals, i, 0)); for (j = av_len(tmp) + 1; j < (items - 1); j++) { av_push(tmp, &PL_sv_undef); } } for (i = 0; i < len; i++) { ST(i) = *av_fetch(retvals, i, 0); } XSRETURN(len); } void extract_by (code, ...) SV *code PROTOTYPE: &\@ CODE: { dMULTICALL; GV *gv; HV *stash; I32 gimme = G_SCALAR, ret_gimme = GIMME_V; SV **args = &PL_stack_base[ax]; IV i, len; AV *ret_vals, *remains, *origs; if (items <= 1) { XSRETURN_EMPTY; } ret_vals = (AV *)sv_2mortal((SV *)newAV()); remains = (AV *)sv_2mortal((SV *)newAV()); cv = sv_2cv(code, &stash, &gv, 0); if (cv == Nullcv) { croak("Not a subroutine reference"); } if (!SvROK(args[1]) || (SvTYPE(SvRV(args[1])) != SVt_PVAV)) { croak("arguments should be ArrayRef"); } origs = (AV*)SvRV(args[1]); len = av_len((AV*)SvRV(args[1])) + 1; PUSH_MULTICALL(cv); SAVESPTR(GvSV(PL_defgv)); for (i = 0; i < len; i++) { SV *val, *arg; arg = *av_fetch(origs, i, 0); GvSV(PL_defgv) = arg; MULTICALL; val = newSVsv(*PL_stack_sp); if (SvTRUE(val)) { av_push(ret_vals, newSVsv(arg)); } else { SV *val = newSVsv(arg); SvFLAGS(val) = SvFLAGS(arg); av_push(remains, val); } } POP_MULTICALL; av_clear(origs); len = av_len(remains) + 1; for (i = 0; i < len; i++) { SV *val = *av_fetch(remains, i, 0); av_push(origs, newSVsv(val)); } if (ret_gimme == G_SCALAR) { len = 1; ST(0) = sv_2mortal(newSViv(av_len(ret_vals)+1)); } else { len = av_len(ret_vals) + 1; EXTEND(SP, len); for (i = 0; i < len; i++) { ST(i) = sv_mortalcopy(*av_fetch(ret_vals, i, 0)); } } XSRETURN(len); } void weighted_shuffle_by (code, ...) SV *code PROTOTYPE: &@ CODE: { dMULTICALL; GV *gv; HV *stash; I32 gimme = G_SCALAR; SV **args = &PL_stack_base[ax]; I32 i, len; AV *weights, *origs, *retvals; if (items <= 1) { XSRETURN_EMPTY; } weights = (AV *)sv_2mortal((SV *)newAV()); origs = (AV *)sv_2mortal((SV *)newAV()); retvals = (AV *)sv_2mortal((SV *)newAV()); cv = sv_2cv(code, &stash, &gv, 0); if (cv == Nullcv) { croak("Not a subroutine reference"); } PUSH_MULTICALL(cv); SAVESPTR(GvSV(PL_defgv)); for (i = 1; i < items; i++) { av_push(origs, newSVsv(args[i])); GvSV(PL_defgv) = args[i]; MULTICALL; av_push(weights, newSVsv(*PL_stack_sp)); } POP_MULTICALL; /* Initialize Drand01 if rand() or srand() has not already been called */ if (!PL_srand_called) { (void)seedDrand01((Rand_seed_t)seed()); PL_srand_called = TRUE; } while ( (av_len(origs) + 1) > 1) { IV total = 0; I32 select; I32 idx; SV *selected, *last; len = av_len(weights) + 1; for (i = 0; i < len; i++) { total += SvIV(*av_fetch(weights, i, 0)); } select = (I32)(Drand01() * (double)total); idx = 0; while (select >= SvIV(*av_fetch(weights, idx, 0))) { select -= SvIV(*av_fetch(weights, idx, 0)); if (av_len(weights) > idx) { idx++; } else { break; } } selected = *av_fetch(origs, idx, 0); av_push(retvals, newSVsv(selected)); last = *av_fetch(origs, av_len(origs), 0); av_store(origs, idx, last); (void)av_pop(origs); last = *av_fetch(weights, av_len(weights), 0); av_store(weights, idx, last); (void)av_pop(weights); } len = av_len(origs) + 1; for (i = 0 ; i < len; i++) { av_push(retvals, av_shift(origs)); } for (i = 1 ; i < items; i++) { ST(i-1) = sv_2mortal(newSVsv( *av_fetch(retvals, i-1, 0) )); } XSRETURN(items-1); } void bundle_by (code, ...) SV *code PROTOTYPE: &@ CODE: { dSP; SV **args = &PL_stack_base[ax]; AV *retvals; IV argnum; I32 i, j, count, len, loop; if (items <= 1) { XSRETURN_EMPTY; } argnum = SvIV(args[1]); if (argnum <= 0) { croak("bundle number is larger than 0"); } retvals = (AV *)sv_2mortal((SV *)newAV()); SAVESPTR(GvSV(PL_defgv)); for (i = 2, loop = 0; i < items; i += argnum, loop++) { ENTER; SAVETMPS; PUSHMARK(sp); for (j = 0; j < argnum; j++) { I32 index = (loop * argnum) + j + 2; if (SvOK(args[index])) { XPUSHs(sv_2mortal(newSVsv(args[index]))); } else { XPUSHs(&PL_sv_undef); } } PUTBACK; count = call_sv(code, G_ARRAY); SPAGAIN; len = av_len(retvals); for (j = 0; j < count; j++) { av_store(retvals, len + (count - j), newSVsv(POPs)); } PUTBACK; FREETMPS; LEAVE; } len = av_len(retvals) + 1; for (i = 0; i < len; i++) { ST(i) = *av_fetch(retvals, i, 0); } XSRETURN(len); } 02_perlcritic.t100644001750001750 70114227505500 17576 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06/xtuse strict; use Test::More; eval q{ use Test::Perl::Critic 1.02 -exclude => [ 'Subroutines::ProhibitSubroutinePrototypes', 'Subroutines::ProhibitExplicitReturnUndef', 'TestingAndDebugging::ProhibitNoStrict', 'ControlStructures::ProhibitMutatingListFunctions', 'InputOutput::RequireEncodingWithUTF8Layer', ] }; plan skip_all => "Test::Perl::Critic 1.02+ is not installed." if $@; all_critic_ok('lib'); META.yml100644001750001750 173714227505500 15620 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06--- abstract: 'XS implementation of List::UtilsBy' author: - 'Syohei YOSHIDA ' build_requires: Test::More: '0.88' configure_requires: Module::Build: '0.4005' Module::Build::XSUtil: '0.02' dynamic_config: 0 generated_by: 'Minilla/v3.1.16, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: List-UtilsBy-XS no_index: directory: - t - xt - inc - share - eg - examples - author - benchmark - builder provides: List::UtilsBy::XS: file: lib/List/UtilsBy/XS.pm version: '0.06' requires: XSLoader: '0.02' parent: '0' perl: '5.008001' resources: bugtracker: https://github.com/syohex/p5-List-Utils-By/issues homepage: https://github.com/syohex/p5-List-Utils-By repository: https://github.com/syohex/p5-List-Utils-By.git version: '0.06' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' x_static_install: 0 MANIFEST100644001750001750 64014227505500 15450 0ustar00syoheisyohei000000000000List-UtilsBy-XS-0.06Build.PL Changes LICENSE META.json README.md benchmark/bench.pl builder/MyBuilder.pm cpanfile lib/List/UtilsBy/XS.pm minil.toml t/00_compile.t t/01_sort_by.t t/02_nsort_by.t t/03_max_by.t t/04_min_by.t t/05_uniq_by.t t/06_partition_by.t t/07_count_by.t t/08_zip_by.t t/09_extract_by.t t/10_weighted_shuffle_by.t t/11_bundle_by.t t/12_unzip_by.t t/99_leaktrace.t xs-src/UtilsBy.xs xt/02_perlcritic.t META.yml MANIFEST