--- a/Bag.st Fri Mar 01 21:42:07 1996 +0100
+++ b/Bag.st Fri Mar 01 21:43:49 1996 +0100
@@ -11,10 +11,10 @@
"
Collection subclass:#Bag
- instanceVariableNames:'contents'
- classVariableNames:''
- poolDictionaries:''
- category:'Collections-Unordered'
+ instanceVariableNames:'contents'
+ classVariableNames:''
+ poolDictionaries:''
+ category:'Collections-Unordered'
!
!Bag class methodsFor:'documentation'!
@@ -107,47 +107,65 @@
add:newObject
"add the argument, anObject to the receiver.
- Returns the object."
+ Returns the object.
+
+ WARNING: do not add/remove elements while iterating over the receiver.
+ Iterate over a copy to do this."
|n|
n := contents at:newObject ifAbsent:[0].
contents at:newObject put:(n + 1).
^ newObject
+
+ "Modified: 1.3.1996 / 21:43:06 / cg"
!
add:newObject withOccurences:anInteger
"add the argument, anObject anInteger times to the receiver.
- Returns the object."
+ Returns the object.
+
+ WARNING: do not add/remove elements while iterating over the receiver.
+ Iterate over a copy to do this."
|n|
n := contents at:newObject ifAbsent:[0].
contents at:newObject put:(n + anInteger).
^ newObject
+
+ "Modified: 1.3.1996 / 21:43:12 / cg"
!
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."
+ otherwise return the removed object.
+
+ WARNING: do not add/remove elements while iterating over the receiver.
+ Iterate over a copy to do this."
|count|
count := contents at:oldObject ifAbsent:[0].
(count == 0) ifTrue:[^ anExceptionBlock value].
(count == 1) ifTrue:[
- contents removeKey:oldObject
+ contents removeKey:oldObject
] ifFalse:[
- contents at:oldObject put:(count - 1)
+ contents at:oldObject put:(count - 1)
].
^ oldObject
+
+ "Modified: 1.3.1996 / 21:43:18 / cg"
!
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."
+ otherwise return the number of removes.
+
+ WARNING: do not add/remove elements while iterating over the receiver.
+ Iterate over a copy to do this."
|count|
@@ -155,6 +173,8 @@
(count == 0) ifTrue:[^ anExceptionBlock value].
contents removeKey:oldObject.
^ oldObject
+
+ "Modified: 1.3.1996 / 21:43:26 / cg"
! !
!Bag methodsFor:'converting'!
@@ -180,20 +200,30 @@
!Bag methodsFor:'enumerating'!
do:aBlock
- "evaluate the block for all elements in the collection."
+ "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."
contents keysAndValuesDo:[:key :value|
- value timesRepeat:[
- aBlock value:key
- ]
+ value timesRepeat:[
+ aBlock value:key
+ ]
]
+
+ "Modified: 1.3.1996 / 21:42:39 / cg"
!
valuesAndCountsDo:aTwoArgBlock
"evaluate the block for all distinct elements in the collection,
- passing both the element and the occurence count as arguments."
+ passing both the element and the occurence count as arguments.
+
+ WARNING: do not add/remove elements while iterating over the receiver.
+ Iterate over a copy to do this."
^ contents keysAndValuesDo:aTwoArgBlock
+
+ "Modified: 1.3.1996 / 21:42:44 / cg"
! !
!Bag methodsFor:'private'!
@@ -243,5 +273,5 @@
!Bag class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.16 1995-11-23 17:29:24 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.17 1996-03-01 20:43:49 cg Exp $'
! !