Bag.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Apr 1996 22:49:28 +0200
changeset 1145 a094d90e11bf
parent 1110 c425b5d28a89
child 1164 38c54a4f1273
permissions -rw-r--r--
dont use [0] blocks - use 0 constant instead
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
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
    14
	instanceVariableNames:'contents'
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
    15
	classVariableNames:''
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
    16
	poolDictionaries:''
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
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
"
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    54
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    55
a27a279701f8 Initial revision
claus
parents:
diff changeset
    56
!Bag class methodsFor:'instance creation'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    57
345
claus
parents: 154
diff changeset
    58
equalityNew:size
claus
parents: 154
diff changeset
    59
    "return a new empty Bag with initial space for size elements.
claus
parents: 154
diff changeset
    60
     Elements will be compared using equality compare (i.e. #= not #== identity)."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    61
a27a279701f8 Initial revision
claus
parents:
diff changeset
    62
    ^ super new initContents:size
345
claus
parents: 154
diff changeset
    63
!
claus
parents: 154
diff changeset
    64
claus
parents: 154
diff changeset
    65
identityNew:size
claus
parents: 154
diff changeset
    66
    "return a new empty Bag with initial space for size elements.
claus
parents: 154
diff changeset
    67
     Elements will be compared using identity compare (i.e. #== not #= equality)."
claus
parents: 154
diff changeset
    68
claus
parents: 154
diff changeset
    69
    ^ super new initContentsForIdentity:size
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    70
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    71
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    72
new
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    73
    "return a new empty Bag which compares for equality (i.e. not identity)"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    74
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    75
    ^ super new initContents
345
claus
parents: 154
diff changeset
    76
!
claus
parents: 154
diff changeset
    77
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    78
new:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    79
    "return a new empty Bag with initial space for size elements.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    80
     Elements will be compared using equality compare (i.e. #= not #== identity)."
345
claus
parents: 154
diff changeset
    81
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    82
    ^ self equalityNew:size
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    83
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
    84
a27a279701f8 Initial revision
claus
parents:
diff changeset
    85
!Bag methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    86
a27a279701f8 Initial revision
claus
parents:
diff changeset
    87
at:index
a27a279701f8 Initial revision
claus
parents:
diff changeset
    88
    "report an error: at: is not allowed for Bags"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    89
a27a279701f8 Initial revision
claus
parents:
diff changeset
    90
    ^ self errorNotKeyed
a27a279701f8 Initial revision
claus
parents:
diff changeset
    91
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    92
a27a279701f8 Initial revision
claus
parents:
diff changeset
    93
at:index put:anObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
    94
    "report an error: at:put: is not allowed for Bags"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    95
a27a279701f8 Initial revision
claus
parents:
diff changeset
    96
    ^ self errorNotKeyed
345
claus
parents: 154
diff changeset
    97
!
claus
parents: 154
diff changeset
    98
claus
parents: 154
diff changeset
    99
contents
claus
parents: 154
diff changeset
   100
    "return the dictionary which associates occurrence-counts
claus
parents: 154
diff changeset
   101
     to the bags elements."
claus
parents: 154
diff changeset
   102
claus
parents: 154
diff changeset
   103
    ^ contents
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   104
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   105
a27a279701f8 Initial revision
claus
parents:
diff changeset
   106
!Bag methodsFor:'adding & removing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   107
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   108
add:newObject
345
claus
parents: 154
diff changeset
   109
    "add the argument, anObject to the receiver.
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   110
     Returns the object.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   111
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   112
     WARNING: do not add/remove elements while iterating over the receiver.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   113
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   114
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   115
    |n|
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   116
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   117
    n := contents at:newObject ifAbsent:0.
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   118
    contents at:newObject put:(n + 1).
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   119
    ^ newObject
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   120
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   121
    "Modified: 1.3.1996 / 21:43:06 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   122
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   123
a27a279701f8 Initial revision
claus
parents:
diff changeset
   124
add:newObject withOccurences:anInteger
345
claus
parents: 154
diff changeset
   125
    "add the argument, anObject anInteger times to the receiver.
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   126
     Returns the object.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   127
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   128
     WARNING: do not add/remove elements while iterating over the receiver.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   129
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   130
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   131
    |n|
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   132
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   133
    n := contents at:newObject ifAbsent:0.
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   134
    contents at:newObject put:(n + anInteger).
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   135
    ^ newObject
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   136
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   137
    "Modified: 1.3.1996 / 21:43:12 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   138
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   139
a27a279701f8 Initial revision
claus
parents:
diff changeset
   140
remove:oldObject ifAbsent:anExceptionBlock
345
claus
parents: 154
diff changeset
   141
    "Remove oldObject from the collection.
claus
parents: 154
diff changeset
   142
     If it was not present, return the value of the exceptionBlock;
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   143
     otherwise return the removed object.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   144
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   145
     WARNING: do not add/remove elements while iterating over the receiver.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   146
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   147
a27a279701f8 Initial revision
claus
parents:
diff changeset
   148
    |count|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   149
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   150
    count := contents at:oldObject ifAbsent:0.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   151
    (count == 0) ifTrue:[^ anExceptionBlock value].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   152
    (count == 1) ifTrue:[
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   153
        contents removeKey:oldObject
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   154
    ] ifFalse:[ 
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   155
        contents at:oldObject put:(count - 1)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   156
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   157
    ^ oldObject
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   158
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   159
    "Modified: 1.3.1996 / 21:43:18 / cg"
345
claus
parents: 154
diff changeset
   160
!
claus
parents: 154
diff changeset
   161
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   162
removeAll
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   163
    "remove all objects from the receiver collection (i.e. make it empty)."
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   164
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   165
    contents := contents species new.
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   166
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   167
    "
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   168
     |b|
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   169
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   170
     b := Bag new.
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   171
     b add:1; add:2; add:3; add:2; add:1.
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   172
     Transcript showCr:b.
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   173
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   174
     b removeAll.
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   175
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   176
     Transcript showCr:b
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   177
    "
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   178
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   179
    "Modified: 21.3.1996 / 15:23:35 / cg"
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   180
!
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   181
345
claus
parents: 154
diff changeset
   182
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock
claus
parents: 154
diff changeset
   183
    "Remove all occurrences of oldObject from the collection.
claus
parents: 154
diff changeset
   184
     If it was not present, return the value of the exceptionBlock;
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   185
     otherwise return the number of removes.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   186
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   187
     WARNING: do not add/remove elements while iterating over the receiver.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   188
              Iterate over a copy to do this."
345
claus
parents: 154
diff changeset
   189
claus
parents: 154
diff changeset
   190
    |count|
claus
parents: 154
diff changeset
   191
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   192
    count := contents at:oldObject ifAbsent:0.
345
claus
parents: 154
diff changeset
   193
    (count == 0) ifTrue:[^ anExceptionBlock value].
claus
parents: 154
diff changeset
   194
    contents removeKey:oldObject.
claus
parents: 154
diff changeset
   195
    ^ oldObject
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   196
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   197
    "Modified: 1.3.1996 / 21:43:26 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   198
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   199
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   200
!Bag methodsFor:'converting'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   201
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   202
asBag
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   203
    "return the receiver as a bag"
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
    "could be an instance of a subclass..."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   206
    self class == Bag ifTrue:[
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   207
	^ self
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   208
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   209
    ^ super asBag
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
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   212
!Bag methodsFor:'copying'!
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
postCopy
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   215
    "must copy the contents as well"
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
    contents := contents copy
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   218
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   219
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   220
!Bag methodsFor:'enumerating'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   221
a27a279701f8 Initial revision
claus
parents:
diff changeset
   222
do:aBlock
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   223
    "evaluate the block for all elements in the collection.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   224
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   225
     WARNING: do not add/remove elements while iterating over the receiver.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   226
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   227
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   228
    contents keysAndValuesDo:[:key :value|
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   229
        value timesRepeat:[
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   230
            aBlock value:key
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   231
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   232
    ]
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   233
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   234
    "Modified: 1.3.1996 / 21:42:39 / cg"
345
claus
parents: 154
diff changeset
   235
!
claus
parents: 154
diff changeset
   236
claus
parents: 154
diff changeset
   237
valuesAndCountsDo:aTwoArgBlock
claus
parents: 154
diff changeset
   238
    "evaluate the block for all distinct elements in the collection,
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   239
     passing both the element and the occurence count as arguments.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   240
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   241
     WARNING: do not add/remove elements while iterating over the receiver.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   242
              Iterate over a copy to do this."
345
claus
parents: 154
diff changeset
   243
claus
parents: 154
diff changeset
   244
    ^ contents keysAndValuesDo:aTwoArgBlock
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   245
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   246
    "Modified: 1.3.1996 / 21:42:44 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   247
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   248
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   249
!Bag methodsFor:'private'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   250
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   251
initContents
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   252
    "set the contents to be an empty Dictionary"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   253
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   254
    contents := Dictionary new
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   255
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   256
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   257
initContents:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   258
    "set the contents to be an empty Dictionary with initial size"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   259
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   260
    contents := Dictionary new:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   261
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   262
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   263
initContentsForIdentity:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   264
    "set the contents to be an empty IdentityDictionary with initial size"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   265
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   266
    contents := IdentityDictionary new:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   267
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   268
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   269
!Bag methodsFor:'testing'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   270
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   271
includes:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   272
    "return true, if anObject is in the receiver"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   273
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   274
    ^ contents includesKey:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   275
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   276
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   277
occurrencesOf:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   278
    "return how many times anObject is in the receiver"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   279
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   280
    ^ contents at:anObject ifAbsent:0
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   281
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   282
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   283
size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   284
    "return the number of bag elements"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   285
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   286
    |count|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   287
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   288
    count := 0.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   289
    contents do:[:element | count := count + element].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   290
    ^ count
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   291
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   292
633
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   293
!Bag class methodsFor:'documentation'!
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   294
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   295
version
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   296
    ^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.19 1996-04-09 20:48:44 cg Exp $'
633
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   297
! !