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.17 date: 2022/07/15 11:23:16
user: cg
file: NumberSet.st directory: libbasic2
module: stx stc-classLibrary: libbasic2

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
    

copyright

COPYRIGHT (c) 1992 by Claus Gittinger All Rights Reserved This software is furnished under a license and may be used only in accordance with the terms of that license and with the inclusion of the above copyright notice. This software may not be provided or otherwise made available to, or used by, any other person. No title to or ownership of the software is hereby transferred.

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).

Usage example(s):

     |ns s string|

     ns := NumberSet new.
     ns add:1.
     ns add:2.
     ns add:3.
     ns add:5.
     s := '' writeStream.
     ns printOn:s.
     string := s contents.

     NumberSet readFrom:string.     

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).

Usage example(s):

     |ns s string|

     ns := NumberSet new.
     ns add:1.
     ns add:2.
     ns add:3.
     ns add:5.
     s := '' writeStream.
     ns printOn:s.
     string := s contents.

     NumberSet readFrom:string.     

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)

o  withAll: aCollectionOfNumbersOrIntervals
return a new numberSet containing all elements from the argument collection,
which has either numbers or intervals as elements

Usage example(s):

     NumberSet with:69
     NumberSet with:(1 to:50)

     NumberSet withAll:(1 to:50)
     NumberSet withAll:{ 1 . 2 . 5 . (10 to:20) . (30 to:50) . 99 }


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.7.0.0; WebServer 1.702 at 20f6060372b9.unknown:8081; Thu, 10 Oct 2024 12:23:37 GMT