Collection.st
branchjv
changeset 18045 c0c600e0d3b3
parent 18043 03660093fe98
parent 15105 ad172479decb
child 18066 89d51443ba6f
--- a/Collection.st	Tue Apr 16 14:27:04 2013 +0200
+++ b/Collection.st	Thu Apr 18 20:41:50 2013 +0200
@@ -259,7 +259,6 @@
     ^ self withSize:n
 ! !
 
-
 !Collection class methodsFor:'Signal constants'!
 
 emptyCollectionSignal
@@ -328,11 +327,6 @@
     ^ self == Collection
 ! !
 
-!Collection methodsFor:'*ST2JS-compatibility'!
-
-inlineDo: aBlock
-	^ self do: aBlock
-! !
 
 !Collection methodsFor:'Compatibility-Dolphin'!
 
@@ -407,6 +401,10 @@
     "Created: / 12-09-2011 / 09:21:58 / cg"
 !
 
+contents
+    ^ self
+!
+
 difference: aCollection
     "Answer the set-theoretic difference of two collections."
 
@@ -533,7 +531,6 @@
     ].
 ! !
 
-
 !Collection methodsFor:'accessing'!
 
 anElement
@@ -1416,7 +1413,9 @@
 !Collection methodsFor:'converting'!
 
 asArray
-    "return a new Array with the collections elements"
+    "return an Array with the collection's elements.
+     Notice: this is redefined in Array, where it returns the receiver. 
+     Use asNewArray, if you intent to modify the returned collection."
 
     |anArray 
      index "{ Class: SmallInteger }" |
@@ -1453,8 +1452,10 @@
 !
 
 asDictionary
-    "return a new Dictionary with the receiver collection's elements,
-     using the original keys of the receiver as dictionary key"
+    "return a Dictionary with the receiver collection's elements,
+     using the original keys of the receiver as dictionary key.
+     Notice: this is redefined in Dictionary, where it returns the receiver. 
+     Use asNewDictionary, if you intent to modify the returned collection."
 
     |d|
 
@@ -1571,14 +1572,29 @@
     ^ self asOrderedCollection
 !
 
+asNewOrderedSet
+    "return a new OrderedSet with the receiver collections elements"
+
+    ^ self asOrderedSet
+!
+
 asNewSet
     "return a new Set with the receiver collections elements"
 
     ^ self asSet
 !
 
+asNilIfEmpty
+    "return mySelf, or nil if I am empty"
+
+    self isEmpty ifTrue:[^ nil].
+    ^ self.
+!
+
 asOrderedCollection
-    "return a new OrderedCollection with the receiver collections elements"
+    "return an OrderedCollection with the receiver collection's elements.
+     Notice: this is redefined in OrderedCollection, where it returns the receiver. 
+     Use asNewOrderedCollection, if you intent to modify the returned collection."
 
     ^ self addAllTo:(OrderedCollection new:self size)
 !
@@ -1629,14 +1645,20 @@
 !
 
 asSet
-    "return a new Set with the receiver collections elements"
+    "return a Set with the receiver collection's elements.
+     Notice: this is redefined in Set, where it returns the receiver. 
+     Use asNewSet, if you intent to modify the returned collection."
 
     ^ self addAllTo:(Set new:self size)
 !
 
 asSharedCollection
     "return a shared collection on the receiver.
-     This implements synchronized access to me."
+     This implements synchronized (i.e. mutually excluded) access to me.
+     Use this for safe access when multiple processes access me concurrently.
+     Notice that this is a general (possibly suboptimal) mechanism, which should
+     work with all collections. Look for specialized collections (SharedQueue), which are
+     tuned for this kind of operation."
 
     ^ SharedCollection for:self.
 !
@@ -1674,7 +1696,7 @@
 asSortedStrings
     "Create & return a SortedCollection that sorts the receiver's
      elements according to the locales collating policy.
-     This is currently not really support - strings are sorted
+     This is currently not really supported - strings are sorted
      without caring for the locale."
 
     |aSortedCollection|
@@ -1691,7 +1713,7 @@
     "Create & return a SortedCollection that sorts the receiver's
      elements using sortBlock and according to the locales collating policy,
      which is passed as first arg to sortBlock.
-     This is currently not really support - strings are sorted
+     This is currently not really supported - strings are sorted
      without caring for the locale."
 
     |aSortedCollection|
@@ -1708,7 +1730,7 @@
 asSortedStrings:sortBlock with:aCollationPolicy
     "Create & return a SortedCollection that sorts the receiver's
      elements using sortBlock and according to the specified locales collating policy.
-     This is currently not really support - strings are sorted
+     This is currently not really supported - strings are sorted
      without caring for the locale."
 
     |aSortedCollection|
@@ -1725,7 +1747,7 @@
 asSortedStringsWith: aCollationPolicy
     "Create & return a SortedCollection that sorts the receiver's
      elements according to the specified locales collating policy.
-     This is currently not really support - strings are sorted
+     This is currently not really supported - strings are sorted
      without caring for the locale."
 
     |aSortedCollection|
@@ -2858,7 +2880,13 @@
      the argument aBlock evaluates to true.
      See also: #removeAllFoundIn: and #removeAllSuchThat:"
 
-    ^ self select:aBlock as:(self species)
+    |newCollection|
+
+    newCollection := self species new.
+    self do:[:each |
+        (aBlock value:each) ifTrue:[newCollection add:each].
+    ].
+    ^ newCollection
 
     "
      #(1 2 3 4) select:[:e | e odd]   
@@ -2873,17 +2901,35 @@
      the argument aBlock evaluates to true.
      See also: #removeAllFoundIn: and #removeAllSuchThat:"
 
-    |newCollection|
-
-    newCollection := aCollectionClass new.
-    self do:[:each |
-        (aBlock value:each) ifTrue:[newCollection add:each].
+    |newCollection idx|
+
+    aCollectionClass growIsCheap ifTrue:[
+        newCollection := aCollectionClass new.
+        self do:[:each |
+            (aBlock value:each) ifTrue:[newCollection add:each].
+        ].
+    ] ifFalse:[
+        newCollection := aCollectionClass new:self size.
+        idx := 1.
+        self do:[:eachElement |
+            (aBlock value:eachElement) ifTrue:[
+                newCollection at:idx put:eachElement.
+                idx := idx + 1.
+            ].
+        ].
+        newCollection := newCollection copyFrom:1 to:idx-1.
     ].
     ^ newCollection
 
     "
-     #(1 2 3 4) select:[:e | e odd] as:OrderedCollection  
-     (1 to:10) select:[:e | e even] as:OrderedCollection       
+     #(1 2 3 4) select:[:e | e odd] as:OrderedCollection.  
+     (1 to:10) select:[:e | e even] as:OrderedCollection.       
+
+     #(1 2 3 4) select:[:e | e odd] as:Set.  
+     (1 to:10) select:[:e | e even] as:Set.       
+
+     #(1 2 3 4) select:[:e | e odd] as:ByteArray.  
+     (1 to:10) select:[:e | e even] as:ByteArray.       
     "
 
     "Created: / 07-08-2010 / 16:26:15 / cg"
@@ -4698,11 +4744,11 @@
 !Collection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.297 2013-03-28 23:14:55 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.302 2013-04-17 20:16:50 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.297 2013-03-28 23:14:55 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.302 2013-04-17 20:16:50 cg Exp $'
 ! !