Bag.st
author Claus Gittinger <cg@exept.de>
Tue, 22 Aug 2000 16:04:56 +0200
changeset 5558 dfdb1198bf6a
parent 5557 f5f8d236027c
child 6411 199e6d88c562
permissions -rw-r--r--
*** empty log message ***
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
5375
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    13
"{ Package: 'stx:libbasic' }"
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    14
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    15
Collection subclass:#Bag
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
    16
	instanceVariableNames:'contents'
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
    17
	classVariableNames:''
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
    18
	poolDictionaries:''
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
    19
	category:'Collections-Unordered'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    20
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    21
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    22
!Bag class methodsFor:'documentation'!
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    23
88
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    24
copyright
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    25
"
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    26
 COPYRIGHT (c) 1991 by Claus Gittinger
345
claus
parents: 154
diff changeset
    27
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    28
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    29
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    30
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    31
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    32
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    33
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    34
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    35
"
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    36
!
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    37
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    38
documentation
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    39
"
88
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    40
    Bag implements collections whose elements are unordered and have no
154
d4236ec280a6 *** empty log message ***
claus
parents: 92
diff changeset
    41
    external key. Elements may occur more than once in a bag. There is no defined
d4236ec280a6 *** empty log message ***
claus
parents: 92
diff changeset
    42
    order within a bag. 
345
claus
parents: 154
diff changeset
    43
    The default implementation uses a dictionary to store each objects occurence 
claus
parents: 154
diff changeset
    44
    count, using the object itself as key (i.e. using = and hash for inclusion 
claus
parents: 154
diff changeset
    45
    tests).
399
claus
parents: 384
diff changeset
    46
