This file is indexed.

/usr/share/gnu-smalltalk/kernel/Collection.st is in gnu-smalltalk-common 3.2.4-2.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
"======================================================================
|
|   Collection Method Definitions
|
|
 ======================================================================"

"======================================================================
|
| Copyright 1988,92,94,95,99,2000,2001,2002,2003,2006,2007,2008,2009
| Free Software Foundation, Inc.
| Written by Steve Byrne.
|
| This file is part of the GNU Smalltalk class library.
|
| The GNU Smalltalk class library is free software; you can redistribute it
| and/or modify it under the terms of the GNU Lesser General Public License
| as published by the Free Software Foundation; either version 2.1, or (at
| your option) any later version.
| 
| The GNU Smalltalk class library is distributed in the hope that it will be
| useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
| General Public License for more details.
| 
| You should have received a copy of the GNU Lesser General Public License
| along with the GNU Smalltalk class library; see the file COPYING.LIB.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02110-1301, USA.  
|
 ======================================================================"



Iterable subclass: Collection [
    
    <category: 'Collections'>
    <comment: 'I am an abstract class.  My instances are collections of objects.  My
subclasses may place some restrictions or add some definitions to how
the objects are stored or organized; I say nothing about this.  I merely
provide some object creation and access routines for general collections
of objects.'>

    Collection class >> from: anArray [
	"Convert anArray to an instance of the receiver.  anArray is
	 structured such that the instance can be conveniently and fully
	 specified using brace-syntax, possibly by imposing some
	 additional structure on anArray."

	<category: 'instance creation'>
	^self withAll: anArray
    ]

    Collection class >> withAll: aCollection [
	"Answer a collection whose elements are all those in aCollection"

	<category: 'instance creation'>
	^(self new)
	    addAll: aCollection;
	    yourself
    ]

    Collection class >> with: anObject [
	"Answer a collection whose only element is anObject"

	<category: 'instance creation'>
	^(self new)
	    add: anObject;
	    yourself
    ]

    Collection class >> with: firstObject with: secondObject [
	"Answer a collection whose only elements are the parameters in the order
	 they were passed"

	<category: 'instance creation'>
	^(self new)
	    add: firstObject;
	    add: secondObject;
	    yourself
    ]

    Collection class >> with: firstObject with: secondObject with: thirdObject [
	"Answer a collection whose only elements are the parameters in the order
	 they were passed"

	<category: 'instance creation'>
	^(self new)
	    add: firstObject;
	    add: secondObject;
	    add: thirdObject;
	    yourself
    ]

    Collection class >> with: firstObject with: secondObject with: thirdObject with: fourthObject [
	"Answer a collection whose only elements are the parameters in the order
	 they were passed"

	<category: 'instance creation'>
	^(self new)
	    add: firstObject;
	    add: secondObject;
	    add: thirdObject;
	    add: fourthObject;
	    yourself
    ]

    Collection class >> with: firstObject with: secondObject with: thirdObject with: fourthObject with: fifthObject [
	"Answer a collection whose only elements are the parameters in the order
	 they were passed"

	<category: 'instance creation'>
	^(self new)
	    add: firstObject;
	    add: secondObject;
	    add: thirdObject;
	    add: fourthObject;
	    add: fifthObject;
	    yourself
    ]

    Collection class >> join: aCollection [
	"Answer a collection formed by treating each element in
	 aCollection as a `withAll:' argument collection to be added to a
	 new instance."

	<category: 'instance creation'>
	| newInst |
	newInst := self 
		    new: (aCollection inject: 0 into: [:size :each | size + each size]).
	aCollection do: [:each | newInst addAll: each].
	^newInst
    ]

    Collection class >> isUnicode [
	"Answer true; the receiver is able to store arbitrary
	 Unicode characters."

	<category: 'multibyte encodings'>
	^true
    ]

    , anIterable [
	"Append anIterable at the end of a copy of the receiver (using #add:),
	 and answer a new collection"

	<category: 'copying SequenceableCollections'>
	^(self copyEmpty: self size + anIterable size)
	    addAll: self;
	    addAll: anIterable;
	    yourself
    ]

    add: newObject [
	"Add newObject to the receiver, answer it"

	<category: 'adding'>
	self subclassResponsibility
    ]

    addAll: aCollection [
	"Adds all the elements of 'aCollection' to the receiver, answer
	 aCollection"

	<category: 'adding'>
	aCollection do: [:element | self add: element].
	^aCollection
    ]

    empty [
	"Remove everything from the receiver."

	<category: 'removing'>
	^self become: self copyEmpty
    ]

    removeAllSuchThat: aBlock [
	"Remove from the receiver all objects for which aBlock returns true."

	<category: 'removing'>
	self removeAll: (self select: aBlock) ifAbsent: []
    ]

    remove: oldObject ifAbsent: anExceptionBlock [
	"Remove oldObject from the receiver. If absent, evaluate anExceptionBlock
	 and answer the result, else answer oldObject."

	<category: 'removing'>
	self subclassResponsibility
    ]

    remove: oldObject [
	"Remove oldObject from the receiver. If absent, fail, else
	 answer oldObject."

	<category: 'removing'>
	^self remove: oldObject
	    ifAbsent: [SystemExceptions.NotFound signalOn: oldObject what: 'object']
    ]

    removeAll: aCollection [
	"Remove each object in aCollection, answer aCollection, fail if some
	 of them is absent. Warning: this could leave the collection in a
	 semi-updated state."

	<category: 'removing'>
	aCollection do: [:element | self remove: element].
	^aCollection
    ]

    removeAll: aCollection ifAbsent: aBlock [
	"Remove each object in aCollection, answer aCollection; if some
	 element is absent, pass it to aBlock."

	<category: 'removing'>
	aCollection 
	    do: [:element | self remove: element ifAbsent: [aBlock cull: element]].
	^aCollection
    ]

    isSequenceable [
	"Answer whether the receiver can be accessed by a numeric index with
	 #at:/#at:put:."

	<category: 'testing collections'>
	^false
    ]

    capacity [
	"Answer how many elements the receiver can hold before having to grow."

	<category: 'testing collections'>
	^self basicSize
    ]

    includesAnyOf: aCollection [
	"Answer whether we include any of the objects in aCollection"

	<category: 'testing collections'>
	self do: [:element | (aCollection includes: element) ifTrue: [^true]].
	^false
    ]

    includes: anObject [
	"Answer whether we include anObject"

	<category: 'testing collections'>
	self do: [:element | anObject = element ifTrue: [^true]].
	^false
    ]

    identityIncludes: anObject [
	"Answer whether we include the anObject object"

	<category: 'testing collections'>
	self do: [:element | anObject == element ifTrue: [^true]].
	^false
    ]

    isEmpty [
	"Answer whether we are (still) empty"

	<category: 'testing collections'>
	^self size == 0
    ]

    notEmpty [
	"Answer whether we include at least one object"

	<category: 'testing collections'>
	^self size > 0
    ]

    occurrencesOf: anObject [
	"Answer how many occurrences of anObject we include"

	<category: 'testing collections'>
	| count |
	count := 0.
	self do: [:element | anObject == element ifTrue: [count := count + 1]].
	^count
    ]

    size [
	"Answer how many objects we include"

	<category: 'testing collections'>
	| count |
	count := 0.
	self do: [:element | count := count + 1].
	^count
    ]

    anyOne [
	"Answer an unspecified element of the collection."

	<category: 'enumeration'>
	self do: [:each | ^each].
	^SystemExceptions.EmptyCollection signalOn: self
    ]

    join [
	"Answer a new collection like my first element, with all the
	 elements (in order) of all my elements, which should be
	 collections.
	 
	 I use my first element instead of myself as a prototype because
	 my elements are more likely to share the desired properties than
	 I am, such as in:
	 
	 #('hello, ' 'world') join => 'hello, world'"

	<category: 'concatenating'>
	^self isEmpty ifTrue: [#()] ifFalse: [self anyOne species join: self]
    ]

    beConsistent [
	"This method is private, but it is quite interesting so it is
	 documented. It ensures that a collection is in a consistent
	 state before attempting to iterate on it; its presence reduces
	 the number of overrides needed by collections who try to
	 amortize their execution times.  The default implementation
	 does nothing, so it is optimized out by the virtual machine
	 and so it loses very little on the performance side.  Note
	 that descendants of Collection have to call it explicitly
	 since #do: is abstract in Collection."

	<category: 'enumeration'>
	
    ]

    readStream [
        "Answer a stream that gives elements of the receiver"

	<category: 'enumeration'>
	^Generator on: self do: [ :gen :each | gen yield: each ]
    ]

    select: aBlock [
	"Answer a new instance of a Collection containing all the elements
	 in the receiver which, when passed to aBlock, answer true"

	<category: 'enumeration'>
	| newCollection |
	newCollection := self copyEmpty.
	self 
	    do: [:element | (aBlock value: element) ifTrue: [newCollection add: element]].
	^newCollection
    ]

    reject: aBlock [
	"Answer a new instance of a Collection containing all the elements
	 in the receiver which, when passed to aBlock, don't answer true"

	<category: 'enumeration'>
	| newCollection |
	newCollection := self copyEmpty.
	self 
	    do: [:element | (aBlock value: element) ifFalse: [newCollection add: element]].
	^newCollection
    ]

    collect: aBlock [
	"Answer a new instance of a Collection containing all the results
	 of evaluating aBlock passing each of the receiver's elements"

	<category: 'enumeration'>
	| newCollection |
	newCollection := self copyEmptyForCollect.
	self do: [:element | newCollection add: (aBlock value: element)].
	^newCollection
    ]

    gather: aBlock [
	"Answer a new instance of a Collection containing all the results
	 of evaluating aBlock, joined together.  aBlock should return
	 collections.  The result is the same kind as the first collection,
	 returned by aBlock (as for #join)."

	<category: 'enumeration'>
	^(self collect: aBlock) join
    ]

    asArray [
	"Answer an Array containing all the elements in the receiver"

	<category: 'converting'>
	^(Array new: self size)
	    replaceFrom: 1
		to: self size
		with: self;
	    yourself
    ]

    asByteArray [
	"Answer a ByteArray containing all the elements in the receiver"

	<category: 'converting'>
	^(ByteArray new: self size)
	    replaceFrom: 1
		to: self size
		with: self;
	    yourself
    ]

    asBag [
	"Answer a Bag containing all the elements in the receiver"

	<category: 'converting'>
	^(Bag new: self size)
	    addAll: self;
	    yourself
    ]

    asSet [
	"Answer a Set containing all the elements in the receiver with no
	 duplicates"

	<category: 'converting'>
	^(Set new: self size * 2)
	    addAll: self;
	    yourself
    ]

    asString [
	"Answer a String containing all the elements in the receiver"

	<category: 'converting'>
	^(String new: self size)
	    replaceFrom: 1
		to: self size
		with: self;
	    yourself
    ]

    asUnicodeString [
	"Answer a UnicodeString containing all the elements in the receiver"

	<category: 'converting'>
	^(UnicodeString new: self size)
	    replaceFrom: 1
		to: self size
		with: self;
	    yourself
    ]

    asOrderedCollection [
	"Answer an OrderedCollection containing all the elements in the receiver"

	<category: 'converting'>
	^(OrderedCollection new: self size * 2)
	    addAll: self;
	    yourself
    ]

    sorted [
	"Return a sequenceable collection with the contents of the receiver
         sorted according to the default sort block, which uses #<=
         to compare items."
        <category: 'sorting'>
	^(Array new: self size)
	    replaceFrom: 1
	    to: self size
	    with: self asSortedCollection
	    startingAt: 1
    ]

    sorted: sortBlock [
	"Return a sequenceable collection with the contents of the receiver
         sorted according to the given sort block, which accepts pair of items
         and returns true if the first item is less than the second one."
        <category: 'sorting'>
	^(Array new: self size)
	    replaceFrom: 1
	    to: self size
	    with: (self asSortedCollection: sortBlock)
	    startingAt: 1
    ]

    asSortedCollection [
	"Answer a SortedCollection containing all the elements in the receiver
	 with the default sort block - [ :a :b | a <= b ]"

	<category: 'converting'>
	^(SortedCollection new: self size + 10)
	    addAll: self;
	    yourself
    ]

    asSortedCollection: aBlock [
	"Answer a SortedCollection whose elements are the elements of the receiver,
	 sorted according to the sort block aBlock"

	<category: 'converting'>
	^(self asSortedCollection)
	    sortBlock: aBlock;
	    yourself
    ]

    copyReplacing: targetObject withObject: newObject [
	"Copy replacing each object which is = to targetObject
	 with newObject"

	<category: 'copying Collections'>
	^self 
	    collect: [:each | each = targetObject ifFalse: [each] ifTrue: [newObject]]
    ]

    copyWith: newElement [
	"Answer a copy of the receiver to which newElement is added"

	<category: 'copying Collections'>
	^(self copy)
	    add: newElement;
	    yourself
    ]

    copyWithout: oldElement [
	"Answer a copy of the receiver to which all occurrences of oldElement are
	 removed"

	<category: 'copying Collections'>
	^self reject: [:element | element = oldElement]
    ]

    copyEmpty [
	"Answer an empty copy of the receiver"

	<category: 'private'>
	^self copyEmpty: self basicSize
    ]

    copyEmpty: newSize [
	"Answer an empty copy of the receiver whose size is newSize"

	<category: 'private'>
	^self species new: newSize
    ]

    copyEmptyForCollect: size [
	"Answer an empty copy of the receiver, with the class answered by the
	 collect: method."

	<category: 'private'>
	^self copyEmpty: size
    ]

    copyEmptyForCollect [
	"Answer an empty copy of the receiver, with the class answered by the
	 collect: method."

	<category: 'private'>
	^self copyEmpty
    ]

    rehash [
	"Private - Do nothing, present for consistency in protocol"

	<category: 'private'>
	
    ]

    examineOn: aStream [
	"Print all the instance variables and objects in the receiver on aStream"

	<category: 'printing'>
	| instVars output object |
	self beConsistent.
	aStream
	    nextPutAll: 'An instance of ';
	    print: self class;
	    nl.
	instVars := self class allInstVarNames.
	1 to: instVars size
	    do: 
		[:i | 
		object := self instVarAt: i.
		output := [object printString] on: Error
			    do: 
				[:ex | 
				ex 
				    return: '%1 %2' % 
						{object class article.
						object class name asString}].
		aStream
		    nextPutAll: '  ';
		    nextPutAll: (instVars at: i);
		    nextPutAll: ': ';
		    nextPutAll: output;
		    nl].
	aStream
	    nextPutAll: '  contents: [';
	    nl.
	self do: 
		[:obj | 
		| output |
		output := [obj printString] on: Error
			    do: 
				[:ex | 
				ex 
				    return: '%1 %2' % 
						{obj class article.
						obj class name asString}].
		aStream
		    nextPutAll: '    ';
		    nextPutAll: output;
		    nl].
	aStream
	    nextPutAll: '  ]';
	    nl
    ]

    printOn: aStream [
	"Print a representation of the receiver on aStream"

	<category: 'printing'>
	aStream nextPutAll: self class storeString.
	aStream nextPutAll: ' ('.
	self do: 
		[:element | 
		element printOn: aStream.
		aStream space].
	aStream nextPut: $)
    ]

    displayLines [
	"Print each element of the receiver to a line on standard output."

	<category: 'printing'>
	self do: [:each | Transcript display: each; nl]
    ]

    storeOn: aStream [
	"Store Smalltalk code compiling to the receiver on aStream"

	<category: 'storing'>
	| noElements |
	aStream nextPut: $(.
	aStream nextPutAll: self class storeString.
	aStream nextPutAll: ' new'.
	noElements := true.
	self do: 
		[:element | 
		aStream nextPutAll: ' add: '.
		element storeOn: aStream.
		aStream nextPut: $;.
		noElements := false].
	noElements ifFalse: [aStream nextPutAll: ' yourself'].
	aStream nextPut: $)
    ]

    mourn: anObject [
	"Private - anObject has been found to have a weak key, remove it
	 and possibly finalize the key."

	<category: 'finalization'>
	self remove: anObject ifAbsent: [].
	self == Object finalizableObjects ifTrue: [anObject key finalize]
    ]
]