Interval.st
branchjv
changeset 17732 a1892eeca6c0
parent 17711 39faaaf888b4
child 17761 b0e5971141bc
--- a/Interval.st	Fri Aug 28 12:38:51 2009 +0100
+++ b/Interval.st	Sat Oct 24 16:48:19 2009 +0100
@@ -539,6 +539,37 @@
     ^ stop - start // step + 1
 ! !
 
+!Interval methodsFor:'set operations'!
+
+intersect:aCollection
+    "return a new interval containing all elements of the receiver, 
+     which are also contained in the argument collection"
+
+    "/ could be much more intelligent here...
+    aCollection class == Interval ifTrue:[
+        aCollection step = step ifTrue:[
+            step = 1 ifTrue:[
+                (self includes:aCollection start) ifTrue:[
+                    ^ (start max:(aCollection start)) to:(stop min:(aCollection stop))
+                ].
+                (self includes:aCollection stop) ifTrue:[
+                    ^ (start max:(aCollection start)) to:(stop min:(aCollection stop))
+                ]
+            ]
+        ].
+    ].
+
+    ^ super intersect:aCollection
+
+    "
+     (1 to:10) intersect:(4 to:20)      
+     (1 to:10) intersect:(11 to:20)      
+     (1 to:10) intersect:(10 to:20)      
+     (4 to:20) intersect:(1 to:10)      
+     (4 to:20) intersect:(1 to:10 by:2)      
+    "
+! !
+
 !Interval methodsFor:'sorting & reordering'!
 
 reversed
@@ -585,7 +616,10 @@
 !Interval methodsFor:'testing'!
 
 includes:anElement
+    "return true if anElement is in the interval (Numeric compare using =)"
+
     |rest|
+
     stop >= start ifTrue:[
         (anElement between:start and:stop) ifFalse:[^ false].
     ] ifFalse:[
@@ -601,6 +635,7 @@
      (1 to:15) includes:15   
      (1 to:15) includes:5    
      (1 to:15) includes:14   
+     (1 to:15) includes:4   
      (1 to:15) includes:4.0   
      (1 to:15) includes:4.4   
 
@@ -650,5 +685,6 @@
 !Interval class methodsFor:'documentation'!
 
 version
-    ^ '$Id: Interval.st 10447 2009-06-14 13:09:55Z vranyj1 $'
+    ^ '$Id: Interval.st 10473 2009-10-24 15:48:19Z vranyj1 $'
 ! !
+