.
authorclaus
Tue, 22 Aug 1995 15:30:17 +0200
changeset 404 e70aaf0e224f
parent 403 e4d9cc32c794
child 405 b70812b3b32b
.
Coll.st
Collection.st
--- a/Coll.st	Sat Aug 19 03:33:23 1995 +0200
+++ b/Coll.st	Tue Aug 22 15:30:17 1995 +0200
@@ -22,7 +22,7 @@
 COPYRIGHT (c) 1989 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.39 1995-08-11 02:59:41 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.40 1995-08-22 13:30:17 claus Exp $
 '!
 
 !Collection class methodsFor:'documentation'!
@@ -43,7 +43,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.39 1995-08-11 02:59:41 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.40 1995-08-22 13:30:17 claus Exp $
 "
 !
 
@@ -283,6 +283,45 @@
      true is returned here - the method is redefined from Object."
 
     ^ true
+!
+
+longestCommonPrefix
+    |try allMatching matchLen|
+
+    "
+     find the longest common prefix
+    "
+    matchLen := 0.
+    self do:[:aName |
+	aName size > matchLen ifTrue:[
+	    matchLen := aName size.
+	    try := aName
+	]
+    ].
+
+    allMatching := true.
+
+    [true] whileTrue:[
+	allMatching := true.
+	self do:[:aName |
+	    (aName startsWith:try) ifFalse:[
+		allMatching := false
+	    ]
+	].
+	allMatching ifTrue:[
+	    ^ try
+	].
+	matchLen := matchLen - 1.
+	try := try copyTo:matchLen.
+    ].
+    ^ try
+
+    "
+     #('Array' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix 
+     #('Arra' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix 
+     #('Arra' 'b' 'c') longestCommonPrefix 
+     #( (1 2 3 4) (1 2 3) (1 2 3 7) (1 2 3 9 10 11)) longestCommonPrefix
+    "
 ! !
 
 !Collection methodsFor:'accessing'!
--- a/Collection.st	Sat Aug 19 03:33:23 1995 +0200
+++ b/Collection.st	Tue Aug 22 15:30:17 1995 +0200
@@ -22,7 +22,7 @@
 COPYRIGHT (c) 1989 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.39 1995-08-11 02:59:41 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.40 1995-08-22 13:30:17 claus Exp $
 '!
 
 !Collection class methodsFor:'documentation'!
@@ -43,7 +43,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.39 1995-08-11 02:59:41 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.40 1995-08-22 13:30:17 claus Exp $
 "
 !
 
@@ -283,6 +283,45 @@
      true is returned here - the method is redefined from Object."
 
     ^ true
+!
+
+longestCommonPrefix
+    |try allMatching matchLen|
+
+    "
+     find the longest common prefix
+    "
+    matchLen := 0.
+    self do:[:aName |
+	aName size > matchLen ifTrue:[
+	    matchLen := aName size.
+	    try := aName
+	]
+    ].
+
+    allMatching := true.
+
+    [true] whileTrue:[
+	allMatching := true.
+	self do:[:aName |
+	    (aName startsWith:try) ifFalse:[
+		allMatching := false
+	    ]
+	].
+	allMatching ifTrue:[
+	    ^ try
+	].
+	matchLen := matchLen - 1.
+	try := try copyTo:matchLen.
+    ].
+    ^ try
+
+    "
+     #('Array' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix 
+     #('Arra' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix 
+     #('Arra' 'b' 'c') longestCommonPrefix 
+     #( (1 2 3 4) (1 2 3) (1 2 3 7) (1 2 3 9 10 11)) longestCommonPrefix
+    "
 ! !
 
 !Collection methodsFor:'accessing'!