ArrayedCollection.st
changeset 17212 2e02d9d621da
parent 17211 19b94763ad16
child 18120 e3a375d5f6a8
child 19100 c5b67cced033
equal deleted inserted replaced
17211:19b94763ad16 17212:2e02d9d621da
   348     ^ aCollection
   348     ^ aCollection
   349 
   349 
   350     "
   350     "
   351         #(1 2 3 4) addAll:#(5 6 7 8); yourself
   351         #(1 2 3 4) addAll:#(5 6 7 8); yourself
   352     "
   352     "
       
   353 !
       
   354 
       
   355 removeAll
       
   356     "{ Pragma: +optSpace }"
       
   357 
       
   358     "remove all elements from the receiver. Returns the receiver.
       
   359 
       
   360      For ArrayedCollections (which are actually fixed-size collections),
       
   361      this is a slow operation, since a #become: is required to update
       
   362      all owners. Better use a collection which is prepared for growing
       
   363      (i.e. an OrderedCollection).
       
   364      We output a warning message here, to remind you about that."
       
   365 
       
   366     'ArrayedCollection [info]: slow removeAll operation (' infoPrint.
       
   367     self class name infoPrint. ')' infoPrintCR.
       
   368 
       
   369     self become:(self copyEmpty)
       
   370 
       
   371     "
       
   372      #(1 2 3 4 5) copy removeAll    
       
   373      #(1 2 3 4 5) removeAll    
       
   374     "
       
   375 
       
   376     "Modified: 10.1.1997 / 15:14:55 / cg"
   353 ! !
   377 ! !
   354 
   378 
   355 !ArrayedCollection methodsFor:'copying'!
   379 !ArrayedCollection methodsFor:'copying'!
   356 
   380 
   357 copyEmptyAndGrow:size
   381 copyEmptyAndGrow:size
   379      Read the documentation on why things are that way ..."
   403      Read the documentation on why things are that way ..."
   380 
   404 
   381     ^ self error:'cannot change size'
   405     ^ self error:'cannot change size'
   382 
   406 
   383     "Modified: 18.7.1996 / 21:39:09 / cg"
   407     "Modified: 18.7.1996 / 21:39:09 / cg"
       
   408 ! !
       
   409 
       
   410 !ArrayedCollection methodsFor:'growing'!
       
   411 
       
   412 grow:newSize
       
   413     "grow the receiver i.e. cut off everything after newSize.
       
   414      Warning: this may be a slow operation due to the use of become 
       
   415      - you should write your collection classes to avoid the use of become. 
       
   416      You have been warned."
       
   417 
       
   418     |newArray oldSize|
       
   419 
       
   420     oldSize := self size.
       
   421     (newSize ~~ oldSize) ifTrue:[
       
   422         InfoPrinting ifTrue:[
       
   423             "/
       
   424             "/ output a warning - you should rewrite your application
       
   425             "/ to use some collection which implements grow: more efficient
       
   426             "/ (i.e. use OrderedCollection instead of Array ..)
       
   427             "/
       
   428             'ArrayedCollection [warning]: slow grow operation (' errorPrint.
       
   429             self class name infoPrint. ') ' errorPrintCR.
       
   430             Context showWhereWeCameFrom.
       
   431         ].
       
   432 
       
   433         newArray := self species new:newSize.
       
   434         newArray replaceFrom:1 to:(newSize min:oldSize) with:self.
       
   435         self become:newArray.
       
   436     ]
       
   437 
       
   438     "
       
   439      #(1 2 3 4 5 6) add:7
       
   440      #(1 2 3 4 5 6) remove:5 
       
   441      #(1 2 3 4 5 6) copy grow:3  
       
   442      #(1 2 3 4 5 6) copy grow:10  
       
   443      'hello world' copy grow:5   
       
   444      'hello' copy grow:20   
       
   445     "
       
   446 
       
   447     "Modified: 10.1.1997 / 15:14:43 / cg"
   384 ! !
   448 ! !
   385 
   449 
   386 
   450 
   387 !ArrayedCollection methodsFor:'printing & storing'!
   451 !ArrayedCollection methodsFor:'printing & storing'!
   388 
   452 
   433       Used by functions which create a growing collection (see collect:with:, for example)"
   497       Used by functions which create a growing collection (see collect:with:, for example)"
   434 
   498 
   435     ^ OrderedCollection
   499     ^ OrderedCollection
   436 ! !
   500 ! !
   437 
   501 
   438 !ArrayedCollection methodsFor:'resizing'!
       
   439 
       
   440 grow:newSize
       
   441     "grow the receiver i.e. cut off everything after newSize.
       
   442      Warning: this may be a slow operation due to the use of become 
       
   443      - you should write your collection classes to avoid the use of become. 
       
   444      You have been warned."
       
   445 
       
   446     |newArray oldSize|
       
   447 
       
   448     oldSize := self size.
       
   449     (newSize ~~ oldSize) ifTrue:[
       
   450         InfoPrinting ifTrue:[
       
   451             "/
       
   452             "/ output a warning - you should rewrite your application
       
   453             "/ to use some collection which implements grow: more efficient
       
   454             "/ (i.e. use OrderedCollection instead of Array ..)
       
   455             "/
       
   456             'ArrayedCollection [warning]: slow grow operation (' errorPrint.
       
   457             self class name infoPrint. ') ' errorPrintCR.
       
   458             Context showWhereWeCameFrom.
       
   459         ].
       
   460 
       
   461         newArray := self species new:newSize.
       
   462         newArray replaceFrom:1 to:(newSize min:oldSize) with:self.
       
   463         self become:newArray.
       
   464     ]
       
   465 
       
   466     "
       
   467      #(1 2 3 4 5 6) add:7
       
   468      #(1 2 3 4 5 6) remove:5 
       
   469      #(1 2 3 4 5 6) copy grow:3  
       
   470      #(1 2 3 4 5 6) copy grow:10  
       
   471      'hello world' copy grow:5   
       
   472      'hello' copy grow:20   
       
   473     "
       
   474 
       
   475     "Modified: 10.1.1997 / 15:14:43 / cg"
       
   476 !
       
   477 
       
   478 removeAll
       
   479     "{ Pragma: +optSpace }"
       
   480 
       
   481     "remove all elements from the receiver. Returns the receiver.
       
   482 
       
   483      For ArrayedCollections (which are actually fixed-size collections),
       
   484      this is a slow operation, since a #become: is required to update
       
   485      all owners. Better use a collection which is prepared for growing
       
   486      (i.e. an OrderedCollection).
       
   487      We output a warning message here, to remind you about that."
       
   488 
       
   489     'ArrayedCollection [info]: slow removeAll operation (' infoPrint.
       
   490     self class name infoPrint. ')' infoPrintCR.
       
   491 
       
   492     self become:(self copyEmpty)
       
   493 
       
   494     "
       
   495      #(1 2 3 4 5) copy removeAll    
       
   496      #(1 2 3 4 5) removeAll    
       
   497     "
       
   498 
       
   499     "Modified: 10.1.1997 / 15:14:55 / cg"
       
   500 ! !
       
   501 
       
   502 !ArrayedCollection class methodsFor:'documentation'!
   502 !ArrayedCollection class methodsFor:'documentation'!
   503 
   503 
   504 version
   504 version
   505     ^ '$Header: /cvs/stx/stx/libbasic/ArrayedCollection.st,v 1.68 2014-12-11 17:07:37 cg Exp $'
   505     ^ '$Header: /cvs/stx/stx/libbasic/ArrayedCollection.st,v 1.69 2014-12-11 17:08:43 cg Exp $'
   506 ! !
   506 !
   507 
   507 
       
   508 version_CVS
       
   509     ^ '$Header: /cvs/stx/stx/libbasic/ArrayedCollection.st,v 1.69 2014-12-11 17:08:43 cg Exp $'
       
   510 ! !
       
   511