summaryrefslogtreecommitdiff
path: root/lib/Data/MultiValued/RangeContainer.pm
blob: b27d9673778834e14edc4b8d7757d0431721d14c (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package Data::MultiValued::RangeContainer; 
{
  $Data::MultiValued::RangeContainer::VERSION = '0.0.1_6';
}
{
  $Data::MultiValued::RangeContainer::DIST = 'Data-MultiValued';
}
use Moose;
use namespace::autoclean;
use Moose::Util::TypeConstraints;
use MooseX::Types::Moose qw(Num Str Any Undef ArrayRef);
use MooseX::Types::Structured qw(Dict);
use Data::MultiValued::Exceptions;
 
# ABSTRACT: container for ranged values 
 
 
has _storage => (
    is => 'rw',
    isa => ArrayRef[
        Dict[
            from => Num|Undef,
            to => Num|Undef,
            value => Any,
        ],
    ],
    init_arg => undef,
    default => sub { [ ]  },
);
 
 
sub get {
    my ($self,$args) = @_;
 
    my $at = $args->{at};
 
    my ($range) = $self->_get_slot_at($at);
 
    if (!$range) {
        Data::MultiValued::Exceptions::RangeNotFound->throw({
            value => $at,
        });
    }
 
    return $range;
}
 
# Num|Undef,Num|Undef,Bool,Bool 
# the bools mean "treat the undef as +inf" (-inf when omitted/false) 
sub _cmp {
    my ($a,$b,$sa,$sb) = @_;
 
    $a //$sa ? 0+'inf' : 0-'inf';
    $b //$sb ? 0+'inf' : 0-'inf';
 
    return $a <=> $b;
}
 
# a binary search would be a good idea. 
 
sub _get_slot_at {
    my ($self,$at) = @_;
 
    for my $slot (@{$self->_storage}) {
        next if _cmp($slot->{to},$at,1,0) <= 0;
        last if _cmp($slot->{from},$at,0,0) > 0;
        return $slot;
    }
    return;
}
 
# this is quite probably uselessly slow: we don't really need all of 
# @before and @after, we just need to know if they're not empty; also, 
# a binary search would be a good idea. 
 
sub _partition_slots {
    my ($self,$from,$to) = @_;
 
    my (@before,@overlap,@after);
    my $st=$self->_storage;
 
    for my $idx (0..$#$st) {
        my $slot = $st->[$idx];
 
        my ($sf,$st) = @$slot{'from','to'};
 
        if (_cmp($st,$from,1,0) <0) {
            push @before,$idx;
        }
        elsif (_cmp($sf,$to,0,1) >=0) {
            push @after,$idx;
        }
        else {
            push @overlap,$idx;
        }
    }
    return \@before,\@overlap,\@after;
}
 
 
sub get_or_create {
    my ($self,$args) = @_;
 
    my $from = $args->{from};
    my $to = $args->{to};
 
    Data::MultiValued::Exceptions::BadRange->throw({
        from => $from,
        to => $to,
    }) if _cmp($from,$to,0,1)>0;
 
    my ($range) = $self->_get_slot_at($from);
 
    if ($range
        && _cmp($range->{from},$from,0,0)==0
        && _cmp($range->{to},$to,1,1)==0) {
        return $range;
    }
 
    $range = $self->_create_slot($from,$to);
    return $range;
}
 
 
sub clear {
    my ($self,$args) = @_;
 
    my $from = $args->{from};
    my $to = $args->{to};
 
    Data::MultiValued::Exceptions::BadRange->throw({
        from => $from,
        to => $to,
    }) if _cmp($from,$to,0,1)>0;
 
    return $self->_clear_slot($from,$to);
}
 
sub _create_slot {
    my ($self,$from,$to) = @_;
 
    $self->_splice_slot($from,$to,{
        from => $from,
        to => $to,
        value => undef,
    });
}
 
sub _clear_slot {
    my ($self,$from,$to) = @_;
 
    $self->_splice_slot($from,$to);
}
 
# Most of the splicing mechanics is here. Given a range and something 
# to put in it, do "the right thing" 
 
sub _splice_slot {
    my ($self,$from,$to,$new) = @_;
 
    # if !$new, it's like C<splice> without a replacement list: we 
    # just delete the range 
 
    if (!@{$self->_storage}) { # empty, just store 
        push @{$self->_storage},$new if $new;
        return $new;
    }
 
    my ($before,$overlap,$after) = $self->_partition_slots($from,$to);
 
    if (!@$before && !@$overlap) {
        # nothing before, nothing overlapping: put $new at the beginning 
        unshift @{$self->_storage},$new if $new;
        return $new;
    }
    if (!@$after && !@$overlap) {
        # nothing after, nothing overlapping: put $new at the end 
        push @{$self->_storage},$new if $new;
        return $new;
    }
 
    # ok, we have to insert in the middle of things, and maybe we have 
    # to trim existing ranges 
 
    my $first_to_replace;
    my $how_many = @$overlap;
 
    my @replacement = $new ? ($new) : ();
 
    if ($how_many > 0) { # we have to splice 
        # by costruction, the first and the last may have to be split, all 
        # others must be removed 
        $first_to_replace = $overlap->[0];
        my $last_to_replace = $overlap->[-1];
        my $first = $self->_storage->[$first_to_replace];
        my $last = $self->_storage->[$last_to_replace];
 
        # does the first overlapping range need trimming? 
        if (_cmp($first->{from},$from,0,0)<0
            && _cmp($first->{to},$from,1,0)>=0) {
            unshift @replacement, {
                from => $first->{from},
                to => $from,
                value => $first->{value},
            }
        }
        # does the last overlapping range need trimming? 
        if (_cmp($last->{from},$to,0,1)<=0
            && _cmp($last->{to},$to,1,1)>0) {
            push @replacement, {
                from => $to,
                to => $last->{to},
                value => $last->{value},
            }
        }
    }
    else {
        # no overlaps, just insert between @before and @after 
        $first_to_replace = $before->[-1]+1;
    }
 
    splice @{$self->_storage},
           $first_to_replace,$how_many,
           @replacement;
 
    return $new;
}
 
 
sub all_ranges {
    my ($self) = @_;
 
    return map { [ $_->{from}, $_->{to} ] } @{$self->_storage};
}
 
__PACKAGE__->meta->make_immutable();
 
1;
 
__END__
=pod
 
=encoding utf-8
 
=head1 NAME
 
Data::MultiValued::RangeContainer - container for ranged values
 
=head1 VERSION
 
version 0.0.1_6
 
=head1 DESCRIPTION
 
Please don't use this module directly, use L<Data::MultiValued::Ranges>.
 
This module implements the storage for ranged data. It's similar to
L<Array::IntSpan>, but simpler (and slower).
 
A range is defined by a pair of numbers, C<from> and C<to>, and it
contains C<< Num $x : $min <= $x < $max >>. C<undef> is treated as
"inf" (negative infinity if used as C<from> or C<at>, positive
infinity if used as C<to>).
 
The internal representation of a range is a hash with three keys,
C<from> C<to> C<value>.
 
=head1 METHODS
 
=head2 C<get>
 
  my $value = $obj->get({ at => $point });
 
Retrieves the range that includes the given point. Throws a
L<Data::MultiValued::Exceptions::RangeNotFound|Data::MultiValued::Exceptions/Data::MultiValued::Exceptions::RangeNotFound>
exception if no range includes the point.
 
=head2 C<get_or_create>
 
  $obj->get_or_create({ from => $min, to => $max });
 
Retrieves the range that has the given extremes. If no such range
exists, creates a new range, splicing any existing overlapping range,
and returns it. Throws
L<Data::MultiValued::Exceptions::BadRange|Data::MultiValued::Exceptions/Data::MultiValued::Exceptions::BadRange>
if C<< $min > $max >>.
 
=head2 C<clear>
 
  $obj->clear({ from => $min, to => $max });
 
Removes the range that has the given extremes. If no such range
exists, splices any existing overlapping range so that C<<
$obj->get({at => $point }) >> for any C<< $min <= $point < $max >>
will die.
 
Throws
L<Data::MultiValued::Exceptions::BadRange|Data::MultiValued::Exceptions/Data::MultiValued::Exceptions::BadRange>
if C<< $min > $max >>.
 
=head2 C<all_ranges>
 
  my @ranges = $obj->all_ranges;
 
Returns all the ranges defined in this object, as a list of 2-elements
arrayrefs.
 
=head1 AUTHOR
 
Gianni Ceccarelli <dakkar@thenautilus.net>
 
=head1 COPYRIGHT AND LICENSE
 
This software is copyright (c) 2011 by Net-a-Porter.com.
 
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
 
=cut