Bag.st
author Claus Gittinger <cg@exept.de>
Tue, 25 Jun 2013 13:23:48 +0200
changeset 15429 087ad8e8da0c
parent 14956 bda5a86c5525
child 15457 af0513df339f
child 18070 d262e3aecaca
permissions -rw-r--r--
added isFixedSize query
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
"
5375
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    12
"{ Package: 'stx:libbasic' }"
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    13
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    14
Collection subclass:#Bag
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
    15
	instanceVariableNames:'contents'
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
    16
	classVariableNames:''
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
    17
	poolDictionaries:''
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
    18
	category:'Collections-Unordered'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    20
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    21
!Bag class methodsFor:'documentation'!
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    22
88
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    23
copyright
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    24
"
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    25
 COPYRIGHT (c) 1991 by Claus Gittinger
345
claus
parents: 154
diff changeset
    26
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    27
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    28
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    29
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    31
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    32
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    33
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    34
"
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    35
!
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    36
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    37
documentation
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    38
"
88
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    39
    Bag implements collections whose elements are unordered and have no
154
d4236ec280a6 *** empty log message ***
claus
parents: 92
diff changeset
    40
    external key. Elements may occur more than once in a bag. There is no defined
8873
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
    41
    order within a bag.
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
    42
    The default implementation uses a dictionary to store each objects occurrence
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
    43
    count, using the object itself as key (i.e. using = and hash for inclusion
345
claus
parents: 154
diff changeset
    44
    tests).
399
claus
parents: 384
diff changeset
    45
