+occurrencesOfAny
authorClaus Gittinger <cg@exept.de>
Tue, 30 Mar 2004 20:45:58 +0200
changeset 8274 729d36911f18
parent 8273 72c29dfc55af
child 8275 1f2d4a880b69
+occurrencesOfAny
Collection.st
--- a/Collection.st	Tue Mar 30 17:18:29 2004 +0200
+++ b/Collection.st	Tue Mar 30 20:45:58 2004 +0200
@@ -2708,6 +2708,34 @@
         ].
     ].
     ^ count
+!
+
+occurrencesOfAny:aCollectionOfElements
+    "return the number of occurrences of any in aCollectionOfElements in the receiver. 
+     Uses #= (i.e. equality) compare.
+     Should be redefined in subclass(es) if ever used heavily."
+
+    |count "{ Class: SmallInteger }" |
+
+    count := 0.
+    aCollectionOfElements size < self size ifTrue:[
+        aCollectionOfElements do:[:element |
+            count := count + (self occurrencesOf:element)
+        ].
+    ] ifFalse:[
+        self do:[:element |
+            (aCollectionOfElements includes:element) ifTrue:[
+                count := count + 1
+            ].
+        ].
+    ].
+    ^ count
+
+    "
+     #(1 4 6 8 4 1) occurrencesOfAny:#(1 4)  
+     #(1 4 6 8 4 1) occurrencesOfAny:#(2 5)  
+     'hello world' occurrencesOfAny:'hel'     
+    "
 ! !
 
 !Collection methodsFor:'tracing'!
@@ -2723,7 +2751,7 @@
 !Collection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.159 2004-03-17 16:15:36 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.160 2004-03-30 18:45:58 cg Exp $'
 ! !
 
 Collection initialize!