The Trunk: Collections-ul.618.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.618.mcz

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

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

Name: Collections-ul.618
Author: ul
Time: 13 April 2015, 12:08:16.127 am
UUID: 5a6b50bd-668b-48d1-b4c5-fe0b78c6271a
Ancestors: Collections-ul.617

Various OrderedCollection changes.

Adding:
- #addAllFirst: can accept any collection as argument, but uses #reverseDo: if the argument has that method. It's implemented with double dispatch by #addAllFirstTo:.
- Simplified and recategorized #at:ifAbsentPut:, but kept the unusual semantics of nil values.
- Simplified #addAllLast: a bit, and updated its comment.

Removing:
- #removeAllSuchThat: stores the read element in a temporary
- #removeFirst: and #removeLast: doesn't use #removeFirst and #removeLast anymore.

Error handling:
- removed the unsent #errorConditionNotSatisfied method
- changed #errorNoSuchElement to use the name of the receiver instead of "an ordered collection"
- added #errorNotEnoughElements, which is used by #removeFirst: and #removeLast:

Other:
- implemented #indexOf:startingAt:ifAbsent: for better performance
- fixed the comment of #isSortedBy:between:and:

=============== Diff against Collections-ul.617 ===============

Item was added:
+ ----- Method: Collection>>addAllFirstTo: (in category 'adding') -----
+ addAllFirstTo: anOrderedCollection
+ "Add all of my elements to the beginning of anOrderedCollection"
+
+ self do: [ :each | anOrderedCollection addFirst: each ]!

Item was added:
+ ----- Method: Interval>>addAllFirstTo: (in category 'adding') -----
+ addAllFirstTo: anOrderedCollection
+ "Add all of my elements to the beginning of anOrderedCollection"
+
+ self reverseDo: [ :each | anOrderedCollection addFirst: each ]!

Item was changed:
  ----- Method: OrderedCollection>>addAllFirst: (in category 'adding') -----
+ addAllFirst: aCollection
+ "Add all elements of aCollection to the beginning of the me. Answer aCollection. Use double dispatch to add elements in reverse order if aCollection implements #reverseDo:."
- addAllFirst: anOrderedCollection
- "Add each element of anOrderedCollection at the beginning of the
- receiver. Answer anOrderedCollection."
 
+ ^aCollection addAllFirstTo: self!
- anOrderedCollection reverseDo: [:each | self addFirst: each].
- ^anOrderedCollection!

Item was changed:
  ----- Method: OrderedCollection>>addAllLast: (in category 'adding') -----
  addAllLast: aCollection
+ "Add each element of aCollection at the end of me. Answer aCollection."
- "Add each element of aCollection at the end of the receiver.
- Answer aCollection."
 
+ ^aCollection do: [ :each | self addLast: each ]!
- aCollection do: [ :each | self addLast: each].
- ^aCollection!

Item was changed:
+ ----- Method: OrderedCollection>>at:ifAbsentPut: (in category 'accessing') -----
- ----- Method: OrderedCollection>>at:ifAbsentPut: (in category 'adding') -----
  at: index ifAbsentPut: block
  "Return value at index, however, if value does not exist (nil or out of bounds) then add block's value at index (growing self if necessary)"
 
+ [ index <= self size ] whileFalse: [ self add: nil ].
+ ^(self at: index) ifNil: [ self at: index put: block value ]!
- | v |
- index <= self size ifTrue: [
- ^ (v := self at: index)
- ifNotNil: [v]
- ifNil: [self at: index put: block value]
- ].
- [self size < index] whileTrue: [self add: nil].
- ^ self at: index put: block value!

Item was removed:
- ----- Method: OrderedCollection>>errorConditionNotSatisfied (in category 'private') -----
- errorConditionNotSatisfied
-
- self error: 'no element satisfies condition'!

Item was changed:
  ----- Method: OrderedCollection>>errorNoSuchElement (in category 'private') -----
  errorNoSuchElement
 
+ self error: (String streamContents: [ :stream |
+ stream nextPutAll: 'attempt to index a non-existent element in '.
+ self printNameOn: stream ])!
- self error: 'attempt to index non-existent element in an ordered collection'!

