--- a/Interval.st Tue Sep 08 13:57:25 2009 +0200
+++ b/Interval.st Tue Sep 08 17:35:04 2009 +0200
@@ -539,6 +539,35 @@
^ 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"
+
+ 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)
+ "
+! !
+
!Interval methodsFor:'sorting & reordering'!
reversed
@@ -585,7 +614,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 +633,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 +683,5 @@
!Interval class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic/Interval.st,v 1.49 2009-02-01 02:09:04 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/Interval.st,v 1.50 2009-09-08 15:35:04 cg Exp $'
! !