Coll.st
author Claus Gittinger <cg@exept.de>
Thu, 23 Nov 1995 15:23:19 +0100
changeset 624 f09ad5cf21d4
parent 602 771ab7a8c4bf
child 628 7aa563e4c64a
permissions -rw-r--r--
cannot access Objects classVars directly in nil-subclass
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) 1989 by Claus Gittinger
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
Object subclass:#Collection
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    14
	 instanceVariableNames:''
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    15
	 classVariableNames:'InvalidKeySignal EmptyCollectionSignal ValueNotFoundSignal
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    16
                NotEnoughElementsSignal'
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    17
	 poolDictionaries:''
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    18
	 category:'Collections-Abstract'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    20
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    21
!Collection class methodsFor:'documentation'!
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    22
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    23
copyright
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    24
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    25
 COPYRIGHT (c) 1989 by Claus Gittinger
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
    26
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    27
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    28
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    29
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    31
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    32
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    33
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    34
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    35
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    36
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    37
documentation
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    38
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    39
    Abstract superclass for all collections.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    40
    This abstract class provides functionality common to all collections,
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    41
    without knowing how the concrete class implements things. Thus, all
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    42
    methods found here depend on some basic mechanisms to be defined in the
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    43
    concrete class. 
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    44
    These basic methods are usually defined as #subclassResponsibility here.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    45
    Some methods are also redefined for better performance.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    46
"
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    47
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    48
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    49
version
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    50
    ^ '$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.43 1995-11-23 01:06:19 cg Exp $'
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    51
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    52
10
claus
parents: 5
diff changeset
    53
!Collection class methodsFor:'initialization'!
claus
parents: 5
diff changeset
    54
claus
parents: 5
diff changeset
    55
initialize
claus
parents: 5
diff changeset
    56
    "setup the signal"
claus
parents: 5
diff changeset
    57
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
    58
    InvalidKeySignal isNil ifTrue:[
302
1f76060d58a4 *** empty log message ***
claus
parents: 293
diff changeset
    59
	InvalidKeySignal := ErrorSignal newSignalMayProceed:true.
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
    60
	InvalidKeySignal nameClass:self message:#invalidKeySignal.
345
claus
parents: 342
diff changeset
    61
	InvalidKeySignal notifierString:'invalid key:'.
claus
parents: 342
diff changeset
    62
claus
parents: 342
diff changeset
    63
	ValueNotFoundSignal := ErrorSignal newSignalMayProceed:true.
claus
parents: 342
diff changeset
    64
	ValueNotFoundSignal nameClass:self message:#valueNotFoundSignal.
claus
parents: 342
diff changeset
    65
	ValueNotFoundSignal notifierString:'value not found:'.
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
    66
348
claus
parents: 345
diff changeset
    67
	NotEnoughElementsSignal := ErrorSignal newSignalMayProceed:true.
claus
parents: 345
diff changeset
    68
	NotEnoughElementsSignal nameClass:self message:#notEnoughElementsSignal.
claus
parents: 345
diff changeset
    69
	NotEnoughElementsSignal notifierString:'not enough elements in collection'.
claus
parents: 345
diff changeset
    70
claus
parents: 345
diff changeset
    71
	EmptyCollectionSignal := NotEnoughElementsSignal newSignalMayProceed:true.
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
    72
	EmptyCollectionSignal nameClass:self message:#emptyCollectionSignal.
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
    73
	EmptyCollectionSignal notifierString:'operation not allowed for empty collections'.
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
    74
    ]
10
claus
parents: 5
diff changeset
    75
! !
claus
parents: 5
diff changeset
    76
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    77
!Collection class methodsFor:'instance creation'!
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
    78
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    79
new:size withAll:element
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    80
    "return a new COllection of size, where all elements are
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    81
     initialized to element"
348
claus
parents: 345
diff changeset
    82
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    83
    |newCollection|
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    84
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    85
    newCollection := self new:size.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    86
    size timesRepeat:[newCollection add:element]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
    87
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    88
a27a279701f8 Initial revision
claus
parents:
diff changeset
    89
with:anObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
    90
    "return a new Collection with one element:anObject"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    91
a27a279701f8 Initial revision
claus
parents:
diff changeset
    92
    |newCollection|
a27a279701f8 Initial revision
claus
parents:
diff changeset
    93
a27a279701f8 Initial revision
claus
parents:
diff changeset
    94
    newCollection := self new.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    95
    newCollection add:anObject.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    96
    ^ newCollection
a27a279701f8 Initial revision
claus
parents:
diff changeset
    97
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    98
a27a279701f8 Initial revision
claus
parents:
diff changeset
    99
with:firstObject with:secondObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
   100
    "return a new Collection with two elements:firstObject and secondObject"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   101
a27a279701f8 Initial revision
claus
parents:
diff changeset
   102
    |newCollection|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   103
a27a279701f8 Initial revision
claus
parents:
diff changeset
   104
    newCollection := self new.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   105
    newCollection add:firstObject.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   106
    newCollection add:secondObject.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   107
    ^ newCollection
a27a279701f8 Initial revision
claus
parents:
diff changeset
   108
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   109
a27a279701f8 Initial revision
claus
parents:
diff changeset
   110
with:firstObject with:secondObject with:thirdObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
   111
    "return a new Collection with three elements"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   112
a27a279701f8 Initial revision
claus
parents:
diff changeset
   113
    |newCollection|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   114
a27a279701f8 Initial revision
claus
parents:
diff changeset
   115
    newCollection := self new.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   116
    newCollection add:firstObject.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   117
    newCollection add:secondObject.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   118
    newCollection add:thirdObject.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   119
    ^ newCollection
a27a279701f8 Initial revision
claus
parents:
diff changeset
   120
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   121
a27a279701f8 Initial revision
claus
parents:
diff changeset
   122
with:firstObject with:secondObject with:thirdObject with:fourthObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
   123
    "return a new Collection with four elements"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   124
a27a279701f8 Initial revision
claus
parents:
diff changeset
   125
    |newCollection|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   126
a27a279701f8 Initial revision
claus
parents:
diff changeset
   127
    newCollection := self new.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   128
    newCollection add:firstObject.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   129
    newCollection add:secondObject.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   130
    newCollection add:thirdObject.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   131
    newCollection add:fourthObject.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   132
    ^ newCollection
a27a279701f8 Initial revision
claus
parents:
diff changeset
   133
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   134
345
claus
parents: 342
diff changeset
   135
with:one with:two with:three with:four with:five
claus
parents: 342
diff changeset
   136
    "return a new Collection with five elements"
claus
parents: 342
diff changeset
   137
claus
parents: 342
diff changeset
   138
    |newCollection|
claus
parents: 342
diff changeset
   139
claus
parents: 342
diff changeset
   140
    newCollection := self new.
claus
parents: 342
diff changeset
   141
    newCollection add:one.
claus
parents: 342
diff changeset
   142
    newCollection add:two.
claus
parents: 342
diff changeset
   143
    newCollection add:three.
claus
parents: 342
diff changeset
   144
    newCollection add:four.
claus
parents: 342
diff changeset
   145
    newCollection add:five.
