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.003, Exporter
Set::Window object represents a window on the integer line;
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.
Creates and returns a new
$left and $right specify the first and last integers in the window.
If $right is less than $left, returns the empty window.
Creates 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.
Returns the number of integers in the window.
->size == $window
->right - $window
->left + 1
holds for all non-empty windows.
Returns true iff $window1 and $window2 are the same.
All empty windows are
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.
Makes 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.
offset is called on the empty window,
it returns the empty window.
Makes a copy of $window, and then shrinks it by $inset at each end. If $inset is negative, the window expands. Returns the new window.
inset is called on the empty window,
it returns the empty window.
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
the list will be empty.
If $length is less than 1, returns undef.
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::Window does not issue any diganostics;
none of the methods can
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
$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:
returns an empty list, because the empty window has no elements, while
returns undef, because the empty window has no first or last element.
Steven W. McDougall, email@example.com
Copyright (c) 1996, 1997 Steven W. McDougall. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.