ActiveState!

ActivePerl Documentation
Table of Contents

(Usage Statistics)
(about this ver)


* Getting Started
    * Welcome To ActivePerl
    * Release Notes
    * Readme
    * ActivePerl Change Log
* Install Notes
    * Linux
    * Solaris
    * Windows
* ActivePerl Components
    * Overview
    * PPM
    * Windows Specifics
       * OLE Browser
       * PerlScript
       * Perl for ISAPI
       * PerlEZ
* ActivePerl FAQ
    * Introduction
    * Availability & Install
    * Using PPM
    * Docs & Support
    * Windows Specifics
       * Perl for ISAPI
       * Windows 9X/NT/2000
       * Quirks
       * Web Server Config
       * Web programming
       * Programming
       * Modules & Samples
       * Embedding & Extending
       * Using OLE with Perl
* Windows Scripting
    * Active Server Pages
    * Windows Script Host
    * Windows Script Components

Core Perl Documentation


* perl
* perlfaq
* perltoc
* perlbook

* perlsyn
* perldata
* perlop
* perlreftut
* perldsc
* perllol

* perllexwarn
* perldebug

* perlrun
* perlfunc
* perlopentut
* perlvar
* perlsub
* perlmod
* perlpod

* perlstyle
* perlmodlib
* perlmodinstall
* perltrap
* perlport
* perlsec

* perlref
* perlre
* perlform
* perllocale
* perlunicode

* perlboot
* perltoot
* perltootc
* perlobj
* perlbot
* perltie

* perlipc
* perlnumber
* perlfork
* perlthrtut

* perldiag
* perlfaq1
* perlfaq2
* perlfaq3
* perlfaq4
* perlfaq5
* perlfaq6
* perlfaq7
* perlfaq8
* perlfaq9

* perlcompile

* perlembed
* perlxstut
* perlxs
* perlguts
* perlcall
* perlfilter
* perldbmfilter
* perlapi
* perlintern
* perlapio
* perltodo
* perlhack

* perlhist
* perldelta
* perl5005delta
* perl5004delta

* perlamiga
* perlcygwin
* perldos
* perlhpux
* perlmachten
* perlos2
* perlos390
* perlvms
* perlwin32

Pragmas


* attributes
* attrs
* autouse
* base
* blib
* bytes
* charnames
* constant
* diagnostics
* fields
* filetest
* integer
* less
* lib
* locale
* lwpcook
* open
* ops
* overload
* perllocal
* re
* sigtrap
* strict
* subs
* utf8
* vars
* warnings

Libraries


* ActivePerl
    * DocTools
        * TOC
            * RDF
* AnyDBM_File
* Archive
    * Tar
* AutoLoader
* AutoSplit
* B
    * Asmdata
    * Assembler
    * Bblock
    * Bytecode
    * C
    * CC
    * Debug
    * Deparse
    * Disassembler
    * Lint
    * Showlex
    * Stackobj
    * Terse
    * Xref
* Benchmark
* Bundle
    * LWP
* ByteLoader
* Carp
    * Heavy
* CGI
    * Apache
    * Carp
    * Cookie
    * Fast
    * Pretty
    * Push
    * Switch
* Class
    * Struct
* Compress
    * Zlib
* Config
* CPAN
    * FirstTime
    * Nox
* Cwd
* Data
    * Dumper
* DB
* Devel
    * DProf
    * Peek
    * SelfStubber
* Digest
    * HMAC
    * HMAC_MD5
    * HMAC_SHA1
    * MD2
    * MD5
    * SHA1
* DirHandle
* Dumpvalue
* DynaLoader
* English
* Env
* Errno
* Exporter
    * Heavy
* ExtUtils
    * Command
    * Embed
    * Install
    * Installed
    * Liblist
    * MakeMaker
    * Manifest
    * Miniperl
    * Mkbootstrap
    * Mksymlists
    * MM_Cygwin
    * MM_OS2
    * MM_Unix
    * MM_VMS
    * MM_Win32
    * Packlist
    * testlib