Item was added:
+ ----- Method: OrderedCollection>>errorNotEnoughElements (in category 'private') -----
+ errorNotEnoughElements
+
+ self error: (String streamContents: [ :stream |
+ stream nextPutAll: 'attempt to remove more elements than possible from '.
+ self printNameOn: stream ])!

Item was added:
+ ----- Method: OrderedCollection>>indexOf:startingAt:ifAbsent: (in category 'accessing') -----
+ indexOf: anElement startingAt: start ifAbsent: exceptionBlock
+ "Optimized version."
+
+ firstIndex + start - 1 to: lastIndex do: [ :index |
+ (array at: index) = anElement ifTrue: [ ^index - firstIndex + 1 ] ].
+ ^exceptionBlock value!

Item was changed:
  ----- Method: OrderedCollection>>isSortedBy:between:and: (in category 'sorting') -----
  isSortedBy: aSortBlockOrNil between: startIndex and: endIndex
+ "Return true if the receiver is sorted by aSortBlockOrNil between startIndex and endIndex. Use #<= for comparison if aSortBlockOrNil is nil."
- "Return true if the receiver is sorted by #<= between startIndex and endIndex."
 
  ^array
  isSortedBy: aSortBlockOrNil
  between: startIndex + firstIndex - 1
  and: endIndex + firstIndex - 1!

Item was changed:
  ----- Method: OrderedCollection>>removeAllSuchThat: (in category 'removing') -----
  removeAllSuchThat: aBlock
  "Remove each element of the receiver for which aBlock evaluates to true.
  The method in Collection is O(N^2), this is O(N)."
 
  | n |
  n := firstIndex.
+ firstIndex to: lastIndex do: [ :index |
+ | element |
+ (aBlock value: (element := array at: index)) ifFalse: [
+ array at: n put: element.
+ n := n + 1 ] ].
- firstIndex to: lastIndex do: [:index |
-    (aBlock value: (array at: index)) ifFalse: [
- array at: n put: (array at: index).
- n := n + 1]].
  array from: n to: lastIndex put: nil.
  lastIndex := n - 1!

Item was changed:
  ----- Method: OrderedCollection>>removeFirst: (in category 'removing') -----
  removeFirst: n
+ "Remove the first n objects into an array."
+
+ | lastIndexToRemove result |
+ n < 1 ifTrue: [ self errorNoSuchElement ].
+ lastIndex < (lastIndexToRemove := firstIndex + n - 1) ifTrue: [ self errorNotEnoughElements ].
+ result := array copyFrom: firstIndex to: lastIndexToRemove.
+ array from: firstIndex to: lastIndexToRemove put: nil.
+ firstIndex := lastIndexToRemove + 1.
+ ^result!
- "Remove first n object into an array"
- | list |
- list := self class arrayType new: n.
- 1
- to: n
- do:
- [ : i | list
- at: i
- put: self removeFirst ].
- ^ list!

Item was changed:
  ----- Method: OrderedCollection>>removeLast: (in category 'removing') -----
  removeLast: n
+ "Remove the last n objects into an array with last in last position."
+
+ | firstIndexToRemove result |
+ n < 1 ifTrue: [ self errorNoSuchElement ].
+ (firstIndexToRemove := lastIndex - n + 1) < firstIndex ifTrue: [ self errorNotEnoughElements ].
+ result := array copyFrom: firstIndexToRemove to: lastIndex.
+ array from: firstIndexToRemove to: lastIndex put: nil.
+ lastIndex := firstIndexToRemove - 1.
+ ^result!
- "Remove last n object into an array with last in last position"
- | list |
- list := self class arrayType new: n.
- n
- to: 1
- by: -1
- do:
- [ : i | list
- at: i
- put: self removeLast ].
- ^ list!

Item was added:
+ ----- Method: SequenceableCollection>>addAllFirstTo: (in category 'adding') -----
+ addAllFirstTo: anOrderedCollection
+ "Add all of my elements to the beginning of anOrderedCollection"
+
+ self reverseDo: [ :each | anOrderedCollection addFirst: each ]!