eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'NumberSet':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: NumberSet


Inheritance:

   Object
   |
   +--Collection
      |
      +--SequenceableCollection
         |
         +--NumberSet

Package:
stx:libbasic2
Category:
Collections-Ordered
Version:
rev: 1.14 date: 2017/11/08 06:14:58
user: cg
file: NumberSet.st directory: libbasic2
module: stx stc-classLibrary: libbasic2
Author:
Claus Gittinger (spring '92)

Description:


NumberSets are sets holding positive integers.

This class has been written especially to represent number-ranges as found in .newsrc-files,
and it supports reading/writing of that format (for example: '0-62,69,82,84,86,88,91').
It is space optimized for sparse sets of numbers, containing a mix of single numbers
and chunks of sequential sub ranges. 
When adding elements, holes between 2 subranges
are detected, and merged into single subranges.
It may need some care to be used in other situations.

The implementation uses an array of intervals or individual numbers.

Reading and writing is in .newsrc-format.


[timing:]
    Time millisecondsToRun:[
        10000 timesRepeat:[
            (NumberSet with:(0 to:255)) removeAllFoundIn:(10 to:20); yourself.
        ]
    ]

    Time millisecondsToRun:[
        10000 timesRepeat:[
            (Set withAll:(0 to:255)) removeAllFoundIn:(10 to:20); yourself.
        ]
    ] 
    
[example:]
    |s|

    s := NumberSet new.
    s add:123.
    s add:255.
    s add:124.
    s add:-5.
    s addAll:(125 to:188).
    s
    


Related information:

    NewsHandler
    Interval
    Set

Class protocol:

input from a stream
o  readFrom: aStringOrStream
read a NumberSet (in .newsrc format) from aStream.
return an empty NumberSet if nothing can be read (or wrong format)
usage example(s):
     NumberSet readFrom:('0-62,69,82,84,86,88,91' readStream).
     NumberSet readFrom:('abc' readStream).

o  readFrom: aStringOrStream onError: exceptionalValue
read a NumberSet (in .newsrc format) from aStream.
return an empty NumberSet if nothing can be read (or wrong format)
usage example(s):
     NumberSet readFrom:('0-62,69,82,84,86,88,91' readStream).

instance creation
o  new: size
(comment from inherited method)
return an instance of myself with anInteger indexed variables

o  with: aNumberOrInterval
return a new numberSet containing a single number
or the numbers from an interval
usage example(s):
     NumberSet with:69
     NumberSet with:(1 to:50)

     NumberSet withAll:(1 to:50)


Instance protocol:

accessing
o  lastOfFirstInterval
return the last element of the first interval.
Return nil if the receiver is empty.

adding & removing
o  add: aNumber
add the argument, aNumber to the NumberSet

o  remove: aNumber ifAbsent: aBlock
remove the argument, aNumber from the NumberSet

enumerating
o  do: aBlock
evaluate the argument, aBlock for each element in the numberSet

printing
o  displayOn: aStream
return a nice string for the inspector
usage example(s):
     (NumberSet readFrom:'1,2,4-10,15') displayOn:Transcript
     (NumberSet readFrom:'1,2,4-10,15') printOn:Transcript

o  printOn: aStream
output the NumberSet (in .newsrc format) on aStream

private
o  readFrom: aStringOrStream
read my value from aStream (in .newsrc format).
Raise an error if the format is invalid
usage example(s):
     NumberSet readFrom:'0-62,69,82,84,86,88,91'
     NumberSet readFrom:'0-62,69,82,84,86,88,91' readStream
     
     NumberSet new readFrom:('0-62,69,82,84,86,88,91' readStream)
     NumberSet new readFrom:'0-62,69,82,84,86,88,91'

     NumberSet readFromString:'0+62,69,82,84,86,88,91'      -- raises an error
     NumberSet readFrom:'1+62,69,82,84,86,88,91'            -- stops at '+'

o  readFrom: aStringOrStream onError: exceptionalValue
read my value from aStream (in .newsrc format)
usage example(s):
     NumberSet readFrom:'0-62,69,82,84,86,88,91'
     NumberSet readFrom:'0-62,69,82,84,86,88,91' readStream
     
     NumberSet new readFrom:('0-62,69,82,84,86,88,91' readStream)
     NumberSet new readFrom:'0-62,69,82,84,86,88,91'

o  tryMerge: index
try to make element at index and index + 1 be one element

private accessing
o  setFirst: aNumberOrInterval
set the first element

queries
o  first
answer the first i.e. lowest number in the set.
If the receiver is empty, return nil.

o  includes: aNumber
answer true, if aNumber is in the set

o  last
answer the last i.e. highest number in the set.
If the receiver is empty, return nil.

o  size
return the number of elements in the Set



ST/X 7.1.0.0; WebServer 1.663 at exept.de:8081; Wed, 19 Sep 2018 09:04:47 GMT