345
claus
parents: 154
diff changeset
    47
    There is also an instance creation variant (#identityNew:) creating a
claus
parents: 154
diff changeset
    48
    bag which compares using #== and hashes using #identityHash.
399
claus
parents: 384
diff changeset
    49
    (I'd say that an IdentityBag was a better thing to implement ... 
claus
parents: 384
diff changeset
    50
     ... but for compatibility ... we do it here as well)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    51
1283
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    52
    [Instance variables:]
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    53
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    54
        contents        <Dictionary>    for each element, the number of occurrences
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    55
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    56
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1283
diff changeset
    57
    [author:]
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1283
diff changeset
    58
        Claus Gittinger
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1283
diff changeset
    59
1283
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    60
    [See also:]
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    61
        Set IdentitySet
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    62
        Dictionary IdentityDictionary
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    63
        OrderedCollection Array
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    64
"
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    65
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    66
a27a279701f8 Initial revision
claus
parents:
diff changeset
    67
!Bag class methodsFor:'instance creation'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    68
345
claus
parents: 154
diff changeset
    69
equalityNew: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 equality compare (i.e. #= not #== identity)."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    72
5558
dfdb1198bf6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5557
diff changeset
    73
    ^ self basicNew initContents:size
345
claus
parents: 154
diff changeset
    74
!
claus
parents: 154
diff changeset
    75
5375
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    76
identityNew
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    77
    "return a new empty Bag which compares for identity
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    78
     Elements will be compared using identity compare (i.e. #== not #= equality)."
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    79
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    80
    ^ super new initContentsForIdentity
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    81
!
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    82
345
claus
parents: 154
diff changeset
    83
identityNew:size
claus
parents: 154
diff changeset
    84
    "return a new empty Bag with initial space for size elements.
claus
parents: 154
diff changeset
    85
     Elements will be compared using identity compare (i.e. #== not #= equality)."
claus
parents: 154
diff changeset
    86
5558
dfdb1198bf6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5557
diff changeset
    87
    ^ self basicNew initContentsForIdentity:size
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    88
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    89
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    90
new
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    91
    "return a new empty Bag which compares for equality (i.e. not identity)"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    92
5558
dfdb1198bf6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5557
diff changeset
    93
    ^ self basicNew initContents
345
claus
parents: 154
diff changeset
    94
!
claus
parents: 154
diff changeset
    95
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    96
new:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    97
    "return a new empty Bag with initial space for size elements.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    98
     Elements will be compared using equality compare (i.e. #= not #== identity)."
345
claus
parents: 154
diff changeset
    99
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   100
    ^ self equalityNew:size
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   101
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   102
a27a279701f8 Initial revision
claus
parents:
diff changeset
   103
!Bag methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   104
a27a279701f8 Initial revision
claus
parents:
diff changeset
   105
at:index
a27a279701f8 Initial revision
claus
parents:
diff changeset
   106
    "report an error: at: is not allowed for Bags"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   107
a27a279701f8 Initial revision
claus
parents:
diff changeset
   108
    ^ self errorNotKeyed
a27a279701f8 Initial revision
claus
parents:
diff changeset
   109
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   110
a27a279701f8 Initial revision
claus
parents:
diff changeset
   111
at:index put:anObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
   112
    "report an error: at:put: is not allowed for Bags"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   113
a27a279701f8 Initial revision
claus
parents:
diff changeset
   114
    ^ self errorNotKeyed
345
claus
parents: 154
diff changeset
   115
!
claus
parents: 154
diff changeset
   116
claus
parents: 154
diff changeset
   117
contents
claus
parents: 154
diff changeset
   118
    "return the dictionary which associates occurrence-counts
claus
parents: 154
diff changeset
   119
     to the bags elements."
claus
parents: 154
diff changeset
   120
claus
parents: 154
diff changeset
   121
    ^ contents
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
!Bag methodsFor:'adding & removing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   125
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   126
add:newObject
345
claus
parents: 154
diff changeset
   127
    "add the argument, anObject to the receiver.
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   128
     Returns the object.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   129
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   130
     WARNING: do not add/remove elements while iterating over the receiver.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   131
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   132
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   133
    |n|
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   134
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   135
    n := contents at:newObject ifAbsent:0.
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   136
    contents at:newObject put:(n + 1).
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   137
    ^ newObject
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   138
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   139
    "Modified: 1.3.1996 / 21:43:06 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   140
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   141
1372
d7bd1b463a65 oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   142
add:newObject withOccurrences:anInteger
345
claus
parents: 154
diff changeset
   143
    "add the argument, anObject anInteger times to the receiver.
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   144
     Returns the object.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   145
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   146
     WARNING: do not add/remove elements while iterating over the receiver.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   147
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   148
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   149
    |n|
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   150
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   151
    n := contents at:newObject ifAbsent:0.
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   152
    contents at:newObject put:(n + anInteger).
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   153
    ^ newObject
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   154
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   155
    "Modified: 1.3.1996 / 21:43:12 / cg"
1372
d7bd1b463a65 oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   156
    "Created: 11.5.1996 / 12:13:43 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   157
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   158
a27a279701f8 Initial revision
claus
parents:
diff changeset
   159
remove:oldObject ifAbsent:anExceptionBlock
345
claus
parents: 154
diff changeset
   160
    "Remove oldObject from the collection.
claus
parents: 154
diff changeset
   161
     If it was not present, return the value of the exceptionBlock;
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   162
     otherwise return the removed object.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   163
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   164
     WARNING: do not add/remove elements while iterating over the receiver.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   165
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   166
a27a279701f8 Initial revision
claus
parents:
diff changeset
   167
    |count|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   168
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   169
    count := contents at:oldObject ifAbsent:0.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   170
    (count == 0) ifTrue:[^ anExceptionBlock value].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   171
    (count == 1) ifTrue:[
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   172
        contents removeKey:oldObject
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   173
    ] ifFalse:[ 
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   174
        contents at:oldObject put:(count - 1)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   175
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   176
    ^ oldObject
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   177
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   178
    "Modified: 1.3.1996 / 21:43:18 / cg"
345
claus
parents: 154
diff changeset
   179
!
claus
parents: 154
diff changeset
   180
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   181
removeAll
1164
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1145
diff changeset
   182
    "remove all objects from the receiver collection (i.e. make it empty).
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1145
diff changeset
   183
     Returns the receiver."
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   184
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   185
    contents := contents species new.
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   186
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
     |b|
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   189
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   190
     b := Bag new.
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   191
     b add:1; add:2; add:3; add:2; add:1.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1372
diff changeset
   192
     Transcript showCR:b.
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   193
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   194
     b removeAll.
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   195
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1372
diff changeset
   196
     Transcript showCR:b
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   197
    "
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   198
1164
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1145
diff changeset
   199
    "Modified: 12.4.1996 / 13:34:34 / cg"
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   200
!
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   201
345
claus
parents: 154
diff changeset
   202
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock
claus
parents: 154
diff changeset
   203
    "Remove all occurrences of oldObject from the collection.
claus
parents: 154
diff changeset
   204
     If it was not present, return the value of the exceptionBlock;
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   205
     otherwise return the number of removes.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   206
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   207
     WARNING: do not add/remove elements while iterating over the receiver.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   208
              Iterate over a copy to do this."
345
claus
parents: 154
diff changeset
   209
claus
parents: 154
diff changeset
   210
    |count|
claus
parents: 154
diff changeset
   211
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   212
    count := contents at:oldObject ifAbsent:0.
345
claus
parents: 154
diff changeset
   213
    (count == 0) ifTrue:[^ anExceptionBlock value].
claus
parents: 154
diff changeset
   214
    contents removeKey:oldObject.
claus
parents: 154
diff changeset
   215
    ^ oldObject
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   216
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   217
    "Modified: 1.3.1996 / 21:43:26 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   218
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   219
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   220
!Bag methodsFor:'converting'!
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
asBag
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   223
    "return the receiver as a bag"
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
    "could be an instance of a subclass..."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   226
    self class == Bag ifTrue:[
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   227
	^ self
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
    ^ super asBag
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
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   232
!Bag methodsFor:'copying'!
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
postCopy
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   235
    "must copy the contents as well"
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
    contents := contents copy
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   238
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   239
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   240
!Bag methodsFor:'enumerating'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   241
a27a279701f8 Initial revision
claus
parents:
diff changeset
   242
do:aBlock
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   243
    "evaluate the block for all elements in the collection.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   244
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   245
     WARNING: do not add/remove elements while iterating over the receiver.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   246
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   247
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   248
    contents keysAndValuesDo:[:key :value|
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   249
        value timesRepeat:[
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   250
            aBlock value:key
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   251
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   252
    ]
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   253
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   254
    "Modified: 1.3.1996 / 21:42:39 / cg"
345
claus
parents: 154
diff changeset
   255
!
claus
parents: 154
diff changeset
   256
claus
parents: 154
diff changeset
   257
valuesAndCountsDo:aTwoArgBlock
claus
parents: 154
diff changeset
   258
    "evaluate the block for all distinct elements in the collection,
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   259
     passing both the element and the occurence count as arguments.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   260
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   261
     WARNING: do not add/remove elements while iterating over the receiver.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   262
              Iterate over a copy to do this."
345
claus
parents: 154
diff changeset
   263
claus
parents: 154
diff changeset
   264
    ^ contents keysAndValuesDo:aTwoArgBlock
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   265
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   266
    "Modified: 1.3.1996 / 21:42:44 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   267
! !
609
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:'private'!
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
initContents
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   272
    "set the contents to be an empty Dictionary"
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 := Dictionary new
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
initContents:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   278
    "set the contents to be an empty Dictionary with initial size"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   279
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   280
    contents := Dictionary new:size
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
5375
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   283
initContentsForIdentity
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   284
    "set the contents to be an empty IdentityDictionary"
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   285
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   286
    contents := IdentityDictionary new
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   287
!
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   288
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   289
initContentsForIdentity:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   290
    "set the contents to be an empty IdentityDictionary with initial size"
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
    contents := IdentityDictionary new:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   293
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   294
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   295
!Bag methodsFor:'queries'!
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   296
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   297
size
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   298
    "return the number of bag elements"
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   299
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   300
    |count|
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   301
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   302
    count := 0.
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   303
    contents do:[:element | count := count + element].
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   304
    ^ count
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   305
! !
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   306
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   307
!Bag methodsFor:'testing'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   308
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   309
includes:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   310
    "return true, if anObject is in the receiver"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   311
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   312
    ^ contents includesKey:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   313
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   314
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   315
occurrencesOf:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   316
    "return how many times anObject is in the receiver"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   317
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   318
    ^ contents at:anObject ifAbsent:0
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   319
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   320
633
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   321
!Bag class methodsFor:'documentation'!
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   322
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   323
version
5558
dfdb1198bf6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5557
diff changeset
   324
    ^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.27 2000-08-22 14:04:56 cg Exp $'
633
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   325
! !