OrderedCollection.st
changeset 2549 9ca8a3fcdaa7
parent 2364 788423a8d67f
child 3095 cdb753308f6e
equal deleted inserted replaced
2548:b6182b559ea2 2549:9ca8a3fcdaa7
   440 
   440 
   441     aCollection isSequenceable ifTrue:[
   441     aCollection isSequenceable ifTrue:[
   442         "/ we are lucky - that thing can count & do bulk copies
   442         "/ we are lucky - that thing can count & do bulk copies
   443 
   443 
   444         count := aCollection size.
   444         count := aCollection size.
   445         idx := self makeRoomAtIndex:(index + firstIndex) for:count.
   445         idx := self makeRoomAtIndex:(index + firstIndex - 1) for:count.
   446         "/ notice: the above may change firstIndex
   446         "/ notice: the above may change firstIndex
   447         contentsArray replaceFrom:idx to:(idx + count - 1) with:aCollection startingAt:1.
   447         contentsArray replaceFrom:idx to:(idx + count - 1) with:aCollection startingAt:1.
   448         ^ self
   448         ^ self
   449     ].
   449     ].
   450 
   450 
   460      c addAll:'here' beforeIndex:3   
   460      c addAll:'here' beforeIndex:3   
   461     "
   461     "
   462     "
   462     "
   463      |c|
   463      |c|
   464      c := #(1 2 3 4) asOrderedCollection.
   464      c := #(1 2 3 4) asOrderedCollection.
       
   465      c removeFirst.
       
   466      c addAll:'here' beforeIndex:3   
       
   467     "
       
   468     "
       
   469      |c|
       
   470      c := #(1 2 3 4) asOrderedCollection.
   465      c addAll:'here' beforeIndex:1    
   471      c addAll:'here' beforeIndex:1    
   466     "
   472     "
   467     "
   473     "
   468      |c|
   474      |c|
   469      c := #(1 2 3 4) asOrderedCollection.
   475      c := #(1 2 3 4) asOrderedCollection.
   474      |c|
   480      |c|
   475      c := #(1 2 3 4) asOrderedCollection.
   481      c := #(1 2 3 4) asOrderedCollection.
   476      c addAll:('hello' asSet) beforeIndex:3
   482      c addAll:('hello' asSet) beforeIndex:3
   477     "
   483     "
   478 
   484 
   479     "Modified: 22.1.1997 / 04:00:27 / cg"
   485     "Modified: 15.4.1997 / 12:43:59 / cg"
   480 !
   486 !
   481 
   487 
   482 addFirst:anObject
   488 addFirst:anObject
   483     "add the argument, anObject to the beginning of the collection.
   489     "add the argument, anObject to the beginning of the collection.
   484      Return the argument, anObject."
   490      Return the argument, anObject."
   659      (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:6 
   665      (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:6 
   660      (SortedCollection withAll:#(5 4 3 2 1)) removeFirst:2; yourself  
   666      (SortedCollection withAll:#(5 4 3 2 1)) removeFirst:2; yourself  
   661     "
   667     "
   662 
   668 
   663     "Modified: 8.2.1997 / 19:20:18 / cg"
   669     "Modified: 8.2.1997 / 19:20:18 / cg"
       
   670 !
       
   671 
       
   672 removeFrom:startIndex to:stopIndex
       
   673     "added for ST-80 compatibility.
       
   674      Same as removeFromIndex:toIndex:."
       
   675 
       
   676     ^ self removeFromIndex:startIndex toIndex:stopIndex
       
   677 
       
   678     "Created: 15.4.1997 / 12:39:00 / cg"
   664 !
   679 !
   665 
   680 
   666 removeFromIndex:startIndex toIndex:stopIndex
   681 removeFromIndex:startIndex toIndex:stopIndex
   667     "remove the elements stored under startIndex up to and including
   682     "remove the elements stored under startIndex up to and including
   668      the elements under stopIndex.
   683      the elements under stopIndex.
  1355     oneFourthOfSize := oldSize // 4.
  1370     oneFourthOfSize := oldSize // 4.
  1356     index := whereToMakeEmptySlots.
  1371     index := whereToMakeEmptySlots.
  1357     first := firstIndex.
  1372     first := firstIndex.
  1358     last := lastIndex.
  1373     last := lastIndex.
  1359 
  1374 
       
  1375     shiftLeft := shiftRight := false.
  1360     ((first > howMany) and:[first > oneFourthOfSize]) ifTrue:[
  1376     ((first > howMany) and:[first > oneFourthOfSize]) ifTrue:[
  1361         "there is room (>25%) at the beginning"
  1377         "there is room (>25%) at the beginning"
  1362 
  1378 
  1363         shiftLeft := true.
  1379         shiftLeft := true.
  1364     ] ifFalse:[
  1380     ] ifFalse:[
  1365         last < (oneFourthOfSize * 3) ifTrue:[
  1381         ((last + howMany) <= oldSize
       
  1382         and:[last < (oneFourthOfSize * 3)]) ifTrue:[
  1366             shiftRight := true
  1383             shiftRight := true
  1367         ]
  1384         ]
  1368     ].
  1385     ].
  1369 
  1386 
  1370     shiftLeft == true ifTrue:[
  1387     shiftLeft == true ifTrue:[
  1371         "there is room (>25%) at the beginning"
  1388         "there is room at the beginning"
  1372 
  1389 
  1373         index == first ifFalse:[
  1390         index == first ifFalse:[
  1374             contentsArray
  1391             contentsArray
  1375                 replaceFrom:(first - howMany)
  1392                 replaceFrom:(first - howMany)
  1376                 to:(index - howMany - 1)
  1393                 to:(index - howMany - 1)
  1381         firstIndex := first - howMany.
  1398         firstIndex := first - howMany.
  1382         ^ index - howMany
  1399         ^ index - howMany
  1383     ].
  1400     ].
  1384 
  1401 
  1385     shiftRight == true ifTrue:[
  1402     shiftRight == true ifTrue:[
  1386         "there is room (>25%) at the end"
  1403         "there is room at the end"
  1387 
  1404 
  1388         last := last + howMany.
  1405         last := last + howMany.
  1389         index == last ifFalse:[
  1406         index == last ifFalse:[
  1390             contentsArray
  1407             contentsArray
  1391                 replaceFrom:(index + howMany)
  1408                 replaceFrom:(index + howMany)
  1423     lastIndex := last - first + howMany + 1.
  1440     lastIndex := last - first + howMany + 1.
  1424 
  1441 
  1425     "/ return the modified index
  1442     "/ return the modified index
  1426     ^ index - (first - firstIndex).
  1443     ^ index - (first - firstIndex).
  1427 
  1444 
  1428     "Modified: 22.1.1997 / 03:51:33 / cg"
  1445     "Modified: 15.4.1997 / 12:34:16 / cg"
  1429 !
  1446 !
  1430 
  1447 
  1431 makeRoomAtLast
  1448 makeRoomAtLast
  1432     "grow/shift the contents for more room at the end.
  1449     "grow/shift the contents for more room at the end.
  1433      Does not change the logical size.
  1450      Does not change the logical size.
  1630 ! !
  1647 ! !
  1631 
  1648 
  1632 !OrderedCollection class methodsFor:'documentation'!
  1649 !OrderedCollection class methodsFor:'documentation'!
  1633 
  1650 
  1634 version
  1651 version
  1635     ^ '$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.61 1997-02-08 18:31:43 cg Exp $'
  1652     ^ '$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.62 1997-04-15 11:05:18 cg Exp $'
  1636 ! !
  1653 ! !