Bag.st
author Claus Gittinger <cg@exept.de>
Thu, 23 Nov 1995 02:26:15 +0100
changeset 609 12be97f6d5a7
parent 528 a083413dfbe8
child 633 46e996b3c18f
permissions -rw-r--r--
checkin from browser
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     2
 COPYRIGHT (c) 1991 by Claus Gittinger
345
claus
parents: 154
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
Collection subclass:#Bag
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    14
	 instanceVariableNames:'contents'
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    15
	 classVariableNames:''
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    16
	 poolDictionaries:''
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    17
	 category:'Collections-Unordered'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    18
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    20
!Bag class methodsFor:'documentation'!
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    21
88
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    22
copyright
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    23
"
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    24
 COPYRIGHT (c) 1991 by Claus Gittinger
345
claus
parents: 154
diff changeset
    25
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    26
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    27
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    28
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    29
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    30
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    31
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    32
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    33
"
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    34
!
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    35
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    36
documentation
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    37
"
88
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    38
    Bag implements collections whose elements are unordered and have no
154
d4236ec280a6 *** empty log message ***
claus
parents: 92
diff changeset
    39
    external key. Elements may occur more than once in a bag. There is no defined
d4236ec280a6 *** empty log message ***
claus
parents: 92
diff changeset
    40
    order within a bag. 
345
claus
parents: 154
diff changeset
    41
    The default implementation uses a dictionary to store each objects occurence 
claus
parents: 154
diff changeset
    42
    count, using the object itself as key (i.e. using = and hash for inclusion 
claus
parents: 154
diff changeset
    43
    tests).
399
claus
parents: 384
diff changeset
    44
