List.st
author Claus Gittinger <cg@exept.de>
Thu, 29 Jan 1998 10:51:27 +0100
changeset 610 3c9436364fac
parent 608 d7a0efe4105f
child 611 23ef909a3fdb
permissions -rw-r--r--
checkin from browser

"
 COPYRIGHT (c) 1996 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"


OrderedCollection subclass:#List
	instanceVariableNames:'dependents'
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Sequenceable'
!

!List class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1996 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

!

documentation
"
    Lists are mostly OrderedCollection, but keep their dependents
    locally (which is adding a bit of performance - not functionality).

    In ST/X, most functionality is already provided by OrderedCollection,
    so there is not much new stuff found here.
    It has been mostly provided, for ST-80 compatibility,
    where it adds sorting capabilities.

    [see also:]
        Array OrderedCollection

    [author:]
        Claus Gittinger
"

! !

!List methodsFor:'accessing'!

at:anIndex put:anObject
    "set the element at index, to be anIndex.
     Return anObject (sigh).
     In contrast to OrderedCollection, Lists allow putting an object
     right after the last element and auto-grow in this case;
     however, putting 2 or more indices after the last element is 
     reported as an error."

    |idx "{ Class: SmallInteger }"|

    idx := anIndex + firstIndex - 1.
    ((anIndex < 1) or:[idx > lastIndex]) ifTrue:[
        idx == (lastIndex+1) ifTrue:[
            self makeRoomAtLast.
            lastIndex := lastIndex + 1.
        ] ifFalse:[
            ^ self subscriptBoundsError:anIndex
        ]
    ].
    
    contentsArray basicAt:idx put:anObject.
    dependents size ~~ 0 ifTrue:[self changed:#at: with:anIndex].
    ^ anObject

    "Modified: / 28.1.1998 / 16:44:49 / cg"
!

list
    ^ self
! !

!List methodsFor:'adding & removing'!

add:anObject
    "add the argument, anObject to the end of the collection
     Return the argument, anObject."

    super add:anObject.
    dependents size ~~ 0 ifTrue:[self changed:#insert: with:(self size)].
    ^ anObject

    "Modified: / 26.1.1998 / 11:12:17 / cg"
!

add:anObject beforeIndex:index
    "add the argument, anObject to the end of the collection
     Return the argument, anObject."

    |idx physicalIndex|

    physicalIndex := index + firstIndex - 1.
    physicalIndex > lastIndex ifTrue:[
        physicalIndex == (lastIndex+1) ifTrue:[
            self addLast:anObject.
            ^ self
        ].
        ^ self subscriptBoundsError:index
    ].

    idx := self makeRoomAtIndex:physicalIndex.
    "/ notice: the above may change firstIndex
    contentsArray basicAt:idx put:anObject.

    dependents size ~~ 0 ifTrue:[self changed:#insert: with:(self size)].
    ^ anObject

    "Modified: / 29.1.1998 / 10:45:14 / cg"
!

addAll:aCollection beforeIndex:index
    "insert all elements of the argument
     Return the receiver."

    |count idx|

    count := aCollection size.
    idx := self makeRoomAtIndex:(index + firstIndex - 1) for:count.

    aCollection isSequenceable ifTrue:[
        "/ we are lucky - that thing can count & do bulk copies
        "/ notice: the above may change firstIndex
        contentsArray replaceFrom:idx to:(idx + count - 1) with:aCollection startingAt:1.
    ] ifFalse:[
        "/ avoid zillions of change messages ...
        aCollection do:[:element |
            contentsArray at:idx put:element.
            idx := idx + 1.
        ].
    ].
    self changed: #insertCollection: with:(Array with:index with:(aCollection size))

    "Modified: / 29.1.1998 / 10:49:14 / cg"
!

addFirst:anObject
    "add the argument, anObject to the beginning of the collection.
     Return the argument, anObject."

    super addFirst:anObject.
    dependents size ~~ 0 ifTrue:[self changed:#insert: with:1].
    ^ anObject

    "Modified: / 26.1.1998 / 11:13:14 / cg"
!

addLast:anObject
    "add the argument, anObject to the end of the collection.
     Return the argument, anObject."

    self add:anObject.
    ^ anObject

    "Created: / 26.1.1998 / 11:10:55 / cg"
!

removeAll
    "remove all elements from the collection.
     Returns the receiver."

     |prevSize|

     prevSize := self size.
     super removeAll.

     prevSize ~~ 0 ifTrue:[
        self changed:#removeFrom: with:(Array with:1 with:prevSize)
     ]

    "Modified: / 26.1.1998 / 11:14:07 / cg"
!

removeFirst
    "remove the first element from the collection; return the element."

     |deletedObject|

     deletedObject := super removeFirst.
     dependents size ~~ 0 ifTrue:[self changed:#remove: with:1].
     ^ deletedObject

    "Modified: / 26.1.1998 / 11:14:17 / cg"
!

removeFirst:n
    "remove the first n elements from the collection; 
     Return a collection containing the removed elements."

    |deletedObjects|

    deletedObjects := super removeFirst:n.
    dependents size ~~ 0 ifTrue:[self changed:#removeFrom: with:(Array with:1 with:n)].
    ^ deletedObjects

    "Modified: / 26.1.1998 / 11:14:38 / cg"
!

removeFromIndex:startIndex toIndex:stopIndex
    "remove the elements stored under startIndex up to and including
     the elements under stopIndex"

     |deletedObjects|

     deletedObjects := super removeFromIndex:startIndex toIndex:stopIndex.
     self changed:#removeFrom: with:(Array with:startIndex with:stopIndex).
     ^ deletedObjects

!

removeLast
    "remove the last element from the collection; return the element"

     |deletedObject|

     deletedObject :=  super removeLast.
     dependents size ~~ 0 ifTrue:[self changed:#remove: with:(1 + self size)].
     ^ deletedObject

    "Modified: / 26.1.1998 / 11:14:52 / cg"
!

removeLast:n
    "remove the last n elements from the collection; 
     Return a collection of removed  elements."

    |deletedObjects stop|

    stop := self size.
    deletedObjects := super removeLast:n.
    self changed:#removeFrom: with:(Array with:(stop - n + 1) with:stop).
    ^ deletedObjects

    "Modified: / 29.1.1998 / 10:42:37 / cg"
! !

!List methodsFor:'converting'!

asList
    ^ self

    "Created: 14.2.1997 / 16:25:55 / cg"
! !

!List methodsFor:'dependents access'!

dependents 
    "return the dependents of the receiver"

    ^ dependents ? #()

    "Created: / 14.2.1997 / 16:05:49 / cg"
    "Modified: / 27.10.1997 / 19:37:33 / cg"
!

dependents:aCollection
    "set the dependents of the receiver"

    dependents := aCollection

    "Created: 14.2.1997 / 16:05:58 / cg"
!

nonWeakDependents
    "return a Collection of dependents - nil if there is none.
     Since all dependencies are nonWeak in List, this is a dummy."

    ^ self dependents

    "Created: / 19.4.1996 / 10:29:43 / cg"
    "Modified: / 26.1.1998 / 11:25:41 / cg"
! !

!List methodsFor:'filling & replacing'!

replaceFrom:start to:stop with:aCollection startingAt:repStart
    "replace elements in the receiver between index start and stop,
     with elements  taken from replacementCollection starting at repStart.
     Redefined - can be done faster"

     |obj|

     obj := super replaceFrom:start to:stop with:aCollection startingAt:repStart.

     dependents notNil ifTrue:[
         self changed:#replace: with:(Array with:start with:stop)
     ].
     ^ obj

! !

!List class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/List.st,v 1.7 1998-01-29 09:51:27 cg Exp $'
! !