* Fatal
* Fcntl
* File
    * Basename
    * CheckTree
    * Compare
    * Copy
    * CounterFile
    * DosGlob
    * Find
    * Glob
    * Listing
    * Path
    * Spec
        * Functions
        * Mac
        * OS2
        * Unix
        * VMS
        * Win32
    * stat
* FileCache
* FileHandle
* FindBin
* Font
    * AFM
* Getopt
    * Long
    * Std
* HTML
    * AsSubs
    * Element
    * Entities
    * Filter
    * Form
    * FormatPS
    * Formatter
    * FormatText
    * HeadParser
    * LinkExtor
    * Parse
    * Parser
    * TokeParser
    * TreeBuilder
* HTTP
    * Cookies
    * Daemon
    * Date
    * Headers
        * Util
    * Message
    * Negotiate
    * Request
        * Common
    * Response
    * Status
* I18N
    * Collate
* IO
    * Dir
    * File
    * Handle
    * Pipe
    * Poll
    * Seekable
    * Select
    * Socket
        * INET
        * UNIX
* IPC
    * Msg
    * Open2
    * Open3
    * Semaphore
    * SysV
* LWP
    * Debug
    * MediaTypes
    * MemberMixin
    * Protocol
    * RobotUA
    * Simple
    * UserAgent
* Math
    * BigFloat
    * BigInt
    * Complex
    * Trig
* MD5
* MIME
    * Base64
    * QuotedPrint
* NDBM_File
* Net
    * Cmd
    * Config
    * Domain
    * DummyInetd
    * FTP
    * hostent
    * libnetFAQ
    * netent
    * Netrc
    * NNTP
    * PH
    * Ping
    * POP3
    * protoent
    * servent
    * SMTP
    * SNPP
    * Time
* O
* ODBM_File
* Opcode
* Pod
    * Checker
    * Find
    * Html
    * InputObjects
    * Man
    * Parser
    * ParseUtils
    * Plainer
    * Select
    * Text
        * Color
        * Termcap
    * Usage
* POSIX
* PPM
    * SOAPClient
    * SOAPServer
* Safe
* SDBM_File
* Search
    * Dict
* SelectSaver
* SelfLoader
* SHA
* Shell
* SOAP
    * Defs
    * Envelope
    * EnvelopeMaker
    * GenericHashSerializer
    * GenericInputStream
    * GenericScalarSerializer
    * Lite
    * OutputStream
    * Packager
    * Parser
    * Transport
        * HTTP
            * Apache
            * CGI
            * Client
            * Server
        * LOCAL
        * MAILTO
        * POP3
        * TCP
    * TypeMapper
* Socket
* Symbol
* Sys
    * Hostname
    * Syslog
* Term
    * ANSIColor
    * Cap
    * Complete
    * ReadLine
* Test
    * Harness
* Text
    * Abbrev
    * ParseWords
    * Soundex
    * Tabs
    * Wrap
* Thread
    * Queue
    * Semaphore
    * Signal
    * Specific
* Tie
    * Array
    * Handle
    * Hash
    * RefHash
    * Scalar
    * SubstrHash
* Time
    * gmtime
    * Local
    * localtime
    * tm
* UDDI
    * Lite
* UNIVERSAL
* URI
    * data
    * Escape
    * file
    * Heuristic
    * ldap
    * URL
    * WithBase
* User
    * grent
    * pwent
* Win32
    * AuthenticateUser
    * ChangeNotify
    * Clipboard
    * Console
    * Event
    * EventLog
    * File
    * FileSecurity
    * Internet
    * IPC
    * Mutex
    * NetAdmin
    * NetResource
    * ODBC
    * OLE
        * Const
        * Enum
        * NEWS
        * NLS
        * TPJ
        * Variant
    * PerfLib
    * Pipe
    * Process
    * Registry
    * Semaphore
    * Service
    * Sound
    * TieRegistry
* Win32API
    * File
    * Net
    * Registry
* WWW
    * RobotRules
        * AnyDBM_File
* XML
    * Element
    * Parser
        * Expat
    * PPD
    * PPMConfig
    * ValidatingElement
* XSLoader

 Set::IntSpan - Manages sets of integers


NAME

Set::IntSpan - Manages sets of integers


