summaryrefslogtreecommitdiff
path: root/Data-MultiValued/lib/Data/MultiValued/RangeContainer.pm
blob: 5c4fb3a27606577a424a7525491a8289ed363b33 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package Data::MultiValued::RangeContainer; 
use Moose;
use Moose::Util::TypeConstraints;
use MooseX::Types::Moose qw(Num Str Any Undef ArrayRef);
use MooseX::Types::Structured qw(Dict);
use Data::MultiValued::Exceptions;
 
has _storage => (
    is => 'rw',
    isa => ArrayRef[
        Dict[
            from => Num,
            to => Num,
            value => Any,
        ],
    ],
    init_arg => undef,
    default => sub { [ ]  },
);
 
sub get {
    my ($self,$args) = @_;
 
    my $at = $args->{at// 0-'inf';
 
    my ($range) = $self->_get_slot_at($at);
 
    if (!$range) {
        Data::MultiValued::Exceptions::RangeNotFound->throw({
            value => $at,
        });
    }
 
    return $range;
}
 
sub _get_slot_at {
    my ($self,$at) = @_;
 
    for my $slot (@{$self->_storage}) {
        next if $slot->{to} <= $at;
        last if $slot->{from} > $at;
        return $slot;
    }
    return;
}
 
sub _partition_slots {
    my ($self,$from,$to) = @_;
 
    my (@before,@overlap,@after);
    my $st=$self->_storage;
 
    keys @$st;
 
    while (my ($idx,$slot) = each @$st) {
        my ($sf,$st) = @$slot{'from','to'};
 
        if ($st<$from) {
            push @before,$idx;
        }
        elsif ($sf>=$to) {
            push @after,$idx;
        }
        else {
            push @overlap,$idx;
        }
    }
    return \@before,\@overlap,\@after;
}
 
sub set_or_create {
    my ($self,$args) = @_;
 
    my $from = $args->{from// 0-'inf';
    my $to = $args->{to// 0+'inf';
 
    Data::MultiValued::Exceptions::BadRange->({
        from => $from,
        to => $to,
    }) if $from > $to;
 
    my ($range) = $self->_get_slot_at($from);
 
    if ($range && $range->{from}==$from && $range->{to}==$to) {
        return $range;
    }
 
    $range = $self->_create_slot($from,$to);
    return $range;
}
 
sub _create_slot {
    my ($self,$from,$to) = @_;
 
    my $new = {
        from => $from,
        to => $to,
        value => undef,
    };
 
    if (!@{$self->_storage}) { # empty 
        push @{$self->_storage},$new;
        return $new;
    }
 
    my ($before,$overlap,$after) = $self->_partition_slots($from,$to);
 
    if (!@$before && !@$overlap) {
        unshift @{$self->_storage},$new;
        return $new;
    }
    if (!@$after && !@$overlap) {
        push @{$self->_storage},$new;
        return $new;
    }
 
    # by costruction, the first and the last may have to be split, all 
    # others must be removed 
    my $first_to_replace = $overlap->[0],
    my $last_to_replace = $overlap->[-1],
    my $how_many = @$overlap;
 
    my @replacement = ($new);
 
    if ($how_many > 0) { # we have to splice 
        my $first = $self->_storage->[$first_to_replace];
        my $last = $self->_storage->[$last_to_replace];
 
        if ($first->{from} < $from && $first->{to} >= $from) {
            unshift @replacement, {
                from => $first->{from},
                to => $from,
                value => $first->{value},
            }
        }
        if ($last->{from} < $to && $last->{to} >= $to) {
            push @replacement, {
                from => $to,
                to => $last->{to},
                value => $last->{value},
            }
        }
    }
 
    splice @{$self->_storage},
           $first_to_replace,$how_many,
           @replacement;
 
    return $new;
}
 
1;