claus
parents: 342
diff changeset
   146
    ^ newCollection
claus
parents: 342
diff changeset
   147
!
claus
parents: 342
diff changeset
   148
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   149
withAll:aCollection
a27a279701f8 Initial revision
claus
parents:
diff changeset
   150
    "return a new Collection with all elements taken from the argument,
a27a279701f8 Initial revision
claus
parents:
diff changeset
   151
     aCollection"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   152
a27a279701f8 Initial revision
claus
parents:
diff changeset
   153
    |newCollection|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   154
a27a279701f8 Initial revision
claus
parents:
diff changeset
   155
    newCollection := self new.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   156
    newCollection addAll:aCollection.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   157
    ^newCollection
a27a279701f8 Initial revision
claus
parents:
diff changeset
   158
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   159
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   160
!Collection class methodsFor:'Signal constants'!
10
claus
parents: 5
diff changeset
   161
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   162
emptyCollectionSignal
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   163
    "return the signal used to report non-allowed operation on empty collections"
345
claus
parents: 342
diff changeset
   164
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   165
    ^ EmptyCollectionSignal
308
f04744ef7b5d *** empty log message ***
claus
parents: 302
diff changeset
   166
!
f04744ef7b5d *** empty log message ***
claus
parents: 302
diff changeset
   167
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   168
invalidKeySignal
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   169
    "return the signal used to report bad key usage"
308
f04744ef7b5d *** empty log message ***
claus
parents: 302
diff changeset
   170
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   171
    ^ InvalidKeySignal
201
1deff0d47f37 changed copyEmpty scheme
claus
parents: 169
diff changeset
   172
!
1deff0d47f37 changed copyEmpty scheme
claus
parents: 169
diff changeset
   173
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   174
notEnoughElementsSignal
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   175
    "return the signal used to report attempts for an operation, for which
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   176
     there are not enough elements in the collection"
249
claus
parents: 244
diff changeset
   177
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   178
    ^ NotEnoughElementsSignal
404
claus
parents: 384
diff changeset
   179
!
claus
parents: 384
diff changeset
   180
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   181
valueNotFoundSignal
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   182
    "return the signal used to report a nonexisting element."
404
claus
parents: 384
diff changeset
   183
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   184
    ^ ValueNotFoundSignal
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   185
! !
404
claus
parents: 384
diff changeset
   186
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   187
!Collection class methodsFor:'queries'!
404
claus
parents: 384
diff changeset
   188
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   189
growIsCheap
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   190
    "return true, if this collection can easily grow
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   191
     (i.e. without a need for become:).
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   192
     Returns true here; this method is redefined in fix-size
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   193
     collections"
404
claus
parents: 384
diff changeset
   194
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   195
    ^ true
257
bc569c7f4ba5 *** empty log message ***
claus
parents: 249
diff changeset
   196
! !
bc569c7f4ba5 *** empty log message ***
claus
parents: 249
diff changeset
   197
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   198
!Collection methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   199
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   200
anElement
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   201
    "return any element from the collection, or nil if there is none"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   202
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   203
    self do: [:each | ^ each].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   204
    ^ nil
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   205
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   206
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   207
first
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   208
    "return the first element of the collection.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   209
     This should be redefined in subclasses."
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   210
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   211
    self do:[:e | ^ e].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   212
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   213
    "error if collection is empty"
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
   214
    ^ self emptyCollectionError
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   215
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   216
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   217
last
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   218
    "return the last element of the collection.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   219
     This should be redefined in subclasses."
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   220
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   221
    |theLastOne any|
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   222
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   223
    any := false.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   224
    self do:[:e | any := true. theLastOne := e].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   225
    any ifTrue:[
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
   226
	^ theLastOne
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   227
    ].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   228
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   229
    "error if collection is empty"
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
   230
    ^ self emptyCollectionError
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   231
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   232
328
claus
parents: 326
diff changeset
   233
upTo:anElement
claus
parents: 326
diff changeset
   234
    "return a new collection consisting of the receivers elements upTo
claus
parents: 326
diff changeset
   235
     (but excluding) anElement.
claus
parents: 326
diff changeset
   236
     If anElement is not in the receiver, the returned collection
claus
parents: 326
diff changeset
   237
     will consist of all elements of the receiver"
claus
parents: 326
diff changeset
   238
claus
parents: 326
diff changeset
   239
    |s|
claus
parents: 326
diff changeset
   240
claus
parents: 326
diff changeset
   241
    s := ReadStream on:self.
claus
parents: 326
diff changeset
   242
    ^ s upTo:anElement
claus
parents: 326
diff changeset
   243
claus
parents: 326
diff changeset
   244
    "
claus
parents: 326
diff changeset
   245
     'test.text' upTo:$.  
claus
parents: 326
diff changeset
   246
     #(1 2 3 4 5) upTo:4  
claus
parents: 326
diff changeset
   247
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   248
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   249
a27a279701f8 Initial revision
claus
parents:
diff changeset
   250
!Collection methodsFor:'adding & removing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   251
a27a279701f8 Initial revision
claus
parents:
diff changeset
   252
add:anObject
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   253
    "add the argument, anObject to the receiver.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   254
     If the receiver is ordered, the position of the new element is undefined
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   255
     (i.e. dont depend on where it will be put)."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   256
a27a279701f8 Initial revision
claus
parents:
diff changeset
   257
    ^ self subclassResponsibility
a27a279701f8 Initial revision
claus
parents:
diff changeset
   258
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   259
a27a279701f8 Initial revision
claus
parents:
diff changeset
   260
addAll:aCollection
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   261
    "add all elements of the argument, aCollection to the receiver."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   262