SUPPORTED PLATFORMS

  • Linux
  • Solaris
  • Windows
This module is not included with the standard ActivePerl distribution. It is available as a separate download using PPM.

SYNOPSIS

  use Set::IntSpan qw(grep_set map_set);

  $Set::IntSpan::Empty_String = $string;

  $set    = new   Set::IntSpan $set_spec;
  $valid  = valid Set::IntSpan $run_list;
  $set    = 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;

  $subset = grep_set { ... } $set;
  $mapset = map_set  { ... } $set;

  for ($element=$set->first; defined $element; $element=$set->next) { ... }
  for ($element=$set->last ; defined $element; $element=$set->prev) { ... }

  $element = $set->start($n);
  $element = $set->current;


REQUIRES

Perl 5.004, Exporter


EXPORTS

@EXPORT

Nothing

@EXPORT_OK

grep_set, map_set


DESCRIPTION

Set::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.

Set::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 INT_MAX on your machine.


SET SPECIFICATIONS

Many of the methods take a set specification. There are four kinds of set specifications.

Empty

If a set specification is omitted, then the empty set is assumed. Thus,

  $set = new Set::IntSpan;

creates a new, empty set. Similarly,

  copy $set;

removes all elements from $set.

Object reference

If an object reference is given, it is taken to be a Set::IntSpan object.

Array reference

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.

Run list

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.

Finite forms

n
{ n }

a-b
{x | a<=x && x<=b}

Infinite forms

(-n
{x | x<=n}

n-)
{x | x>=n}

(-)
The set of all integers

Empty forms

The empty set is consistently written as '' (the null string). It is also denoted by the special form '-' (a single dash).

Restrictions

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.

Examples

 -
{ }

 1
{ 1 }

 1-2
{ 1, 2 }

 -5--1
{ -5, -4, -3, -2, -1 }

 (-)
