Bag.st
author Stefan Vogel <sv@exept.de>
Tue, 28 Apr 2020 16:21:34 +0200
changeset 25373 f030619565e1
parent 24855 bb545059703b
permissions -rw-r--r--
#REFACTORING by stefan class: ArrayedCollection class changed: #with:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24621
10d24a1c67e4 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 24181
diff changeset
     1
"{ Encoding: utf8 }"
10d24a1c67e4 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 24181
diff changeset
     2
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     3
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     4
 COPYRIGHT (c) 1991 by Claus Gittinger
345
claus
parents: 154
diff changeset
     5
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
"
5375
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    14
"{ Package: 'stx:libbasic' }"
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    15
18468
65a48bf2cc8e class: Bag
Claus Gittinger <cg@exept.de>
parents: 16888
diff changeset
    16
"{ NameSpace: Smalltalk }"
65a48bf2cc8e class: Bag
Claus Gittinger <cg@exept.de>
parents: 16888
diff changeset
    17
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    18
Collection subclass:#Bag
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
    19
	instanceVariableNames:'contents'
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
    20
	classVariableNames:''
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
    21
	poolDictionaries:''
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
    22
	category:'Collections-Unordered'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    23
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    24
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    25
!Bag class methodsFor:'documentation'!
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    26
88
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    27
copyright
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    28
"
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    29
 COPYRIGHT (c) 1991 by Claus Gittinger
345
claus
parents: 154
diff changeset
    30
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    31
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    32
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    33
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    35
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    36
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    37
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    38
"
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    39
!
81dacba7a63a *** empty log message ***
claus
parents: 54
diff changeset
    40
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    41
documentation
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    42
"
20628
8679e14fa67d #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20324
diff changeset
    43
    Bags are collections where the elements are unordered and have no external key. 
8679e14fa67d #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20324
diff changeset
    44
    Elements may occur more than once in a bag. 
8679e14fa67d #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20324
diff changeset
    45
    There is no defined order within a bag.
8679e14fa67d #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20324
diff changeset
    46
    The default implementation uses a dictionary to store each object's occurrence count, 
8679e14fa67d #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20324
diff changeset
    47
    using the object itself as key 
8679e14fa67d #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20324
diff changeset
    48
    (i.e. using = and hash for inclusion tests).
399
claus
parents: 384
diff changeset
    49
