The Trunk: Collections-ul.598.mcz

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

The Trunk: Collections-ul.598.mcz

commits-2
Levente Uzonyi uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-ul.598.mcz

==================== Summary ====================

Name: Collections-ul.598
Author: ul
Time: 18 January 2015, 8:28:40.971 pm
UUID: 9c200c9a-6210-44da-a42a-0287f65906f1
Ancestors: Collections-ul.589, Collections-mt.597

- Fix: OrderedCollection >> #asArray returns an Array instead of an instance of the class returned by the class side #arrayType method.
- Slightly more efficient OrderedCollection >> #sort:.
- Merged Collections-ul.589:
        - Deprecated Collection class >> #randomForPicking.
        - Replaced all accesses to RandomForPicking with ThreadSafeRandom value.
        - WeakKeyDictionary >> associationsDo: ignores associations with GC'd keys. This affects all enumerator methods, and makes overriding #keysDo: unnecessary.
        - Added a new method SequenceableCollection #>> groupsDo:, which works like #groupsOf:atATimeDo:, but uses the block's argument count as the group size.

Depends on Kernel-ul.893

=============== Diff against Collections-mt.597 ===============

Item was changed:
  ----- Method: Collection class>>randomForPicking (in category 'private') -----
  randomForPicking
+
+ self deprecated: 'Use ThreadSafeRandom value instead. It''s not thread-safe to use this instance without the unaccessible MutexForPicking semaphore.'.
  ^ RandomForPicking!

Item was changed:
  ----- Method: Collection>>atRandom (in category 'accessing') -----
  atRandom
+ "Answer a random element of the receiver.  Uses the process-local random number generator. Causes an error if self has no elements."
- "Answer a random element of the receiver.  Uses a shared random
- number generator owned by class Collection.  If you use this a lot,
- define your own instance of Random and use #atRandom:.  Causes
- an error if self has no elements."
 
+ ^self atRandom: ThreadSafeRandom value
- ^ self class mutexForPicking critical: [
- self atRandom: self class randomForPicking ]
 
  "Examples:
  #('one' 'or' 'the' 'other') atRandom
  (1 to: 10) atRandom
  'Just pick one of these letters at random' atRandom
  #(3 7 4 9 21) asSet atRandom (just to show it also works for Sets)
  "!

Item was changed:
  ----- Method: OrderedCollection>>asArray (in category 'converting') -----
  asArray
  "Overriden for speed"
+
+ | result |
+ result := Array new: self size.
+ result
+ replaceFrom: 1
+ to: result size
+ with: array
+ startingAt: firstIndex.
+ ^result!
- ^array copyFrom: firstIndex to: lastIndex!

Item was changed:
  ----- Method: OrderedCollection>>sort: (in category 'sorting') -----
  sort: aSortBlock
+ "Sort this collection using aSortBlock. The block should take two arguments and return true if the first element should preceed the second one. If aSortBlock is nil then <= is used for comparison."
- "Sort this collection using aSortBlock. The block should take two arguments
- and return true if the first element should preceed the second one.
- If aSortBlock is nil then <= is used for comparison."
 
+ self size <= 1 ifTrue: [ ^self ].
+ array
+ mergeSortFrom: firstIndex
+ to: lastIndex
+ by: aSortBlock!
- self ifNotEmpty: [
- array
- mergeSortFrom: firstIndex
- to: lastIndex
- by: aSortBlock ]!

Item was added:
+ ----- Method: SequenceableCollection>>groupsDo: (in category 'enumerating') -----
+ groupsDo: aBlock
+ "Evaluate aBlock with my elements taken n at a time, where n is the number of arguments of aBlock. Ignore any leftovers at the end."
+
+ | index argumentArray numArgs endIndex |
+ numArgs := aBlock numArgs.
+ numArgs
+ caseOf: {
+ [ 0 ] -> [ ^self error: 'At least one block argument expected.' ].
+ [ 1 ] -> [ ^self do: aBlock ].
+ [ 2 ] -> [ ^self pairsDo: aBlock ] }
+ otherwise: [].
+ argumentArray := Array new: numArgs.
+ index := 1.
+ endIndex := self size - numArgs + 1.
+ [ index <= endIndex ] whileTrue: [
+ argumentArray
+ replaceFrom: 1
+ to: numArgs
+ with: self
+ startingAt: index.
+ aBlock valueWithArguments: argumentArray.
+ index := index + numArgs ].!

Item was changed:
  ----- Method: SequenceableCollection>>shuffle (in category 'shuffling') -----
  shuffle
 
+ ^self shuffleBy: ThreadSafeRandom value!
- ^self shuffleBy: Collection randomForPicking!

Item was changed:
  ----- Method: SequenceableCollection>>shuffled (in category 'copying') -----
  shuffled
+ ^ self shuffledBy: ThreadSafeRandom value
- ^ self shuffledBy: Collection randomForPicking
 
  "Examples:
  ($A to: $Z) shuffled
  "!

Item was added:
+ ----- Method: WeakKeyDictionary>>associationsDo: (in category 'enumerating') -----
+ associationsDo: aBlock
+ "Evaluate aBlock for each of the receiver's elements (key/value
+ associations)."
+
+ tally = 0 ifTrue: [ ^self].
+ 1 to: array size do: [ :index |
+ (array at: index) ifNotNil: [ :association |
+ association key ifNotNil: [ :key | "Don't let the key go away."
+ aBlock value: association ] ] ]!

Item was removed:
- ----- Method: WeakKeyDictionary>>keysDo: (in category 'enumerating') -----
- keysDo: aBlock
- "Evaluate aBlock for each of the receiver's keys."
-
- self associationsDo: [ :association |
- association key ifNotNil: [ :key | "Don't let the key go away"
- aBlock value: key ] ].!