--- a/Bag.st Fri May 12 14:35:09 1995 +0200
+++ b/Bag.st Tue May 16 19:09:45 1995 +0200
@@ -1,6 +1,6 @@
"
COPYRIGHT (c) 1991 by Claus Gittinger
- All Rights Reserved
+ 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
@@ -19,9 +19,9 @@
Bag comment:'
COPYRIGHT (c) 1991 by Claus Gittinger
- All Rights Reserved
+ All Rights Reserved
-$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.9 1994-10-10 00:19:48 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.10 1995-05-16 17:05:36 claus Exp $
'!
!Bag class methodsFor:'documentation'!
@@ -29,7 +29,7 @@
copyright
"
COPYRIGHT (c) 1991 by Claus Gittinger
- All Rights Reserved
+ 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
@@ -42,7 +42,7 @@
version
"
-$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.9 1994-10-10 00:19:48 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.10 1995-05-16 17:05:36 claus Exp $
"
!
@@ -51,27 +51,47 @@
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 implementation uses a dictionary to store each objects occurence count,
- using the object itself as key (i.e. using = and hash for inclusion tests).
+ The default implementation uses a dictionary to store each objects occurence
+ 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 would say that an IdentityBag would have been a better thing to
+ implement ... but for compatibility ...)
Instance variables:
- contents <Dictionary> for each element, the number of occurrences
+ contents <Dictionary> for each element, the number of occurrences
"
! !
!Bag class methodsFor:'instance creation'!
new
- "return a new empty Bag"
+ "return a new empty Bag which compares for equality (i.e. not identity)"
^ super new initContents
!
new:size
- "return a new empty Bag with initial space for size elements"
+ "return a new empty Bag with initial space for size elements.
+ Elements will be compared using equality compare (i.e. #= not #== identity)."
+
+ ^ self equalityNew:size
+!
+
+equalityNew:size
+ "return a new empty Bag with initial space for size elements.
+ Elements will be compared using equality compare (i.e. #= not #== identity)."
^ super new initContents:size
+!
+
+identityNew:size
+ "return a new empty Bag with initial space for size elements.
+ Elements will be compared using identity compare (i.e. #== not #= equality)."
+
+ ^ super new initContentsForIdentity:size
! !
!Bag methodsFor:'private'!
@@ -86,6 +106,12 @@
"set the contents to be an empty Dictionary with initial size"
contents := Dictionary new:size
+!
+
+initContentsForIdentity:size
+ "set the contents to be an empty IdentityDictionary with initial size"
+
+ contents := IdentityDictionary new:size
! !
!Bag methodsFor:'accessing'!
@@ -100,6 +126,13 @@
"report an error: at:put: is not allowed for Bags"
^ self errorNotKeyed
+!
+
+contents
+ "return the dictionary which associates occurrence-counts
+ to the bags elements."
+
+ ^ contents
! !
!Bag methodsFor:'testing'!
@@ -141,7 +174,7 @@
"could be an instance of a subclass..."
self class == Bag ifTrue:[
- ^ self
+ ^ self
].
^ super asBag
! !
@@ -149,7 +182,8 @@
!Bag methodsFor:'adding & removing'!
add:newObject
- "add the argument, anObject to the receiver"
+ "add the argument, anObject to the receiver.
+ Returns the object."
|n|
@@ -159,7 +193,8 @@
!
add:newObject withOccurences:anInteger
- "add the argument, anObject anInteger times to the receiver"
+ "add the argument, anObject anInteger times to the receiver.
+ Returns the object."
|n|
@@ -169,29 +204,50 @@
!
remove:oldObject ifAbsent:anExceptionBlock
- "Remove oldObject from the collection and return it
- - if it was not present, return the value of the exceptionBlock."
+ "Remove oldObject from the collection.
+ If it was not present, return the value of the exceptionBlock;
+ otherwise return the removed object."
|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
+!
+
+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."
+
+ |count|
+
+ count := contents at:oldObject ifAbsent:[0].
+ (count == 0) ifTrue:[^ anExceptionBlock value].
+ contents removeKey:oldObject.
+ ^ oldObject
! !
!Bag methodsFor:'enumerating'!
do:aBlock
- "Perform the block for all members in the collection."
+ "evaluate the block for all elements in the collection."
contents keysAndValuesDo:[:key :value|
- value timesRepeat:[
- aBlock value:key
- ]
+ value timesRepeat:[
+ aBlock value:key
+ ]
]
+!
+
+valuesAndCountsDo:aTwoArgBlock
+ "evaluate the block for all distinct elements in the collection,
+ passing both the element and the occurence count as arguments."
+
+ ^ contents keysAndValuesDo:aTwoArgBlock
! !