| 1 | package Acme::CPANModules::OrderedHash; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||
| 6 | 1 | our $DATE = '2023-10-06'; # DATE | |||||||||||||||||
| 7 | 1 | our $DIST = 'Acme-CPANModules-OrderedHash'; # DIST | |||||||||||||||||
| 8 | 1 | our $VERSION = '0.003'; # VERSION | |||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | our $LIST = { | ||||||||||||||||||
| 11 | summary => "List of modules that provide ordered hash data type", | ||||||||||||||||||
| 12 | description => <<'_', | ||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | When you ask a Perl's hash for the list of keys, the answer comes back | ||||||||||||||||||
| 15 | unordered. In fact, Perl explicitly randomizes the order of keys it returns | ||||||||||||||||||
| 16 | everytime. The random ordering is a (security) feature, not a bug. However, | ||||||||||||||||||
| 17 | sometimes you want to know the order of insertion. These modules provide you | ||||||||||||||||||
| 18 | with an ordered hash; most of them implement it by recording the order of | ||||||||||||||||||
| 19 | insertion of keys in an additional array. | ||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | Other related modules: | ||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | <pm:Tie::SortHash> - will automatically sort keys when you call `keys()`, | ||||||||||||||||||
| 24 | `values()`, `each()`. But this module does not maintain insertion order. | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | _ | ||||||||||||||||||
| 27 | entries => [ | ||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | { | ||||||||||||||||||
| 30 | module => 'Tie::IxHash', | ||||||||||||||||||
| 31 | bench_code => sub { | ||||||||||||||||||
| 32 | 0 | ▶ | my ($op, $numkeys, $numrep) = @_; | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | 0 | tie my %hash, "Tie::IxHash"; | |||||||||||||||||
| 35 | 0 | for (1..$numkeys) { $hash{"key$_"} = $_ } | |||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | 0 | ▶ | if ($op eq 'delete') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 38 | 0 | for (1..$numkeys) { delete $hash{"key$_"} } | |||||||||||||||||
| 39 | } elsif ($op eq 'keys') { | ||||||||||||||||||
| 40 | 0 | for (1..$numrep) { my @keys = keys %hash } | |||||||||||||||||
| 41 | } elsif ($op eq 'iterate') { | ||||||||||||||||||
| 42 | 0 | ▶ | for (1..$numrep) { while (my ($k,$v) = each %hash) {} } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 43 | } | ||||||||||||||||||
| 44 | }, | ||||||||||||||||||
| 45 | }, | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | { | ||||||||||||||||||
| 48 | module => 'Hash::Ordered', | ||||||||||||||||||
| 49 | bench_code => sub { | ||||||||||||||||||
| 50 | 0 | ▶ | my ($op, $numkeys, $numrep) = @_; | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | 0 | my $hash = Hash::Ordered->new; | |||||||||||||||||
| 53 | 0 | for (1..$numkeys) { $hash->set("key$_" => $_) } | |||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | 0 | ▶ | if ($op eq 'delete') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 56 | 0 | for (1..$numkeys) { $hash->delete("key$_") } | |||||||||||||||||
| 57 | } elsif ($op eq 'keys') { | ||||||||||||||||||
| 58 | 0 | for (1..$numrep) { my @keys = $hash->keys } | |||||||||||||||||
| 59 | } elsif ($op eq 'iterate') { | ||||||||||||||||||
| 60 | 0 | ▶ | for (1..$numrep) { my $iter = $hash->iterator; while (my ($k,$v) = $iter->()) {} } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 61 | } | ||||||||||||||||||
| 62 | }, | ||||||||||||||||||
| 63 | }, | ||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | { | ||||||||||||||||||
| 66 | module => 'Tie::Hash::Indexed', | ||||||||||||||||||
| 67 | description => <<'MARKDOWN', | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | Provides two interfaces: tied hash and OO. | ||||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | MARKDOWN | ||||||||||||||||||
| 72 | bench_code => sub { | ||||||||||||||||||
| 73 | 0 | ▶ | my ($op, $numkeys, $numrep) = @_; | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | 0 | tie my %hash, "Tie::Hash::Indexed"; | |||||||||||||||||
| 76 | 0 | for (1..$numkeys) { $hash{"key$_"} = $_ } | |||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 0 | ▶ | if ($op eq 'delete') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 79 | 0 | for (1..$numkeys) { delete $hash{"key$_"} } | |||||||||||||||||
| 80 | } elsif ($op eq 'keys') { | ||||||||||||||||||
| 81 | 0 | for (1..$numrep) { my @keys = keys %hash } | |||||||||||||||||
| 82 | } elsif ($op eq 'iterate') { | ||||||||||||||||||
| 83 | 0 | ▶ | for (1..$numrep) { while (my ($k,$v) = each %hash) {} } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 84 | } | ||||||||||||||||||
| 85 | }, | ||||||||||||||||||
| 86 | }, | ||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | { | ||||||||||||||||||
| 89 | module => 'Tie::LLHash', | ||||||||||||||||||
| 90 | bench_code => sub { | ||||||||||||||||||
| 91 | 0 | ▶ | my ($op, $numkeys, $numrep) = @_; | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | 0 | tie my %hash, "Tie::LLHash"; | |||||||||||||||||
| 94 | 0 | for (1..$numkeys) { (tied %hash)->insert("key$_" => $_) } | |||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | 0 | ▶ | if ($op eq 'delete') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 97 | 0 | for (1..$numkeys) { delete $hash{"key$_"} } | |||||||||||||||||
| 98 | } elsif ($op eq 'keys') { | ||||||||||||||||||
| 99 | 0 | for (1..$numrep) { my @keys = keys %hash } | |||||||||||||||||
| 100 | } elsif ($op eq 'iterate') { | ||||||||||||||||||
| 101 | 0 | ▶ | for (1..$numrep) { while (my ($k,$v) = each %hash) {} } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 102 | } | ||||||||||||||||||
| 103 | }, | ||||||||||||||||||
| 104 | }, | ||||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | { | ||||||||||||||||||
| 107 | module => 'Tie::StoredOrderHash', | ||||||||||||||||||
| 108 | bench_code => sub { | ||||||||||||||||||
| 109 | 0 | ▶ | my ($op, $numkeys, $numrep) = @_; | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||||||
| 110 | |||||||||||||||||||
| 111 | 0 | tie my %hash, "Tie::StoredOrderHash"; | |||||||||||||||||
| 112 | 0 | for (1..$numkeys) { $hash{"key$_"} = $_ } | |||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | 0 | ▶ | if ($op eq 'delete') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 115 | 0 | for (1..$numkeys) { delete $hash{"key$_"} } | |||||||||||||||||
| 116 | } elsif ($op eq 'keys') { | ||||||||||||||||||
| 117 | 0 | for (1..$numrep) { my @keys = keys %hash } | |||||||||||||||||
| 118 | } elsif ($op eq 'iterate') { | ||||||||||||||||||
| 119 | 0 | ▶ | for (1..$numrep) { while (my ($k,$v) = each %hash) {} } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 120 | } | ||||||||||||||||||
| 121 | }, | ||||||||||||||||||
| 122 | }, | ||||||||||||||||||
| 123 | |||||||||||||||||||
| 124 | { | ||||||||||||||||||
| 125 | module => 'Array::OrdHash', | ||||||||||||||||||
| 126 | description => <<'_', | ||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | Provide something closest to PHP's associative array, where you can refer | ||||||||||||||||||
| 129 | elements by key or by numeric index, and insertion order is remembered. | ||||||||||||||||||
| 130 | |||||||||||||||||||
| 131 | _ | ||||||||||||||||||
| 132 | bench_code => sub { | ||||||||||||||||||
| 133 | 0 | ▶ | my ($op, $numkeys, $numrep) = @_; | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||||||
| 134 | |||||||||||||||||||
| 135 | 0 | my $hash = Array::OrdHash->new; | |||||||||||||||||
| 136 | 0 | for (1..$numkeys) { $hash->{"key$_"} = $_ } | |||||||||||||||||
| 137 | |||||||||||||||||||
| 138 | 0 | ▶ | if ($op eq 'delete') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 139 | 0 | for (1..$numkeys) { delete $hash->{"key$_"} } | |||||||||||||||||
| 140 | } elsif ($op eq 'keys') { | ||||||||||||||||||
| 141 | 0 | for (1..$numrep) { my @keys = keys %$hash } | |||||||||||||||||
| 142 | } elsif ($op eq 'iterate') { | ||||||||||||||||||
| 143 | 0 | ▶ | for (1..$numrep) { while (my ($k,$v) = each %$hash) {} } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 144 | } | ||||||||||||||||||
| 145 | }, | ||||||||||||||||||
| 146 | }, | ||||||||||||||||||
| 147 | |||||||||||||||||||
| 148 | { | ||||||||||||||||||
| 149 | module => 'List::Unique::DeterministicOrder', | ||||||||||||||||||
| 150 | description => <<'MARKDOWN', | ||||||||||||||||||
| 151 | |||||||||||||||||||
| 152 | Provide a list, not hash. | ||||||||||||||||||
| 153 | |||||||||||||||||||
| 154 | MARKDOWN | ||||||||||||||||||
| 155 | bench_tags => ['no_iterate'], | ||||||||||||||||||
| 156 | bench_code => sub { | ||||||||||||||||||
| 157 | 0 | ▶ | my ($op, $numkeys, $numrep) = @_; | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||
| 158 | |||||||||||||||||||
| 159 | 0 | my $hash = List::Unique::DeterministicOrder->new(data=>[]); | |||||||||||||||||
| 160 | 0 | for (1..$numkeys) { $hash->push("key$_") } | |||||||||||||||||
| 161 | |||||||||||||||||||
| 162 | 0 | ▶ | if ($op eq 'delete') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 163 | 0 | for (1..$numkeys) { $hash->delete("key$_") } | |||||||||||||||||
| 164 | } elsif ($op eq 'keys') { | ||||||||||||||||||
| 165 | 0 | for (1..$numrep) { my @keys = $hash->keys } | |||||||||||||||||
| 166 | } elsif ($op eq 'iterate') { | ||||||||||||||||||
| 167 | 0 | die "Not implemented"; | |||||||||||||||||
| 168 | } | ||||||||||||||||||
| 169 | }, | ||||||||||||||||||
| 170 | }, | ||||||||||||||||||
| 171 | 1 | ], | |||||||||||||||||
| 172 | |||||||||||||||||||
| 173 | bench_datasets => [ | ||||||||||||||||||
| 174 | {name=>'insert 1000 pairs', argv => ['insert', 1000]}, | ||||||||||||||||||
| 175 | {name=>'insert 1000 pairs + delete', argv => ['delete', 1000]}, | ||||||||||||||||||
| 176 | {name=>'insert 1000 pairs + return keys 100 times', argv => ['keys', 1000, 100]}, | ||||||||||||||||||
| 177 | {name=>'insert 1000 pairs + iterate 10 times', argv => ['iterate', 1000, 10], exclude_participant_tags => ['no_iterate']}, | ||||||||||||||||||
| 178 | ], | ||||||||||||||||||
| 179 | }; | ||||||||||||||||||
| 180 | |||||||||||||||||||
| 181 | 1 | 1; | |||||||||||||||||
| 182 | # ABSTRACT: List of modules that provide ordered hash data type | ||||||||||||||||||
| 183 | |||||||||||||||||||
| 184 | __END__ | ||||||||||||||||||