20628
8679e14fa67d #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20324
diff changeset
    50
    There is also an instance creation variant (#identityNew:) which creats a
345
claus
parents: 154
diff changeset
    51
    bag which compares using #== and hashes using #identityHash.
20628
8679e14fa67d #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20324
diff changeset
    52
    (I'd say that instantiating an IdentityBag explicitly is better,
399
claus
parents: 384
diff changeset
    53
     ... but for compatibility ... we do it here as well)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    54
1283
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    55
    [Instance variables:]
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    56
20628
8679e14fa67d #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20324
diff changeset
    57
        contents        <Dictionary>    for each element, the number of occurrences
1283
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    58
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    59
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1283
diff changeset
    60
    [author:]
20628
8679e14fa67d #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20324
diff changeset
    61
        Claus Gittinger
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1283
diff changeset
    62
1283
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    63
    [See also:]
20628
8679e14fa67d #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20324
diff changeset
    64
        Set IdentitySet
8679e14fa67d #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20324
diff changeset
    65
        Dictionary IdentityDictionary
8679e14fa67d #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20324
diff changeset
    66
        OrderedCollection Array
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    67
"
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
    68
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    69
a27a279701f8 Initial revision
claus
parents:
diff changeset
    70
!Bag class methodsFor:'instance creation'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    71
14956
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    72
contentsClass
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    73
    "the default class to use for the underlying contents array,
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    74
     used when instantiated with new/new:"
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    75
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    76
    ^ Dictionary
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    77
!
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
equalityNew
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    80
    "return a new empty Bag.
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    81
     Elements will be compared using equality compare (i.e. #= not #== identity)."
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    82
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    83
    ^ self basicNew initContentsForEquality
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    84
!
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    85
345
claus
parents: 154
diff changeset
    86
equalityNew:size
claus
parents: 154
diff changeset
    87
    "return a new empty Bag with initial space for size elements.
claus
parents: 154
diff changeset
    88
     Elements will be compared using equality compare (i.e. #= not #== identity)."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    89
14956
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    90
    ^ self basicNew initContentsForEquality:size
345
claus
parents: 154
diff changeset
    91
!
claus
parents: 154
diff changeset
    92
5375
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    93
identityNew
14956
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    94
    "return a new empty Identity-Bag.
5375
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    95
     Elements will be compared using identity compare (i.e. #== not #= equality)."
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    96
14956
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
    97
    ^ self basicNew initContentsForIdentity
5375
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    98
!
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
    99
345
claus
parents: 154
diff changeset
   100
identityNew:size
claus
parents: 154
diff changeset
   101
    "return a new empty Bag with initial space for size elements.
claus
parents: 154
diff changeset
   102
     Elements will be compared using identity compare (i.e. #== not #= equality)."
claus
parents: 154
diff changeset
   103
5558
dfdb1198bf6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5557
diff changeset
   104
    ^ self basicNew initContentsForIdentity:size
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   105
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   106
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   107
new
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   108
    "return a new empty Bag which compares for equality (i.e. not identity)"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   109
5558
dfdb1198bf6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5557
diff changeset
   110
    ^ self basicNew initContents
345
claus
parents: 154
diff changeset
   111
!
claus
parents: 154
diff changeset
   112
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   113
new:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   114
    "return a new empty Bag with initial space for size elements.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   115
     Elements will be compared using equality compare (i.e. #= not #== identity)."
345
claus
parents: 154
diff changeset
   116
24855
bb545059703b #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 24668
diff changeset
   117
    ^ self basicNew initContents:size
bb545059703b #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 24668
diff changeset
   118
bb545059703b #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 24668
diff changeset
   119
    "Modified: / 22-10-2019 / 20:21:58 / Stefan Vogel"
22308
d6552a1b2fb9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20628
diff changeset
   120
!
d6552a1b2fb9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20628
diff changeset
   121
d6552a1b2fb9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20628
diff changeset
   122
newWithCapacity:size
d6552a1b2fb9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20628
diff changeset
   123
    "return a new empty Collection with capacity for n elements."
d6552a1b2fb9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20628
diff changeset
   124
d6552a1b2fb9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20628
diff changeset
   125
    ^ self new:size
d6552a1b2fb9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20628
diff changeset
   126
d6552a1b2fb9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20628
diff changeset
   127
    "Created: / 10-10-2017 / 17:49:15 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   128
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   129
7261
f35fc9cee675 method category rename
Claus Gittinger <cg@exept.de>
parents: 7161
diff changeset
   130
!Bag methodsFor:'Compatibility-Dolphin'!
6411
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5558
diff changeset
   131
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5558
diff changeset
   132
asAssociations
11935
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   133
    "return the dictionary which associates occurrence-counts
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   134
     to the bags elements. 
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   135
     Same as #contents for dolphin compatibility."
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   136
14685
43b852db6777 class: Bag
Claus Gittinger <cg@exept.de>
parents: 12823
diff changeset
   137
    ^ self contents
6411
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5558
diff changeset
   138
! !
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5558
diff changeset
   139
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   140
!Bag methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   141
a27a279701f8 Initial revision
claus
parents:
diff changeset
   142
at:index
a27a279701f8 Initial revision
claus
parents:
diff changeset
   143
    "report an error: at: is not allowed for Bags"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   144
a27a279701f8 Initial revision
claus
parents:
diff changeset
   145
    ^ self errorNotKeyed
a27a279701f8 Initial revision
claus
parents:
diff changeset
   146
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   147
a27a279701f8 Initial revision
claus
parents:
diff changeset
   148
at:index put:anObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
   149
    "report an error: at:put: is not allowed for Bags"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   150
a27a279701f8 Initial revision
claus
parents:
diff changeset
   151
    ^ self errorNotKeyed
345
claus
parents: 154
diff changeset
   152
!
claus
parents: 154
diff changeset
   153
claus
parents: 154
diff changeset
   154
contents
claus
parents: 154
diff changeset
   155
    "return the dictionary which associates occurrence-counts
claus
parents: 154
diff changeset
   156
     to the bags elements."
claus
parents: 154
diff changeset
   157
claus
parents: 154
diff changeset
   158
    ^ contents
15732
e57fb13f5af2 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15731
diff changeset
   159
e57fb13f5af2 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15731
diff changeset
   160
    "
e57fb13f5af2 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15731
diff changeset
   161
     Bag new
e57fb13f5af2 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15731
diff changeset
   162
        add:'abc';
e57fb13f5af2 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15731
diff changeset
   163
        add:'def';
e57fb13f5af2 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15731
diff changeset
   164
        add:'ghi';
e57fb13f5af2 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15731
diff changeset
   165
        add:'abc';
e57fb13f5af2 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15731
diff changeset
   166
        add:'def';
e57fb13f5af2 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15731
diff changeset
   167
        add:'abc';
e57fb13f5af2 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15731
diff changeset
   168
        add:'abc';
e57fb13f5af2 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15731
diff changeset
   169
        contents
e57fb13f5af2 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15731
diff changeset
   170
    "
15686
2efc686ef383 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15457
diff changeset
   171
!
2efc686ef383 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15457
diff changeset
   172
2efc686ef383 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15457
diff changeset
   173
sortedCounts
15732
e57fb13f5af2 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15731
diff changeset
   174
    "Answer with a collection of counts associated to elements, sorted by decreasing count."
15686
2efc686ef383 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15457
diff changeset
   175
    "Suggested by l. Uzonyi"
2efc686ef383 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15457
diff changeset
   176
    
2efc686ef383 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15457
diff changeset
   177
    ^ (Array new:contents size
2efc686ef383 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15457
diff changeset
   178
        streamContents:[:stream | 
2efc686ef383 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15457
diff changeset
   179
            contents associationsDo:[:each | stream nextPut:each value -> each key ]
2efc686ef383 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15457
diff changeset
   180
        ])
16766
c71254785e72 class: Bag
Stefan Vogel <sv@exept.de>
parents: 16098
diff changeset
   181
        sort:[:x :y | y < x];
15686
2efc686ef383 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15457
diff changeset
   182
        yourself
15731
bb22629fc445 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15686
diff changeset
   183
bb22629fc445 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15686
diff changeset
   184
    "
bb22629fc445 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15686
diff changeset
   185
     Bag new
bb22629fc445 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15686
diff changeset
   186
        add:'abc';
bb22629fc445 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15686
diff changeset
   187
        add:'def';
bb22629fc445 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15686
diff changeset
   188
        add:'ghi';
bb22629fc445 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15686
diff changeset
   189
        add:'abc';
bb22629fc445 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15686
diff changeset
   190
        add:'def';
bb22629fc445 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15686
diff changeset
   191
        add:'abc';
bb22629fc445 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15686
diff changeset
   192
        add:'abc';
bb22629fc445 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15686
diff changeset
   193
        sortedCounts
bb22629fc445 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15686
diff changeset
   194
    "
16098
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   195
!
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   196
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   197
valuesSortedByCounts
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   198
    "Answer with a collection of values, sorted by decreasing count. 
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   199
     Count informtion is lost in the result"
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   200
    
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   201
    ^ self sortedCounts collect:[:assoc | assoc value].
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   202
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   203
    "
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   204
     Bag new
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   205
        add:'abc';
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   206
        add:'def';
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   207
        add:'ghi';
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   208
        add:'abc';
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   209
        add:'def';
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   210
        add:'abc';
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   211
        add:'abc';
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   212
        valuesSortedByCounts   
28eb81300399 class: Bag
Claus Gittinger <cg@exept.de>
parents: 15732
diff changeset
   213
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   214
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   215
a27a279701f8 Initial revision
claus
parents:
diff changeset
   216
!Bag methodsFor:'adding & removing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   217
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   218
add:newObject
345
claus
parents: 154
diff changeset
   219
    "add the argument, anObject to the receiver.
14896
148dd722af88 comment/format in: #add:
Claus Gittinger <cg@exept.de>
parents: 14685
diff changeset
   220
     Returns the object (sigh).
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   221
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   222
     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
   223
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   224
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   225
    |n|
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   226
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   227
    n := contents at:newObject ifAbsent:0.
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   228
    contents at:newObject put:(n + 1).
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   229
    ^ newObject
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   230
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   231
    "Modified: 1.3.1996 / 21:43:06 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   232
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   233
1372
d7bd1b463a65 oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   234
add:newObject withOccurrences:anInteger
345
claus
parents: 154
diff changeset
   235
    "add the argument, anObject anInteger times to the receiver.
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   236
     Returns the object.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   237
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   238
     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
   239
	      Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   240
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   241
    |n|
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   242
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   243
    n := contents at:newObject ifAbsent:0.
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   244
    contents at:newObject put:(n + anInteger).
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   245
    ^ newObject
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   246
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   247
    "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
   248
    "Created: 11.5.1996 / 12:13:43 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   249
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   250
a27a279701f8 Initial revision
claus
parents:
diff changeset
   251
remove:oldObject ifAbsent:anExceptionBlock
19081
fce3c0a198c4 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18468
diff changeset
   252
    "Remove one occurrence of oldObject from the collection.
345
claus
parents: 154
diff changeset
   253
     If it was not present, return the value of the exceptionBlock;
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   254
     otherwise return the removed object.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   255
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   256
     WARNING: do not add/remove elements while iterating over the receiver.
19081
fce3c0a198c4 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18468
diff changeset
   257
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   258
a27a279701f8 Initial revision
claus
parents:
diff changeset
   259
    |count|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   260
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   261
    count := contents at:oldObject ifAbsent:0.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   262
    (count == 0) ifTrue:[^ anExceptionBlock value].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   263
    (count == 1) ifTrue:[
19081
fce3c0a198c4 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18468
diff changeset
   264
        contents removeKey:oldObject
8873
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   265
    ] ifFalse:[
19081
fce3c0a198c4 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18468
diff changeset
   266
        contents at:oldObject put:(count - 1)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   267
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   268
    ^ oldObject
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   269
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   270
    "Modified: 1.3.1996 / 21:43:18 / cg"
345
claus
parents: 154
diff changeset
   271
!
claus
parents: 154
diff changeset
   272
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   273
removeAll
1164
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1145
diff changeset
   274
    "remove all objects from the receiver collection (i.e. make it empty).
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1145
diff changeset
   275
     Returns the receiver."
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   276
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   277
    contents := contents species new.
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   278
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   279
    "
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   280
     |b|
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   281
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   282
     b := Bag new.
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   283
     b add:1; add:2; add:3; add:2; add:1.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1372
diff changeset
   284
     Transcript showCR:b.
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   285
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   286
     b removeAll.
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   287
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1372
diff changeset
   288
     Transcript showCR:b
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   289
    "
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   290
1164
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1145
diff changeset
   291
    "Modified: 12.4.1996 / 13:34:34 / cg"
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   292
!
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 1057
diff changeset
   293
345
claus
parents: 154
diff changeset
   294
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock
claus
parents: 154
diff changeset
   295
    "Remove all occurrences of oldObject from the collection.
claus
parents: 154
diff changeset
   296
     If it was not present, return the value of the exceptionBlock;
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   297
     otherwise return the number of removes.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   298
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   299
     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
   300
	      Iterate over a copy to do this."
345
claus
parents: 154
diff changeset
   301
claus
parents: 154
diff changeset
   302
    |count|
claus
parents: 154
diff changeset
   303
1145
a094d90e11bf dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   304
    count := contents at:oldObject ifAbsent:0.
345
claus
parents: 154
diff changeset
   305
    (count == 0) ifTrue:[^ anExceptionBlock value].
claus
parents: 154
diff changeset
   306
    contents removeKey:oldObject.
claus
parents: 154
diff changeset
   307
    ^ oldObject
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   308
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   309
    "Modified: 1.3.1996 / 21:43:26 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   310
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   311
8860
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   312
!Bag methodsFor:'bulk operations'!
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   313
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   314
sum
11522
6cda81ef426a changed #sum
Claus Gittinger <cg@exept.de>
parents: 11490
diff changeset
   315
    "sum up all elements; return 0 for an empty collection.
6cda81ef426a changed #sum
Claus Gittinger <cg@exept.de>
parents: 11490
diff changeset
   316
     can be done easier, using bags knowledge."
8860
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   317
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   318
    |accu|
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   319
11522
6cda81ef426a changed #sum
Claus Gittinger <cg@exept.de>
parents: 11490
diff changeset
   320
    self isEmpty ifTrue:[ ^ 0 ].
11474
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   321
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   322
    accu := nil.
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   323
    self 
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   324
        valuesAndCountsDo:[:n :count |
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   325
            |thisAmount|
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   326
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   327
            thisAmount := count * n.
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   328
            accu := (accu isNil ifTrue:[thisAmount] ifFalse:[accu + thisAmount]) ].
8860
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   329
    ^ accu
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   330
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   331
    "
11474
f4c8118aab2c showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 10835
diff changeset
   332
     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
   333
    "
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   334
! !
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   335
6735
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   336
!Bag methodsFor:'comparing'!
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   337
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   338
= aBag
11935
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   339
    "Compare the receiver with the argument and return true if the
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   340
     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
   341
     Otherwise return false."
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   342
6735
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   343
    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
   344
    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
   345
    self valuesAndCountsDo:[:val :cnt |
11935
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   346
        (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
   347
    ].
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   348
    ^ true
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   349
!
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   350
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   351
hash
11935
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   352
    "return an integer useful for hashing on the receiver;
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   353
     redefined since = is redefined here."
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   354
6735
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   355
    |h|
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   356
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   357
    h := self size.
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   358
    self valuesAndCountsDo:[:val :cnt |
11935
799201e55e7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11523
diff changeset
   359
        h := h + cnt hash.
6735
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   360
    ].
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   361
    ^ h
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   362
! !
fad09454a025 added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   363
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   364
!Bag methodsFor:'converting'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   365
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   366
asBag
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   367
    "return the receiver as a bag"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   368
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   369
    "could be an instance of a subclass..."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   370
    self class == Bag ifTrue:[
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   371
	^ self
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   372
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   373
    ^ super asBag
7161
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   374
!
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   375
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   376
asSet
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   377
    "return the receiver as a set"
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   378
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   379
    ^ contents keys copy
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   380
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   381
    "
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   382
     |b|
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   383
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   384
     b := Bag new.
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   385
     b add:1; add:2; add:3; add:1; add:1.
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   386
     b asSet.
Claus Gittinger <cg@exept.de>
parents: 6735
diff changeset
   387
    "
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   388
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   389
24181
fbc74b464089 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23699
diff changeset
   390
!Bag methodsFor:'copying-private'!
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   391
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   392
postCopy
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   393
    "must copy the contents as well"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   394
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   395
    contents := contents copy
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
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   398
!Bag methodsFor:'enumerating'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   399
a27a279701f8 Initial revision
claus
parents:
diff changeset
   400
do:aBlock
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   401
    "evaluate the block for all elements in the collection.
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   402
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   403
     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
   404
	      Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   405
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 11
diff changeset
   406
    contents keysAndValuesDo:[:key :value|
8873
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   407
	value timesRepeat:[
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   408
	    aBlock value:key
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   409
	]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   410
    ]
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   411
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   412
    "Modified: 1.3.1996 / 21:42:39 / cg"
345
claus
parents: 154
diff changeset
   413
!
claus
parents: 154
diff changeset
   414
16779
148f0fa5ad67 class: Bag
Stefan Vogel <sv@exept.de>
parents: 16766
diff changeset
   415
keysAndValuesDo:aTwoArgBlock
148f0fa5ad67 class: Bag
Stefan Vogel <sv@exept.de>
parents: 16766
diff changeset
   416
    "evaluate the block for all distinct elements in the collection,
148f0fa5ad67 class: Bag
Stefan Vogel <sv@exept.de>
parents: 16766
diff changeset
   417
     passing both the element and the occurrence count as arguments.
148f0fa5ad67 class: Bag
Stefan Vogel <sv@exept.de>
parents: 16766
diff changeset
   418
148f0fa5ad67 class: Bag
Stefan Vogel <sv@exept.de>
parents: 16766
diff changeset
   419
     WARNING: do not add/remove elements while iterating over the receiver.
148f0fa5ad67 class: Bag
Stefan Vogel <sv@exept.de>
parents: 16766
diff changeset
   420
              Iterate over a copy to do this."
148f0fa5ad67 class: Bag
Stefan Vogel <sv@exept.de>
parents: 16766
diff changeset
   421
24621
10d24a1c67e4 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 24181
diff changeset
   422
    contents keysAndValuesDo:aTwoArgBlock
16779
148f0fa5ad67 class: Bag
Stefan Vogel <sv@exept.de>
parents: 16766
diff changeset
   423
!
148f0fa5ad67 class: Bag
Stefan Vogel <sv@exept.de>
parents: 16766
diff changeset
   424
10835
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   425
valuesAndCounts
11481
9da83b7e8c57 comment
Claus Gittinger <cg@exept.de>
parents: 11474
diff changeset
   426
    "return an orderedCollection containing value->count associations"
10835
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   427
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   428
    |coll|
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   429
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   430
    coll := OrderedCollection new.
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   431
    self valuesAndCountsDo:[:value :count | coll add:(value->count)].
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   432
    ^ coll
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   433
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   434
    "Modified: 1.3.1996 / 21:42:44 / cg"
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   435
!
e23097df1639 +valuesAndCounts
Claus Gittinger <cg@exept.de>
parents: 8873
diff changeset
   436
24668
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   437
valuesAndCountsDetect:aTwoArgBlock ifNone:exceptionValue
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   438
    "evaluate the block for all distinct elements in the collection,
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   439
     passing both the element and the occurrence count as arguments.
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   440
     If any returns true, return an association consisting of value and occurrence count.
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   441
     If not, return the value from exceptionValue.
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   442
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   443
     WARNING: do not add/remove elements while iterating over the receiver.
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   444
              Iterate over a copy to do this."
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   445
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   446
    |collected|
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   447
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   448
    collected := OrderedCollection new.
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   449
    self valuesAndCountsDo:[:eachValue :eachCount|
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   450
        (aTwoArgBlock value:eachValue value:eachCount) ifTrue:[
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   451
            ^ eachValue -> eachCount
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   452
        ].
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   453
    ].
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   454
    ^ exceptionValue value
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   455
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   456
    "
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   457
     #(10 20 20 30 40) asBag
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   458
        valuesAndCountsDetect:[:eachValue :eachCount | eachCount > 1]
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   459
        ifNone:nil     
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   460
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   461
     #(10 20 20 30 40) asBag
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   462
        valuesAndCountsDetect:[:eachValue :eachCount | eachCount > 3]
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   463
        ifNone:nil     
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   464
    "
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   465
!
428fc2bc187b #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24621
diff changeset
   466
345
claus
parents: 154
diff changeset
   467
valuesAndCountsDo:aTwoArgBlock
claus
parents: 154
diff changeset
   468
    "evaluate the block for all distinct elements in the collection,
8873
eecab5af2679 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8860
diff changeset
   469
     passing both the element and the occurrence count as arguments.
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   470
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   471
     WARNING: do not add/remove elements while iterating over the receiver.
24621
10d24a1c67e4 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 24181
diff changeset
   472
              Iterate over a copy to do this."
345
claus
parents: 154
diff changeset
   473
24621
10d24a1c67e4 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 24181
diff changeset
   474
    contents keysAndValuesDo:aTwoArgBlock
1057
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   475
75be0a10cc29 commentary
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   476
    "Modified: 1.3.1996 / 21:42:44 / cg"
20324
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   477
!
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   478
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   479
valuesAndCountsSelect:aTwoArgBlock
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   480
    "evaluate the block for all distinct elements in the collection,
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   481
     passing both the element and the occurrence count as arguments.
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   482
     If that returns true, add the element to the OrderedCollection.
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   483
     Answer the OrderedCollection.
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   484
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   485
     WARNING: do not add/remove elements while iterating over the receiver.
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   486
              Iterate over a copy to do this."
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   487
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   488
    |collected|
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   489
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   490
    collected := OrderedCollection new.
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   491
    self valuesAndCountsDo:[:eachValue :eachCount|
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   492
        (aTwoArgBlock value:eachValue value:eachCount) ifTrue:[
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   493
            collected add:eachValue
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   494
        ].
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   495
    ].
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   496
    ^ collected
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   497
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   498
    "
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   499
     #(10 20 20 30 40) asBag
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   500
        valuesAndCountsSelect:[:eachValue :eachCount | eachCount > 1] 
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   501
    "
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   502
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   503
767fdd53f30a #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 19081
diff changeset
   504
    "Modified: 1.3.1996 / 21:42:44 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   505
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   506
16766
c71254785e72 class: Bag
Stefan Vogel <sv@exept.de>
parents: 16098
diff changeset
   507
12823
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   508
!Bag methodsFor:'printing & storing'!
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   509
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   510
printElementsDo:aBlock
14685
43b852db6777 class: Bag
Claus Gittinger <cg@exept.de>
parents: 12823
diff changeset
   511
    self valuesAndCountsDo:[:value :count|
43b852db6777 class: Bag
Claus Gittinger <cg@exept.de>
parents: 12823
diff changeset
   512
        aBlock value:('%1(*%2)' bindWith:value with:count).
12823
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   513
    ]
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   514
! !
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   515
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   516
!Bag methodsFor:'private'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   517
16888
3b93adf0c7ae class: Bag
Claus Gittinger <cg@exept.de>
parents: 16779
diff changeset
   518
grow:newSize
3b93adf0c7ae class: Bag
Claus Gittinger <cg@exept.de>
parents: 16779
diff changeset
   519
    "/ ignored here
3b93adf0c7ae class: Bag
Claus Gittinger <cg@exept.de>
parents: 16779
diff changeset
   520
!
3b93adf0c7ae class: Bag
Claus Gittinger <cg@exept.de>
parents: 16779
diff changeset
   521
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   522
initContents
14956
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   523
    "set the contents to be an empty Dictionary.
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   524
     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
   525
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   526
    contents := self class contentsClass new
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   527
!
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   528
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   529
initContents:size
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   530
    "set the contents to be an empty Dictionary with initial size.
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   531
     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
   532
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   533
    contents := self class contentsClass new: size
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   534
!
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   535
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   536
initContentsForEquality
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   537
    "set the contents to be an empty Dictionary"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   538
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   539
    contents := Dictionary new
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   540
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   541
14956
bda5a86c5525 class: Bag
Claus Gittinger <cg@exept.de>
parents: 14896
diff changeset
   542
initContentsForEquality:size
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   543
    "set the contents to be an empty Dictionary with initial size"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   544
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   545
    contents := Dictionary new:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   546
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   547
5375
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   548
initContentsForIdentity
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   549
    "set the contents to be an empty IdentityDictionary"
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   550
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   551
    contents := IdentityDictionary new
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   552
!
ed4815509a57 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   553
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   554
initContentsForIdentity:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   555
    "set the contents to be an empty IdentityDictionary with initial size"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   556
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   557
    contents := IdentityDictionary new:size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   558
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   559
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   560
!Bag methodsFor:'queries'!
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   561
23699
360ee3499a15 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22308
diff changeset
   562
includes:anObject
360ee3499a15 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22308
diff changeset
   563
    "return true, if anObject is in the receiver"
360ee3499a15 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22308
diff changeset
   564
360ee3499a15 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22308
diff changeset
   565
    ^ (self occurrencesOf:anObject) > 0
360ee3499a15 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22308
diff changeset
   566
!
360ee3499a15 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22308
diff changeset
   567
360ee3499a15 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22308
diff changeset
   568
occurrencesOf:anObject
360ee3499a15 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22308
diff changeset
   569
    "return how many times anObject is in the receiver"
360ee3499a15 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22308
diff changeset
   570
360ee3499a15 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22308
diff changeset
   571
    ^ contents at:anObject ifAbsent:0
360ee3499a15 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22308
diff changeset
   572
!
360ee3499a15 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22308
diff changeset
   573
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   574
size
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   575
    "return the number of bag elements"
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   576
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   577
    |count|
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   578
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   579
    count := 0.
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   580
    contents do:[:element | count := count + element].
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   581
    ^ count
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   582
! !
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5375
diff changeset
   583
15457
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   584
!Bag methodsFor:'statistical functions'!
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   585
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   586
variance
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   587
    "compute the variance over a complete data set (and not of a sample)"
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   588
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   589
    |m sz sumDeltaSquares|
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   590
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   591
    m := self arithmeticMean.
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   592
    sumDeltaSquares := 0.
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   593
    sz := 0.
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   594
    self 
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   595
        valuesAndCountsDo:[:val :count |
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   596
            sumDeltaSquares :=  sumDeltaSquares + ((val - m) squared).
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   597
            sz := sz + 1.
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   598
        ].
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   599
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   600
    ^ sumDeltaSquares / sz
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   601
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   602
    "
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   603
     TestCase assert:( #(1 1 1 2 2 2 1 1 1 2 2 2) asBag variance = #(1 1 1 2 2 2 1 1 1 2 2 2) variance).
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   604
     TestCase assert:( #(1 1 1 1 1 0 0 0 0 0) asBag variance = #(1 1 1 1 1 0 0 0 0 0) variance).
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   605
    "
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   606
! !
af0513df339f class: Bag
Stefan Vogel <sv@exept.de>
parents: 15429
diff changeset
   607
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   608
!Bag methodsFor:'testing'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   609
15429
087ad8e8da0c added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 14956
diff changeset
   610
isFixedSize
087ad8e8da0c added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 14956
diff changeset
   611
    "return true if the receiver cannot grow"
087ad8e8da0c added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 14956
diff changeset
   612
087ad8e8da0c added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 14956
diff changeset
   613
    ^ false
087ad8e8da0c added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 14956
diff changeset
   614
!
087ad8e8da0c added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 14956
diff changeset
   615
18468
65a48bf2cc8e class: Bag
Claus Gittinger <cg@exept.de>
parents: 16888
diff changeset
   616
isOrdered
65a48bf2cc8e class: Bag
Claus Gittinger <cg@exept.de>
parents: 16888
diff changeset
   617
    "return true, if the receiver's elements are ordered.
65a48bf2cc8e class: Bag
Claus Gittinger <cg@exept.de>
parents: 16888
diff changeset
   618
     Redefined to return false here, because the order of keys and values
65a48bf2cc8e class: Bag
Claus Gittinger <cg@exept.de>
parents: 16888
diff changeset
   619
     may change due to rehashing, when elements are added/removed"
65a48bf2cc8e class: Bag
Claus Gittinger <cg@exept.de>
parents: 16888
diff changeset
   620
65a48bf2cc8e class: Bag
Claus Gittinger <cg@exept.de>
parents: 16888
diff changeset
   621
    ^ false
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   622
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   623
633
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   624
!Bag class methodsFor:'documentation'!
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   625
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   626
version
19081
fce3c0a198c4 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18468
diff changeset
   627
    ^ '$Header$'
12823
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   628
!
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   629
aa0b7ba87240 added: #printElementsDo:
Stefan Vogel <sv@exept.de>
parents: 11935
diff changeset
   630
version_CVS
19081
fce3c0a198c4 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18468
diff changeset
   631
    ^ '$Header$'
633
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   632
! !
14685
43b852db6777 class: Bag
Claus Gittinger <cg@exept.de>
parents: 12823
diff changeset
   633