1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1991-93 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
Collection subclass:#Bag
|
|
14 |
instanceVariableNames:'contents'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Unordered'
|
|
18 |
!
|
|
19 |
|
|
20 |
Bag comment:'
|
|
21 |
|
|
22 |
COPYRIGHT (c) 1991-93 by Claus Gittinger
|
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
Bag implements collections whose elements are unordered and have no
|
2
|
26 |
external keys. Elements may occur more than once. The implementation
|
|
27 |
uses a dictionary to store the objects occurence count using the object
|
|
28 |
as key.
|
1
|
29 |
|
|
30 |
Instance variables:
|
|
31 |
|
|
32 |
contents <Dictionary> for each element, the number of occurrences
|
|
33 |
|
|
34 |
%W% %E%
|
|
35 |
written jun 91 by claus
|
|
36 |
'!
|
|
37 |
|
|
38 |
!Bag class methodsFor:'instance creation'!
|
|
39 |
|
|
40 |
new
|
|
41 |
"return a new empty Bag"
|
|
42 |
|
|
43 |
^ super new initContents
|
|
44 |
!
|
|
45 |
|
|
46 |
new:size
|
|
47 |
"return a new empty Bag with initial space for size elements"
|
|
48 |
|
|
49 |
^ super new initContents:size
|
|
50 |
! !
|
|
51 |
|
|
52 |
!Bag methodsFor:'private'!
|
|
53 |
|
|
54 |
initContents
|
|
55 |
"set the contents to be an empty Dictionary"
|
|
56 |
|
|
57 |
contents := Dictionary new
|
|
58 |
!
|
|
59 |
|
|
60 |
initContents:size
|
|
61 |
"set the contents to be an empty Dictionary with initial size"
|
|
62 |
|
|
63 |
contents := Dictionary new:size
|
|
64 |
! !
|
|
65 |
|
|
66 |
!Bag methodsFor:'accessing'!
|
|
67 |
|
|
68 |
at:index
|
|
69 |
"report an error: at: is not allowed for Bags"
|
|
70 |
|
|
71 |
^ self errorNotKeyed
|
|
72 |
!
|
|
73 |
|
|
74 |
at:index put:anObject
|
|
75 |
"report an error: at:put: is not allowed for Bags"
|
|
76 |
|
|
77 |
^ self errorNotKeyed
|
|
78 |
! !
|
|
79 |
|
|
80 |
!Bag methodsFor:'testing'!
|
|
81 |
|
|
82 |
size
|
|
83 |
"return the number of bag elements"
|
|
84 |
|
|
85 |
|count|
|
|
86 |
|
|
87 |
count := 0.
|
|
88 |
contents do:[:element | count := count + element].
|
|
89 |
^ count
|
|
90 |
!
|
|
91 |
|
|
92 |
occurrencesOf:anObject
|
|
93 |
"return how many times anObject is in the receiver"
|
|
94 |
|
|
95 |
^ contents at:anObject ifAbsent:[0]
|
|
96 |
!
|
|
97 |
|
|
98 |
includes:anObject
|
|
99 |
"return true, if anObject is in the receiver"
|
|
100 |
|
|
101 |
^ contents includesKey:anObject
|
|
102 |
! !
|
|
103 |
|
|
104 |
!Bag methodsFor:'adding & removing'!
|
|
105 |
|
|
106 |
add:anObject
|
|
107 |
"add the argument, anObject to the receiver"
|
|
108 |
|
|
109 |
^ self add:anObject withOccurences:1
|
|
110 |
!
|
|
111 |
|
|
112 |
add:newObject withOccurences:anInteger
|
|
113 |
"add the argument, anObject anInteger times to the receiver"
|
|
114 |
|
|
115 |
contents at:newObject
|
|
116 |
put:(self occurrencesOf:newObject) + anInteger.
|
|
117 |
^ newObject
|
|
118 |
!
|
|
119 |
|
|
120 |
remove:oldObject ifAbsent:anExceptionBlock
|
|
121 |
"Remove oldObject from the collection and return it
|
|
122 |
- if it was not present, return the value of the exceptionBlock."
|
|
123 |
|
|
124 |
|count|
|
|
125 |
|
|
126 |
count := self occurrencesOf:oldObject.
|
|
127 |
(count == 0) ifTrue:[^ anExceptionBlock value].
|
|
128 |
(count == 1) ifTrue:[
|
|
129 |
contents removeKey:oldObject
|
|
130 |
] ifFalse:[
|
|
131 |
contents at:oldObject put:(count - 1)
|
|
132 |
].
|
|
133 |
^ oldObject
|
|
134 |
! !
|
|
135 |
|
|
136 |
!Bag methodsFor:'enumerating'!
|
|
137 |
|
|
138 |
do:aBlock
|
|
139 |
"Perform the block for all members in the collection."
|
|
140 |
|
|
141 |
contents associationsDo:[:assoc |
|
|
142 |
assoc value timesRepeat:[
|
|
143 |
aBlock value:(assoc key)
|
|
144 |
]
|
|
145 |
]
|
|
146 |
! !
|