/usr/share/gnu-smalltalk/kernel/OtherArrays.st is in gnu-smalltalk-common 3.2.5-1.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 | "=====================================================================
|
| Variations on the Array class
|
|
======================================================================"
"======================================================================
|
| Copyright 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.
|
======================================================================"
ArrayedCollection subclass: WordArray [
<shape: #word>
<category: 'Collections-Sequenceable'>
<comment: '
I am similar to a plain array, but my items are 32-bit integers.'>
at: anIndex ifAbsent: aBlock [
"Answer the index-th indexed instance variable of the receiver"
<category: 'built ins'>
<primitive: VMpr_Object_basicAt>
^self checkIndexableBounds: anIndex ifAbsent: aBlock
]
]
Namespace current: Kernel [
Magnitude subclass: LargeArraySubpart [
| first last index |
<category: 'Collections-Sequenceable'>
<comment: '
This class is an auxiliary class used to store information
about a LargeArrayedCollection''s contents. LargeArrayedCollections
store their items non-contiguously in a separate storage object, and
use a SortedCollection to map between indices in the array and indices
in the storage object; instances of this class represent a block of
indices that is stored contiguously in the storage object.'>
LargeArraySubpart class >> first: first last: last index: index [
"Answer a LargeArraySubpart which answers first, last, and index
when it is sent (respectively) #first, #last and #firstIndex."
<category: 'instance creation'>
^self new
first: first
last: last
index: index
]
< anObject [
"Answer whether the receiver points to a part of the array that
is before anObject (this makes sense only if the receiver and
anObject are two LargeArraySubparts referring to the same
LargeArrayedCollection)."
<category: 'comparing'>
^self first < anObject first
]
<= anObject [
"Answer whether the receiver points to a part of the array that
is before anObject or starts at the same point (this makes sense
only if the receiver and anObject are two LargeArraySubparts
referring to the same LargeArrayedCollection)."
<category: 'comparing'>
^self first <= anObject first
]
= anObject [
"Answer whether the receiver and anObject are equal (assuming that
the receiver and anObject are two LargeArraySubparts
referring to the same LargeArrayedCollection, which the receiver
cannot check for)."
<category: 'comparing'>
^self first = anObject first
]
hash [
"Answer an hash value for the receiver"
<category: 'comparing'>
^self first hash
]
first: firstIndex last: lastIndex index: storagePosition [
"Set up the receiver so that it answers first, last, and index
when it is sent (respectively) #first, #last and #firstIndex."
<category: 'accessing'>
first := firstIndex.
last := lastIndex.
index := storagePosition
]
first [
"Answer the index of the first item of the LargeArrayedCollection
that the receiver refers to."
<category: 'accessing'>
^first
]
last [
"Answer the index of the last item of the LargeArrayedCollection
that the receiver refers to."
<category: 'accessing'>
^last
]
firstIndex [
"Answer the index in the collection's storage object of the first
item of the LargeArrayedCollection that the receiver refers to."
<category: 'accessing'>
^index
]
lastIndex [
"Answer the index in the collection's storage object of the last
item of the LargeArrayedCollection that the receiver refers to."
<category: 'accessing'>
^index + last - first
]
cutAt: position [
"Answer a new LargeArraySubpart whose lastIndex is position - 1,
and apply a #removeFirst: to the receiver so that the firstIndex
becomes position"
<category: 'modifying'>
| newPart newFirst |
newFirst := first + (position - index).
newPart := self class
first: first
last: newFirst - 1
index: index.
first := newFirst.
index := position.
^newPart
]
grow [
"Add one to last and lastIndex"
<category: 'modifying'>
last := last + 1
]
growBy: numberOfElements [
"Add numberOfElements to last and lastIndex"
<category: 'modifying'>
last := last + numberOfElements
]
relocateTo: position [
"Move the firstIndex to position, and the lastIndex accordingly."
<category: 'modifying'>
index := position
]
removeFirst: n [
"Sum n to first and firstIndex, but leave last/lastIndex untouched"
<category: 'modifying'>
first := first + n.
index := index + n
]
removeLast: n [
"Subtract n from last and lastIndex, but leave first/firstIndex untouched"
<category: 'modifying'>
last := last - n
]
]
]
ArrayedCollection subclass: LargeArrayedCollection [
| storage indices position size |
<category: 'Collections-Sequenceable'>
<comment: '
I am an abstract class specially designed to save
memory when lots of items have the same value.'>
LargeArrayedCollection class >> new: anInteger [
"Answer a new instance of the receiver, with room for anInteger elements."
<category: 'instance creation'>
^self basicNew initialize: anInteger
]
at: anIndex [
"Answer the anIndex-th item of the receiver."
<category: 'accessing'>
| subpart |
self checkIndex: anIndex.
subpart := self atSubpart: anIndex.
subpart isNil ifTrue: [^self defaultElement].
^storage at: anIndex - subpart first + subpart firstIndex
]
at: anIndex put: anObject [
"Replace the anIndex-th item of the receiver with anObject."
<category: 'accessing'>
| subpart |
self checkIndex: anIndex.
"Reset compression flag"
position < 0 ifTrue: [position := position negated].
subpart := self atPutSubpart: anIndex.
subpart isNil
ifTrue:
[self addToStorage: anObject.
indices add: (Kernel.LargeArraySubpart
first: anIndex
last: anIndex
index: position - 1).
^anObject].
"The item was not a nil one"
subpart last >= anIndex
ifTrue:
[^storage at: anIndex - subpart first + subpart firstIndex put: anObject].
self addToStorage: anObject.
subpart lastIndex = (position - 2)
ifTrue:
["Extend the last subpart we created"
subpart grow]
ifFalse:
["Create a new subpart."
indices add: (Kernel.LargeArraySubpart
first: anIndex
last: anIndex
index: position - 1)].
^anObject
]
compress [
"Arrange the representation of the array for maximum memory saving."
<category: 'accessing'>
| newStorage newIndices last startOfNils trailingNils |
position < 0 ifTrue: [^self].
newStorage := WriteStream on: (self newCollection: self size // 100 + 10).
newIndices := WriteStream on: (Array new: self size // 1000 + 10).
"This algorithm is complicated to code but intuitive. Read it slowly
and follow its rhythm..."
indices do:
[:each |
"First, do a pass on the indices, searching for spans of nils
that can be removed from the array."
| oldPosition i element |
startOfNils := i := each firstIndex.
[i <= each lastIndex] whileTrue:
[element := storage at: i.
i := i + 1.
element == self defaultElement ifFalse: [startOfNils := i].
i - startOfNils >= self costOfNewIndex
ifTrue:
["Find the end of this run of nil elements, and
remove the nils from the start of the subpart"
[i <= each lastIndex and: [(storage at: i) == self defaultElement]]
whileTrue: [i := i + 1].
"Create a new part that spans until the start of the nils"
self
from: each firstIndex
to: startOfNils - 1
putOn: newStorage.
last := each cutAt: startOfNils.
newIndices nextPut: last.
each removeFirst: i - each firstIndex.
startOfNils := i]].
startOfNils <= each lastIndex
ifTrue: [each removeLast: each lastIndex - startOfNils + 1].
"Now check whether we can merge the last LargeArraySubpart and
this one"
last isNil
ifFalse:
[each first - last last <= self costOfNewIndex
ifTrue:
[newStorage next: each first - last last - 1 put: self defaultElement.
last growBy: each last - last last]
ifFalse: [last := nil]].
"Anyway, add the items to the newStorage"
oldPosition := newStorage position + 1.
self
from: each firstIndex
to: each lastIndex
putOn: newStorage.
"Then add a new LargeArraySubpart if necessary"
(last isNil and: [each lastIndex >= each firstIndex])
ifTrue:
[each relocateTo: oldPosition.
newIndices nextPut: each.
last := each]].
indices := newIndices contents asSortedCollection.
storage := newStorage contents.
position := newStorage size negated
]
= aLargeArray [
"Answer whether the receiver and aLargeArray have the same contents"
<category: 'basic'>
self class == aLargeArray class ifFalse: [^false].
self == aLargeArray ifTrue: [^true].
self compress.
aLargeArray compress.
^indices = aLargeArray indices and: [storage = aLargeArray storage]
]
hash [
"Answer an hash value for the receiver"
<category: 'basic'>
self compress.
^storage hash
]
size [
"Answer the maximum valid index for the receiver"
<category: 'basic'>
^size
]
addToStorage: anObject [
"Add anObject to the storage, possibly growing it if necessary."
<category: 'private'>
position > storage size
ifTrue:
[storage := (self newCollection: storage size * 2)
replaceFrom: 1
to: storage size
with: storage
startingAt: 1;
yourself].
storage at: position put: anObject.
position := position + 1.
^anObject
]
atSubpart: index [
"Private - Perform a binary search on the indices, searching for
a LargeArraySubpart referring to index."
<category: 'private'>
| i j last mid element |
i := 1.
j := last := indices size.
[i > j] whileFalse:
[mid := (i + j + 1) // 2.
element := indices at: mid.
index > element last
ifTrue: [i := mid + 1]
ifFalse: [index < element first ifTrue: [j := mid - 1] ifFalse: [^element]]].
^nil
]
atPutSubpart: index [
"Private - Perform a binary search on the indices, searching for
a LargeArraySubpart referring to index or (if it cannot be found)
to index - 1."
<category: 'private'>
| i j last mid element |
i := 1.
j := last := indices size.
[i > j] whileFalse:
[mid := (i + j + 1) // 2.
element := indices at: mid.
index > element last
ifTrue:
["Answer a LargeArraySubpart to be extended"
index = (element last + 1)
ifTrue:
[(j = last or: [(indices at: mid + 1) first > index])
ifTrue: [^element]
ifFalse: [^indices at: mid + 1]].
"Discard up to this element"
i := mid + 1]
ifFalse: [index < element first ifTrue: [j := mid - 1] ifFalse: [^element]]].
^nil
]
checkIndex: index [
"Check if the given index is valid"
<category: 'private'>
index isInteger
ifFalse: [^SystemExceptions.WrongClass signalOn: index mustBe: Integer].
index < 1
ifTrue: [^SystemExceptions.IndexOutOfRange signalOn: self withIndex: index].
index > self size
ifTrue: [^SystemExceptions.IndexOutOfRange signalOn: self withIndex: index]
]
from: first to: last putOn: newStorage [
"Store on newStorage every item of the current storage from the first-th
to the last-th"
<category: 'private'>
storage
from: first
to: last
do: [:element | newStorage nextPut: element]
]
indices [
<category: 'private'>
^indices
]
storage [
<category: 'private'>
^storage
]
costOfNewIndex [
"Answer the maximum number of consecutive items set to the defaultElement
that can be present in a compressed array."
<category: 'private-abstract'>
^5
]
defaultElement [
"Answer the value which is hoped to be the most common in the array"
<category: 'private-abstract'>
^nil
]
newCollection: size [
<category: 'private-abstract'>
self subclassResponsibility
]
initialize: mySize [
"Initialize the receiver's state"
<category: 'private-initialization'>
indices := SortedCollection new: mySize // 1000 + 10.
storage := self newCollection: mySize // 100 + 10.
size := mySize.
position := -1
]
]
LargeArrayedCollection subclass: LargeArray [
<category: 'Collections-Sequenceable'>
<comment: '
I am similar to a plain array, but I''m specially designed to save
memory when lots of items are nil.'>
newCollection: size [
"Create an Array of the given size"
<category: 'overridden'>
^Array new: size
]
]
LargeArrayedCollection subclass: LargeByteArray [
<category: 'Collections-Sequenceable'>
<comment: '
I am similar to a plain ByteArray, but I''m specially designed to save
memory when lots of items are zero.'>
costOfNewIndex [
"Answer the maximum number of consecutive items set to the defaultElement
that can be present in a compressed array."
"### Should be 40 on 64-bit machines (super costOfNewIndex * CLong sizeof)"
<category: 'overridden'>
^20
]
defaultElement [
"Answer the value which is hoped to be the most common in the array"
<category: 'overridden'>
^0
]
newCollection: size [
"Create a ByteArray of the given size"
<category: 'overridden'>
^ByteArray new: size
]
]
LargeArrayedCollection subclass: LargeWordArray [
<category: 'Collections-Sequenceable'>
<comment: '
I am similar to a plain WordArray, but I''m specially designed to save
memory when lots of items are zero.'>
defaultElement [
"Answer the value which is hoped to be the most common in the array"
<category: 'overridden'>
^0
]
newCollection: size [
"Create a WordArray of the given size"
<category: 'overridden'>
^WordArray new: size
]
]
|