345
claus
parents: 154
diff changeset
    46
    There is also an instance creation variant (#identityNew:) creating a
claus
parents: 154
diff changeset
    47
    bag which compares using #== and hashes using #identityHash.
8873
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
    48
    (I'd say that an IdentityBag was a better thing to implement ...
399
claus
parents: 384
diff changeset
    49
     ... but for compatibility ... we do it here as well)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    50
1283
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    51
    [Instance variables:]
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    52
8873
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
    53
	contents        <Dictionary>    for each element, the number of occurrences
1283
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    54
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    55
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1283
diff changeset
    56
    [author:]
8873
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
    57
	Claus Gittinger
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1283
diff changeset
    58
1283
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    59
    [See also:]
8873
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
    60
	Set IdentitySet
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
    61
	Dictionary IdentityDictionary
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
    62
	OrderedCollection Array
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    63
"
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    64
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    65
a27a279701f8 Initial revision
claus
parents:
diff changeset
    66
!Bag class methodsFor:'instance creation'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    67
14956
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    68
contentsClass
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    69
    "the default class to use for the underlying contents array,
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    70
     used when instantiated with new/new:"
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    71
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    72
    ^ Dictionary
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    73
!
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    74
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    75
equalityNew
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    76
    "return a new empty Bag.
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    77
     Elements will be compared using equality compare (i.e. #= not #== identity)."
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    78
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    79
    ^ self basicNew initContentsForEquality
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    80
!
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    81
345
claus
parents: 154
diff changeset
    82
equalityNew:size
claus
parents: 154
diff changeset
    83
    "return a new empty Bag with initial space for size elements.
claus
parents: 154
diff changeset
    84
     Elements will be compared using equality compare (i.e. #= not #== identity)."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    85
14956
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    86
    ^ self basicNew initContentsForEquality:size
345
claus
parents: 154
diff changeset
    87
!
claus
parents: 154
diff changeset
    88
5375
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    89
identityNew
14956
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    90
    "return a new empty Identity-Bag.
5375
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    91
     Elements will be compared using identity compare (i.e. #== not #= equality)."
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    92
14956
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    93
    ^ self basicNew initContentsForIdentity
5375
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    94
!
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    95
345
claus
parents: 154
diff changeset
    96
identityNew:size
claus
parents: 154
diff changeset
    97
    "return a new empty Bag with initial space for size elements.
claus
parents: 154
diff changeset
    98
     Elements will be compared using identity compare (i.e. #== not #= equality)."
claus
parents: 154
diff changeset
    99
5558
dfdb1198bf6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5557
diff changeset
   100
    ^ self basicNew initContentsForIdentity:size
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   101
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   102
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   103
new
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   104
    "return a new empty Bag which compares for equality (i.e. not identity)"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   105
5558
dfdb1198bf6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5557
diff changeset
   106
    ^ self basicNew initContents
345
claus
parents: 154
diff changeset
   107
!
claus
parents: 154
diff changeset
   108
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   109
new:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   110
    "return a new empty Bag with initial space for size elements.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   111
     Elements will be compared using equality compare (i.e. #= not #== identity)."
345
claus
parents: 154
diff changeset
   112
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   113
    ^ self equalityNew:size
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   114
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   115
7261
f35fc9cee675 method category rename
Claus Gittinger <cg@exept.de>
parents: 7161
diff changeset
   116
!Bag methodsFor:'Compatibility-Dolphin'!
6411
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5558
diff changeset
   117
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5558
diff changeset
   118
asAssociations
11935
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   119
    "return the dictionary which associates occurrence-counts
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   120
     to the bags elements. 
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   121
     Same as #contents for dolphin compatibility."
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   122
14685
43b852db6777 class: Bag
Claus Gittinger <cg@exept.de>
parents: 12823
diff changeset
   123
    ^ self contents
6411
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5558
diff changeset
   124
! !
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5558
diff changeset
   125
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   126
!Bag methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   127
a27a279701f8 Initial revision
claus
parents:
diff changeset
   128
at:index
a27a279701f8 Initial revision
claus
parents:
diff changeset
   129
    "report an error: at: is not allowed for Bags"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   130
a27a279701f8 Initial revision
claus
parents:
diff changeset
   131
    ^ self errorNotKeyed
a27a279701f8 Initial revision
claus
parents:
diff changeset
   132
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   133
a27a279701f8 Initial revision
claus
parents:
diff changeset
   134
at:index put:anObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
   135
    "report an error: at:put: is not allowed for Bags"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   136
a27a279701f8 Initial revision
claus
parents:
diff changeset
   137
    ^ self errorNotKeyed
345
claus
parents: 154
diff changeset
   138
!
claus
parents: 154
diff changeset
   139
claus
parents: 154
diff changeset
   140
contents
claus
parents: 154
diff changeset
   141
    "return the dictionary which associates occurrence-counts
claus
parents: 154
diff changeset
   142
     to the bags elements."
claus
parents: 154
diff changeset
   143
claus
parents: 154
diff changeset
   144
    ^ contents
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   145
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   146
a27a279701f8 Initial revision
claus
parents:
diff changeset
   147
!Bag methodsFor:'adding & removing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   148
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   149
add:newObject
345
claus
parents: 154
diff changeset
   150
    "add the argument, anObject to the receiver.
14896
148dd722af88 comment/format in: #add:
Claus Gittinger <cg@exept.de>
parents: 14685
diff changeset
   151
     Returns the object (sigh).
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   152
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   153
     WARNING: do not add/remove elements while iterating over the receiver.
14896
148dd722af88 comment/format in: #add:
Claus Gittinger <cg@exept.de>
parents: 14685
diff changeset
   154
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   155
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   156
    |n|
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   157
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   158
    n := contents at:newObject ifAbsent:0.
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   159
    contents at:newObject put:(n + 1).
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   160
    ^ newObject
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   161
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   162
    "Modified: 1.3.1996 / 21:43:06 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   163
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   164
1372
d7bd1b463a65 oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   165
add:newObject withOccurrences:anInteger
345
claus
parents: 154
diff changeset
   166
    "add the argument, anObject anInteger times to the receiver.
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   167
     Returns the object.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   168
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   169
     WARNING: do not add/remove elements while iterating over the receiver.
8873
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   170
	      Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   171
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   172
    |n|
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   173
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   174
    n := contents at:newObject ifAbsent:0.
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   175
    contents at:newObject put:(n + anInteger).
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   176
    ^ newObject
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:12 / cg"
1372
d7bd1b463a65 oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   179
    "Created: 11.5.1996 / 12:13:43 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   180
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   181
a27a279701f8 Initial revision
claus
parents:
diff changeset
   182
remove:oldObject ifAbsent:anExceptionBlock
345
claus
parents: 154
diff changeset
   183
    "Remove 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 removed object.
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.
8873
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   188
	      Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   189
a27a279701f8 Initial revision
claus
parents:
diff changeset
   190
    |count|
a27a279701f8 Initial revision
claus
parents:
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.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   193
    (count == 0) ifTrue:[^ anExceptionBlock value].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   194
    (count == 1) ifTrue:[
8873
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   195
	contents removeKey:oldObject
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   196
    ] ifFalse:[
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   197
	contents at:oldObject put:(count - 1)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   198
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   199
    ^ oldObject
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   200
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   201
    "Modified: 1.3.1996 / 21:43:18 / cg"
345
claus
parents: 154
diff changeset
   202
!
claus
parents: 154
diff changeset
   203
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   204
removeAll
1164
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1145
diff changeset
   205
    "remove all objects from the receiver collection (i.e. make it empty).
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1145
diff changeset
   206
     Returns the receiver."
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   207
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   208
    contents := contents species new.
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   209
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   210
    "
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   211
     |b|
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   212
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   213
     b := Bag new.
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   214
     b add:1; add:2; add:3; add:2; add:1.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1372
diff changeset
   215
     Transcript showCR:b.
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   216
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   217
     b removeAll.
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   218
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1372
diff changeset
   219
     Transcript showCR:b
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   220
    "
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   221
1164
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1145
diff changeset
   222
    "Modified: 12.4.1996 / 13:34:34 / cg"
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   223
!
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   224
345
claus
parents: 154
diff changeset
   225
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock
claus
parents: 154
diff changeset
   226
    "Remove all occurrences of oldObject from the collection.
claus
parents: 154
diff changeset
   227
     If it was not present, return the value of the exceptionBlock;
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   228
     otherwise return the number of removes.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   229
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   230
     WARNING: do not add/remove elements while iterating over the receiver.
8873
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   231
	      Iterate over a copy to do this."
345
claus
parents: 154
diff changeset
   232
claus
parents: 154
diff changeset
   233
    |count|
claus
parents: 154
diff changeset
   234
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   235
    count := contents at:oldObject ifAbsent:0.
345
claus
parents: 154
diff changeset
   236
    (count == 0) ifTrue:[^ anExceptionBlock value].
claus
parents: 154
diff changeset
   237
    contents removeKey:oldObject.
claus
parents: 154
diff changeset
   238
    ^ oldObject
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   239
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   240
    "Modified: 1.3.1996 / 21:43:26 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   241
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   242
8860
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   243
!Bag methodsFor:'bulk operations'!
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   244
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   245
sum
11522
6cda81ef426a changed #sum
Claus Gittinger <cg@exept.de>
parents: 11490
diff changeset
   246
    "sum up all elements; return 0 for an empty collection.
6cda81ef426a changed #sum
Claus Gittinger <cg@exept.de>
parents: 11490
diff changeset
   247
     can be done easier, using bags knowledge."
8860
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   248
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   249
    |accu|
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   250
11522
6cda81ef426a changed #sum
Claus Gittinger <cg@exept.de>
parents: 11490
diff changeset
   251
    self isEmpty ifTrue:[ ^ 0 ].
11474
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   252
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   253
    accu := nil.
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   254
    self 
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   255
        valuesAndCountsDo:[:n :count |
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   256
            |thisAmount|
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   257
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   258
            thisAmount := count * n.
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   259
            accu := (accu isNil ifTrue:[thisAmount] ifFalse:[accu + thisAmount]) ].
8860
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   260
    ^ accu
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   261
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   262
    "
11474
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   263
     TestCase assert:((Bag new add:1; add:2; add:3; add:1; add:2; add:1; yourself) sum = 10).
8860
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   264
    "
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   265
! !
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   266
6735
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   267
!Bag methodsFor:'comparing'!
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   268
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   269
= aBag
11935
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   270
    "Compare the receiver with the argument and return true if the
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   271
     receiver is equal to the argument (i.e. has the same size and elements).
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   272
     Otherwise return false."
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   273
6735
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   274
    aBag species == self species ifFalse:[^ false].
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   275
    self size == aBag size ifFalse:[^ false].
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   276
    self valuesAndCountsDo:[:val :cnt |
11935
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   277
        (aBag occurrencesOf:val) == cnt ifFalse:[^ false]
6735
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   278
    ].
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   279
    ^ true
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   280
!
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   281
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   282
hash
11935
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   283
    "return an integer useful for hashing on the receiver;
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   284
     redefined since = is redefined here."
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   285
6735
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   286
    |h|
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   287
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   288
    h := self size.
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   289
    self valuesAndCountsDo:[:val :cnt |
11935
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   290
        h := h + cnt hash.
6735
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   291
    ].
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   292
    ^ h
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   293
! !
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   294
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   295
!Bag methodsFor:'converting'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   296
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   297
asBag
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   298
    "return the receiver as a bag"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   299
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   300
    "could be an instance of a subclass..."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   301
    self class == Bag ifTrue:[
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   302
	^ self
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   303
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   304
    ^ super asBag
7161
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   305
!
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   306
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   307
asSet
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   308
    "return the receiver as a set"
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   309
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   310
    ^ contents keys copy
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   311
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   312
    "
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   313
     |b|
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   314
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   315
     b := Bag new.
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   316
     b add:1; add:2; add:3; add:1; add:1.
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   317
     b asSet.
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   318
    "
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
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   321
!Bag methodsFor:'copying'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   322
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   323
postCopy
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   324
    "must copy the contents as well"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   325
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   326
    contents := contents copy
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   327
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   328
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   329
!Bag methodsFor:'enumerating'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   330
a27a279701f8 Initial revision
claus
parents:
diff changeset
   331
do:aBlock
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   332
    "evaluate the block for all elements in the collection.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   333
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   334
     WARNING: do not add/remove elements while iterating over the receiver.
8873
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   335
	      Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   336
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   337
    contents keysAndValuesDo:[:key :value|
8873
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   338
	value timesRepeat:[
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   339
	    aBlock value:key
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   340
	]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   341
    ]
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   342
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   343
    "Modified: 1.3.1996 / 21:42:39 / cg"
345
claus
parents: 154
diff changeset
   344
!
claus
parents: 154
diff changeset
   345
10835
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   346
valuesAndCounts
11481
9da83b7e8c57 comment
Claus Gittinger <cg@exept.de>
parents: 11474
diff changeset
   347
    "return an orderedCollection containing value->count associations"
10835
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   348
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   349
    |coll|
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   350
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   351
    coll := OrderedCollection new.
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   352
    self valuesAndCountsDo:[:value :count | coll add:(value->count)].
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   353
    ^ coll
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   354
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   355
    "Modified: 1.3.1996 / 21:42:44 / cg"
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   356
!
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   357
345
claus
parents: 154
diff changeset
   358
valuesAndCountsDo:aTwoArgBlock
claus
parents: 154
diff changeset
   359
    "evaluate the block for all distinct elements in the collection,
8873
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   360
     passing both the element and the occurrence count as arguments.
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   361
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   362
     WARNING: do not add/remove elements while iterating over the receiver.
8873
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   363
	      Iterate over a copy to do this."
345
claus
parents: 154
diff changeset
   364
claus
parents: 154
diff changeset
   365
    ^ contents keysAndValuesDo:aTwoArgBlock
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   366
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   367
    "Modified: 1.3.1996 / 21:42:44 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   368
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   369
12823
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   370
!Bag methodsFor:'printing & storing'!
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   371
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   372
printElementsDo:aBlock
14685
43b852db6777 class: Bag
Claus Gittinger <cg@exept.de>
parents: 12823
diff changeset
   373
    self valuesAndCountsDo:[:value :count|
43b852db6777 class: Bag
Claus Gittinger <cg@exept.de>
parents: 12823
diff changeset
   374
        aBlock value:('%1(*%2)' bindWith:value with:count).
12823
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   375
    ]
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   376
! !
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   377
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   378
!Bag methodsFor:'private'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   379
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   380
initContents
14956
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   381
    "set the contents to be an empty Dictionary.
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   382
     This is the default method for initialization, which can be redefined in subclasses."
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   383
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   384
    contents := self class contentsClass new
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   385
!
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   386
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   387
initContents:size
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   388
    "set the contents to be an empty Dictionary with initial size.
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   389
     This is the default method for initialization, which can be redefined in subclasses."
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   390
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   391
    contents := self class contentsClass new: size
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   392
!
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   393
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   394
initContentsForEquality
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   395
    "set the contents to be an empty Dictionary"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   396
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   397
    contents := Dictionary new
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   398
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   399
14956
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   400
initContentsForEquality:size
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   401
    "set the contents to be an empty Dictionary with initial size"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   402
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   403
    contents := Dictionary new:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   404
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   405
5375
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   406
initContentsForIdentity
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   407
    "set the contents to be an empty IdentityDictionary"
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   408
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   409
    contents := IdentityDictionary new
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   410
!
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   411
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   412
initContentsForIdentity:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   413
    "set the contents to be an empty IdentityDictionary with initial size"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   414
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   415
    contents := IdentityDictionary new:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   416
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   417
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   418
!Bag methodsFor:'queries'!
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   419
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   420
size
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   421
    "return the number of bag elements"
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   422
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   423
    |count|
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   424
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   425
    count := 0.
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   426
    contents do:[:element | count := count + element].
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   427
    ^ count
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   428
! !
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   429
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   430
!Bag methodsFor:'testing'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   431
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   432
includes:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   433
    "return true, if anObject is in the receiver"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   434
14685
43b852db6777 class: Bag
Claus Gittinger <cg@exept.de>
parents: 12823
diff changeset
   435
    ^ (self occurrencesOf:anObject) > 0
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   436
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   437
15429
087ad8e8da0c added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 14956
diff changeset
   438
isFixedSize
087ad8e8da0c added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 14956
diff changeset
   439
    "return true if the receiver cannot grow"
087ad8e8da0c added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 14956
diff changeset
   440
087ad8e8da0c added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 14956
diff changeset
   441
    ^ false
087ad8e8da0c added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 14956
diff changeset
   442
!
087ad8e8da0c added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 14956
diff changeset
   443
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   444
occurrencesOf:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   445
    "return how many times anObject is in the receiver"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   446
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   447
    ^ contents at:anObject ifAbsent:0
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   448
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   449
633
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   450
!Bag class methodsFor:'documentation'!
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   451
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   452
version
15429
087ad8e8da0c added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 14956
diff changeset
   453
    ^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.45 2013-06-25 11:23:48 cg Exp $'
12823
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   454
!
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   455
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   456
version_CVS
15429
087ad8e8da0c added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 14956
diff changeset
   457
    ^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.45 2013-06-25 11:23:48 cg Exp $'
633
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   458
! !
14685
43b852db6777 class: Bag
Claus Gittinger <cg@exept.de>
parents: 12823
diff changeset
   459