Levente Uzonyi uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-ul.736.mcz ==================== Summary ==================== Name: Collections-ul.736 Author: ul Time: 26 February 2017, 9:40:39.679834 pm UUID: 81b6c60e-5ac9-4fd9-a53e-ba9259958a1d Ancestors: Collections-ul.735 Part #1 of Improve SequenceableCollection's index-related search methods - moved actual implementations (in subclasses too) to block-free methods: #indexOf:startingAt:, #identityIndexOf:startingAt:, #indexOfAnyOf:startingAt:, #indexOfSubCollection:startingAt:, #lastIndexOf:startingAt: and #lastIndexOfAnyOf:startingAt: - added #identityIndexOf:startingAt:, #identityIndexOf:startingAt:ifAbsent:, #lastIndexOfAnyOf:, #lastIndexOfAnyOf:ifAbsent: - removed the now unnecessary #indexOf: override from ByteArray ans String - pulled up #indexOfSubCollection: from String - improved #replaceAll:with: =============== Diff against Collections-ul.735 =============== Item was removed: - ----- Method: ByteArray>>indexOf: (in category 'accessing') ----- - indexOf: anInteger - - ^self indexOf: anInteger startingAt: 1! Item was added: + ----- Method: Interval>>indexOf:startingAt: (in category 'accessing') ----- + indexOf: anElement startingAt: startIndex + "startIndex is an positive integer, the collection index where the search is started." + "during the computation of val , floats are only used when the receiver contains floats" + + | index val | + (self rangeIncludes: anElement) ifFalse: [ ^0 ]. + val := anElement - self first / self increment. + val isFloat + ifTrue: [ + (val - val rounded) abs * 100000000 < 1 ifFalse: [ ^0 ]. + index := val rounded + 1 ] + ifFalse: [ + val isInteger ifFalse: [ ^0 ]. + index := val + 1 ]. + "finally, the value of startIndex comes into play:" + (index between: startIndex and: self size) ifFalse: [ ^0 ]. + ^index! Item was added: + ----- Method: LinkedList>>indexOf:startingAt: (in category 'private') ----- + indexOf: anElement startingAt: start + "Answer the index of the first occurence of anElement after start + within the receiver. If the receiver does not contain anElement, + answer the result of evaluating the argument, exceptionBlock." + + |currentLink index| + currentLink := self linkAt: start ifAbsent: [nil]. + index := start. + [ currentLink == nil ] + whileFalse: [currentLink value = anElement value ifTrue: [^index]. + currentLink := currentLink nextLink. + index := index +1]. + ^0! Item was added: + ----- Method: OrderedCollection>>indexOf:startingAt: (in category 'accessing') ----- + indexOf: anElement startingAt: start + "Optimized version." + + firstIndex + start - 1 to: lastIndex do: [ :index | + (array at: index) = anElement ifTrue: [ ^index - firstIndex + 1 ] ]. + ^0! Item was changed: ----- Method: SequenceableCollection>>identityIndexOf: (in category 'accessing') ----- identityIndexOf: anElement "Answer the index of anElement within the receiver. If the receiver does not contain anElement, answer 0." + ^self identityIndexOf: anElement startingAt: 1! - ^self identityIndexOf: anElement ifAbsent: [0]! Item was changed: ----- Method: SequenceableCollection>>identityIndexOf:ifAbsent: (in category 'accessing') ----- identityIndexOf: anElement ifAbsent: exceptionBlock "Answer the index of anElement within the receiver. If the receiver does not contain anElement, answer the result of evaluating the argument, exceptionBlock." + + | index | + (index := self identityIndexOf: anElement startingAt: 1) = 0 ifFalse: [ ^index ]. + ^exceptionBlock value! - 1 to: self size do: - [:i | (self at: i) == anElement ifTrue: [^ i]]. - ^ exceptionBlock value! Item was added: + ----- Method: SequenceableCollection>>identityIndexOf:startingAt: (in category 'accessing') ----- + identityIndexOf: anElement startingAt: startIndex + "Answer the index of anElement within the receiver starting at startIndex. + If the receiver does not contain anElement, answer 0." + + startIndex to: self size do: [ :index | + (self at: index) == anElement ifTrue: [ ^index ] ]. + ^0! Item was added: + ----- Method: SequenceableCollection>>identityIndexOf:startingAt:ifAbsent: (in category 'accessing') ----- + identityIndexOf: anElement startingAt: startIndex ifAbsent: exceptionBlock + "Answer the index of anElement within the receiver starting at startIndex. + If the receiver does not contain anElement, answer the result of evaluating + the argument, exceptionBlock." + + | index | + (index := self identityIndexOf: anElement startingAt: 1) = 0 ifFalse: [ ^index ]. + ^exceptionBlock value! Item was changed: ----- Method: SequenceableCollection>>indexOf: (in category 'accessing') ----- indexOf: anElement "Answer the index of the first occurence of anElement within the receiver. If the receiver does not contain anElement, answer 0." + ^self indexOf: anElement startingAt: 1! - ^ self indexOf: anElement ifAbsent: [0]! Item was changed: ----- Method: SequenceableCollection>>indexOf:ifAbsent: (in category 'accessing') ----- indexOf: anElement ifAbsent: exceptionBlock "Answer the index of the first occurence of anElement within the receiver. If the receiver does not contain anElement, answer the result of evaluating the argument, exceptionBlock." + | index | + (index := self indexOf: anElement startingAt: 1) = 0 ifFalse: [ ^index ]. + ^exceptionBlock value! - ^ self indexOf: anElement startingAt: 1 ifAbsent: exceptionBlock! Item was changed: ----- Method: SequenceableCollection>>indexOf:startingAt: (in category 'accessing') ----- indexOf: anElement startingAt: start "Answer the index of the first occurence of anElement after start within the receiver. If the receiver does not contain anElement, answer 0." + start to: self size do: [ :index | + (self at: index) = anElement ifTrue: [ ^index ] ]. + ^0! - ^self indexOf: anElement startingAt: start ifAbsent: 0! Item was changed: ----- Method: SequenceableCollection>>indexOfAnyOf: (in category 'accessing') ----- indexOfAnyOf: aCollection "Answer the index of the first occurence of any element included in aCollection within the receiver. If the receiver does not contain anElement, answer zero, which is an invalid index." + ^self indexOfAnyOf: aCollection startingAt: 1! - ^self indexOfAnyOf: aCollection startingAt: 1 ifAbsent: [0]! Item was changed: ----- Method: SequenceableCollection>>indexOfAnyOf:startingAt: (in category 'accessing') ----- indexOfAnyOf: aCollection startingAt: start "Answer the index of the first occurence of any element included in aCollection after start within the receiver. If the receiver does not contain anElement, answer zero, which is an invalid index." + start to: self size do: [ :index | + (aCollection includes: (self at: index)) ifTrue: [ ^index ] ]. + ^0! - ^self indexOfAnyOf: aCollection startingAt: start ifAbsent: [0]! Item was added: + ----- Method: SequenceableCollection>>indexOfSubCollection: (in category 'accessing') ----- + indexOfSubCollection: aSubCollection + "Answer the index of the receiver's first element, such that that element + equals the first element of aSubCollection, and the next elements equal + the rest of the elements of aSubCollection. Begin the search at the first + element of the receiver. If no such match is found, answer 0." + + ^self + indexOfSubCollection: aSubCollection + startingAt: 1! Item was changed: ----- Method: SequenceableCollection>>indexOfSubCollection:startingAt: (in category 'accessing') ----- + indexOfSubCollection: subCollection startingAt: start - indexOfSubCollection: aSubCollection startingAt: anIndex "Answer the index of the receiver's first element, such that that element + equals the first element of sub, and the next elements equal + the rest of the elements of sub. Begin the search at element + start of the receiver. If no such match is found, answer 0." - equals the first element of aSubCollection, and the next elements equal - the rest of the elements of aSubCollection. Begin the search at element - anIndex of the receiver. If no such match is found, answer 0." + | first index subCollectionSize | + (subCollectionSize := subCollection size) = 0 ifTrue: [ ^0 ]. + first := subCollection at: 1. + (start max: 1) to: self size - subCollectionSize + 1 do: [ :startIndex | + (self at: startIndex) = first ifTrue: [ + index := 2. + [ index <= subCollectionSize + and: [ (self at: startIndex + index - 1) = (subCollection at: index) ] ] + whileTrue: [ index := index + 1 ]. + index <= subCollectionSize ifFalse: [ ^startIndex ] ] ]. + ^0! - ^self - indexOfSubCollection: aSubCollection - startingAt: anIndex - ifAbsent: [0]! Item was changed: ----- Method: SequenceableCollection>>lastIndexOf: (in category 'accessing') ----- lastIndexOf: anElement "Answer the index of the last occurence of anElement within the receiver. If the receiver does not contain anElement, answer 0." + ^self lastIndexOf: anElement startingAt: self size! - ^ self lastIndexOf: anElement startingAt: self size ifAbsent: [0]! Item was changed: ----- Method: SequenceableCollection>>lastIndexOf:ifAbsent: (in category 'accessing') ----- lastIndexOf: anElement ifAbsent: exceptionBlock "Answer the index of the last occurence of anElement within the receiver. If the receiver does not contain anElement, answer the result of evaluating the argument, exceptionBlock." + + | index | + (index := self lastIndexOf: anElement startingAt: self size) = 0 ifFalse: [ ^index ]. + ^exceptionBlock value! - ^self lastIndexOf: anElement startingAt: self size ifAbsent: exceptionBlock! Item was added: + ----- Method: SequenceableCollection>>lastIndexOf:startingAt: (in category 'accessing') ----- + lastIndexOf: anElement startingAt: lastIndex + "Answer the index of the last occurence of anElement within the + receiver. If the receiver does not contain anElement, answer the + result of evaluating the argument, exceptionBlock." + + lastIndex to: 1 by: -1 do: [ :index | + (self at: index) = anElement ifTrue: [ ^index ] ]. + ^0! Item was changed: ----- Method: SequenceableCollection>>lastIndexOf:startingAt:ifAbsent: (in category 'accessing') ----- lastIndexOf: anElement startingAt: lastIndex ifAbsent: exceptionBlock "Answer the index of the last occurence of anElement within the receiver. If the receiver does not contain anElement, answer the result of evaluating the argument, exceptionBlock." + | index | + (index := self lastIndexOf: anElement startingAt: lastIndex) = 0 ifFalse: [ ^index ]. + ^exceptionBlock value! - lastIndex to: 1 by: -1 do: - [:index | - (self at: index) = anElement ifTrue: [^ index]]. - ^ exceptionBlock value! Item was added: + ----- Method: SequenceableCollection>>lastIndexOfAnyOf: (in category 'accessing') ----- + lastIndexOfAnyOf: aCollection + "Answer the index of the last occurence of any element of aCollection + within the receiver. If the receiver does not contain any of those + elements, answer 0" + + ^self lastIndexOfAnyOf: aCollection startingAt: self size! Item was added: + ----- Method: SequenceableCollection>>lastIndexOfAnyOf:ifAbsent: (in category 'accessing') ----- + lastIndexOfAnyOf: aCollection ifAbsent: exceptionBlock + "Answer the index of the last occurence of any element of aCollection + within the receiver. If the receiver does not contain any of those + elements, answer the result of evaluating the argument, exceptionBlock." + + | index | + (index := self lastIndexOfAnyOf: aCollection startingAt: self size) = 0 ifFalse: [ ^index ]. + ^exceptionBlock value! Item was added: + ----- Method: SequenceableCollection>>lastIndexOfAnyOf:startingAt: (in category 'accessing') ----- + lastIndexOfAnyOf: aCollection startingAt: lastIndex + "Answer the index of the last occurence of any element of aCollection + within the receiver. If the receiver does not contain any of those + elements, answer 0" + + lastIndex to: 1 by: -1 do: [ :index | + (aCollection includes: (self at: index)) ifTrue: [ ^index ] ]. + ^0! Item was changed: ----- Method: SequenceableCollection>>lastIndexOfAnyOf:startingAt:ifAbsent: (in category 'accessing') ----- lastIndexOfAnyOf: aCollection startingAt: lastIndex ifAbsent: exceptionBlock + "Answer the index of the last occurence of any element of aCollection + within the receiver. If the receiver does not contain any of those + elements, answer the result of evaluating the argument, exceptionBlock." - "Answer the index of the last occurence of anElement within the - receiver. If the receiver does not contain anElement, answer the - result of evaluating the argument, exceptionBlock." + | index | + (index := self lastIndexOfAnyOf: aCollection startingAt: lastIndex) = 0 ifFalse: [ ^index ]. + ^exceptionBlock value! - lastIndex to: 1 by: -1 do: - [:index | - (aCollection includes: (self at: index)) ifTrue: [^ index]]. - ^ exceptionBlock value! Item was changed: ----- Method: SequenceableCollection>>replaceAll:with: (in category 'accessing') ----- replaceAll: oldObject with: newObject "Replace all occurences of oldObject with newObject" + | index | + index := 0. + [ (index := self indexOf: oldObject startingAt: index + 1) = 0 ] + whileFalse: [ self at: index put: newObject ]! - index := self - indexOf: oldObject - startingAt: 1 - ifAbsent: [0]. - [index = 0] - whileFalse: - [self at: index put: newObject. - index := self - indexOf: oldObject - startingAt: index + 1 - ifAbsent: [0]]! Item was removed: - ----- Method: String>>indexOf: (in category 'accessing') ----- - indexOf: aCharacter - - ^self indexOf: aCharacter startingAt: 1 - ! Item was removed: - ----- Method: String>>indexOfSubCollection: (in category 'accessing') ----- - indexOfSubCollection: sub - #Collectn. - "Added 2000/04/08 For ANSI <sequenceReadableCollection> protocol." - ^ self - indexOfSubCollection: sub - startingAt: 1 - ifAbsent: [0]! |
Free forum by Nabble | Edit this page |