Collection.st
changeset 20211 c02d61712505
parent 20187 567d9526c40d
child 20212 e9d7cf16d560
--- a/Collection.st	Wed Aug 03 12:42:44 2016 +0200
+++ b/Collection.st	Mon Aug 08 11:45:28 2016 +0200
@@ -658,7 +658,7 @@
      instead, returns what is there.
 
      For unordered collections, this simply returns the first
-     n elements when enumerating them. 
+     n elements when enumerating them.
      (Warning: the contents of the returned collection is not deterministic in this case).
      This should be redefined in subclasses."
 
@@ -667,8 +667,8 @@
     n < 0 ifTrue:[self error:'bad (negative) argument'].
 
     coll := OrderedCollection new.
-    remain := n.    
-    self do:[:e | 
+    remain := n.
+    self do:[:e |
         coll add:e.
         remain := remain - 1.
         remain == 0 ifTrue:[
@@ -683,10 +683,10 @@
     ^ coll.
 
     "
-     #(1 2 3 4 5) first:3           
-     #(1 2 3 4 5) asSet first:3           
-     #(1 2 3) first:5          
-     #(1 2 3) asSet first:5          
+     #(1 2 3 4 5) first:3
+     #(1 2 3 4 5) asSet first:3
+     #(1 2 3) first:5
+     #(1 2 3) asSet first:5
     "
 
     "Modified (format): / 29-09-2011 / 10:16:49 / cg"
@@ -780,7 +780,7 @@
      No longer raises an error if there are not enough elements;
      instead, returns what is there.
      For unordered collections, this simply returns the last
-     n elements when enumerating them 
+     n elements when enumerating them
      (Warning: the contents of the returned collection is not deterministic in this case).
      This should be redefined in subclasses since the implementation here is VERY inefficient."
 
@@ -789,8 +789,8 @@
     n < 0 ifTrue:[self error:'bad (negative) argument'].
 
     coll := OrderedCollection new:n.
-    remain := n.    
-    self do:[:e | 
+    remain := n.
+    self do:[:e |
         remain > 0 ifTrue:[
             remain := remain - 1.
         ] ifFalse:[
@@ -807,11 +807,11 @@
     ^ coll
 
     "
-     #(1 2 3 4 5) last:3           
-     #(1 2 3 4 5 6 7 8 9 0) asSet last:3           
-     'hello world' last:5           
-     'hello' last:10           
-     'hello' asSet last:10           
+     #(1 2 3 4 5) last:3
+     #(1 2 3 4 5 6 7 8 9 0) asSet last:3
+     'hello world' last:5
+     'hello' last:10
+     'hello' asSet last:10
     "
 
     "Modified (format): / 29-09-2011 / 10:16:22 / cg"
@@ -2372,7 +2372,7 @@
     ^ newCollection
 
     "
-     #(1 2 3 4) asSet collect:[:n | n * 2]  
+     #(1 2 3 4) asSet collect:[:n | n * 2]
     "
 !
 
@@ -4140,23 +4140,23 @@
 
     |creator msg context|
 
-    creator := Method allSubInstances 
-                detect:[:aMethod | (aMethod referencesGlobal:self)] 
+    creator := Method allSubInstances
+                detect:[:aMethod | (aMethod referencesGlobal:self)]
                 ifNone:nil.
-    creator isNil ifTrue:[        
-        creator := Method allSubInstances 
-                detect:[:aMethod | (aMethod literalsDetect:[:l | l == self] ifNone:nil) notNil] 
-                ifNone:nil.   
+    creator isNil ifTrue:[
+        creator := Method allSubInstances
+                detect:[:aMethod | (aMethod literalsDetect:[:l | l == self] ifNone:nil) notNil]
+                ifNone:nil.
     ].
     creator notNil ifTrue:[
         msg := ' (' , creator whoString , ')'
     ].
     context := thisContext sender.
      "
-     this error is reported on an attempt to store into an immutable collection (typically: a literal). 
+     this error is reported on an attempt to store into an immutable collection (typically: a literal).
      The literal was created in creator.
      If you press continue in the debugger, the store will be performed.
-     If you don't want this, press abort and check your code.
+     If you do not want this, press abort and check your code.
      Storing into literals is VERY VERY bad coding style.
     "
     NoModificationError
@@ -4369,14 +4369,14 @@
     "
      #(1 2 3 'hello' $a) printOn:Transcript
      (Array new:100000) printOn:Transcript
-     (Array new:100000) printOn:Stdout          
-     (Array new:100000) printString size 
-     (Dictionary new at:#hello put:'world'; 
+     (Array new:100000) printOn:Stdout
+     (Array new:100000) printString size
+     (Dictionary new at:#hello put:'world';
                      at:#foo put:'bar'; yourself) printOn:Transcript
     "
     "
-     |a| 
-     a := Array new:3. 
+     |a|
+     a := Array new:3.
      a at:2 put:a.
      a printOn:Transcript
     "
@@ -4959,9 +4959,9 @@
     ^ longest.
 
     "
-     #('Array' 'ByteArray' 'BigArray') longestCommonSuffixCaseSensitive:false 
-     #('AAA' 'BBBAA' 'CCCAAAA') longestCommonSuffixCaseSensitive:true       
-     #('AAA' 'BBB' 'CCC') longestCommonSuffixCaseSensitive:true             
+     #('Array' 'ByteArray' 'BigArray') longestCommonSuffixCaseSensitive:false
+     #('AAA' 'BBBAA' 'CCCAAAA') longestCommonSuffixCaseSensitive:true
+     #('AAA' 'BBB' 'CCC') longestCommonSuffixCaseSensitive:true
     "
 !
 
@@ -5032,20 +5032,20 @@
 min
     "return the minimum value in the receiver collection,
      using < to compare elements.
-     Raises an error, if the receiver is empty."
-
-    ^ self 
-        fold:[:minSoFar :each | 
+     Raises an error if the receiver is empty."
+
+    ^ self
+        fold:[:minSoFar :each |
             each < minSoFar
                 ifTrue:[each]
                 ifFalse:[minSoFar]
         ]
 
     "
-     #(15 1 -9 10 5) min 
-     (1 to:15) min        
-     (-1 to:-15 by:-1) min  
-     (-1 to:-15 by:-4) min  
+     #(15 1 -9 10 5) min
+     (1 to:15) min
+     (-1 to:-15 by:-1) min
+     (-1 to:-15 by:-4) min
     "
 
     "Modified: / 11-07-2010 / 17:06:38 / cg"
@@ -5154,7 +5154,7 @@
 smallest:n
     "return the n smallest elements"
 
-    |mySize loopAction actionForFirstN actionForRest 
+    |mySize loopAction actionForFirstN actionForRest
      nSmallest sz_nSmallest maxInSmallest|
 
     mySize := self size.
@@ -5170,9 +5170,9 @@
         n == 2 ifTrue:[
             |l1 l2|
 
-            loopAction := [:el | l1 := el. 
-                                 loopAction := 
-                                        [:el | 
+            loopAction := [:el | l1 := el.
+                                 loopAction :=
+                                        [:el |
                                             el < l1 ifTrue:[
                                                 l2 := l1. l1 := el
                                             ] ifFalse:[
@@ -5181,7 +5181,7 @@
                                             loopAction := actionForRest
                                         ]
                           ].
-            actionForRest := [:el | 
+            actionForRest := [:el |
                                 el < l2 ifTrue:[
                                     el < l1 ifTrue:[
                                         l2 := l1. l1 := el
@@ -5197,8 +5197,8 @@
 
         nSmallest := SortedCollection new:n.
         sz_nSmallest := 0.
-        actionForFirstN := 
-            [:el | 
+        actionForFirstN :=
+            [:el |
                 nSmallest add:el.
                 sz_nSmallest := sz_nSmallest + 1.
                 sz_nSmallest == n ifTrue:[
@@ -5206,7 +5206,7 @@
                     maxInSmallest := nSmallest max.
                 ].
             ].
-        actionForRest := 
+        actionForRest :=
             [:el |
                 el < maxInSmallest ifTrue:[
                     nSmallest removeLast.
@@ -5220,16 +5220,16 @@
         ^ nSmallest
     ].
 
-    ^ self asSortedCollection copyFrom:(self size - n + 1) 
-
-    "
-     #(10 35 20 45 30 5) smallest:1     
-     #(10 35 20 45 30 5) smallest:2     
-     #(10 35 20 45 30 5) smallest:3     
-     #(10 35 20 45 30 5) smallest:5     
-     #(10 35 20 45 30 5) smallest:6 
+    ^ self asSortedCollection copyFrom:(self size - n + 1)
+
+    "
+     #(10 35 20 45 30 5) smallest:1
+     #(10 35 20 45 30 5) smallest:2
+     #(10 35 20 45 30 5) smallest:3
+     #(10 35 20 45 30 5) smallest:5
+     #(10 35 20 45 30 5) smallest:6
       (1 to:10000) asArray shuffled smallest:10
-      (1 to:10000) asArray shuffled largest:10 
+      (1 to:10000) asArray shuffled largest:10
      #(10 35 20 45 30 5) smallest:8
     "
 
@@ -5239,7 +5239,7 @@
      data := (1 to:10000) collect:[:i | Random nextInteger ].
      t1 := Time millisecondsToRun:[
         40 timesRepeat:[
-            data asSortedCollection at:3 
+            data asSortedCollection at:3
         ]
      ].
      t2 := Time millisecondsToRun:[
@@ -5316,7 +5316,7 @@
 !
 
 xor:aCollection
-    "return a new set containing all elements, 
+    "return a new set containing all elements,
      which are contained in either the receiver or aCollection, but not in both.
 
      For large collections you better use Sets for both self and aCollection"
@@ -5334,11 +5334,11 @@
             newCollection add:element.
         ].
     ].
-        
+
     ^ newCollection
 
     "
-     #(0 1 2 3 4 5 6 7 8 9) xor:#(1 2 3 11)   
+     #(0 1 2 3 4 5 6 7 8 9) xor:#(1 2 3 11)
     "
 
     "
@@ -5346,7 +5346,7 @@
 
      c1 := #( foo bar baz baloo ).
      c2 := #( foe bar banana baloo ).
-     c1 symmetricDifference:c2.         
+     c1 symmetricDifference:c2.
      self assert:(c1 symmetricDifference:c2) asSet = (c2 symmetricDifference:c1) asSet
     "
 ! !
@@ -5627,7 +5627,7 @@
     "return the number of elements, that the receiver is
      prepared to take. For most collections, this is the actual
      size. However, some have more space preallocated to allow
-     for faster adding of elements. 
+     for faster adding of elements.
      Not used by the system; added for ST-80 compatibility."
 
     ^ self size
@@ -5677,10 +5677,10 @@
 !
 
 includesAll:aCollection
-    "return true, if the receiver includes all elements of
+    "return true if the receiver includes all elements of
      the argument, aCollection; false if any is missing.
      Notice: this method has OČ runtime behavior and may be
-             slow for big receivers/args. 
+             slow for big receivers/args.
              Think about using a Set, or Dictionary."
 
     ^ aCollection conform:[:element | (self includes:element)]
@@ -5695,19 +5695,19 @@
 !
 
 includesAny:searchedElementsCollection
-    "return true, if the receiver includes any from the argument, aCollection. 
+    "return true if the receiver includes any from the argument, aCollection.
      Return false if it includes none.
      Uses #= (value compare)
-     Notice: 
-        this method has OČ runtime behavior for some subclasses and may be slow for big receivers/args. 
-        Think about using a Set or Dictionary. 
+     Notice:
+        this method has OČ runtime behavior for some subclasses and may be slow for big receivers/args.
+        Think about using a Set or Dictionary.
         Some speedup is also possible, by arranging highly
         probable elements towards the beginning of aCollection, to avoid useless searches.
 
         Also: I am not sure, if (and if so, at which breakeven),
         it is better to reverse the loops, and walk over the receiver's
         elements once, walking over the searched elements in the inner loop.
-        If the receiver is large, caching effects will definitely favour this.        
+        If the receiver is large, caching effects will definitely favour this.
     "
 
     |mySize searchedSize|
@@ -5853,8 +5853,8 @@
      for example, a file directory may change its order even though smalltalk does not touch it;
      or a collection which is based on computed block values may return completely differently
      ordered elements (also random value collections, etc.).
-     Therefore, use this only as a hint 
-     (eg. when showing values, to avoid sorting and destroying
+     Therefore, use this only as a hint
+     (e.g. when showing values, to avoid sorting and destroying
       any previous order in the visual representation)"
 
     ^ true
@@ -5863,7 +5863,7 @@
 isSorted
     "return true, if the receiver is sorted.
      Collections which hold their elements in sorted order
-     should return true. Some algorithms (quicksort) degenerate when 
+     should return true. Some algorithms (quicksort) degenerate when
      operating on sorted collections and can be avoided if this information
      is given. The default returned here (false) should not hurt.
      I.e. you should NEVER depend on that in your application."
@@ -5876,13 +5876,12 @@
 isSortedBy:aBlock
     "return true, if my elements are sorted (already) by the given criterion (sortBlock).
      Collections which hold their elements in sorted order
-     should return true. Some algorithms (quicksort) degenerate when 
+     should return true. Some algorithms (quicksort) degenerate when
      operating on sorted collections and can be avoided if this information
      is given. The default returned here (false) should not hurt.
      I.e. you should NEVER depend on that in your application."
 
     ^ false
-
 !
 
 isSortedCollection
@@ -5921,7 +5920,7 @@
 !
 
 occurrencesOfAny:aCollectionOfElements
-    "return the number of occurrences of any in aCollectionOfElements in the receiver. 
+    "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."
 
@@ -5934,9 +5933,9 @@
     ^ count
 
     "
-     #(1 4 6 8 4 1) occurrencesOfAny:#(1 4)  
-     #(1 4 6 8 4 1) occurrencesOfAny:#(2 5)  
-     'hello world' occurrencesOfAny:'hel'     
+     #(1 4 6 8 4 1) occurrencesOfAny:#(1 4)
+     #(1 4 6 8 4 1) occurrencesOfAny:#(2 5)
+     'hello world' occurrencesOfAny:'hel'
     "
 !