Bio::GMOD::Blast::Graph::IntSpan
use Bio::GMOD::Blast::Graph::IntSpan; $Bio::GMOD::Blast::Graph::IntSpan::Empty_String = $string; $set = new Bio::GMOD::Blast::Graph::IntSpan $set_spec; $valid = valid Bio::GMOD::Blast::Graph::IntSpan $run_list; copy $set $set_spec; $run_list = run_list $set; @elements = elements $set; $u_set = union $set $set_spec; $i_set = intersect $set $set_spec; $x_set = xor $set $set_spec; $d_set = diff $set $set_spec; $c_set = complement $set; equal $set $set_spec; equivalent $set $set_spec; superset $set $set_spec; subset $set $set_spec; $n = cardinality $set; empty $set; finite $set; neg_inf $set; pos_inf $set; infinite $set; universal $set; member $set $n; insert $set $n; remove $set $n; $min = min $set; $max = max $set;
Bio::GMOD::Blast::Graph::IntSpan manages sets of integers. It is optimized for sets that have long runs of consecutive integers. These arise, for example, in .newsrc files, which maintain lists of articles:
alt.foo: 1-21,28,31 alt.bar: 1-14192,14194,14196-14221
Sets are stored internally in a run-length coded form. This provides for both compact storage and efficient computation. In particular, set operations can be performed directly on the encoded representation.
Bio::GMOD::Blast::Graph::IntSpan is designed to manage finite sets. However, it can also represent some simple infinite sets, such as {x | x>n}. This allows operations involving complements to be carried out consistently, without having to worry about the actual value of MAXINT on your machine.
Copyright (c) 1996 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.
Bio::GMOD::Blast::Graph::IntSpan - Manages sets of integers
Perl 5.002
Exporter
None
Many of the methods take a set specification. There are four kinds of set specifications.
If a set specification is omitted, then the empty set is assumed. Thus,
$set = new Bio::GMOD::Blast::Graph::IntSpan;
creates a new, empty, set. Similarly,
copy $set;
removes all elements from $set.
If an object reference is given, it is taken to be a Bio::GMOD::Blast::Graph::IntSpan object.
If an array reference is given, then the elements of the array are taken to be the elements of the set. The array may contain duplicate elements. The elements of the array may be in any order.
If a string is given, it is taken to be a run list. A run list specifies a set using a syntax similar to that in .newsrc files.
A run list is a comma-separated list of runs. Each run specifies a set of consecutive integers. The set is the union of all the runs.
Runs may be written in any of several forms.
The empty set is consistently written as '' (the null string). It is also denoted by the special form '-' (a single dash).
The runs in a run list must be disjoint, and must be listed in increasing order.
Valid characters in a run list are 0-9, '(', ')', '-' and ','. White space and underscore (_) are ignored. Other characters are not allowed.
{ }
{ 1 }
{ 1, 2 }
{ -5, -4, -3, -2, -1 }
the integers
the negative integers
{ 1, 2, 3, 4, 18, 19, 20, 21 }
Creates and returns a new set. The initial contents of the set are given by $set_spec.
Returns true if $run_list is a valid run list. Otherwise, returns false and leaves an error message in $@.
Copies $set_spec into $set. The previous contents of $set are lost. For convenience, copy() returns $set.
Returns a run list that represents $set. The run list will not contain white space. $set is not affected.
By default, the empty set is formatted as '-'; a different string may be specified in $Bio::GMOD::Blast::Graph::IntSpan::Empty_String.
Returns an array containing the elements of $set. The elements will be sorted in numerical order. In scalar context, returns an array reference. $set is not affected.
returns the set of integers in either $set or $set_spec
returns the set of integers in both $set and $set_spec
returns the set of integers in $set or $set_spec, but not both
returns the set of integers in $set but not in $set_spec
returns the complement of $set.
For all set operations, a new Bio::GMOD::Blast::Graph::IntSpan object is created and returned. The operands are not affected.
Returns true iff $set and $set_spec contain the same elements.
Returns true iff $set and $set_spec contain the same number of elements. All infinite sets are equivalent.
Returns true iff $set is a superset of $set_spec.
Returns true iff $set is a subset of $set_spec.
Returns the number of elements in $set. Returns -1 for infinite sets.
Returns true iff $set is empty.
Returns true iff $set is finite.
Returns true iff $set contains {x | x<n} for some n.
Returns true iff $set contains {x | x>n} for some n.
Returns true iff $set is infinite.
Returns true iff $set contains all integers.
Returns true iff the integer $n is a member of $set.
Inserts the integer $n into $set. Does nothing if $n is already a member of $set.
Removes the integer $n from $set. Does nothing if $n is not a member of $set.
Returns the smallest element of $set, or undef if there is none.
Returns the largest element of $set, or undef if there is none.
$Bio::GMOD::Blast::Graph::IntSpan::Empty_String contains the string that is returned when run_list() is called on the empty set. $Empty_String is initially '-'; alternatively, it may be set to ''. Other values should be avoided, to ensure that run_list() always returns a valid run list.
run_list() accesses $Empty_String through a reference stored in $set->{empty_string}. Subclasses that wish to override the value of $Empty_String can reassign this reference.
Any method (except valid()) will die() if it is passed an invalid run list. Possible messages are:
$run_list has bad syntax
$run_list has overlapping runs or runs that are out of order.
elements $set will die() if $set is infinite.
elements $set can generate an "Out of memory!" message on sufficiently large finite sets.
Beware of forms like
union $set [1..5];
This passes an element of @set to union, which is probably not what you want. To force interpretation of $set and [1..5] as separate arguments, use forms like
union $set +[1..5];
or
$set->union([1..5]);
There are two common approaches to error handling: exceptions and return codes. There seems to be some religion on the topic, so Bio::GMOD::Blast::Graph::IntSpan provides support for both.
To catch exceptions, protect method calls with an eval:
$run_list = <STDIN>; eval { $set = new Bio::GMOD::Blast::Graph::IntSpan $run_list }; $@ and print "$@: try again\n";
To check return codes, use an appropriate method call to validate arguments:
$run_list = <STDIN>; if (valid Bio::GMOD::Blast::Graph::IntSpan $run_list) { $set = new Bio::GMOD::Blast::Graph::IntSpan $run_list } else { print "$@ try again\n" }
Similarly, use finite() to protect calls to elements():
finite $set and @elements = elements $set;
Calling elements() on a large, finite set can generate an "Out of memory!" message, which cannot be trapped. Applications that must retain control after an error can use intersect() to protect calls to elements():
@elements = elements { intersect $set "-1_000_000 - 1_000_000" };
or check the size of $set first:
finite $set and cardinality $set < 2_000_000 and @elements = elements $set;
Although Bio::GMOD::Blast::Graph::IntSpan can represent some infinite sets, it does not perform infinite-precision arithmetic. Therefore, finite elements are restricted to the range of integers on your machine.
The sets implemented here are based on Macintosh data structures called "regions". See Inside Macintosh for more information.
Steven McDougall <swm@cric.com>
Copyright (c) 1996 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.
This software is Copyright (c) 2011 by The Board of Trustees of Leland Stanford Junior University.
This is free software, licensed under:
The Artistic License 1.0