a27a279701f8 Initial revision
claus
parents:
diff changeset
   263
    aCollection do:[:element |
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
   264
	self add:element
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   265
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   266
    ^ aCollection
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   267
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   268
    "
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   269
     #(1 2 3 4) copy addAll:#(5 6 7 8)
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   270
     #(1 2 3 4) asOrderedCollection addAll:#(5 6 7 8)
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   271
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   272
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   273
12
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   274
addAllFirst:aCollection
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   275
    "insert all elements of the argument, aCollection at the beginning
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   276
     of the receiver"
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   277
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   278
    aCollection reverseDo:[:element | 
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
   279
	self addFirst:element 
12
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   280
    ].
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   281
    ^ aCollection
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   282
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   283
    "
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   284
     |c|
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   285
     c := #(4 3 2 1) asOrderedCollection.
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   286
     c addAllFirst:#(9 8 7 6 5)
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   287
    "
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   288
!
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   289
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   290
addAllLast:aCollection
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   291
    "add all elements of the argument, aCollection to the receiver"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   292
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   293
    aCollection do:[:element | 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   294
	self addLast:element 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   295
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   296
    ^ aCollection
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   297
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   298
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   299
     |c|
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   300
     c := #(4 3 2 1) asOrderedCollection.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   301
     c addAllLast:#(9 8 7 6 5)
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   302
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   303
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   304
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   305
addFirst:anObject
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   306
    "add the argument, anObject to the receiver.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   307
     If the receiver is ordered, the new element will be added at the beginning."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   308
a27a279701f8 Initial revision
claus
parents:
diff changeset
   309
    ^ self subclassResponsibility
a27a279701f8 Initial revision
claus
parents:
diff changeset
   310
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   311
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   312
addLast:anObject
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   313
    "add the argument, anObject to the receiver. 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   314
     If the receiver is ordered, the new element will be added at the end."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   315
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   316
    ^ self add:anObject
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   317
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   318
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   319
remove:anObject
348
claus
parents: 345
diff changeset
   320
    "remove the (first occurrence of) argument, anObject from the receiver,
claus
parents: 345
diff changeset
   321
     and return it.
claus
parents: 345
diff changeset
   322
     If it was not in the collection, raise an error."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   323
345
claus
parents: 342
diff changeset
   324
    self remove:anObject ifAbsent:[self errorValueNotFound:anObject]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   325
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   326
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   327
remove:anObject ifAbsent:exceptionBlock
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   328
    "remove the (first occurrence of) argument, anObject from the receiver,
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   329
     and return it.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   330
     If it was not in the collection, return the the value of the exceptionBlock"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   331
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   332
    ^ self subclassResponsibility
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   333
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   334
134
c96945f0aa1d added removeAll
claus
parents: 92
diff changeset
   335
removeAll
c96945f0aa1d added removeAll
claus
parents: 92
diff changeset
   336
    "remove all elements from the receiver.
c96945f0aa1d added removeAll
claus
parents: 92
diff changeset
   337
     This should be reimplemented in subclasses for better
c96945f0aa1d added removeAll
claus
parents: 92
diff changeset
   338
     performance."
c96945f0aa1d added removeAll
claus
parents: 92
diff changeset
   339
c96945f0aa1d added removeAll
claus
parents: 92
diff changeset
   340
    [self notEmpty] whileTrue:[
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
   341
	self removeFirst
134
c96945f0aa1d added removeAll
claus
parents: 92
diff changeset
   342
    ].
c96945f0aa1d added removeAll
claus
parents: 92
diff changeset
   343
!
c96945f0aa1d added removeAll
claus
parents: 92
diff changeset
   344
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   345
removeAll:aCollection
360
claus
parents: 348
diff changeset
   346
    "remove all elements of the argument, aCollection from the receiver.
claus
parents: 348
diff changeset
   347
     Notice: for some collections (those not tuned for
claus
parents: 348
diff changeset
   348
	     resizing themself) this may be very slow.
claus
parents: 348
diff changeset
   349
	     If the number of removed elements is big compared to to
claus
parents: 348
diff changeset
   350
	     the receivers size, it may be better to copy the
claus
parents: 348
diff changeset
   351
	     ones which are not to be removed into a new collection."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   352
a27a279701f8 Initial revision
claus
parents:
diff changeset
   353
    aCollection do:[:element | self remove:element].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   354
    ^ aCollection
348
claus
parents: 345
diff changeset
   355
!
claus
parents: 345
diff changeset
   356
claus
parents: 345
diff changeset
   357
removeFirst
claus
parents: 345
diff changeset
   358
    "remove the first element from the receiver.
claus
parents: 345
diff changeset
   359
     Return the removed element."
claus
parents: 345
diff changeset
   360
claus
parents: 345
diff changeset
   361
    ^ self subclassResponsibility
claus
parents: 345
diff changeset
   362
!
claus
parents: 345
diff changeset
   363
claus
parents: 345
diff changeset
   364
removeFirst:n
claus
parents: 345
diff changeset
   365
    "remove the first n elements from the receiver.
360
claus
parents: 348
diff changeset
   366
     Return an array filled with the removed elements.
claus
parents: 348
diff changeset
   367
     Notice: for some collections (those not tuned for
claus
parents: 348
diff changeset
   368
	     resizing themself) this may be very slow."
348
claus
parents: 345
diff changeset
   369
claus
parents: 345
diff changeset
   370
    |ret|
claus
parents: 345
diff changeset
   371
claus
parents: 345
diff changeset
   372
    self size < n ifTrue:[
claus
parents: 345
diff changeset
   373
	^ self notEnoughElementsError
claus
parents: 345
diff changeset
   374
    ].
claus
parents: 345
diff changeset
   375
    ret := Array new:n.
claus
parents: 345
diff changeset
   376
    1 to:n do:[:i |
claus
parents: 345
diff changeset
   377
	ret at:i put:(self removeFirst).
claus
parents: 345
diff changeset
   378
    ].
claus
parents: 345
diff changeset
   379
    ^ ret
claus
parents: 345
diff changeset
   380
!
claus
parents: 345
diff changeset
   381
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   382
removeLast
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   383
    "remove the last element from the receiver.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   384
     Return the removed element."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   385
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   386
    ^ self subclassResponsibility
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   387
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   388
348
claus
parents: 345
diff changeset
   389
removeLast:n
claus
parents: 345
diff changeset
   390
    "remove the last n elements from the receiver.
360
claus
parents: 348
diff changeset
   391
     Return an array filled with the removed elements.
claus
parents: 348
diff changeset
   392
     Notice: for some collections (those not tuned for
claus
parents: 348
diff changeset
   393
	     resizing themself) this may be very slow."
348
claus
parents: 345
diff changeset
   394
claus
parents: 345
diff changeset
   395
    |ret|
claus
parents: 345
diff changeset
   396
claus
parents: 345
diff changeset
   397
    self size < n ifTrue:[
claus
parents: 345
diff changeset
   398
	^ self notEnoughElementsError
claus
parents: 345
diff changeset
   399
    ].
claus
parents: 345
diff changeset
   400
    ret := Array new:n.
claus
parents: 345
diff changeset
   401
    n to:1 by:-1 do:[:i |
claus
parents: 345
diff changeset
   402
	ret at:i put:(self removeLast).
claus
parents: 345
diff changeset
   403
    ].
claus
parents: 345
diff changeset
   404
    ^ ret
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   405
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   406
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   407
!Collection methodsFor:'converting'!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   408
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   409
asArray
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   410
    "return a new Array with the collections elements"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   411
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   412
    |anArray 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   413
     index "{ Class: SmallInteger }" |
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   414
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   415
    anArray := Array new:(self size).
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   416
    index := 1.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   417
    self do:[:each |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   418
	anArray at:index put:each.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   419
	index := index + 1
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   420
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   421
    ^ anArray
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   422
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   423
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   424
asBag
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   425
    "return a new Bag with the receiver collections elements"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   426
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   427
    ^ self addAllTo:(Bag new)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   428
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   429
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   430
asByteArray
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   431
    "return a new ByteArray with the collections elements
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   432
     (which must convert to integers in the range 0..255)."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   433
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   434
    |aByteArray 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   435
     index "{ Class: SmallInteger }" |
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   436
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   437
    aByteArray := ByteArray new:(self size).
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   438
    index := 1.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   439
    self do:[:each |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   440
	aByteArray at:index put:each asInteger.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   441
	index := index + 1
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   442
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   443
    ^ aByteArray
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   444
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   445
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   446
asDoubleArray
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   447
    "return a new DoubleArray with the collections elements
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   448
     (which must convert to floats)."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   449
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   450
    |aDoubleArray
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   451
     index "{ Class: SmallInteger }" |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   452
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   453
    aDoubleArray := DoubleArray new:(self size).
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   454
    index := 1.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   455
    self do:[:each |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   456
	aDoubleArray at:index put:each asFloat.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   457
	index := index + 1
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   458
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   459
    ^ aDoubleArray
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   460
!
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   461
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   462
asFloatArray
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   463
    "return a new FloatArray with the collections elements
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   464
     (which must convert to floats)."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   465
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   466
    |aFloatArray
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   467
     index "{ Class: SmallInteger }" |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   468
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   469
    aFloatArray := FloatArray new:(self size).
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   470
    index := 1.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   471
    self do:[:each |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   472
	aFloatArray at:index put:each asFloat.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   473
	index := index + 1
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   474
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   475
    ^ aFloatArray
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   476
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   477
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   478
asIdentitySet
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   479
    "return a new IdentitySet with the receiver collections elements"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   480
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   481
    ^ self addAllTo:(IdentitySet new:self size)
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   482
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   483
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   484
asOrderedCollection
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   485
    "return a new OrderedCollection with the receiver collections elements"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   486
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   487
    ^ self addAllTo:(OrderedCollection new:self size)
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   488
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   489
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   490
asSet
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   491
    "return a new Set with the receiver collections elements"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   492
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   493
    ^ self addAllTo:(Set new:self size)
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   494
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   495
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   496
asSortedCollection
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   497
    "return a new SortedCollection with the receiver collections elements"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   498
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   499
    |aSortedCollection|
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   500
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   501
    aSortedCollection := SortedCollection new:self size.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   502
    aSortedCollection addAll:self.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   503
    ^ aSortedCollection
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   504
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   505
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   506
asSortedCollection:sortBlock
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   507
    "return a new SortedCollection with the receiver collections elements,
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   508
     using sortBlock for comparing"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   509
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   510
    |aSortedCollection|
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   511
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   512
    aSortedCollection := SortedCollection sortBlock:sortBlock.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   513
    aSortedCollection addAll:self.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   514
    ^ aSortedCollection
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   515
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   516
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   517
asString
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   518
    "return a String with the collections elements 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   519
     (which must convert to characters)"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   520
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   521
    |aString 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   522
     index "{ Class: SmallInteger }" |
360
claus
parents: 348
diff changeset
   523
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   524
    aString := String new:(self size).
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   525
    index := 1.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   526
    self do:[:each |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   527
	aString at:index put:each asCharacter.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   528
	index := index + 1
360
claus
parents: 348
diff changeset
   529
    ].
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   530
    ^ aString
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   531
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   532
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   533
asStringCollection
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   534
    "return a new Text-object (collection of lines) with the elements printstings"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   535
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   536
    ^ self addAllTo:(StringCollection new)
360
claus
parents: 348
diff changeset
   537
!
claus
parents: 348
diff changeset
   538
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   539
asText
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   540
    "return a Text-object (collection of lines) from myself.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   541
     BIG warning: asText is totally misnamed here 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   542
     - ST/X's asText has nothing to do with PP's asText.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   543
     Therefore it will be removed/renamed soon. 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   544
     Please use #asStringCollection."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   545
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   546
    self obsoleteMethodWarning:'use #asStringCollection; #asText will change its meaning'.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   547
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   548
    ^ self asStringCollection
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   549
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   550
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   551
readStream
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   552
    "return a stream for reading from the receiver"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   553
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   554
    ^ ReadStream on:self
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   555
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   556
    "
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   557
     |s|
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   558
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   559
     s := 'hello world' readStream.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   560
     s next:5.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   561
     s next.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   562
     (s next:5) inspect
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   563
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   564
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   565
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   566
writeStream
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   567
    "return a stream for writing onto the receiver"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   568
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   569
    ^ WriteStream on:self
2
claus
parents: 1
diff changeset
   570
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   571
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   572
     |s|
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   573
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   574
     s := #() copy writeStream.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   575
     s nextPut:1.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   576
     s nextPut:2.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   577
     s nextPut:3.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   578
     s contents inspect
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   579
    "
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   580
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   581
    "
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   582
     |s|
2
claus
parents: 1
diff changeset
   583
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   584
     s := OrderedCollection new writeStream.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   585
     s nextPut:1.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   586
     s nextPut:2.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   587
     s nextPut:3.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   588
     s contents inspect
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   589
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   590
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   591
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   592
!Collection methodsFor:'copying'!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   593
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   594
copyEmpty
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   595
    "return a copy of the receiver with no elements.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   596
     This is used by copying and enumeration methods
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   597
     to get a new instance which is similar to the receiver."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   598
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   599
    ^ self species new
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   600
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   601
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   602
copyEmpty:size
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   603
    "return a copy of the receiver with no elements, but space for
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   604
     size elements. This is used by copying and enumeration methods
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   605
     to get a new instance which is similar to the receiver.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   606
     This method should be redefined in subclasses with instance
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   607
     variables, which should be put into the copy too.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   608
     For example, SortedCollection has to copy its sortBlock into the
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   609
     new collection."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   610
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   611
    ^ self species new:size
360
claus
parents: 348
diff changeset
   612
!
claus
parents: 348
diff changeset
   613
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   614
copyEmptyAndGrow:size
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   615
    "return a copy of the receiver with size nil elements.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   616
     This is used by copying and enumeration methods
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   617
     to get a new instance which is similar to the receiver."
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
   618
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   619
    ^ (self copyEmpty:size) grow:size
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   620
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   621
a27a279701f8 Initial revision
claus
parents:
diff changeset
   622
!Collection methodsFor:'enumerating'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   623
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   624
addAllTo:aCollection
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   625
    "add all elements of the receiver, to aCollection.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   626
     Return aCollection."
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   627
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   628
    self do:[:each | aCollection add:each].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   629
    ^ aCollection
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   630
!
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   631
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   632
collect:aBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
   633
    "for each element in the receiver, evaluate the argument, aBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
   634
     and return a new collection with the results"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   635
a27a279701f8 Initial revision
claus
parents:
diff changeset
   636
    |newCollection|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   637
201
1deff0d47f37 changed copyEmpty scheme
claus
parents: 169
diff changeset
   638
    newCollection := self species new.
1deff0d47f37 changed copyEmpty scheme
claus
parents: 169
diff changeset
   639
    self do:[:element | newCollection add:(aBlock value:element)].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   640
    ^ newCollection
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   641
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   642
    "
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   643
     #(1 2 3 4) collect:[:n | n * 2]  
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   644
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   645
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   646
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   647
conform:aBlock
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   648
    "return true, if every element conforms to some condition.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   649
     I.e. return false, if aBlock returns false for any element;
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   650
     true otherwise."
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   651
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   652
    self do:[:element | (aBlock value:element) ifFalse:[^ false]].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   653
    ^ true
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   654
!
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   655
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   656
count:aBlock
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   657
    "count elements, for which aBlock returns true.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   658
     Return the sum."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   659
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   660
    ^ self inject:0 into:[:sumSoFar :element |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   661
	    (aBlock value:element) ifTrue:[sumSoFar + 1]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   662
				   ifFalse:[sumSoFar]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   663
      ]
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   664
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   665
    "
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   666
     #(1 2 3 4 6 8 10) count:[:a | a even]     
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   667
     #(1 nil nil nil 2 3 nil 4 5) count:[:a | a isNil] 
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   668
    "
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   669
!
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   670
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   671
detect:aBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
   672
    "evaluate the argument, aBlock for each element in the receiver until
a27a279701f8 Initial revision
claus
parents:
diff changeset
   673
     the block returns true; in this case return the element which caused
a27a279701f8 Initial revision
claus
parents:
diff changeset
   674
     the true evaluation.
345
claus
parents: 342
diff changeset
   675
     If none of the evaluations returns true, report an error"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   676
a27a279701f8 Initial revision
claus
parents:
diff changeset
   677
    ^ self detect:aBlock ifNone:[self errorNotFound]
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   678
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   679
    "
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   680
     #(1 2 3 4) detect:[:n | n odd]   
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   681
     #(2 4 6 8) detect:[:n | n odd]  
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   682
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   683
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   684
a27a279701f8 Initial revision
claus
parents:
diff changeset
   685
detect:aBlock ifNone:exceptionBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
   686
    "evaluate the argument, aBlock for each element in the receiver until
a27a279701f8 Initial revision
claus
parents:
diff changeset
   687
     the block returns true; in this case return the element which caused
a27a279701f8 Initial revision
claus
parents:
diff changeset
   688
     the true evaluation.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   689
     If none of the evaluations returns true, return the result of the
a27a279701f8 Initial revision
claus
parents:
diff changeset
   690
     evaluation of the exceptionBlock"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   691
a27a279701f8 Initial revision
claus
parents:
diff changeset
   692
    self do:[:each | 
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
   693
	(aBlock value:each) ifTrue:[^ each].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   694
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   695
    ^ exceptionBlock value
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   696
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   697
    "
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   698
     #(1 2 3 4) detect:[:n | n odd] ifNone:['sorry']    
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   699
     #(2 4 6 8) detect:[:n | n odd] ifNone:['sorry']     
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   700
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   701
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   702
421
claus
parents: 404
diff changeset
   703
detectLast:aBlock
claus
parents: 404
diff changeset
   704
    "evaluate the argument, aBlock for each element in the receiver until
claus
parents: 404
diff changeset
   705
     the block returns true; in this case return the element which caused
claus
parents: 404
diff changeset
   706
     the true evaluation. The elements are processed in reverse order.
claus
parents: 404
diff changeset
   707
     If none of the evaluations returns true, report an error"
claus
parents: 404
diff changeset
   708
claus
parents: 404
diff changeset
   709
    ^ self detectLast:aBlock ifNone:[self errorNotFound]
claus
parents: 404
diff changeset
   710
claus
parents: 404
diff changeset
   711
    "
claus
parents: 404
diff changeset
   712
     #(1 2 3 4) detectLast:[:n | n odd]   
claus
parents: 404
diff changeset
   713
     #(2 4 6 8) detectLast:[:n | n odd]  
claus
parents: 404
diff changeset
   714
    "
claus
parents: 404
diff changeset
   715
!
claus
parents: 404
diff changeset
   716
claus
parents: 404
diff changeset
   717
detectLast:aBlock ifNone:exceptionBlock
claus
parents: 404
diff changeset
   718
    "evaluate the argument, aBlock for each element in the receiver until
claus
parents: 404
diff changeset
   719
     the block returns true; in this case return the element which caused
claus
parents: 404
diff changeset
   720
     the true evaluation. The elements are processed in reverse order.
claus
parents: 404
diff changeset
   721
     If none of the evaluations returns true, return the result of the
claus
parents: 404
diff changeset
   722
     evaluation of the exceptionBlock"
claus
parents: 404
diff changeset
   723
claus
parents: 404
diff changeset
   724
    self reverseDo:[:each | 
claus
parents: 404
diff changeset
   725
	(aBlock value:each) ifTrue:[^ each].
claus
parents: 404
diff changeset
   726
    ].
claus
parents: 404
diff changeset
   727
    ^ exceptionBlock value
claus
parents: 404
diff changeset
   728
claus
parents: 404
diff changeset
   729
    "
claus
parents: 404
diff changeset
   730
     #(1 2 3 4) detectLast:[:n | n odd] ifNone:['sorry']    
claus
parents: 404
diff changeset
   731
     #(2 4 6 8) detectLast:[:n | n odd] ifNone:['sorry']     
claus
parents: 404
diff changeset
   732
    "
claus
parents: 404
diff changeset
   733
!
claus
parents: 404
diff changeset
   734
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   735
do:aBlock
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   736
    "evaluate the argument, aBlock for each element"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   737
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   738
    ^ self subclassResponsibility
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   739
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   740
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   741
inject:thisValue into:binaryBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
   742
    |nextValue|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   743
a27a279701f8 Initial revision
claus
parents:
diff changeset
   744
    nextValue := thisValue.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   745
    self do: [:each | nextValue := binaryBlock value:nextValue value:each].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   746
    ^ nextValue
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   747
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   748
    "
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   749
     #(1 2 3 4) inject:0 into:[:accu :element | accu + element]   
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   750
     (1 to:10) inject:0 into:[:accu :element | accu + element]     
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   751
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   752
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   753
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   754
keysAndValuesDo:aTwoArgBlock
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   755
    "evaluate the argument, aBlock for every element in the collection,
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   756
     passing both index and element as arguments."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   757
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   758
    ^ self errorNotKeyed
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   759
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   760
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   761
pairsDo:aBlock
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   762
    "evaluate the argument, aBlock for every element in the collection,
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   763
     which is supposed to consist of 2-element collections.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   764
     The block is called with 2 arguments for each collection in the receiver."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   765
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   766
    self do:[:aPair |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   767
	aBlock value:(aPair at:1) value:(aPair at:2).
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   768
    ]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   769
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   770
     #( (1 one) (2 two) (3 three) (4 four) (5 five) (6 six)) 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   771
     pairsDo:[:num :sym | Transcript show:num; show:' is: '; showCr:sym]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   772
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   773
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   774
     #( (1 1)  (1 2)  (1 3)  (1 4)  (1 5)) 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   775
     pairsDo:[:x :y | Transcript showCr:x@y]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   776
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   777
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   778
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   779
reject:aBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
   780
    "return a new collection with all elements from the receiver, for which
a27a279701f8 Initial revision
claus
parents:
diff changeset
   781
     the argument aBlock evaluates to false"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   782
a27a279701f8 Initial revision
claus
parents:
diff changeset
   783
    ^ self select:[:element | (aBlock value:element) == false]
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   784
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   785
    "
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   786
     #(1 2 3 4) reject:[:e | e odd]   
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   787
     (1 to:10) reject:[:e | e even]     
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   788
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   789
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   790
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   791
reverseDo:aBlock
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   792
    "evaluate the argument, aBlock for each element in reverse order."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   793
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   794
    "it could be defined in terms of do: - but very inefficient.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   795
     Better force programmer to define a better version ..."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   796
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   797
    ^ self subclassResponsibility
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   798
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   799
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   800
select:aBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
   801
    "return a new collection with all elements from the receiver, for which
a27a279701f8 Initial revision
claus
parents:
diff changeset
   802
     the argument aBlock evaluates to true"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   803
a27a279701f8 Initial revision
claus
parents:
diff changeset
   804
    |newCollection|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   805
201
1deff0d47f37 changed copyEmpty scheme
claus
parents: 169
diff changeset
   806
    newCollection := self species new.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   807
    self do:[:each |
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
   808
	(aBlock value:each) ifTrue:[newCollection add:each].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   809
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   810
    ^ newCollection
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   811
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   812
    "
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   813
     #(1 2 3 4) select:[:e | e odd]   
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   814
     (1 to:10) select:[:e | e even]     
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   815
    "
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   816
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   817
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   818
select:aBlock ifNone:exceptionBlock
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   819
    "try a new collection with all elements from the receiver, for which
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   820
     the argument aBlock evaluates to true. If none of the elements passes
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   821
     the check of aBlock, return the result of evaluating exceptionBlock."
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   822
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   823
    |newCollection|
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   824
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   825
    newCollection := self select:aBlock.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   826
    newCollection isEmpty ifTrue:[^ exceptionBlock value].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   827
    ^ newCollection
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   828
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   829
    "
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   830
     #(1 2 3 4) select:[:e | e > 10] ifNone:['sorry']  
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   831
    "
12
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   832
!
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
   833
362
claus
parents: 360
diff changeset
   834
triplesDo:aBlock
claus
parents: 360
diff changeset
   835
    "evaluate the argument, aBlock for every element in the collection,
claus
parents: 360
diff changeset
   836
     which is supposed to consist of 3-element collections.
claus
parents: 360
diff changeset
   837
     The block is called with 3 arguments for each collection in the receiver."
claus
parents: 360
diff changeset
   838
claus
parents: 360
diff changeset
   839
    self do:[:aTriple |
claus
parents: 360
diff changeset
   840
	aBlock value:(aTriple at:1) value:(aTriple at:2) value:(aTriple at:3).
claus
parents: 360
diff changeset
   841
    ]
claus
parents: 360
diff changeset
   842
    "
claus
parents: 360
diff changeset
   843
     #( (1 one eins) (2 two zwei) (3 three drei) (4 four vier) (5 five #'fünf') (6 six sechs)) 
claus
parents: 360
diff changeset
   844
     triplesDo:[:num :sym1 :sym2 | Transcript show:num; space; show:sym1; space; showCr:sym2]
claus
parents: 360
diff changeset
   845
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   846
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   847
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   848
with:aCollection collect:aTwoArgBlock
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   849
    "evaluate the argument, aBlock for successive elements from
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   850
     each the receiver and the argument, aSequenceableCollection;
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   851
     The second argument, aBlock must be a two-argument block, which is
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   852
     evaluated for each element-pair.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   853
     Collect the results and return a collection containing them.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   854
     This method fails if neither the receiver nor aCollection is
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   855
     a sequenceable collection (i.e. implements numeric key access)."
260
cefb485445a7 *** empty log message ***
claus
parents: 257
diff changeset
   856
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   857
    |index  "{ Class: SmallInteger }" 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   858
     newCollection|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   859
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   860
    newCollection := self species new.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   861
    index := 1.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   862
    aCollection isSequenceable ifFalse:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   863
	self isSequenceable ifFalse:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   864
	    ^ self error:'neither collection is sequenceable'.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   865
	].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   866
	aCollection do:[:element |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   867
	    newCollection add:(aTwoArgBlock value:(self at:index) 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   868
					    value:element).
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   869
	    index := index + 1
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   870
	]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   871
    ] ifTrue:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   872
	self do:[:element |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   873
	    newCollection add:(aTwoArgBlock value:element 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   874
					    value:(aCollection at:index)).
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   875
	    index := index + 1
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   876
	]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   877
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   878
    ^ newCollection
201
1deff0d47f37 changed copyEmpty scheme
claus
parents: 169
diff changeset
   879
1deff0d47f37 changed copyEmpty scheme
claus
parents: 169
diff changeset
   880
    "
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   881
     (1 to:3) with:#(one two three) collect:[:num :sym | (num->sym)]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   882
     #(1 2 3) with:#(10 20 30) collect:[:x :y | (x@y)]
201
1deff0d47f37 changed copyEmpty scheme
claus
parents: 169
diff changeset
   883
    "
2
claus
parents: 1
diff changeset
   884
!
claus
parents: 1
diff changeset
   885
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   886
with:aCollection do:aTwoArgBlock
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   887
    "evaluate the argument, aBlock for successive elements from
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   888
     each the receiver and the argument, aSequenceableCollection.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   889
     The second argument, aBlock must be a two-argument block.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   890
     This method fails if neither the receiver nor aCollection is
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   891
     a sequenceable collection (i.e. implements numeric key access)."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   892
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   893
    |index  "{ Class: SmallInteger }"|
2
claus
parents: 1
diff changeset
   894
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   895
    index := 1.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   896
    aCollection isSequenceable ifFalse:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   897
	self isSequenceable ifFalse:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   898
	    ^ self error:'neither collection is sequenceable'.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   899
	].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   900
	aCollection do:[:element |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   901
	    aTwoArgBlock value:(self at:index) value:element.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   902
	    index := index + 1
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   903
	]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   904
    ] ifTrue:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   905
	self do:[:element |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   906
	    aTwoArgBlock value:element value:(aCollection at:index).
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   907
	    index := index + 1
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   908
	]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   909
    ]
201
1deff0d47f37 changed copyEmpty scheme
claus
parents: 169
diff changeset
   910
1deff0d47f37 changed copyEmpty scheme
claus
parents: 169
diff changeset
   911
    "
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   912
     (1 to:3) with:#(one two three) do:[:num :sym |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   913
	Transcript showCr:(num->sym)
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   914
     ]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   915
     (1 to:3) with:#(one two three) asSet do:[:num :sym |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   916
	Transcript showCr:(num->sym)
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   917
     ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   918
     (1 to:3) asSet with:#(one two three) do:[:num :sym |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   919
	Transcript showCr:(num->sym)
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   920
     ]
201
1deff0d47f37 changed copyEmpty scheme
claus
parents: 169
diff changeset
   921
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   922
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   923
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   924
!Collection methodsFor:'error handling'!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   925
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   926
emptyCheck 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   927
    "check if the receiver is empty; report an error if so"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   928
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   929
    self size == 0 ifTrue:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   930
	^ self emptyCollectionError.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   931
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   932
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   933
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   934
emptyCollectionError
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   935
    "report an error that the operation is not allowed for
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   936
     empty collections"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   937
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   938
    ^ EmptyCollectionSignal raise
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   939
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   940
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   941
errorInvalidKey:aKey
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   942
    "report an error that the given key was invalid"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   943
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   944
    ^ InvalidKeySignal raiseRequestWith:aKey
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   945
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   946
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   947
errorNotKeyed
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   948
    "report an error that keyed access methods are not allowed"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   949
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   950
    ^ self error:(self class name, 's do not respond to keyed accessing messages')
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   951
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   952
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   953
errorValueNotFound:anObject
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   954
    "report an error that an object was not found in the collection"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   955
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   956
    ^ ValueNotFoundSignal raiseRequestWith:anObject
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   957
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   958
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   959
notEnoughElementsError
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   960
    "report an error that the operation is not allowed,  
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   961
     since not enough elements are in the collection"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   962
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   963
    ^ NotEnoughElementsSignal raise
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   964
! !
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   965
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   966
!Collection methodsFor:'growing'!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   967
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   968
grow
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   969
    "make the receiver larger"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   970
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   971
    self grow:(self size + self growSize)
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   972
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   973
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   974
grow:howBig
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   975
    "change the receivers size"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   976
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   977
    ^ self subclassResponsibility
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   978
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   979
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   980
growSize
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   981
    "return a suitable size increment for growing.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   982
     The default returned here may be (and is) redefined in subclasses."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   983
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   984
    ^ self size max:2
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   985
! !
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   986
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   987
!Collection methodsFor:'printing & storing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   988
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   989
displayString
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   990
    "return a printed representation of the receiver for display in inspectors etc."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   991
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   992
    ^ self printOrDisplayStringUsing:#displayString 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   993
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   994
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   995
maxPrint
169
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
   996
    "the print-limit; printOn: will try to not produce more output
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   997
     than the limit defined here."
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   998
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   999
    ^ 5000
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1000
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1001
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1002
printOn:aStream
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1003
    "append a user readable representation of the receiver to aStream.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1004
     The text appended is not meant to be read back for reconstruction of
169
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1005
     the receiver. Also, this method limits the size of generated string.
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1006
    "
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1007
169
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1008
    |limit firstOne string|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1009
44
b262907c93ea *** empty log message ***
claus
parents: 34
diff changeset
  1010
    thisContext isRecursive ifTrue:[
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
  1011
	Transcript showCr:'Error: printOn: of self referencing collection.'.
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
  1012
	aStream nextPutAll:'#("recursive")'.
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
  1013
	^ self
44
b262907c93ea *** empty log message ***
claus
parents: 34
diff changeset
  1014
    ].
b262907c93ea *** empty log message ***
claus
parents: 34
diff changeset
  1015
b262907c93ea *** empty log message ***
claus
parents: 34
diff changeset
  1016
    limit := self maxPrint.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1017
    aStream nextPutAll:self class name.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1018
    aStream nextPut:$(.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1019
    firstOne := true.
169
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1020
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1021
    self do:[:element |
169
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1022
	firstOne ifFalse:[
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1023
	    aStream space
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1024
	] ifTrue:[
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1025
	    firstOne := false
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1026
	].
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1027
	(limit <= 0) ifTrue:[
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1028
	    aStream nextPutAll:'...etc...)'.
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1029
	    ^ self
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1030
	] ifFalse:[
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1031
"/ old code, worked only on positionable streams
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1032
"/
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1033
"/              pos1 := aStream position.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1034
"/              element printOn:aStream.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1035
"/              limit := limit - (aStream position - pos1)
169
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1036
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1037
	    string := element printString.
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1038
	    aStream nextPutAll:string.
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1039
	    limit := limit - string size.
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
  1040
	].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1041
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1042
    aStream nextPut:$)
44
b262907c93ea *** empty log message ***
claus
parents: 34
diff changeset
  1043
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1044
    "
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1045
     #(1 2 3 'hello' $a) printOn:Transcript
169
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1046
     (Array new:100000) printOn:Transcript
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1047
     (Array new:100000) printString size 
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1048
     (Dictionary new at:#hello put:'world'; yourself) printOn:Transcript
169
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1049
    "
1a3042b58fb9 printLimit
claus
parents: 155
diff changeset
  1050
    "
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1051
     |a| 
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1052
     a := Array new:3. 
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1053
     a at:2 put:a.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1054
     a printOn:Transcript
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1055
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1056
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1057
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1058
printOrDisplayStringUsing:aSelector
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1059
    "common code for printString and displayString; they only differ in
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1060
     the print-message sent to the elements"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1061
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1062
    |thisString buffer count string noneYet total limit|
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1063
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1064
    thisContext isRecursive ifTrue:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1065
	Transcript showCr:'Error: print/storeString of self referencing collection.'.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1066
	^ '#("recursive")'
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1067
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1068
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1069
    string := (self class name) , '('.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1070
    noneYet := true.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1071
    buffer := ''.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1072
    count := 0.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1073
    total := 0.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1074
    limit := self maxPrint.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1075
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1076
    self do: [:element |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1077
	thisString := element perform:aSelector.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1078
	noneYet ifTrue:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1079
	    noneYet := false.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1080
	    buffer := buffer , thisString
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1081
	] ifFalse:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1082
	    buffer := buffer , (' ' , thisString)
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1083
	].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1084
	count := count + 1.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1085
	(count == 20) ifTrue:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1086
	    string := string , buffer.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1087
	    buffer := ''.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1088
	    count := 0
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1089
	].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1090
	total := total + 1.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1091
	(total > limit) ifTrue:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1092
	    string := string , buffer , '... )'.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1093
	    ^ string
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1094
	]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1095
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1096
    string := string , buffer , ')'.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1097
    ^ string
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1098
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1099
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1100
storeOn:aStream
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1101
    "output a printed representation onto the argument, aStream.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1102
     The text can be re-read to reconstruct (a copy of) the receiver.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1103
     Recursive (i.e. cyclic) collections cannot be stored correctly
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1104
     (use storeBinaryOn: to handle those)."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1105
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1106
    |isEmpty|
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1107
44
b262907c93ea *** empty log message ***
claus
parents: 34
diff changeset
  1108
    thisContext isRecursive ifTrue:[
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
  1109
	Transcript showCr:'Error: storeOn: of self referencing collection.'.
293
31df3850e98c *** empty log message ***
claus
parents: 260
diff changeset
  1110
	aStream nextPutAll:'#recursive'.
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
  1111
	^ self
44
b262907c93ea *** empty log message ***
claus
parents: 34
diff changeset
  1112
    ].
