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