eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'Bag':

Home

everywhere
www.exept.de
for:
[back]

Class: Bag


Inheritance:

   Object
   |
   +--Collection
      |
      +--Bag

Package:
stx:libbasic
Category:
Collections-Unordered
Version:
rev: 1.41 date: 2010/03/30 13:38:23
user: stefan
file: Bag.st directory: libbasic
module: stx stc-classLibrary: libbasic
Author:
Claus Gittinger

Description:


Bag implements collections whose elements are unordered and have no
external key. Elements may occur more than once in a bag. There is no defined
order within a bag.
The default implementation uses a dictionary to store each objects occurrence
count, using the object itself as key (i.e. using = and hash for inclusion
tests).

There is also an instance creation variant (#identityNew:) creating a
bag which compares using #== and hashes using #identityHash.
(I'd say that an IdentityBag was a better thing to implement ...
 ... but for compatibility ... we do it here as well)

[Instance variables:]

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


Related information:

    Set
    IdentitySet
    Dictionary
    IdentityDictionary
    OrderedCollection
    Array

Class protocol:

instance creation
o  equalityNew: size
return a new empty Bag with initial space for size elements.
Elements will be compared using equality compare (i.e. #= not #== identity).

o  identityNew
return a new empty Bag which compares for identity
Elements will be compared using identity compare (i.e. #== not #= equality).

o  identityNew: size
return a new empty Bag with initial space for size elements.
Elements will be compared using identity compare (i.e. #== not #= equality).

o  new
return a new empty Bag which compares for equality (i.e. not identity)

o  new: size
return a new empty Bag with initial space for size elements.
Elements will be compared using equality compare (i.e. #= not #== identity).


Instance protocol:

Compatibility-Dolphin
o  asAssociations
return the dictionary which associates occurrence-counts
to the bags elements.
Same as #contents for dolphin compatibility.

accessing
o  at: index
report an error: at: is not allowed for Bags

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

o  contents
return the dictionary which associates occurrence-counts
to the bags elements.

adding & removing
o  add: newObject
add the argument, anObject to the receiver.
Returns the object.

WARNING: do not add/remove elements while iterating over the receiver.
Iterate over a copy to do this.

o  add: newObject withOccurrences: anInteger
add the argument, anObject anInteger times to the receiver.
Returns the object.

WARNING: do not add/remove elements while iterating over the receiver.
Iterate over a copy to do this.

o  remove: oldObject ifAbsent: anExceptionBlock
Remove oldObject from the collection.
If it was not present, return the value of the exceptionBlock;
otherwise return the removed object.

WARNING: do not add/remove elements while iterating over the receiver.
Iterate over a copy to do this.

o  removeAll
remove all objects from the receiver collection (i.e. make it empty).
Returns the receiver.

o  removeAllOccurrencesOf: oldObject ifAbsent: anExceptionBlock
Remove all occurrences of oldObject from the collection.
If it was not present, return the value of the exceptionBlock;
otherwise return the number of removes.

WARNING: do not add/remove elements while iterating over the receiver.
Iterate over a copy to do this.

bulk operations
o  sum
sum up all elements; return 0 for an empty collection.
can be done easier, using bags knowledge.

comparing
o  = aBag
Compare the receiver with the argument and return true if the
receiver is equal to the argument (i.e. has the same size and elements).
Otherwise return false.

o  hash
return an integer useful for hashing on the receiver;
redefined since = is redefined here.

converting
o  asBag
return the receiver as a bag

o  asSet
return the receiver as a set

copying
o  postCopy
must copy the contents as well

enumerating
o  do: aBlock
evaluate the block for all elements in the collection.

WARNING: do not add/remove elements while iterating over the receiver.
Iterate over a copy to do this.

o  valuesAndCounts
return an orderedCollection containing value->count associations

o  valuesAndCountsDo: aTwoArgBlock
evaluate the block for all distinct elements in the collection,
passing both the element and the occurrence count as arguments.

WARNING: do not add/remove elements while iterating over the receiver.
Iterate over a copy to do this.

printing & storing
o  printElementsDo: aBlock

private
o  initContents
set the contents to be an empty Dictionary

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

o  initContentsForIdentity
set the contents to be an empty IdentityDictionary

o  initContentsForIdentity: size
set the contents to be an empty IdentityDictionary with initial size

queries
o  size
return the number of bag elements

testing
o  includes: anObject
return true, if anObject is in the receiver

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



ST/X 6.1.1; WebServer 1.620 at exept:8081; Wed, 23 May 2012 07:55:14 GMT