b262907c93ea *** empty log message ***
claus
parents: 34
diff changeset
  1113
b262907c93ea *** empty log message ***
claus
parents: 34
diff changeset
  1114
    aStream nextPutAll:'('.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1115
    aStream nextPutAll:(self class name).
44
b262907c93ea *** empty log message ***
claus
parents: 34
diff changeset
  1116
    aStream nextPutAll:' new'.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1117
    isEmpty := true.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1118
    self do:[:element |
155
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
  1119
	aStream nextPutAll:' add:'.
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
  1120
	element storeOn:aStream.
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
  1121
	aStream nextPutAll:';'.
edd7fc34e104 *** empty log message ***
claus
parents: 140
diff changeset
  1122
	isEmpty := false
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1123
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1124
    isEmpty ifFalse:[aStream nextPutAll:' yourself'].
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1125
    aStream nextPut:$)
44
b262907c93ea *** empty log message ***
claus
parents: 34
diff changeset
  1126
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1127
    "
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1128
     OrderedCollection new storeOn:Transcript
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1129
     (1 to:10) storeOn:Transcript
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1130
     (Set new add:1; add:'hello'; yourself) storeOn:Transcript
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
  1131
    "
293
31df3850e98c *** empty log message ***
claus
parents: 260
diff changeset
  1132
    "
