Tie-IxHash-1.23000755000000000000 012112375415 13672 5ustar00unknownunknown000000000000Tie-IxHash-1.23/Build.PL000444000000000000 76412112371221 15301 0ustar00unknownunknown000000000000use strict; use Module::Build; #created by eumm-migrate.pl my $build = Module::Build->new( 'auto_configure_requires' => 0, 'module_name' => 'Tie::IxHash', 'requires' => { 'perl' => '5.005', }, 'build_requires' => { 'Test::More' => 0, }, 'meta_merge' => { 'resources' => { 'repository' => 'git://github.com/chorny/Tie-IxHash.git' } }, 'license' => 'perl', 'dist_version_from' => 'lib/Tie/IxHash.pm', ); $build->create_build_script(); Tie-IxHash-1.23/Changes000444000000000000 131512112373217 15320 0ustar00unknownunknown000000000000=head1 NAME HISTORY - release history for Tie::IxHash =head1 DESCRIPTION =over 8 =item 1.23 (24 February 2013) New method Clear() Deleting current element when doing cycle using each will work (test by OLEG, RT#82248) =item 1.22 (27 February 2010) Build.PL added Better META.yml Distribution upgrade =item 1.21 (5 January 1998) Key()/Values()/Indices() now return a single value when called with single argument (makes them useful in scalar contexts) =item 1.2 (18 February 1997) Repackaged into a tarball. Added a testsuite. C suggested by Michael De La Rue . =item 1.1 Initial release (ancient). =back =cut Tie-IxHash-1.23/Makefile.PL000444000000000000 265112112170016 15773 0ustar00unknownunknown000000000000use ExtUtils::MakeMaker; WriteMakefile1( NAME => "Tie::IxHash", VERSION_FROM => 'lib/Tie/IxHash.pm', LICENSE => 'perl', MIN_PERL_VERSION => '5.005', META_MERGE => { resources => { repository => 'git://github.com/chorny/Tie-IxHash.git', }, }, PL_FILES => {}, #SKIP => [qw(static dynamic)], #'linkext' => {LINKTYPE => '' }, #'dist' => {COMPRESS=>'gzip -9f', SUFFIX => 'gz'}, ); sub WriteMakefile1 { #Written by Alexandr Ciornii, version 0.21. Added by eumm-upgrade. my %params=@_; my $eumm_version=$ExtUtils::MakeMaker::VERSION; $eumm_version=eval $eumm_version; die "EXTRA_META is deprecated" if exists $params{EXTRA_META}; die "License not specified" if not exists $params{LICENSE}; if ($params{BUILD_REQUIRES} and $eumm_version < 6.5503) { #EUMM 6.5502 has problems with BUILD_REQUIRES $params{PREREQ_PM}={ %{$params{PREREQ_PM} || {}} , %{$params{BUILD_REQUIRES}} }; delete $params{BUILD_REQUIRES}; } delete $params{CONFIGURE_REQUIRES} if $eumm_version < 6.52; delete $params{MIN_PERL_VERSION} if $eumm_version < 6.48; delete $params{META_MERGE} if $eumm_version < 6.46; delete $params{META_ADD} if $eumm_version < 6.46; delete $params{LICENSE} if $eumm_version < 6.31; delete $params{AUTHOR} if $] < 5.005; delete $params{ABSTRACT_FROM} if $] < 5.005; delete $params{BINARY_LOCATION} if $] < 5.005; WriteMakefile(%params); } Tie-IxHash-1.23/MANIFEST000444000000000000 20012112371620 15122 0ustar00unknownunknown000000000000MANIFEST README Changes Makefile.PL Build.PL lib/Tie/IxHash.pm t/ixhash.t t/each-delete.t t/pod.t META.yml META.json Tie-IxHash-1.23/META.json000444000000000000 201012112375415 15441 0ustar00unknownunknown000000000000{ "abstract" : "ordered associative arrays for Perl", "author" : [ "Gurusamy Sarathy gsar@umich.edu" ], "dynamic_config" : 1, "generated_by" : "Module::Build version 0.4003, CPAN::Meta::Converter version 2.112621", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Tie-IxHash", "prereqs" : { "build" : { "requires" : { "Test::More" : 0 } }, "runtime" : { "requires" : { "perl" : "5.005" } } }, "provides" : { "Tie::IxHash" : { "file" : "lib/Tie/IxHash.pm", "version" : "1.23" } }, "release_status" : "stable", "resources" : { "license" : [ "http://dev.perl.org/licenses/" ], "repository" : { "url" : "git://github.com/chorny/Tie-IxHash.git" } }, "version" : "1.23" } Tie-IxHash-1.23/META.yml000444000000000000 112112112375415 15273 0ustar00unknownunknown000000000000--- abstract: ordered associative arrays for Perl author: - Gurusamy Sarathy gsar@umich.edu build_requires: Test::More: 0 dynamic_config: 1 generated_by: 'Module::Build version 0.4003, CPAN::Meta::Converter version 2.112621' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Tie-IxHash provides: Tie::IxHash: file: lib/Tie/IxHash.pm version: 1.23 requires: perl: 5.005 resources: license: http://dev.perl.org/licenses/ repository: git://github.com/chorny/Tie-IxHash.git version: 1.23 Tie-IxHash-1.23/README000444000000000000 240712112375321 14706 0ustar00unknownunknown000000000000This is the README file for Tie::IxHash, the Perl module that implements ordered in-memory associative arrays. It requires: Perl Version 5.005 or later. If you have been led to believe that associative arrays in perl don't preserve order, and if you have ever craved for that feature, this module is for you. Simply declare a "tie" for the hash variable that you want to be order-preserving, and forget that limitation ever existed. You can do other nifty things with the tied hash object that you may be used to doing with arrays, like Push(), Pop() and Splice(). If you don't know what "tie" means, you should look at the perltie(1) manpage in a recent perl distribution, or in the index of one of the numerous books on perl. If you don't know what "perl" is, you don't need this software. See the embedded documentation in the module file for details. Don't forget to send your comments! - Sarathy. gsar@umich.edu ----------- Installation: perl Makefile.PL make install If you run into problems due to whatever reason in running the above, simply move the file IxHash.pm over into $PERL5LIB/Tie/IxHash.pm (where $PERL5LIB stands for the place where your standard perl library files are located) and you'll be okay. Tie-IxHash-1.23/lib000755000000000000 012112375415 14440 5ustar00unknownunknown000000000000Tie-IxHash-1.23/lib/Tie000755000000000000 012112375415 15161 5ustar00unknownunknown000000000000Tie-IxHash-1.23/lib/Tie/IxHash.pm000444000000000000 3510212112373175 17062 0ustar00unknownunknown000000000000# # Tie/IxHash.pm # # Indexed hash implementation for Perl # # See below for documentation. # require 5.005; package Tie::IxHash; use strict; use integer; require Tie::Hash; use vars qw/@ISA $VERSION/; @ISA = qw(Tie::Hash); $VERSION = $VERSION = '1.23'; # # standard tie functions # sub TIEHASH { my($c) = shift; my($s) = []; $s->[0] = {}; # hashkey index $s->[1] = []; # array of keys $s->[2] = []; # array of data $s->[3] = 0; # iter count bless $s, $c; $s->Push(@_) if @_; return $s; } #sub DESTROY {} # costly if there's nothing to do sub FETCH { my($s, $k) = (shift, shift); return exists( $s->[0]{$k} ) ? $s->[2][ $s->[0]{$k} ] : undef; } sub STORE { my($s, $k, $v) = (shift, shift, shift); if (exists $s->[0]{$k}) { my($i) = $s->[0]{$k}; $s->[1][$i] = $k; $s->[2][$i] = $v; $s->[0]{$k} = $i; } else { push(@{$s->[1]}, $k); push(@{$s->[2]}, $v); $s->[0]{$k} = $#{$s->[1]}; } } sub DELETE { my($s, $k) = (shift, shift); if (exists $s->[0]{$k}) { my($i) = $s->[0]{$k}; for ($i+1..$#{$s->[1]}) { # reset higher elt indexes $s->[0]{ $s->[1][$_] }--; # timeconsuming, is there is better way? } if ( $i == $s->[3]-1 ) { $s->[3]--; } delete $s->[0]{$k}; splice @{$s->[1]}, $i, 1; return (splice(@{$s->[2]}, $i, 1))[0]; } return undef; } sub EXISTS { exists $_[0]->[0]{ $_[1] }; } sub FIRSTKEY { $_[0][3] = 0; &NEXTKEY; } sub NEXTKEY { return $_[0][1][ $_[0][3]++ ] if ($_[0][3] <= $#{ $_[0][1] } ); return undef; } # # # class functions that provide additional capabilities # # sub new { TIEHASH(@_) } sub Clear { my $s = shift; $s->[0] = {}; # hashkey index $s->[1] = []; # array of keys $s->[2] = []; # array of data $s->[3] = 0; # iter count return; } # # add pairs to end of indexed hash # note that if a supplied key exists, it will not be reordered # sub Push { my($s) = shift; while (@_) { $s->STORE(shift, shift); } return scalar(@{$s->[1]}); } sub Push2 { my($s) = shift; $s->Splice($#{$s->[1]}+1, 0, @_); return scalar(@{$s->[1]}); } # # pop last k-v pair # sub Pop { my($s) = shift; my($k, $v, $i); $k = pop(@{$s->[1]}); $v = pop(@{$s->[2]}); if (defined $k) { delete $s->[0]{$k}; return ($k, $v); } return undef; } sub Pop2 { return $_[0]->Splice(-1); } # # shift # sub Shift { my($s) = shift; my($k, $v, $i); $k = shift(@{$s->[1]}); $v = shift(@{$s->[2]}); if (defined $k) { delete $s->[0]{$k}; for (keys %{$s->[0]}) { $s->[0]{$_}--; } return ($k, $v); } return undef; } sub Shift2 { return $_[0]->Splice(0, 1); } # # unshift # if a supplied key exists, it will not be reordered # sub Unshift { my($s) = shift; my($k, $v, @k, @v, $len, $i); while (@_) { ($k, $v) = (shift, shift); if (exists $s->[0]{$k}) { $i = $s->[0]{$k}; $s->[1][$i] = $k; $s->[2][$i] = $v; $s->[0]{$k} = $i; } else { push(@k, $k); push(@v, $v); $len++; } } if (defined $len) { for (keys %{$s->[0]}) { $s->[0]{$_} += $len; } $i = 0; for (@k) { $s->[0]{$_} = $i++; } unshift(@{$s->[1]}, @k); return unshift(@{$s->[2]}, @v); } return scalar(@{$s->[1]}); } sub Unshift2 { my($s) = shift; $s->Splice(0,0,@_); return scalar(@{$s->[1]}); } # # splice # # any existing hash key order is preserved. the value is replaced for # such keys, and the new keys are spliced in the regular fashion. # # supports -ve offsets but only +ve lengths # # always assumes a 0 start offset # sub Splice { my($s, $start, $len) = (shift, shift, shift); my($k, $v, @k, @v, @r, $i, $siz); my($end); # inclusive # XXX inline this ($start, $end, $len) = $s->_lrange($start, $len); if (defined $start) { if ($len > 0) { my(@k) = splice(@{$s->[1]}, $start, $len); my(@v) = splice(@{$s->[2]}, $start, $len); while (@k) { $k = shift(@k); delete $s->[0]{$k}; push(@r, $k, shift(@v)); } for ($start..$#{$s->[1]}) { $s->[0]{$s->[1][$_]} -= $len; } } while (@_) { ($k, $v) = (shift, shift); if (exists $s->[0]{$k}) { # $s->STORE($k, $v); $i = $s->[0]{$k}; $s->[1][$i] = $k; $s->[2][$i] = $v; $s->[0]{$k} = $i; } else { push(@k, $k); push(@v, $v); $siz++; } } if (defined $siz) { for ($start..$#{$s->[1]}) { $s->[0]{$s->[1][$_]} += $siz; } $i = $start; for (@k) { $s->[0]{$_} = $i++; } splice(@{$s->[1]}, $start, 0, @k); splice(@{$s->[2]}, $start, 0, @v); } } return @r; } # # delete elements specified by key # other elements higher than the one deleted "slide" down # sub Delete { my($s) = shift; for (@_) { # # XXX potential optimization: could do $s->DELETE only if $#_ < 4. # otherwise, should reset all the hash indices in one loop # $s->DELETE($_); } } # # replace hash element at specified index # # if the optional key is not supplied the value at index will simply be # replaced without affecting the order. # # if an element with the supplied key already exists, it will be deleted first. # # returns the key of replaced value if it succeeds. # sub Replace { my($s) = shift; my($i, $v, $k) = (shift, shift, shift); if (defined $i and $i <= $#{$s->[1]} and $i >= 0) { if (defined $k) { delete $s->[0]{ $s->[1][$i] }; $s->DELETE($k) ; #if exists $s->[0]{$k}; $s->[1][$i] = $k; $s->[2][$i] = $v; $s->[0]{$k} = $i; return $k; } else { $s->[2][$i] = $v; return $s->[1][$i]; } } return undef; } # # Given an $start and $len, returns a legal start and end (where start <= end) # for the current hash. # Legal range is defined as 0 to $#s+1 # $len defaults to number of elts upto end of list # # 0 1 2 ... # | X | X | X ... X | X | X | # -2 -1 (no -0 alas) # X's above are the elements # sub _lrange { my($s) = shift; my($offset, $len) = @_; my($start, $end); # both inclusive my($size) = $#{$s->[1]}+1; return undef unless defined $offset; if($offset < 0) { $start = $offset + $size; $start = 0 if $start < 0; } else { ($offset > $size) ? ($start = $size) : ($start = $offset); } if (defined $len) { $len = -$len if $len < 0; $len = $size - $start if $len > $size - $start; } else { $len = $size - $start; } $end = $start + $len - 1; return ($start, $end, $len); } # # Return keys at supplied indices # Returns all keys if no args. # sub Keys { my($s) = shift; return ( @_ == 1 ? $s->[1][$_[0]] : ( @_ ? @{$s->[1]}[@_] : @{$s->[1]} ) ); } # # Returns values at supplied indices # Returns all values if no args. # sub Values { my($s) = shift; return ( @_ == 1 ? $s->[2][$_[0]] : ( @_ ? @{$s->[2]}[@_] : @{$s->[2]} ) ); } # # get indices of specified hash keys # sub Indices { my($s) = shift; return ( @_ == 1 ? $s->[0]{$_[0]} : @{$s->[0]}{@_} ); } # # number of k-v pairs in the ixhash # note that this does not equal the highest index # owing to preextended arrays # sub Length { return scalar @{$_[0]->[1]}; } # # Reorder the hash in the supplied key order # # warning: any unsupplied keys will be lost from the hash # any supplied keys that dont exist in the hash will be ignored # sub Reorder { my($s) = shift; my(@k, @v, %x, $i); return unless @_; $i = 0; for (@_) { if (exists $s->[0]{$_}) { push(@k, $_); push(@v, $s->[2][ $s->[0]{$_} ] ); $x{$_} = $i++; } } $s->[1] = \@k; $s->[2] = \@v; $s->[0] = \%x; return $s; } sub SortByKey { my($s) = shift; $s->Reorder(sort $s->Keys); } sub SortByValue { my($s) = shift; $s->Reorder(sort { $s->FETCH($a) cmp $s->FETCH($b) } $s->Keys) } 1; __END__ =head1 NAME Tie::IxHash - ordered associative arrays for Perl =head1 SYNOPSIS # simple usage use Tie::IxHash; tie HASHVARIABLE, 'Tie::IxHash' [, LIST]; # OO interface with more powerful features use Tie::IxHash; TIEOBJECT = Tie::IxHash->new( [LIST] ); TIEOBJECT->Splice( OFFSET [, LENGTH [, LIST]] ); TIEOBJECT->Push( LIST ); TIEOBJECT->Pop; TIEOBJECT->Shift; TIEOBJECT->Unshift( LIST ); TIEOBJECT->Keys( [LIST] ); TIEOBJECT->Values( [LIST] ); TIEOBJECT->Indices( LIST ); TIEOBJECT->Delete( [LIST] ); TIEOBJECT->Replace( OFFSET, VALUE, [KEY] ); TIEOBJECT->Reorder( LIST ); TIEOBJECT->SortByKey; TIEOBJECT->SortByValue; TIEOBJECT->Length; =head1 DESCRIPTION This Perl module implements Perl hashes that preserve the order in which the hash elements were added. The order is not affected when values corresponding to existing keys in the IxHash are changed. The elements can also be set to any arbitrary supplied order. The familiar perl array operations can also be performed on the IxHash. =head2 Standard C Interface The standard C mechanism is available. This interface is recommended for simple uses, since the usage is exactly the same as regular Perl hashes after the C is declared. =head2 Object Interface This module also provides an extended object-oriented interface that can be used for more powerful operations with the IxHash. The following methods are available: =over 8 =item FETCH, STORE, DELETE, EXISTS These standard C methods mandated by Perl can be used directly. See the C entry in perlfunc(1) for details. =item Push, Pop, Shift, Unshift, Splice These additional methods resembling Perl functions are available for operating on key-value pairs in the IxHash. The behavior is the same as the corresponding perl functions, except when a supplied hash key already exists in the hash. In that case, the existing value is updated but its order is not affected. To unconditionally alter the order of a supplied key-value pair, first C the IxHash element. =item Keys Returns an array of IxHash element keys corresponding to the list of supplied indices. Returns an array of all the keys if called without arguments. Note the return value is mostly only useful when used in a list context (since perl will convert it to the number of elements in the array when used in a scalar context, and that may not be very useful). If a single argument is given, returns the single key corresponding to the index. This is usable in either scalar or list context. =item Values Returns an array of IxHash element values corresponding to the list of supplied indices. Returns an array of all the values if called without arguments. Note the return value is mostly only useful when used in a list context (since perl will convert it to the number of elements in the array when used in a scalar context, and that may not be very useful). If a single argument is given, returns the single value corresponding to the index. This is usable in either scalar or list context. =item Indices Returns an array of indices corresponding to the supplied list of keys. Note the return value is mostly only useful when used in a list context (since perl will convert it to the number of elements in the array when used in a scalar context, and that may not be very useful). If a single argument is given, returns the single index corresponding to the key. This is usable in either scalar or list context. =item Delete Removes elements with the supplied keys from the IxHash. =item Replace Substitutes the IxHash element at the specified index with the supplied value-key pair. If a key is not supplied, simply substitutes the value at index with the supplied value. If an element with the supplied key already exists, it will be removed from the IxHash first. =item Reorder This method can be used to manipulate the internal order of the IxHash elements by supplying a list of keys in the desired order. Note however, that any IxHash elements whose keys are not in the list will be removed from the IxHash. =item Length Returns the number of IxHash elements. =item SortByKey Reorders the IxHash elements by textual comparison of the keys. =item SortByValue Reorders the IxHash elements by textual comparison of the values. =item Clear Resets the IxHash to its pristine state: with no elements at all. =back =head1 EXAMPLE use Tie::IxHash; # simple interface $t = tie(%myhash, 'Tie::IxHash', 'a' => 1, 'b' => 2); %myhash = (first => 1, second => 2, third => 3); $myhash{fourth} = 4; @keys = keys %myhash; @values = values %myhash; print("y") if exists $myhash{third}; # OO interface $t = Tie::IxHash->new(first => 1, second => 2, third => 3); $t->Push(fourth => 4); # same as $myhash{'fourth'} = 4; ($k, $v) = $t->Pop; # $k is 'fourth', $v is 4 $t->Unshift(neg => -1, zeroth => 0); ($k, $v) = $t->Shift; # $k is 'neg', $v is -1 @oneandtwo = $t->Splice(1, 2, foo => 100, bar => 101); @keys = $t->Keys; @values = $t->Values; @indices = $t->Indices('foo', 'zeroth'); @itemkeys = $t->Keys(@indices); @itemvals = $t->Values(@indices); $t->Replace(2, 0.3, 'other'); $t->Delete('second', 'zeroth'); $len = $t->Length; # number of key-value pairs $t->Reorder(reverse @keys); $t->SortByKey; $t->SortByValue; =head1 BUGS You cannot specify a negative length to C. Negative indexes are OK, though. =head1 NOTE Indexing always begins at 0 (despite the current C<$[> setting) for all the functions. =head1 TODO Addition of elements with keys that already exist to the end of the IxHash must be controlled by a switch. Provide C interface when it stabilizes in Perl. Rewrite using XSUBs for efficiency. =head1 AUTHOR Gurusamy Sarathy gsar@umich.edu Copyright (c) 1995 Gurusamy Sarathy. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 VERSION Version 1.23 =head1 SEE ALSO perl(1) =cut Tie-IxHash-1.23/t000755000000000000 012112375415 14135 5ustar00unknownunknown000000000000Tie-IxHash-1.23/t/each-delete.t000444000000000000 51512112371163 16574 0ustar00unknownunknown000000000000 use strict; use Test::More tests=>2; use Tie::IxHash; my $o = tie my %h, 'Tie::IxHash'; $h{a} = 1; $h{b} = 2; $h{c} = 3; $h{d} = 4; $h{e} = 5; while (my ($k) = each %h) { if ($k =~ /b|d|e/) { delete $h{$k}; } } is(scalar(keys(%h)), 2) or diag explain(\%h); is(join(',',keys(%h)), 'a,c') or diag explain(\%h); Tie-IxHash-1.23/t/ixhash.t000444000000000000 333512112167503 15744 0ustar00unknownunknown000000000000#!../perl -w use Tie::IxHash; my $TNUM = 0; print "1..26\n"; sub T { print $_[0] ? "ok " : "not ok ", ++$TNUM, "\n" } my %bar; my $ixh = tie (%bar, 'Tie::IxHash', 'a' => 1, 'q' => 2, 'm' => 'X', 'n' => 'Y'); #$ixh = Tie::IxHash->new('a' => 1, 'q' => 2, 'm' => 'X', n => 'Y'); $ixh->Push(e => 5, f => 6); T 'a|1|q|2|m|X|n|Y|e|5|f|6' eq join('|', %bar); $ixh->Delete('e', 'a'); T 'q|2|m|X|n|Y|f|6' eq join '|', %bar; T 'q|m|n|f' eq join '|', $ixh->Keys; T '2|X|Y|6' eq join '|', $ixh->Values; T 'm|n|f' eq join '|', $ixh->Keys(1, 2, 3); T 'X|Y|6' eq join '|', $ixh->Values(1, 2, 3); $ixh->Replace(1, 9); T 'q|2|m|9|n|Y|f|6' eq join '|', %bar; $ixh->Replace(0, 8, 'f'); T 'f|8|m|9|n|Y' eq join '|', %bar; T '2|1' eq join '|', $ixh->Indices('n', 'm'); $ixh->Push(z => 1); $ixh->SortByValue; T 'z|f|m|n' eq join '|', $ixh->Keys; $ixh->SortByKey; T 'f|m|n|z' eq join '|', $ixh->Keys; T 'm' eq $ixh->Keys(1); T 'Y' eq $ixh->Values(2); T 3 == $ixh->Indices('z'); %bar = ('a' => 9, 'c' => 6, 'z' => 7, 'f' => 1); delete $bar{'z'}; $bar{'a'} = 10; T 'a|10|c|6|f|1' eq join '|', %bar; T 'a|c|f' eq join '|', keys %bar; T '10|6|1' eq join '|', values %bar; $ixh->Reorder(sort { $bar{$a} <=> $bar{$b} } keys %bar); T 'f|c|a' eq join '|', keys %bar; $ixh->Reorder('c', 'a', 'z'); T 'c|6|a|10' eq join '|', %bar; @tmp = $ixh->Splice(0, 3, 'z' => 7, 'm' => 4); T 'c|6|a|10' eq join '|', @tmp; T 'z|7|m|4' eq join '|', %bar; $ixh->Push('m' => 8); @tmp = $ixh->Pop; T 'm|8' eq join '|', @tmp; $ixh->Push('o' => 2, 'r' => 8); T 'z|7|o|2|r|8' eq join '|', %bar; $ixh->Pop; T 'z|7|o|2' eq join '|', %bar; $ixh->Splice($ixh->Length,0,$ixh->Pop); T 'z|7|o|2' eq join '|', %bar; $ixh->Clear; T $ixh->Length == 0; Tie-IxHash-1.23/t/pod.t000444000000000000 22211250273365 15220 0ustar00unknownunknown000000000000#!perl -T use Test::More; eval "use Test::Pod 1.14"; plan skip_all => "Test::Pod 1.14 required for testing POD" if $@; all_pod_files_ok();