SequenceableCollection.st
branchjv
changeset 20131 4118d61ddba0
parent 20079 8d884971c2ed
parent 20110 bd8b7a160539
child 20205 03e626304d06
--- a/SequenceableCollection.st	Wed Jul 06 06:50:27 2016 +0200
+++ b/SequenceableCollection.st	Sat Jul 09 21:10:24 2016 +0100
@@ -451,6 +451,8 @@
     ^ self == SequenceableCollection
 ! !
 
+
+
 !SequenceableCollection methodsFor:'Compatibility-Squeak'!
 
 allButFirst
@@ -7876,6 +7878,7 @@
     "Created: 14.2.1997 / 16:13:03 / cg"
 ! !
 
+
 !SequenceableCollection methodsFor:'searching'!
 
 detect:aBlock startingAt:startIndex
@@ -8104,6 +8107,55 @@
     ^ self indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:[0]
 !
 
+indexOfSubCollection:aCollection startingAt:startIndex endingAt:endIndex ifAbsent:exceptionBlock
+    "find a subcollection, starting at index. If found, return the index;
+     if not found, return the result of evaluating exceptionBlock.
+     This is a q&d hack - not very efficient"
+
+    |same first lastIndex checkIndex "{Class: SmallInteger }"
+     cmpIndex   "{Class: SmallInteger }"
+     sz         "{Class: SmallInteger }"|
+
+    sz := aCollection size.
+    sz == 0 ifTrue:[^ exceptionBlock value].
+    first := aCollection at:1.
+
+    lastIndex := self size.
+    (endIndex notNil and:[endIndex < lastIndex]) ifTrue:[
+        lastIndex := endIndex.
+    ].
+
+    checkIndex := startIndex - 1.
+    [
+        checkIndex := self indexOf:first startingAt:checkIndex+1 endingAt:lastIndex.
+        checkIndex == 0 ifTrue:[^ exceptionBlock value].
+
+        (checkIndex + sz - 1) > lastIndex ifTrue:[
+            ^ 0
+        ].
+        same := true.
+        cmpIndex := 1.
+        [same and:[cmpIndex <= sz]] whileTrue:[
+            (self at:checkIndex + cmpIndex - 1) = (aCollection at:cmpIndex) ifFalse:[
+                same := false
+            ] ifTrue:[
+                cmpIndex := cmpIndex + 1
+            ]
+        ].
+        same ifTrue:[^ checkIndex].
+    ] loop.
+
+    "
+     #(1 2 3 4 5 6 7) indexOfSubCollection:#()  startingAt:2 endingAt:nil ifAbsent:0
+     #(1 2 3 4 5 6 7) indexOfSubCollection:#(1) startingAt:2 endingAt:nil ifAbsent:0
+     #(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 endingAt:nil ifAbsent:0
+     #(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 endingAt:6 ifAbsent:0
+     #(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2)   startingAt:2 endingAt:nil ifAbsent:0
+     #(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2)   startingAt:3 endingAt:nil ifAbsent:0
+     #(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2)   startingAt:4 endingAt:nil ifAbsent:0
+    "
+!
+
 indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock
     "find a subcollection, starting at index. If found, return the index;
      if not found, return the result of evaluating exceptionBlock.