File: Collection.pm

package info (click to toggle)
ack 3.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,676 kB
  • sloc: perl: 10,935; ansic: 21; fortran: 11; python: 6; makefile: 5; javascript: 3
file content (71 lines) | stat: -rw-r--r-- 1,342 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package App::Ack::Filter::Collection;

=head1 NAME

App::Ack::Filter::Collection

=head1 DESCRIPTION

The Ack::Filter::Collection class can contain filters and internally sort
them into groups. The groups can then be optimized for faster filtering.

Filters are grouped and replaced by a fast hash lookup. This leads to
improved performance when many such filters are active, like when using
the C<--known> command line option.

=cut

use strict;
use warnings;
use parent 'App::Ack::Filter';

sub new {
    my ( $class ) = @_;

    return bless {
        groups => {},
        ungrouped => [],
    }, $class;
}

sub filter {
    my ( $self, $file ) = @_;

    for my $group (values %{$self->{groups}}) {
        return 1 if $group->filter($file);
    }

    for my $filter (@{$self->{ungrouped}}) {
        return 1 if $filter->filter($file);
    }

    return 0;
}

sub add {
    my ( $self, $filter ) = @_;

    if (exists $filter->{'groupname'}) {
        my $group = ($self->{groups}->{$filter->{groupname}} ||= $filter->create_group());
        $group->add($filter);
    }
    else {
        push @{$self->{'ungrouped'}}, $filter;
    }

    return;
}

sub inspect {
    my ( $self ) = @_;

    return ref($self) . " - $self";
}

sub to_string {
    my ( $self ) = @_;

    return join(', ', map { "($_)" } @{$self->{ungrouped}});
}

1;