Collection.st
branchjv
changeset 18043 03660093fe98
parent 18040 a11a12546f23
parent 14996 d0c96a4314bd
child 18045 c0c600e0d3b3
equal deleted inserted replaced
18042:2aa6ef1820fe 18043:03660093fe98
   257      Kludges around the stupid definition of OrderedCollection>>new:"
   257      Kludges around the stupid definition of OrderedCollection>>new:"
   258 
   258 
   259     ^ self withSize:n
   259     ^ self withSize:n
   260 ! !
   260 ! !
   261 
   261 
       
   262 
   262 !Collection class methodsFor:'Signal constants'!
   263 !Collection class methodsFor:'Signal constants'!
   263 
   264 
   264 emptyCollectionSignal
   265 emptyCollectionSignal
   265     "return the signal used to report non-allowed operation on empty collections"
   266     "return the signal used to report non-allowed operation on empty collections"
   266 
   267 
   324      True is returned for Collection here; false for subclasses.
   325      True is returned for Collection here; false for subclasses.
   325      Abstract subclasses must redefine again."
   326      Abstract subclasses must redefine again."
   326 
   327 
   327     ^ self == Collection
   328     ^ self == Collection
   328 ! !
   329 ! !
   329 
       
   330 
   330 
   331 !Collection methodsFor:'*ST2JS-compatibility'!
   331 !Collection methodsFor:'*ST2JS-compatibility'!
   332 
   332 
   333 inlineDo: aBlock
   333 inlineDo: aBlock
   334 	^ self do: aBlock
   334 	^ self do: aBlock
   530 
   530 
   531     ^ self keysAndValuesDo:[:key :index |
   531     ^ self keysAndValuesDo:[:key :index |
   532         aTwoArgBlock value:index value:key
   532         aTwoArgBlock value:index value:key
   533     ].
   533     ].
   534 ! !
   534 ! !
       
   535 
   535 
   536 
   536 !Collection methodsFor:'accessing'!
   537 !Collection methodsFor:'accessing'!
   537 
   538 
   538 anElement
   539 anElement
   539     "return any element from the collection, 
   540     "return any element from the collection, 
  1504 !
  1505 !
  1505 
  1506 
  1506 asIdentitySet
  1507 asIdentitySet
  1507     "return a new IdentitySet with the receiver collections elements"
  1508     "return a new IdentitySet with the receiver collections elements"
  1508 
  1509 
  1509     ^ self addAllNonNilElementsTo:(IdentitySet new:self size)
  1510     ^ self addAllTo:(IdentitySet new:self size)
  1510 !
  1511 !
  1511 
  1512 
  1512 asIntegerArray
  1513 asIntegerArray
  1513     "return a new IntegerArray with the collection's elements
  1514     "return a new IntegerArray with the collection's elements
  1514      (which must convert to 32bit integers in the range 0..16rFFFFFFFF)."
  1515      (which must convert to 32bit integers in the range 0..16rFFFFFFFF)."
  1542 asLongIntegerArray
  1543 asLongIntegerArray
  1543     "return a new LongIntegerArray with the collections elements
  1544     "return a new LongIntegerArray with the collections elements
  1544      (which must convert to 64bit integers in the range 0..16rFFFFFFFFFFFFFFFF)."
  1545      (which must convert to 64bit integers in the range 0..16rFFFFFFFFFFFFFFFF)."
  1545 
  1546 
  1546     ^ self asIntegerArray:LongIntegerArray
  1547     ^ self asIntegerArray:LongIntegerArray
       
  1548 !
       
  1549 
       
  1550 asNewArray
       
  1551     "return a new Array with the receiver collections elements"
       
  1552 
       
  1553     ^ self asArray
       
  1554 !
       
  1555 
       
  1556 asNewDictionary
       
  1557     "return a new Dictionary with the receiver collections elements"
       
  1558 
       
  1559     ^ self asDictionary
       
  1560 !
       
  1561 
       
  1562 asNewIdentitySet
       
  1563     "return a new IdentitySet with the receiver collections elements"
       
  1564 
       
  1565     ^ self asIdentitySet
       
  1566 !
       
  1567 
       
  1568 asNewOrderedCollection
       
  1569     "return a new OrderedCollection with the receiver collections elements"
       
  1570 
       
  1571     ^ self asOrderedCollection
       
  1572 !
       
  1573 
       
  1574 asNewSet
       
  1575     "return a new Set with the receiver collections elements"
       
  1576 
       
  1577     ^ self asSet
  1547 !
  1578 !
  1548 
  1579 
  1549 asOrderedCollection
  1580 asOrderedCollection
  1550     "return a new OrderedCollection with the receiver collections elements"
  1581     "return a new OrderedCollection with the receiver collections elements"
  1551 
  1582 
  2878 select:selectBlock thenCollect:collectBlock
  2909 select:selectBlock thenCollect:collectBlock
  2879     "combination of select followed by collect.
  2910     "combination of select followed by collect.
  2880      May be redefined by some subclasses for optimal performance
  2911      May be redefined by some subclasses for optimal performance
  2881      (avoiding the creation of intermediate garbage)"
  2912      (avoiding the creation of intermediate garbage)"
  2882 
  2913 
       
  2914 "/  We do not do this now, since some classes reimplement select or collect....
       
  2915 "/    ^ self select:selectBlock thenCollect:collectBlock as:self species
  2883     ^ (self select:selectBlock) collect:collectBlock
  2916     ^ (self select:selectBlock) collect:collectBlock
  2884 
  2917 
  2885     "
  2918     "
  2886      #(1 2 3 4 5 6 7) select:[:i | i even] thenCollect:[:i | i * 2]
  2919      #(1 2 3 4 5 6 7) select:[:i | i even] thenCollect:[:i | i * 2]
  2887     "
  2920     "
       
  2921 !
       
  2922 
       
  2923 select:selectBlock thenCollect:collectBlock as:aCollectionClass
       
  2924     "return a new collection with all elements from the receiver, for which
       
  2925      the argument selectBlock evaluates to true.
       
  2926      Process the elements throgh collectBlock before adding."
       
  2927 
       
  2928     |newCollection|
       
  2929 
       
  2930     newCollection := aCollectionClass new.
       
  2931     self do:[:each |
       
  2932         (selectBlock value:each) ifTrue:[newCollection add:(collectBlock value:each)].
       
  2933     ].
       
  2934     ^ newCollection
       
  2935 
       
  2936     "
       
  2937      #(1 2 3 4) select:[:e | e odd] thenCollect:[:e| e*e] as:OrderedCollection  
       
  2938      (1 to:10) select:[:e | e even] thenCollect:[:e| e*e] as:IdentitySet       
       
  2939     "
       
  2940 
       
  2941     "Created: / 07-08-2010 / 16:26:15 / cg"
  2888 !
  2942 !
  2889 
  2943 
  2890 select:selectBlock thenDo:doBlock
  2944 select:selectBlock thenDo:doBlock
  2891     "combination of select followed by do
  2945     "combination of select followed by do
  2892      Avoids the creation of intermediate garbage"
  2946      Avoids the creation of intermediate garbage"
  4312     "
  4366     "
  4313 ! !
  4367 ! !
  4314 
  4368 
  4315 !Collection methodsFor:'testing'!
  4369 !Collection methodsFor:'testing'!
  4316 
  4370 
       
  4371 allElementsHaveTheIdenticalValue
       
  4372     "true if all elements of the receiver have the same value"
       
  4373 
       
  4374     ^ self identicalValuesComputedBy:[:el | el]
       
  4375 
       
  4376     "
       
  4377      #(1 2 3 5 6 7 8 9) allElementsHaveTheIdenticalValue
       
  4378      #(1 1 1 1 1 1) allElementsHaveTheIdenticalValue
       
  4379      #(1 1 1.0 1.0 1) allElementsHaveTheIdenticalValue
       
  4380      #(1 1 1.0 1.0 1) allElementsHaveTheSameValue
       
  4381     "
       
  4382 !
       
  4383 
  4317 allElementsHaveTheSameValue
  4384 allElementsHaveTheSameValue
  4318     "true if all elements of the receiver have the same value"
  4385     "true if all elements of the receiver have the same value"
  4319 
  4386 
  4320     ^ self sameValuesComputedBy:[:el | el]
  4387     ^ self sameValuesComputedBy:[:el | el]
  4321 
  4388 
  4334      size. However, some have more space preallocated to allow
  4401      size. However, some have more space preallocated to allow
  4335      for faster adding of elements. 
  4402      for faster adding of elements. 
  4336      Not used by the system; added for ST-80 compatibility."
  4403      Not used by the system; added for ST-80 compatibility."
  4337 
  4404 
  4338     ^ self size
  4405     ^ self size
       
  4406 !
       
  4407 
       
  4408 identicalValuesComputedBy:aBlock
       
  4409     "true if aBlock answers the same value for all elements of the receiver"
       
  4410 
       
  4411     |first valueForFirstElement|
       
  4412 
       
  4413     first := true.
       
  4414     self do:[:each |
       
  4415         first ifTrue:[
       
  4416             first := false.
       
  4417             valueForFirstElement := aBlock value:each.
       
  4418         ] ifFalse:[
       
  4419             valueForFirstElement == (aBlock value:each) ifFalse:[
       
  4420                 ^ false.
       
  4421             ].
       
  4422         ].
       
  4423     ].
       
  4424     ^ true
       
  4425 
       
  4426     "
       
  4427      #(1 2 3 5 6 7 8 9) sameValuesComputedBy:[:el | el even]
       
  4428      #(1 1 1 1 1 1) sameValuesComputedBy:[:el | el even]
       
  4429      #(1 1 1.0 1.0 1) sameValuesComputedBy:[:el | el even]
       
  4430      #(1 3 3 15 1) sameValuesComputedBy:[:el | el even]
       
  4431     "
       
  4432 
       
  4433     "Created: / 21-12-2011 / 15:59:19 / cg"
  4339 !
  4434 !
  4340 
  4435 
  4341 includes:anElement
  4436 includes:anElement
  4342     "return true, if an object equal to the argument, anObject is in the list.
  4437     "return true, if an object equal to the argument, anObject is in the list.
  4343      This compares using #= (i.e. it does not look for the object itself,
  4438      This compares using #= (i.e. it does not look for the object itself,
  4601 ! !
  4696 ! !
  4602 
  4697 
  4603 !Collection class methodsFor:'documentation'!
  4698 !Collection class methodsFor:'documentation'!
  4604 
  4699 
  4605 version
  4700 version
  4606     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.295 2013-03-26 16:09:26 cg Exp $'
  4701     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.297 2013-03-28 23:14:55 stefan Exp $'
  4607 !
  4702 !
  4608 
  4703 
  4609 version_CVS
  4704 version_CVS
  4610     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.295 2013-03-26 16:09:26 cg Exp $'
  4705     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.297 2013-03-28 23:14:55 stefan Exp $'
  4611 ! !
  4706 ! !
  4612 
  4707 
  4613 
  4708 
  4614 Collection initialize!
  4709 Collection initialize!