Bag.st
author claus
Fri, 16 Jul 1993 11:39:45 +0200
changeset 1 a27a279701f8
child 2 6526dde5f3ac
permissions -rw-r--r--
Initial revision

"
 COPYRIGHT (c) 1991-93 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.
"

Collection subclass:#Bag
       instanceVariableNames:'contents'
       classVariableNames:''
       poolDictionaries:''
       category:'Collections-Unordered'
!

Bag comment:'

COPYRIGHT (c) 1991-93 by Claus Gittinger
              All Rights Reserved

Bag implements collections whose elements are unordered and have no
external keys. Elements may occur more than once.

Instance variables:

contents        <Dictionary>        for each element, the number of occurrences

%W% %E%
written jun 91 by claus
'!

!Bag class methodsFor:'instance creation'!

new
    "return a new empty Bag"

    ^ super new initContents
!

new:size
    "return a new empty Bag with initial space for size elements"

    ^ super new initContents:size
! !

!Bag methodsFor:'private'!

initContents
    "set the contents to be an empty Dictionary"

    contents := Dictionary new
!

initContents:size
    "set the contents to be an empty Dictionary with initial size"

    contents := Dictionary new:size
! !

!Bag methodsFor:'accessing'!

at:index
    "report an error: at: is not allowed for Bags"

    ^ self errorNotKeyed
!

at:index put:anObject
    "report an error: at:put: is not allowed for Bags"

    ^ self errorNotKeyed
! !

!Bag methodsFor:'testing'!

size
    "return the number of bag elements"

    |count|

    count := 0.
    contents do:[:element | count := count + element].
    ^ count
!

occurrencesOf:anObject
    "return how many times anObject is in the receiver"

    ^ contents at:anObject ifAbsent:[0]
!

includes:anObject
    "return true, if anObject is in the receiver"

    ^ contents includesKey:anObject
! !

!Bag methodsFor:'adding & removing'!

add:anObject
    "add the argument, anObject to the receiver"

    ^ self add:anObject withOccurences:1
!

add:newObject withOccurences:anInteger
    "add the argument, anObject anInteger times to the receiver"

    contents at:newObject
            put:(self occurrencesOf:newObject) + anInteger.
    ^ newObject
!

remove:oldObject ifAbsent:anExceptionBlock
    "Remove oldObject from the collection and return it
     - if it was not present, return the value of the exceptionBlock."

    |count|

    count := self occurrencesOf:oldObject.
    (count == 0) ifTrue:[^ anExceptionBlock value].
    (count == 1) ifTrue:[
        contents removeKey:oldObject
    ] ifFalse:[ 
        contents at:oldObject put:(count - 1)
    ].
    ^ oldObject
! !

!Bag methodsFor:'enumerating'!

do:aBlock
    "Perform the block for all members in the collection."

    contents associationsDo:[:assoc |
        assoc value timesRepeat:[
            aBlock value:(assoc key)
        ]
    ]
! !