This file is indexed.

/usr/share/gnu-smalltalk/kernel/OrderColl.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
"======================================================================
|
|   OrderedCollection Method Definitions
|
|
 ======================================================================"

"======================================================================
|
| Copyright 1988,92,94,95,99,2000,2001,2002,2008
| 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.  
|
 ======================================================================"



SequenceableCollection subclass: OrderedCollection [
    | firstIndex lastIndex |
    
    <shape: #pointer>
    <category: 'Collections-Sequenceable'>
    <comment: 'My instances represent ordered collections of arbitrary typed objects which
are not directly accessible by an index.  They can be accessed indirectly
through an index, and can be manipulated by adding to the end or based
on content (such as add:after:)'>

    OrderedCollection class >> new: anInteger [
	"Answer an OrderedCollection of size anInteger"

	<category: 'instance creation'>
	^(self basicNew: anInteger) initIndices
    ]

    OrderedCollection class >> new [
	"Answer an OrderedCollection of default size"

	<category: 'instance creation'>
	^self new: 16
    ]

    do: aBlock [
        "Evaluate aBlock for all the elements in the collection"

        <category: 'enumerating'>
        | index |
        self beConsistent.
        index := firstIndex.
        [ index <= lastIndex ] whileTrue: [
            aBlock value: (self basicAt: index).
            index := index + 1 ]
    ]

    first [
	"Answer the first item of the receiver"

	<category: 'accessing'>
	self beConsistent.
	^lastIndex >= firstIndex
	    ifTrue: [self basicAt: firstIndex]
	    ifFalse: [SystemExceptions.IndexOutOfRange signalOn: self withIndex: 1]
    ]

    last [
	"Answer the last item of the receiver"

	<category: 'accessing'>
	self beConsistent.
	^lastIndex >= firstIndex
	    ifTrue: [self basicAt: lastIndex]
	    ifFalse: [SystemExceptions.IndexOutOfRange signalOn: self withIndex: 0]
    ]

    at: anIndex [
	"Answer the anIndex-th item of the receiver"

	<category: 'accessing'>
	| index |
	self beConsistent.
	index := anIndex + firstIndex - 1.
	^(index >= firstIndex and: [index <= lastIndex]) 
	    ifTrue: [self basicAt: index]
	    ifFalse: [SystemExceptions.IndexOutOfRange signalOn: self withIndex: anIndex]
    ]

    at: anIndex put: anObject [
	"Store anObject at the anIndex-th item of the receiver, answer anObject"

	<category: 'accessing'>
	| index |
	self beConsistent.
	index := anIndex + firstIndex - 1.
	(index >= firstIndex and: [index <= lastIndex]) 
	    ifTrue: [^self basicAt: index put: anObject]
	    ifFalse: [^SystemExceptions.IndexOutOfRange signalOn: self withIndex: anIndex]
    ]

    size [
	"Return the number of objects in the receiver"

	<category: 'accessing'>
	^lastIndex - firstIndex + 1
    ]

    add: anObject [
	"Add anObject in the receiver, answer it"

	<category: 'adding'>
	self makeRoomLastFor: 1.
	lastIndex := lastIndex + 1.
	^self basicAt: lastIndex put: anObject
    ]

    add: newObject after: oldObject [
	"Add newObject in the receiver just after oldObject, answer it.
	 Fail if oldObject can't be found"

	<category: 'adding'>
	^self add: newObject
	    afterIndex: (self indexOf: oldObject
		    ifAbsent: [^SystemExceptions.NotFound signalOn: oldObject what: 'object'])
    ]

    add: newObject before: oldObject [
	"Add newObject in the receiver just before oldObject, answer it.
	 Fail if oldObject can't be found"

	<category: 'adding'>
	^self add: newObject
	    beforeIndex: (self indexOf: oldObject
		    ifAbsent: [^SystemExceptions.NotFound signalOn: oldObject what: 'object'])
    ]

    add: newObject afterIndex: i [
	"Add newObject in the receiver just after the i-th, answer it.
	 Fail if i < 0 or i > self size"

	<category: 'adding'>
	| index |
	(i between: 0 and: self size) 
	    ifFalse: [^SystemExceptions.IndexOutOfRange signalOn: self withIndex: i].
	index := i + firstIndex.
	self makeRoomLastFor: 1.
	lastIndex to: index
	    by: -1
	    do: [:i | self basicAt: i + 1 put: (self basicAt: i)].
	lastIndex := lastIndex + 1.
	^self basicAt: index put: newObject
    ]

    add: newObject beforeIndex: i [
	"Add newObject in the receiver just before the i-th, answer it.
	 Fail if i < 1 or i > self size + 1"

	<category: 'adding'>
	^self add: newObject afterIndex: i - 1
    ]

    addAll: aCollection [
	"Add every item of aCollection to the receiver, answer it"

	<category: 'adding'>
	| index |
	self makeRoomLastFor: aCollection size.
	index := lastIndex + 1.
	lastIndex := lastIndex + aCollection size.
	aCollection do: 
		[:element | 
		self basicAt: index put: element.
		index := index + 1].
	^aCollection
    ]

    addAll: newCollection after: oldObject [
	"Add every item of newCollection to the receiver just after
	 oldObject, answer it. Fail if oldObject is not found"

	<category: 'adding'>
	^self addAll: newCollection
	    afterIndex: (self indexOf: oldObject
		    ifAbsent: [^SystemExceptions.NotFound signalOn: oldObject what: 'object'])
    ]

    addAll: newCollection afterIndex: i [
	"Add every item of newCollection to the receiver just after
	 the i-th, answer it. Fail if i < 0 or i > self size"

	<category: 'adding'>
	| index |
	(i between: 0 and: self size) 
	    ifFalse: [^SystemExceptions.IndexOutOfRange signalOn: self withIndex: i].
	index := i + firstIndex.
	self makeRoomLastFor: newCollection size.
	lastIndex to: index
	    by: -1
	    do: [:i | self basicAt: i + newCollection size put: (self basicAt: i)].
	lastIndex := lastIndex + newCollection size.
	(1 to: newCollection size) with: newCollection
	    do: [:i :each | self basicAt: index + i - 1 put: each].
	^newCollection
    ]

    addAll: newCollection before: oldObject [
	"Add every item of newCollection to the receiver just before
	 oldObject, answer it. Fail if oldObject is not found"

	<category: 'adding'>
	^self addAll: newCollection
	    beforeIndex: (self indexOf: oldObject
		    ifAbsent: [^SystemExceptions.NotFound signalOn: oldObject what: 'object'])
    ]

    addAll: newCollection beforeIndex: i [
	"Add every item of newCollection to the receiver just before
	 the i-th, answer it. Fail if i < 1 or i > self size + 1"

	<category: 'adding'>
	^self addAll: newCollection afterIndex: i - 1
    ]

    addAllFirst: aCollection [
	"Add every item of newCollection to the receiver right at the start
	 of the receiver. Answer aCollection"

	<category: 'adding'>
	| index |
	self makeRoomFirstFor: aCollection size.
	index := firstIndex := firstIndex - aCollection size.
	aCollection do: 
		[:element | 
		self basicAt: index put: element.
		index := index + 1].
	^aCollection
    ]

    addAllLast: aCollection [
	"Add every item of newCollection to the receiver right at the end
	 of the receiver. Answer aCollection"

	<category: 'adding'>
	| index |
	self makeRoomLastFor: aCollection size.
	index := lastIndex + 1.
	lastIndex := lastIndex + aCollection size.
	aCollection do: 
		[:element | 
		self basicAt: index put: element.
		index := index + 1].
	^aCollection
    ]

    addFirst: newObject [
	"Add newObject to the receiver right at the start of the receiver.
	 Answer newObject"

	<category: 'adding'>
	self makeRoomFirstFor: 1.
	firstIndex := firstIndex - 1.
	^self basicAt: firstIndex put: newObject
    ]

    addLast: newObject [
	"Add newObject to the receiver right at the end of the receiver.
	 Answer newObject"

	<category: 'adding'>
	self makeRoomLastFor: 1.
	lastIndex := lastIndex + 1.
	^self basicAt: lastIndex put: newObject
    ]

    removeFirst [
	"Remove an object from the start of the receiver. Fail if the receiver
	 is empty"

	<category: 'removing'>
	| answer |
	self beConsistent.
	lastIndex < firstIndex 
	    ifTrue: [^SystemExceptions.EmptyCollection signalOn: self].
	answer := self basicAt: firstIndex.	"Get the element"
	self basicAt: firstIndex put: nil.	"Allow it to be garbage collected"
	lastIndex = firstIndex 
	    ifTrue: [self initIndices]
	    ifFalse: [firstIndex := firstIndex + 1].
	self size < self shrinkSize ifTrue: [self shrink].
	^answer
    ]

    removeLast [
	"Remove an object from the end of the receiver. Fail if the receiver
	 is empty"

	<category: 'removing'>
	| answer |
	self beConsistent.
	lastIndex < firstIndex 
	    ifTrue: [^SystemExceptions.EmptyCollection signalOn: self].
	answer := self basicAt: lastIndex.	"Get the element"
	self basicAt: lastIndex put: nil.	"Allow it to be garbage collected"
	lastIndex = firstIndex 
	    ifTrue: [self initIndices]
	    ifFalse: [lastIndex := lastIndex - 1].
	self size < self shrinkSize ifTrue: [self shrink].
	^answer
    ]

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

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

    identityRemove: anObject ifAbsent: aBlock [
	"Remove anObject from the receiver. If it can't be found, answer the
	 result of evaluating aBlock"

	<category: 'removing'>
	^self removeAtIndex: (self 
		    identityIndexOf: anObject
		    startingAt: 1
		    ifAbsent: [^aBlock value])
    ]

    remove: anObject ifAbsent: aBlock [
	"Remove anObject from the receiver. If it can't be found, answer the
	 result of evaluating aBlock"

	<category: 'removing'>
	^self removeAtIndex: (self 
		    indexOf: anObject
		    startingAt: 1
		    ifAbsent: [^aBlock value])
    ]

    removeAtIndex: anIndex [
	"Remove the object at index anIndex from the receiver. Fail if the
	 index is out of bounds."

	<category: 'removing'>
	| answer |
	answer := self basicRemoveAtIndex: anIndex.
	self size < self shrinkSize ifTrue: [self shrink].
	^answer
    ]

    basicRemoveAtIndex: anIndex [
	"Remove the object at index anIndex from the receiver. Fail if the
	 index is out of bounds."

	<category: 'private methods'>
	| answer |
	self beConsistent.
	lastIndex < firstIndex 
	    ifTrue: [^SystemExceptions.EmptyCollection signalOn: self].
	(anIndex < 1 or: [anIndex > self size]) 
	    ifTrue: [^SystemExceptions.IndexOutOfRange signalOn: self withIndex: anIndex].
	answer := self basicAt: anIndex + firstIndex - 1.
	anIndex + firstIndex to: lastIndex
	    do: [:index | self basicAt: index - 1 put: (self basicAt: index)].
	self basicAt: lastIndex put: nil.
	lastIndex = firstIndex 
	    ifTrue: [self initIndices]
	    ifFalse: [lastIndex := lastIndex - 1].
	^answer
    ]

    basicAddLast: newObject [
	"Private - Add to the end of the receiver newObject, answer newObject.
	 Don't override this method!"

	<category: 'private methods'>
	self makeRoomLastFor: 1.
	lastIndex := lastIndex + 1.
	^self basicAt: lastIndex put: newObject
    ]

    basicAddAllLast: aCollection [
	"Private - Add to the end of the receiver all the items in aCollection,
	 answer newObject. Don't override this method!"

	<category: 'private methods'>
	| index |
	self makeRoomLastFor: aCollection size.
	index := lastIndex + 1.
	lastIndex := lastIndex + aCollection size.
	aCollection do: 
		[:element | 
		self basicAt: index put: element.
		index := index + 1].
	^aCollection
    ]

    basicRemoveFirst [
	"Remove an object from the start of the receiver. Fail if the receiver
	 is empty"

	<category: 'private methods'>
	| answer |
	lastIndex < firstIndex 
	    ifTrue: [^SystemExceptions.EmptyCollection signalOn: self].
	answer := self basicAt: firstIndex.	"Get the element"
	self basicAt: firstIndex put: nil.	"Allow it to be garbage collected"
	lastIndex = firstIndex 
	    ifTrue: [self initIndices]
	    ifFalse: [firstIndex := firstIndex + 1].
	self size < self shrinkSize ifTrue: [self shrink].
	^answer
    ]

    basicRemoveLast [
	"Remove an object from the end of the receiver. Fail if the receiver
	 is empty"

	<category: 'private methods'>
	| answer |
	lastIndex < firstIndex 
	    ifTrue: [^SystemExceptions.EmptyCollection signalOn: self].
	answer := self basicAt: lastIndex.	"Get the element"
	self basicAt: lastIndex put: nil.	"Allow it to be garbage collected"
	lastIndex = firstIndex 
	    ifTrue: [self initIndices]
	    ifFalse: [lastIndex := lastIndex - 1].
	self size < self shrinkSize ifTrue: [self shrink].
	^answer
    ]

    initIndices [
	<category: 'private methods'>
	firstIndex := self basicSize // 2 max: 1.
	lastIndex := firstIndex - 1
    ]

    firstIndex: first lastIndex: last [
	<category: 'private methods'>
	firstIndex := first.
	lastIndex := last
    ]

    makeRoomFirstFor: n [
	"Private - Make room for n elements at the start of the collection"

	<category: 'private methods'>
	firstIndex <= n 
	    ifTrue: [self growBy: (n max: self growSize) shiftBy: (n max: self growSize)]
    ]

    makeRoomLastFor: n [
	"Private - Make room for n elements at the end of the collection"

	<category: 'private methods'>
	lastIndex + n > self basicSize 
	    ifTrue: [self growBy: (n max: self growSize) shiftBy: 0]
    ]

    shrinkSize [
	<category: 'private methods'>
	^self basicSize // 3
    ]

    shrink [
	"Decrease the room in the collection by shrinkSize"

	<category: 'private methods'>
	| shift shrink |
	shrink := self shrinkSize.

	"Check that the new firstIndex is >= 1."
	shift := firstIndex - 1 min: shrink // 2.

	"Check that the new lastIndex is <= basicSize."
	shrink := shrink min: self basicSize - (lastIndex - shift).
	self growBy: shrink negated shiftBy: shift negated
    ]

    grow [
	"Make growSize room in the collection, putting the old contents in the
	 middle."

	<category: 'private methods'>
	self growBy: self growSize shiftBy: firstIndex // 2
    ]

    growBy: delta shiftBy: shiftCount [
	"Make room for delta more places in the collection, shifting the old
	 contents by shiftCount places"

	<category: 'private methods'>
	| newOrderedCollection |
	newOrderedCollection := self copyEmpty: self basicSize + delta.
	firstIndex to: lastIndex
	    do: 
		[:index | 
		newOrderedCollection basicAt: index + shiftCount put: (self basicAt: index)].
	newOrderedCollection firstIndex: firstIndex + shiftCount
	    lastIndex: lastIndex + shiftCount.
	self become: newOrderedCollection
    ]
]