31df3850e98c *** empty log message ***
claus
parents: 260
diff changeset
  1133
     |s|
31df3850e98c *** empty log message ***
claus
parents: 260
diff changeset
  1134
31df3850e98c *** empty log message ***
claus
parents: 260
diff changeset
  1135
     s := Set new.
31df3850e98c *** empty log message ***
claus
parents: 260
diff changeset
  1136
     s add:1; add:'hello'; add:s.
31df3850e98c *** empty log message ***
claus
parents: 260
diff changeset
  1137
     s storeOn:Transcript
31df3850e98c *** empty log message ***
claus
parents: 260
diff changeset
  1138
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1139
! !
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1140
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1141
!Collection methodsFor:'queries'!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1142
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1143
isCollection
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1144
    "return true, if the receiver is some kind of collection;
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1145
     true is returned here - the method is redefined from Object."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1146
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1147
    ^ true
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1148
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1149
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1150
longestCommonPrefix
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1151
    |try allMatching matchLen|
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1152
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1153
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1154
     find the longest common prefix
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1155
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1156
    matchLen := 0.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1157
    self do:[:aName |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1158
	aName size > matchLen ifTrue:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1159
	    matchLen := aName size.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1160
	    try := aName
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1161
	]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1162
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1163
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1164
    allMatching := true.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1165
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1166
    [true] whileTrue:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1167
	allMatching := true.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1168
	self do:[:aName |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1169
	    (aName startsWith:try) ifFalse:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1170
		allMatching := false
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1171
	    ]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1172
	].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1173
	allMatching ifTrue:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1174
	    ^ try
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1175
	].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1176
	matchLen := matchLen - 1.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1177
	try := try copyTo:matchLen.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1178
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1179
    ^ try
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1180
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1181
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1182
     #('Array' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1183
     #('Arra' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1184
     #('Arra' 'b' 'c') longestCommonPrefix 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1185
     #( (1 2 3 4) (1 2 3) (1 2 3 7) (1 2 3 9 10 11)) longestCommonPrefix
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1186
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1187
! !
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1188
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1189
!Collection methodsFor:'testing'!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1190
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1191
capacity
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1192
    "return the number of elements, that the receiver is
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1193
     prepared to take. For most collections, this is the actual
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1194
     size. However, some have more space preallocated to allow
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1195
     for faster adding of elements. 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1196
     Not used by the system; added for ST-80 compatibility."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1197
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1198
    ^ self size
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1199
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1200
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1201
includes:anElement
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1202
    "return true, if an object equal to the argument, anObject is in the list.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1203
     This compares using #= (i.e. it does not look for the object itself,
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1204
     instead, one that compares equal).
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1205
     See #includesIdentical: when identity is asked for."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1206
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1207
    self do:[:element |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1208
	(anElement = element) ifTrue:[^ true].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1209
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1210
    ^ false
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1211
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1212
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1213
includesAll:aCollection
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1214
    "return true, if the the receiver includes all elements of
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1215
     the argument, aCollection; false if any is missing.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1216
     Notice: this method has O-square runtime behavior and may be
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1217
	     slow for big receivers/args. Think about using a Set,
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1218
	     or Dictionary."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1219
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1220
    aCollection do:[:element |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1221
	(self includes:element) ifFalse:[^ false].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1222
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1223
    ^ true
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1224
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1225
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1226
     #(1 2 3 4 5 6 7) includesAll:#(1 2 3)
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1227
     #('hello' 'there' 'world') includesAll:#('hello' 'world')
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1228
     #(1 2 3 4 5 6 7) includesAll:#(7 8 9)
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1229
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1230
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1231
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1232
includesAny:aCollection
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1233
    "return true, if the the receiver includes any elements of
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1234
     the argument, aCollection; false if it includes none.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1235
     Notice: this method has O-square runtime behavior and may be
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1236
	     slow for big receivers/args. Think about using a Set,
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1237
	     or Dictionary. Speedup is possible, by arrangy highly
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1238
	     probable elements towards the beginning of aCollection,
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1239
	     to avoid useless searches."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1240
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1241
    aCollection do:[:element |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1242
	(self includes:element) ifTrue:[^ true].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1243
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1244
    ^ false
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1245
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1246
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1247
     #(1 2 3 4 5 6 7) includesAny:#(1 2 3)
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1248
     #('hello' 'there' 'world') includesAny:#('hello' 'world')
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1249
     #(1 2 3 4 5 6 7) includesAny:#(7 8 9)
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1250
     #(1 2 3 4 5 6 7) includesAny:#(8 9 10)
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1251
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1252
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1253
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1254
includesIdentical:anElement
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1255
    "return true, if the argument, anObject is in the list.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1256
     This compares using #== (i.e. object identity).
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1257
     See #includes: when equality is asked for."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1258
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1259
    self do:[:element |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1260
	(anElement == element) ifTrue:[^ true].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1261
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1262
    ^ false
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1263
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1264
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1265
isEmpty
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1266
    "return true, if the receiver is empty"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1267
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1268
    ^ self size == 0
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1269
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1270
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1271
max
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1272
    "return the maximum value in the receiver collection"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1273
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1274
    ^ self inject:nil
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1275
	    into:[:maxSoFar :this | 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1276
			(maxSoFar isNil 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1277
			or:[this > maxSoFar]) ifTrue:[this]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1278
			ifFalse:[maxSoFar]]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1279
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1280
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1281
     #(15 1 -9 10 5) max  
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1282
     (1 to:15) max  
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1283
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1284
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1285
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1286
min
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1287
    "return the minimum value in the receiver collection"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1288
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1289
    ^ self inject:nil
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1290
	    into:[:minSoFar :this | 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1291
			(minSoFar isNil 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1292
			or:[this < minSoFar]) ifTrue:[this]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1293
			ifFalse:[minSoFar]]
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1294
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1295
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1296
     #(15 1 -9 10 5) min 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1297
     (1 to:15) min        
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1298
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1299
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1300
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1301
notEmpty
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1302
    "return true, if the receiver is not empty"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1303
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1304
    ^ self isEmpty not
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1305
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1306
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1307
occurrencesOf:anElement
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1308
    "return the number of occurrences of the argument, anElement in
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1309
     the receiver"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1310
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1311
    |count "{ Class: SmallInteger }" |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1312
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1313
    count := 0.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1314
    self do:[:element |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1315
	(anElement = element) ifTrue:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1316
	    count := count + 1
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1317
	].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1318
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1319
    ^ count
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1320
!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1321
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1322
size
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1323
    "return the number of elements in the receiver.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1324
     This is usually redefined in subclasses for more performance."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1325
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1326
    |count "{ Class: SmallInteger }" |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1327
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1328
    count := 0.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1329
    self do:[:element |
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1330
	count := count + 1
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1331
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1332
    ^ count
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1333
! !
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1334
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1335
Collection initialize!