Set::Window - Manages an interval on the integer line
use Set::Window; $window = new_lr Set::Window $left, $right; $window = new_ll Set::Window $left, $length; $window = empty Set::Window; $left = $window->left; $right = $window->right; $size = $window->size; @bounds = $window->bounds; @elements = $window->elements; empty $window; eqivalent $window1 $window2; equal $window1 $window2; $window = copy $window $window = offset $window $offset $window = inset $window $inset $window = cover $window @windows $window = intersect $window @windows @windows = $window->series($length);
Perl 5.6.0, Exporter
Nothing
A Set::Window
object represents a window on the integer line; that is, a finite set of consecutive integers.
Methods are provided for creating and modifying windows, for obtaining information about windows, and for performing some simple set operations on windows.
The empty window represents the empty set. Like the empty set, the empty window is unique.
new_lr Set::Window
$left,
$rightCreates and returns a new Set::Window
object. $left and $right specify the first and last integers in the window.
If $right is less than $left, returns the empty window.
new_ll Set::Window
$left,
$lengthCreates and returns a new Set::Window object. $left is the first integer in the interval, and $length is the number of integers in the interval
If $length is less than one, returns the empty window.
empty Set::Window
Creates and returns an empty Set::Window
object.
->left
Returns the first integer in the window, or undef if $window is empty.
->right
Returns the last integer in the window, or undef if $window is empty.
->size
Returns the number of integers in the window.
The identity $window->size ==
$window->right -
$window->left + 1
holds for all non-empty windows.
->bounds
Returns a list of the first and last integers in $window, or undef if $window is empty. In scalar context, returns an array reference.
->elements
Returns a list of the integers in $window, in order. In scalar context, returns an array reference.
empty
$windowReturns true iff $window is empty.
equal
$window1 $window2Returns true iff $window1 and $window2 are the same.
All empty windows are equal
.
equivalent
$window1 $window2Returns true iff $window1 and $window2 are the same size.
These methods implement copy semantics: modifications are made to a copy of the original window. The original window is unaltered, and the new window is returned.
copy
$windowCreates and returns a (deep) copy of $window.
offset
$window $offsetMakes a copy of $window, and then shifts it by $offset. Positive values of $offset move the window to the right; negative values move it to the left. Returns the new window.
If offset
is called on the empty window, it returns the empty window.
inset
$window $insetMakes a copy of $window, and then shrinks it by $inset at each end. If $inset is negative, the window expands. Returns the new window.
If inset
is called on the empty window, it returns the empty window.
cover
$window @windowsCreates and returns the smallest window that covers (i.e. contains) $window and all the @windows.
intersect
$window @windowsCreates and returns the largest window that is contained by $window and all the @windows. This may be the empty window.
->series(
$length)
Returns a list of all the windows of $length that are contained in $window, ordered from left to right. In scalar context, returns an array reference.
If $length is greater than $window->length
, the list will be empty. If $length is less than 1, returns undef.
None.
Belive it or not, I actually needed this structure in a program. Maybe someone else will need it, too.
Set::Window
objects are designed to be lightweight. If you need more functionality, consider using Set::IntSpan
.
Set::Window
does not issue any diganostics; in particular, none of the methods can die
.
Calling elements
on a large window can lead to an out of memory!
message, which cannot be trapped (as of perl 5.003). Applications that must retain control can protect calls to elements
with an intersect
$limit = new_lr Set::Window -1_000_000, 1_000_000; @elements = $window->intersect($limit)->elements;
or check the size of $window first:
length $window < 2_000_000 and @elements = elements $window;
Operations involving the empty window are handled consistently. They return valid results if they make sense, and undef otherwise. Thus:
Set::Window->empty->elements
returns an empty list, because the empty window has no elements, while
Set::Windows->empty->bounds
returns undef, because the empty window has no first or last element.
perl(1), Set::IntSpan
Steven McDougall, swmcd@world.std.com
Copyright 1996-200 by Steven McDougall. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.