345
claus
parents: 154
diff changeset
    45
    There is also an instance creation variant (#identityNew:) creating a
claus
parents: 154
diff changeset
    46
    bag which compares using #== and hashes using #identityHash.
399
claus
parents: 384
diff changeset
    47
    (I'd say that an IdentityBag was a better thing to implement ... 
claus
parents: 384
diff changeset
    48
     ... but for compatibility ... we do it here as well)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    49
88
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    50
    Instance variables:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    51
345
claus
parents: 154
diff changeset
    52
	contents        <Dictionary>    for each element, the number of occurrences
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    53
"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    54
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    55
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    56
version
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    57
    ^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.15 1995-11-23 01:24:57 cg Exp $'
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    58
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    59
a27a279701f8 Initial revision
claus
parents:
diff changeset
    60
!Bag class methodsFor:'instance creation'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    61
345
claus
parents: 154
diff changeset
    62
equalityNew:size
claus
parents: 154
diff changeset
    63
    "return a new empty Bag with initial space for size elements.
claus
parents: 154
diff changeset
    64
     Elements will be compared using equality compare (i.e. #= not #== identity)."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    65
a27a279701f8 Initial revision
claus
parents:
diff changeset
    66
    ^ super new initContents:size
345
claus
parents: 154
diff changeset
    67
!
claus
parents: 154
diff changeset
    68
claus
parents: 154
diff changeset
    69
identityNew:size
claus
parents: 154
diff changeset
    70
    "return a new empty Bag with initial space for size elements.
claus
parents: 154
diff changeset
    71
     Elements will be compared using identity compare (i.e. #== not #= equality)."
claus
parents: 154
diff changeset
    72
claus
parents: 154
diff changeset
    73
    ^ super new initContentsForIdentity:size
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    74
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    75
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    76
new
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    77
    "return a new empty Bag which compares for equality (i.e. not identity)"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    78
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    79
    ^ super new initContents
345
claus
parents: 154
diff changeset
    80
!
claus
parents: 154
diff changeset
    81
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    82
new:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    83
    "return a new empty Bag with initial space for size elements.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    84
     Elements will be compared using equality compare (i.e. #= not #== identity)."
345
claus
parents: 154
diff changeset
    85
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    86
    ^ self equalityNew:size
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    87
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
    88
a27a279701f8 Initial revision
claus
parents:
diff changeset
    89
!Bag methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    90
a27a279701f8 Initial revision
claus
parents:
diff changeset
    91
at:index
a27a279701f8 Initial revision
claus
parents:
diff changeset
    92
    "report an error: at: is not allowed for Bags"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    93
a27a279701f8 Initial revision
claus
parents:
diff changeset
    94
    ^ self errorNotKeyed
a27a279701f8 Initial revision
claus
parents:
diff changeset
    95
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    96
a27a279701f8 Initial revision
claus
parents:
diff changeset
    97
at:index put:anObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
    98
    "report an error: at:put: is not allowed for Bags"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    99
a27a279701f8 Initial revision
claus
parents:
diff changeset
   100
    ^ self errorNotKeyed
345
claus
parents: 154
diff changeset
   101
!
claus
parents: 154
diff changeset
   102
claus
parents: 154
diff changeset
   103
contents
claus
parents: 154
diff changeset
   104
    "return the dictionary which associates occurrence-counts
claus
parents: 154
diff changeset
   105
     to the bags elements."
claus
parents: 154
diff changeset
   106
claus
parents: 154
diff changeset
   107
    ^ contents
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   108
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   109
a27a279701f8 Initial revision
claus
parents:
diff changeset
   110
!Bag methodsFor:'adding & removing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   111
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   112
add:newObject
345
claus
parents: 154
diff changeset
   113
    "add the argument, anObject to the receiver.
claus
parents: 154
diff changeset
   114
     Returns the object."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   115
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   116
    |n|
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   117
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   118
    n := contents at:newObject ifAbsent:[0].
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   119
    contents at:newObject put:(n + 1).
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   120
    ^ newObject
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   121
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   122
a27a279701f8 Initial revision
claus
parents:
diff changeset
   123
add:newObject withOccurences:anInteger
345
claus
parents: 154
diff changeset
   124
    "add the argument, anObject anInteger times to the receiver.
claus
parents: 154
diff changeset
   125
     Returns the object."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   126
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   127
    |n|
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   128
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   129
    n := contents at:newObject ifAbsent:[0].
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   130
    contents at:newObject put:(n + anInteger).
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   131
    ^ newObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
   132
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   133
a27a279701f8 Initial revision
claus
parents:
diff changeset
   134
remove:oldObject ifAbsent:anExceptionBlock
345
claus
parents: 154
diff changeset
   135
    "Remove oldObject from the collection.
claus
parents: 154
diff changeset
   136
     If it was not present, return the value of the exceptionBlock;
claus
parents: 154
diff changeset
   137
     otherwise return the removed object."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   138
a27a279701f8 Initial revision
claus
parents:
diff changeset
   139
    |count|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   140
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   141
    count := contents at:oldObject ifAbsent:[0].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   142
    (count == 0) ifTrue:[^ anExceptionBlock value].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   143
    (count == 1) ifTrue:[
345
claus
parents: 154
diff changeset
   144
	contents removeKey:oldObject
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   145
    ] ifFalse:[ 
345
claus
parents: 154
diff changeset
   146
	contents at:oldObject put:(count - 1)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   147
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   148
    ^ oldObject
345
claus
parents: 154
diff changeset
   149
!
claus
parents: 154
diff changeset
   150
claus
parents: 154
diff changeset
   151
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock
claus
parents: 154
diff changeset
   152
    "Remove all occurrences of oldObject from the collection.
claus
parents: 154
diff changeset
   153
     If it was not present, return the value of the exceptionBlock;
claus
parents: 154
diff changeset
   154
     otherwise return the number of removes."
claus
parents: 154
diff changeset
   155
claus
parents: 154
diff changeset
   156
    |count|
claus
parents: 154
diff changeset
   157
claus
parents: 154
diff changeset
   158
    count := contents at:oldObject ifAbsent:[0].
claus
parents: 154
diff changeset
   159
    (count == 0) ifTrue:[^ anExceptionBlock value].
claus
parents: 154
diff changeset
   160
    contents removeKey:oldObject.
claus
parents: 154
diff changeset
   161
    ^ oldObject
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   162
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   163
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   164
!Bag methodsFor:'converting'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   165
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   166
asBag
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   167
    "return the receiver as a bag"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   168
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   169
    "could be an instance of a subclass..."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   170
    self class == Bag ifTrue:[
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   171
	^ self
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   172
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   173
    ^ super asBag
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   174
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   175
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   176
!Bag methodsFor:'copying'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   177
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   178
postCopy
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   179
    "must copy the contents as well"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   180
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   181
    contents := contents copy
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   182
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   183
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   184
!Bag methodsFor:'enumerating'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   185
a27a279701f8 Initial revision
claus
parents:
diff changeset
   186
do:aBlock
345
claus
parents: 154
diff changeset
   187
    "evaluate the block for all elements in the collection."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   188
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   189
    contents keysAndValuesDo:[:key :value|
345
claus
parents: 154
diff changeset
   190
	value timesRepeat:[
claus
parents: 154
diff changeset
   191
	    aBlock value:key
claus
parents: 154
diff changeset
   192
	]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   193
    ]
345
claus
parents: 154
diff changeset
   194
!
claus
parents: 154
diff changeset
   195
claus
parents: 154
diff changeset
   196
valuesAndCountsDo:aTwoArgBlock
claus
parents: 154
diff changeset
   197
    "evaluate the block for all distinct elements in the collection,
claus
parents: 154
diff changeset
   198
     passing both the element and the occurence count as arguments."
claus
parents: 154
diff changeset
   199
claus
parents: 154
diff changeset
   200
    ^ contents keysAndValuesDo:aTwoArgBlock
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   201
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   202
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   203
!Bag methodsFor:'private'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   204
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   205
initContents
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   206
    "set the contents to be an empty Dictionary"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   207
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   208
    contents := Dictionary new
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   209
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   210
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   211
initContents:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   212
    "set the contents to be an empty Dictionary with initial size"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   213
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   214
    contents := Dictionary new:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   215
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   216
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   217
initContentsForIdentity:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   218
    "set the contents to be an empty IdentityDictionary with initial size"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   219
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   220
    contents := IdentityDictionary new:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   221
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   222
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   223
!Bag methodsFor:'testing'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   224
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   225
includes:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   226
    "return true, if anObject is in the receiver"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   227
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   228
    ^ contents includesKey:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   229
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   230
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   231
occurrencesOf:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   232
    "return how many times anObject is in the receiver"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   233
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   234
    ^ contents at:anObject ifAbsent:[0]
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   235
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   236
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   237
size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   238
    "return the number of bag elements"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   239
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   240
    |count|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   241
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   242
    count := 0.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   243
    contents do:[:element | count := count + element].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   244
    ^ count
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   245
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   246