the integers

 (--1
the negative integers

 1-3, 4, 18-21
{ 1, 2, 3, 4, 18, 19, 20, 21 }


ITERATORS

Each set has a single iterator, which is shared by all calls to first, last, start, next, prev, and current. At all times, the iterator is either an element of the set, or undef.

first, last, and start set the iterator; next, and prev move it; and current returns it. Calls to these methods may be freely intermixed.

Using next and prev, a single loop can move both forwards and backwards through a set. Using start, a loop can iterate over portions of an infinite set.


METHODS

Creation

$set = new Set::IntSpan $set_spec
Creates and returns a Set::IntSpan object. The initial contents of the set are given by $set_spec.

$ok = valid Set::IntSpan $run_list
Returns true if $run_list is a valid run list. Otherwise, returns false and leaves an error message in $@.

$set = copy $set $set_spec
Copies $set_spec into $set. The previous contents of $set are lost. For convenience, copy returns $set.

$run_list = run_list $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 $Set::IntSpan::Empty_String.

@elements = elements $set
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.

Set operations

$u_set = union $set $set_spec
Returns the set of integers in either $set or $set_spec.

$i_set = intersect $set $set_spec
Returns the set of integers in both $set and $set_spec.

$x_set = xor $set $set_spec
Returns the set of integers in $set or $set_spec, but not both.

$d_set = diff $set $set_spec
Returns the set of integers in $set but not in $set_spec.

$c_set = complement $set
Returns the set of integers that are not in $set.

For all set operations, a new Set::IntSpan object is created and returned. The operands are not affected.

Comparison

equal $set $set_spec
Returns true iff $set and $set_spec contain the same elements.

equivalent $set $set_spec
Returns true iff $set and $set_spec contain the same number of elements. All infinite sets are equivalent.

superset $set $set_spec
Returns true iff $set is a superset of $set_spec.

subset $set $set_spec
Returns true iff $set is a subset of $set_spec.

Cardinality

$n = cardinality $set
Returns the number of elements in $set. Returns -1 for infinite sets.

empty $set
Returns true iff $set is empty.

finite $set
Returns true iff $set is finite.

neg_inf $set
Returns true iff $set contains {x | x<n} for some n.

pos_inf $set
Returns true iff $set contains {x | x>n} for some n.

infinite $set
Returns true iff $set is infinite.

universal $set
Returns true iff $set contains all integers.

Membership

member $set $n
Returns true iff the integer $n is a member of $set.

insert $set $n
Inserts the integer $n into $set. Does nothing if $n is already a member of $set.

remove $set $n
Removes the integer $n from $set. Does nothing if $n is not a member of $set.

Extrema

min $set
Returns the smallest element of $set, or undef if there is none.

max $set
Returns the largest element of $set, or undef if there is none.

Iterators

$set->first
Sets the iterator for $set to the smallest element of $set. If there is no smallest element, sets the iterator to undef. Returns the iterator.

$set->last
Sets the iterator for $set to the largest element of $set. If there is no largest element, sets the iterator to undef. Returns the iterator.

$set->start($n)
Sets the iterator for $set to $n. If $n is not an element of $set, sets the iterator to undef. Returns the iterator.

$set->next
Sets the iterator for $set to the next element of $set. If there is no next element, sets the iterator to undef. Returns the iterator.

next will return undef only once; the next call to next will reset the iterator to the smallest element of $set.

$set->prev
Sets the iterator for $set to the previous element of $set. If there is no previous element, sets the iterator to undef. Returns the iterator.

prev will return undef only once; the next call to prev will reset the iterator to the largest element of $set.

$set->current
Returns the iterator for $set.


FUNCTIONS

$sub_set = grep_set { ... } $set
Evaluates the BLOCK for each integer in $set (locally setting $_ to each integer) and returns a Set::IntSpan object containing those integers for which the BLOCK returns TRUE.

Returns undef if $set is infinite.

$map_set = map_set { ... } $set
Evaluates the BLOCK for each integer in $set (locally setting $_ to each integer) and returns a Set::IntSpan object containg all the integers returned as results of all those evaluations.

Evaluates the BLOCK in list context, so each element of $set may produce zero, one, or more elements in the returned set.

Returns undef if $set is infinite.


CLASS VARIABLES

$Set::IntSpan::Empty_String
$Set::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.


DIAGNOSTICS

Any method (except valid) will die if it is passed an invalid run list.

Set::IntSpan::_copy_run_list: Bad syntax: $runList
(F) $run_list has bad syntax

Set::IntSpan::_copy_run_list: Bad order: $runList
(F) $run_list has overlapping runs or runs that are out of order.

Set::IntSpan::elements: infinite set
(F) An infinte set was passed to elements.

Out of memory!
(X) elements $set can generate an ``Out of memory!'' message on sufficiently large finite sets.


NOTES

Traps

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]);

Cardinality

You cannot use the obvious comparison routine

  { $a->cardinality <=> $b->cardinality }

to sort sets by size, because cardinality returns -1 for infinte sets. (All the non-negative integers were taken. Sorry.)

Instead, you have to write something like

  {  my $a_card = $a->cardinality;
     my $b_card = $b->cardinality;

     $a_card == $b_card and return  0;
     $a_card <  0       and return  1;
     $b_card <  0       and return -1;
     $a_card <=> $b_card                }

grep_set and map_set

grep_set and map_set make it easy to construct sets for which the internal representation used by Set::IntSpan is not small. Consider:

  $billion = new Set::IntSpan '0-1_000_000_000';   # OK
  $odd     = grep_set { $_ & 1 } $billion;         # trouble
  $even    = map_set  { $_ * 2 } $billion;         # double trouble

Error handling

There are two common approaches to error handling: exceptions and return codes. There seems to be some religion on the topic, so Set::IntSpan provides support for both.

To catch exceptions, protect method calls with an eval:

    $run_list = <STDIN>;
    eval { $set = new Set::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 Set::IntSpan $run_list) 
       { $set = new Set::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 (easily) 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;

Limitations

Although Set::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.

Roots

The sets implemented here are based on a Macintosh data structure called a region. See Inside Macintosh for more information.


AUTHOR

Steven McDougall, swmcd@world.std.com


COPYRIGHT

Copyright 1996-1998 by Steven McDougall. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

 Set::IntSpan - Manages sets of integers