|
Class: SmallBag
Object
|
+--Collection
|
+--Bag
|
+--SmallBag
- Package:
- stx:libbasic2
- Category:
- Collections-Unordered
- Version:
- rev:
1.5
date: 2021/01/20 15:40:16
- user: cg
- file: SmallBag.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
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
copyrightCOPYRIGHT (c) 2013 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.
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
-
(comment from inherited method)
remove all objects from the receiver collection (i.e. make it empty).
Returns the receiver.
-
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
Usage example(s):
|b|
b := Bag new.
b add:1; add:2; add:3; add:1; add:1.
b asSet.
|
enumerating
-
do: aBlock
-
evaluate the block for all elements in the collection.
Notice: the order of evaluation is not defined (and may appear random, actually)
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
-
initContentsForEquality: size
-
set the contents to be an empty contents array with initial size
-
initContentsForIdentity: 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
|