|
Class: SmallBag
Object
|
+--Collection
|
+--Bag
|
+--SmallBag
- Package:
- stx:libbasic2
- Category:
- Collections-Unordered
- Version:
- rev:
1.2
date: 2019/02/10 13:44:06
- user: cg
- file: SmallBag.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
- Author:
- Claus Gittinger
SmallBag behaves like Bag, but is tuned for a small number of distinct objects (in the order of 10),
by not using a dictionary, but a linear array holding count-value pairs in consecutive slots.
Only use it when many small bags are required, as this will be slower if too many (distinct) elements are added.
[Instance variables:]
contents <Array> holds count/value info in consecutive slots
Bag
Set
IdentitySet
Dictionary
IdentityDictionary
OrderedCollection
Array
accessing
-
contents
-
return the dictionary which associates occurrence-counts
to the bags elements.
usage example(s):
SmallBag new contents
SmallBag new size
|
adding & removing
-
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.
-
add: newObject withOccurrences: nMore
-
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.
-
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.
-
removeAll
-
-
removeAllOccurrencesOf: oldObject ifAbsent: anExceptionBlock
-
Remove oldObject completely 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.
converting
-
asSet
-
return the receiver as a set
enumerating
-
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.
-
valuesAndCountsDo: aBlock
-
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.
private
-
initContents
-
set the contents to be an empty contents array
-
initContents: size
-
set the contents to be an empty contents array with initial size
queries
-
occurrencesOf: anObject
-
return how often anObject is in the receiver
-
size
-
return the number of bag elements
|