This file is indexed.

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

"======================================================================
|
| Copyright 1999, 2000, 2001, 2002, 2009 Free Software Foundation, Inc.
| Written by Paolo Bonzini.
|
| 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.  
|
 ======================================================================"

"Some of the methods I define (first, last, indexOf:startingAt:ifAbsent:,
 shallowCopy, deepCopy, =, hash) are here only for performance purposes (their
 inherited implementation works, but it is slow)"



OrderedCollection subclass: RunArray [
    | map mapIndex firstInRun lastInRun size |
    
    <category: 'Collections-Sequenceable'>
    <comment: 'My instances are OrderedCollections that automatically
apply Run Length Encoding compression to the things they store. Be careful
when using me: I can provide great space savings, but my instances don''t
grant linear access time. RunArray''s behavior currently is similar to that
of OrderedCollection (you can add elements to RunArrays); maybe it should
behave like an ArrayedCollection.'>

    RunArray class >> new [
	"Answer an empty RunArray"

	<category: 'instance creation'>
	^(self basicNew)
	    map: OrderedCollection new;
	    initialize
    ]

    RunArray class >> new: aSize [
	"Answer a RunArray with space for aSize runs"

	<category: 'instance creation'>
	^(self basicNew)
	    map: (OrderedCollection new: aSize);
	    initialize
    ]

    at: anIndex [
	"Answer the element at index anIndex"

	<category: 'accessing'>
	self updateMapIndexFor: anIndex.
	^(map at: mapIndex) key
    ]

    at: anIndex put: anObject [
	"Replace the element at index anIndex with anObject and answer anObject"

	<category: 'accessing'>
	^self 
	    at: anIndex
	    splitAndPut: anObject
	    decrementBy: 1
    ]

    first [
	"Answer the first element in the receiver"

	<category: 'basic'>
	^(map at: 1) key
    ]

    last [
	"Answer the last element of the receiver"

	<category: 'basic'>
	^(map at: map size) key
    ]

    size [
	"Answer the number of elements in the receiver"

	<category: 'basic'>
	^size
    ]

    addAll: aCollection afterIndex: anIndex [
	"Add all the elements of aCollection after the one at index anIndex. If
	 aCollection is unordered, its elements could be added in an order which is
	 not the #do: order"

	<category: 'adding'>
	| newMap |
	aCollection isEmpty ifTrue: [^self].
	newMap := aCollection asRunArrayMap.
	self updateMapIndexFor: anIndex.
	self splitAt: anIndex decrementBy: 0.
	map addAll: newMap afterIndex: mapIndex.
	self
	    packTwoRuns: mapIndex + newMap size - 1;
	    packTwoRuns: mapIndex.
	size := size + aCollection size
    ]

    addAllFirst: aCollection [
	"Add all the elements of aCollection at the beginning of the receiver. If
	 aCollection is unordered, its elements could be added in an order which is
	 not the #do: order"

	<category: 'adding'>
	^self addAll: aCollection afterIndex: 0
    ]

    addAllLast: aCollection [
	"Add all the elements of aCollection at the end of the receiver. If aCollection
	is unordered, its elements could be added in an order which is not
	 the #do: order"

	<category: 'adding'>
	^self addAll: aCollection afterIndex: self size
    ]

    addFirst: anObject [
	"Add anObject at the beginning of the receiver. Watch out: this operation
	 can cause serious performance pitfalls"

	<category: 'adding'>
	^self add: anObject afterIndex: 0
    ]

    addLast: anObject [
	"Add anObject at the end of the receiver"

	<category: 'adding'>
	^self add: anObject afterIndex: self size
    ]

    add: anObject afterIndex: anIndex [
	"Add anObject after the element at index anIndex"

	<category: 'adding'>
	size := size + 1.
	^self 
	    at: anIndex
	    splitAndPut: anObject
	    decrementBy: 0
    ]

    shallowCopy [
	"Answer a copy of the receiver. The elements are not copied"

	<category: 'copying'>
	^(self species basicNew)
	    map: (map collect: [:assoc | assoc shallowCopy]);
	    initialize
    ]

    deepCopy [
	"Answer a copy of the receiver containing copies of the receiver's elements
	 (#copy is used to obtain them)"

	<category: 'copying'>
	^(self species basicNew)
	    map: (map collect: [:assoc | assoc deepCopy]);
	    initialize
    ]

    objectsAndRunLengthsDo: aBlock [
	"Enumerate all the runs in the receiver, passing to aBlock two parameters
	 for every run: the first is the repeated object, the second is the number
	 of copies"

	<category: 'enumerating'>
	map do: [:each | aBlock value: each key value: each value]
    ]

    do: aBlock [
	"Enumerate all the objects in the receiver, passing each one to aBlock"

	<category: 'enumerating'>
	map do: [:each | each value timesRepeat: [aBlock value: each key]]
    ]

    afterMapIndexAdd: n copiesOf: anObject [
	"Private - Add a run of n copies of anObject after the mapIndex-th run.
	 Answer anObject"

	<category: 'private'>
	map add: (Association key: anObject value: n) afterIndex: mapIndex.
	^anObject
    ]

    at: anIndex splitAndPut: anObject decrementBy: i [
	"Private - Split the run at index anIndex (say it's made of n elements)
	 into two runs for a total of n-i elements; between them, put a one element
	 run for anObject. Answer anObject"

	<category: 'private'>
	| run |
	(self at: (1 max: anIndex)) = anObject 
	    ifTrue: 
		["No need to split, simply update the current run"

		run := map at: mapIndex.
		run value: run value + (1 - i)]
	    ifFalse: 
		[self
		    splitAt: anIndex decrementBy: i;
		    afterMapIndexAdd: 1 copiesOf: anObject].
	^anObject
    ]

    initialize [
	"Private - Initialize mapIndex, firstInRun, lastInRun"

	<category: 'private'>
	map isEmpty 
	    ifTrue: [mapIndex := firstInRun := lastInRun := 0]
	    ifFalse: 
		[mapIndex := firstInRun := 1.
		lastInRun := (map at: 1) value]
    ]

    map [
	"Private - Answer the receiver's map"

	<category: 'private'>
	^map
    ]

    map: anOrderedCollection [
	"Private - Initialize size and set the map to anOrderedCollection"

	<category: 'private'>
	map := anOrderedCollection.
	size := map inject: 0 into: [:sz :assoc | sz + assoc value]
    ]

    packTwoRuns: indexInMap [
	"Private - Check if the two runs at indexes indexInMap and indexInMap + 1
	 are runs for equal elements. If so, pack them in a single run"

	<category: 'private'>
	| run nextRun |
	indexInMap < 1 ifTrue: [^self].
	indexInMap > self size ifTrue: [^self].
	run := map at: indexInMap.
	nextRun := map at: indexInMap + 1.
	run key = nextRun key 
	    ifTrue: 
		[run value: run value + nextRun value.
		map removeAtIndex: indexInMap + 1]
    ]

    splitAt: anIndex decrementBy: i [
	"Private - Split the run at index anIndex (say it's made of n elements)
	 into two runs for a total of n-i elements.
	 You must have already called #at: or #updateMapIndexFor: passing them
	 anIndex"

	<category: 'private'>
	| run |
	anIndex < 1 ifTrue: [^self].
	anIndex > self size ifTrue: [^self].
	run := map at: mapIndex.
	anIndex = firstInRun 
	    ifTrue: 
		[run value: run value - i.

		"Decrement mapIndex, update firstInRun and lastInRun"
		self updateMapIndexFor: anIndex - 1].
	anIndex = lastInRun ifTrue: [run value: run value - i].
	run value: anIndex - firstInRun + (1 - i).
	self afterMapIndexAdd: lastInRun - anIndex copiesOf: run key
    ]

    updateMapIndexFor: anIndex [
	"Private - Update mapIndex so that it points to the run containing the
	 object at index anIndex. To set mapIndex to 0, set anIndex to 0"

	<category: 'private'>
	(anIndex >= firstInRun and: [anIndex <= lastInRun]) ifTrue: [^self].

	"anIndex = 0 is used internally by RunArray"
	anIndex = 0 
	    ifTrue: 
		[mapIndex := firstInRun := lastInRun := 0.
		^self].
	anIndex < 0 
	    ifTrue: [SystemExceptions.IndexOutOfRange signalOn: self withIndex: anIndex].
	anIndex > self size 
	    ifTrue: [SystemExceptions.IndexOutOfRange signalOn: self withIndex: anIndex].
	anIndex < firstInRun 
	    ifTrue: 
		[
		[mapIndex := mapIndex - 1.
		lastInRun := firstInRun - 1.
		firstInRun := firstInRun - (map at: mapIndex) value.
		anIndex < firstInRun] 
			whileTrue]
	    ifFalse: 
		[
		[mapIndex := mapIndex + 1.
		firstInRun := lastInRun + 1.
		lastInRun := lastInRun + (map at: mapIndex) value.
		lastInRun < anIndex] 
			whileTrue]
    ]

    indexOf: anObject startingAt: anIndex ifAbsent: aBlock [
	"Answer the index of the first copy of anObject in the receiver, starting
	 the search at the element at index anIndex. If no equal object is found,
	 answer the result of evaluating aBlock"

	<category: 'searching'>
	| first last |
	last := 0.
	map do: 
		[:each | 
		first := last + 1.
		last := last + each value.
		(first >= anIndex and: [each key = anIndex]) ifTrue: [^first]].
	^aBlock value
    ]

    removeAtIndex: anIndex [
	"Remove the object at index anIndex from the receiver and answer the
	 removed object"

	<category: 'removing'>
	| run |
	self updateMapIndexFor: anIndex.
	run := map at: mapIndex.
	size := size - 1.
	run value = 1 
	    ifFalse: 
		[run value: run value - 1.
		lastInRun := lastInRun - 1.
		^run key].
	map removeAtIndex: mapIndex.
	mapIndex > map size 
	    ifTrue: 
		[mapIndex := map size.
		lastInRun := self size.
		firstInRun := lastInRun - (map at: map size) value + 1]
	    ifFalse: [lastInRun := firstInRun + (map at: mapIndex) value - 1].
	^run key
    ]

    removeFirst [
	"Remove the first object from the receiver and answer the removed object"

	<category: 'removing'>
	^self removeAtIndex: 1
    ]

    removeLast [
	"Remove the last object from the receiver and answer the removed object"

	<category: 'removing'>
	^self removeAtIndex: self size
    ]

    = anObject [
	"Answer true if the receiver is equal to anObject"

	<category: 'testing'>
	^anObject class == self class and: [anObject map = self map]
    ]

    hash [
	"Answer an hash value for the receiver"

	<category: 'testing'>
	^map hash
    ]
]



Collection extend [

    asRunArray [
	"Answer the receiver converted to a RunArray. If the receiver is not
	 ordered the order of the elements in the RunArray might not be the #do:
	 order."

	<category: 'converting'>
	^(RunArray basicNew)
	    map: self asRunArrayMap;
	    initialize
    ]

    asRunArrayMap [
	"Private - Answer the receiver converted to an OrderedCollection of
	Associations whose keys are the actual objects and whose values are
	the number of consecutive copies of them"

	"Bags can be easily packed, because they are made of runs of unordered
	 elements like RunArrays. As the #do: order of non-sequenceable collections
	 is undefined, we choose the ordering which yields the best map."

	<category: 'private'>
	^self asBag asRunArrayMap
    ]

]



Bag extend [

    asRunArrayMap [
	"Private - Answer the receiver converted to an OrderedCollection of
	Associations whose keys are the actual objects and whose values are
	the number of consecutive copies of them"

	<category: 'private'>
	| map |
	map := OrderedCollection new: contents size.
	contents associationsDo: [:assoc | map addLast: assoc].
	^map
    ]

]



SequenceableCollection extend [

    asRunArrayMap [
	"Private - Answer the receiver converted to an OrderedCollection of
	Associations whose keys are the actual objects and whose values are
	the number of consecutive copies of them"

	<category: 'private'>
	| map prev startIndex |
	map := OrderedCollection new.
	prev := self at: 1.
	startIndex := 1.
	self 
	    from: 2
	    to: self size
	    keysAndValuesDo: 
		[:currIndex :each | 
		each = prev 
		    ifFalse: 
			[map addLast: (Association key: prev value: currIndex - startIndex).
			prev := each.
			startIndex := currIndex]].
	map addLast: (Association key: prev value: self size + 1 - startIndex).
	^map
    ]

]