Nicolas Cellier uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-nice.245.mcz ==================== Summary ==================== Name: Collections-nice.245 Author: nice Time: 9 December 2009, 4:06:35 am UUID: cd040b28-e703-8241-9671-a3b500f3c4b5 Ancestors: Collections-nice.244 Move indexOfAnyOf: & co from String to SequenceableCollection Make it work for any collection argument, not just CharacterSet Handle optimized ByteString+CharacterSet case by double dispatching -- this removes ugly class checks -- Implement a fast ReadStream upToAnyOf:do: thanks to above changes =============== Diff against Collections-nice.244 =============== Item was added: + ----- Method: CharacterSet>>findFirstInByteString:startingAt: (in category 'collection ops') ----- + findFirstInByteString: aByteString startingAt: startIndex + "Double dispatching: since we know this is a ByteString, we can use a superfast primitive using a ByteArray map with 0 slots for byte characters not included and 1 for byte characters included in the receiver." + ^ByteString + findFirstInString: aByteString + inSet: self byteArrayMap + startingAt: startIndex! Item was added: + ----- Method: ByteString>>indexOfAnyOf:startingAt:ifAbsent: (in category 'accessing') ----- + indexOfAnyOf: aCollection startingAt: start ifAbsent: aBlock + "Use double dispatching for speed" + | index | + ^(index := aCollection findFirstInByteString: self startingAt: start) = 0 + ifTrue: [aBlock value] + ifFalse: [index]! Item was added: + ----- Method: Collection>>findFirstInByteString:startingAt: (in category 'enumerating') ----- + findFirstInByteString: aByteString startingAt: start + "Find the index of first character starting at start in aByteString that is included in the receiver. + Default is to use a naive algorithm. + Subclasses might want to implement a more efficient scheme" + + start to: aByteString size do: + [:index | + (self includes: (aByteString at: index)) ifTrue: [^ index]]. + ^ 0! Item was added: + ----- Method: ByteSymbol>>indexOfAnyOf:startingAt:ifAbsent: (in category 'accessing') ----- + indexOfAnyOf: aCollection startingAt: start ifAbsent: aBlock + "Use double dispatching for speed" + | index | + ^(index := aCollection findFirstInByteString: self startingAt: start) = 0 + ifTrue: [aBlock value] + ifFalse: [index]! Item was added: + ----- Method: WideCharacterSet>>findFirstInByteString:startingAt: (in category 'collection ops') ----- + findFirstInByteString: aByteString startingAt: startIndex + "Double dispatching: since we know this is a ByteString, we can use a superfast primitive using a ByteArray map with 0 slots for byte characters not included and 1 for byte characters included in the receiver." + ^ByteString + findFirstInString: aByteString + inSet: self byteArrayMap + startingAt: startIndex! Item was added: + ----- Method: ByteString>>indexOfAnyOf:startingAt: (in category 'accessing') ----- + indexOfAnyOf: aCollection startingAt: start + "Use double dispatching for speed" + ^aCollection findFirstInByteString: self startingAt: start! Item was added: + ----- Method: ByteSymbol>>indexOfAnyOf:startingAt: (in category 'accessing') ----- + indexOfAnyOf: aCollection startingAt: start + "Use double dispatching for speed" + ^aCollection findFirstInByteString: self startingAt: start! Item was added: + ----- Method: SequenceableCollection>>indexOfAnyOf:startingAt:ifAbsent: (in category 'accessing') ----- + indexOfAnyOf: aCollection startingAt: start ifAbsent: exceptionBlock + "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 the result of evaluating the argument, exceptionBlock. + Note: it is user responsibility to provide aCollection that behaves relatevily fast when asked for includes: (like a Set)" + + start to: self size do: + [:index | + (aCollection includes: (self at: index)) ifTrue: [^ index]]. + ^ exceptionBlock value! Item was added: + ----- Method: SequenceableCollection>>indexOfAnyOf:ifAbsent: (in category 'accessing') ----- + indexOfAnyOf: aCollection ifAbsent: exceptionBlock + "Answer the index of the first occurence of any element included in aCollection within the receiver. + If the receiver does not contain anElement, answer the result of evaluating the argument, exceptionBlock." + + ^self indexOfAnyOf: aCollection startingAt: 1 ifAbsent: exceptionBlock! Item was added: + ----- 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 ifAbsent: [0]! Item was added: + ----- 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." + + ^self indexOfAnyOf: aCollection startingAt: start ifAbsent: [0]! Item was added: + ----- Method: ReadStream>>upToAnyOf:do: (in category 'accessing') ----- + upToAnyOf: aCollection do: aBlock + "Overriden for speed" + | end result | + end := collection indexOfAnyOf: aCollection startingAt: 1 + position ifAbsent: [0]. + (end = 0 or: [end > readLimit]) ifTrue: [^self upToEnd]. + result := collection copyFrom: 1 + position to: -1 + end. + position := end. + aBlock value: (collection at: end). + ^result! Item was removed: - ----- Method: String>>indexOfAnyOf:startingAt:ifAbsent: (in category 'accessing') ----- - indexOfAnyOf: aCharacterSet startingAt: start ifAbsent: aBlock - "returns the index of the first character in the given set, starting from start " - - | ans | - ans := self isWideString - ifTrue: ["Fallback to naive implementation" - self class - findFirstInString: self - inCharacterSet: aCharacterSet - startingAt: start] - ifFalse: ["We know we contain only byte characters - So use a byteArrayMap opimized for primitive call" - self class - findFirstInString: self - inSet: aCharacterSet byteArrayMap - startingAt: start]. - ans = 0 - ifTrue: [^ aBlock value] - ifFalse: [^ ans]! Item was removed: - ----- Method: String>>indexOfAnyOf:ifAbsent: (in category 'accessing') ----- - indexOfAnyOf: aCharacterSet ifAbsent: aBlock - "returns the index of the first character in the given set. Returns the evaluation of aBlock if none are found" - ^self indexOfAnyOf: aCharacterSet startingAt: 1 ifAbsent: aBlock! Item was removed: - ----- Method: String>>indexOfAnyOf: (in category 'accessing') ----- - indexOfAnyOf: aCharacterSet - "returns the index of the first character in the given set. Returns 0 if none are found" - ^self indexOfAnyOf: aCharacterSet startingAt: 1! Item was removed: - ----- Method: String>>indexOfAnyOf:startingAt: (in category 'accessing') ----- - indexOfAnyOf: aCharacterSet startingAt: start - "returns the index of the first character in the given set, starting from start. Returns 0 if none are found" - ^self indexOfAnyOf: aCharacterSet startingAt: start ifAbsent: [ 0 ]! Item was removed: - ----- Method: String class>>findFirstInString:inCharacterSet:startingAt: (in category 'primitives') ----- - findFirstInString: aString inCharacterSet: aCharacterSet startingAt: start - "Trivial, non-primitive version" - - start - to: aString size - do: [:i | (aCharacterSet - includes: (aString at: i)) - ifTrue: [^ i]]. - ^ 0! |
Free forum by Nabble | Edit this page |