use strict;
use Maasha::Filesys;
-use Storable;
use Data::Dumper;
-use Json::XS;
+use Time::HiRes;
+use JSON::XS;
use vars qw( @ISA @EXPORT );
{
# Martin A. Hansen, February 2010.
- # Recursively construct a Nested Containment (NC) list by added
+ # Recursively construct a Nested Containment (NC) list by adding
# a given feature to an existing NC list.
my ( $nc_list, # NC list
# Given a Nested Containment (NC) list use binary search to locate
# the NC list containing a given search position. The index of the NC
- # list containing the search position is returned.
+ # list containing the search position is returned. Extremely fast.
my ( $nc_list, # NC list
$pos, # search position
# Martin A. Hansen, February 2010.
# Recursively retrieve all features from a Nested Containment (NC) list
- # from a specified index begin to a specified index end. The index is
- # stripped.
+ # from a specified index begin to a specified index end.
+
+ # WARNING: The NC list is distroyed because the sublists are stripped.
my ( $nc_list, # NC list
$index_beg, # index begin
# Returns a list.
- my ( $i, $nc, @features );
+ my ( $i, @features );
for ( $i = $index_beg; $i <= $index_end; $i++ )
{
- $nc = Storable::dclone( $nc_list->[ $i ] );
-
- push @features, $nc;
+ push @features, $nc_list->[ $i ];
if ( defined $nc_list->[ $i ]->[ $index ] ) # sublist exists so recurse to this.
{
push @features, nc_list_get( $nc_list->[ $i ]->[ $index ], 0, scalar @{ $nc_list->[ $i ]->[ $index ] } - 1, $index );
- delete $nc->[ $index ];
+ delete $nc_list->[ $i ]->[ $index ];
}
}
}
+sub nc_list_search
+{
+ # Martin A. Hansen, February 2010.
+
+ # Recursively search a Nested Containment (NC) list for features matching
+ # a given REGEX.
+
+ my ( $nc_list, # NC list
+ $regex, # regex to search for
+ $index, # feature element with index position
+ ) = @_;
+
+ # Returns a list.
+
+ my ( $feature, @features );
+
+ foreach $feature ( @{ $nc_list } )
+ {
+ push @features, $feature if grep { $_ =~ /$regex/i if defined $_ } @{ $feature };
+
+ if ( defined $feature->[ $index ] ) # sublist exists so recurse to this.
+ {
+ push @features, nc_list_search( $feature->[ $index ], $regex, $index );
+
+ delete $feature->[ $index ];
+ }
+ }
+
+ return wantarray ? @features : \@features;
+}
+
+
sub nc_list_store
{
+ # Martin A. Hansen, February 2010.
+
+ # Store a Nested Containment (NC) list to a
+ # given file.
+
my ( $nc_list, # NC list
$file, # path to file
) = @_;
}
-sub nc_list_retieve
+sub nc_list_retrieve
{
# Martin A. Hansen, February 2010.
1;
+
+__END__
+
+ my $t0 = Time::HiRes::gettimeofday();
+ my $t1 = Time::HiRes::gettimeofday(); print STDERR "Time: " . ( $t1 - $t0 ) . "\n";