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