List.st
author Claus Gittinger <cg@exept.de>
Mon, 02 Feb 1998 19:07:57 +0100
changeset 613 bc304634b22d
parent 612 cc2d6bd50cdc
child 632 7008f9f8ad20
permissions -rw-r--r--
fixed change-message

"
 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 notNil ifTrue:[self changed:#insert: with:(self size)].
    ^ anObject

    "Modified: / 29.1.1998 / 10:52:32 / 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 notNil ifTrue:[self changed:#insert: with:index].
    ^ anObject

    "Modified: / 2.2.1998 / 19:06:21 / 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.
        ].
    ].
    dependents notNil ifTrue:[
        self changed: #insertCollection: with:(Array with:index with:(aCollection size))
    ]

    "Modified: / 29.1.1998 / 10:52:57 / cg"
!

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

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

    "Modified: / 29.1.1998 / 10:53:09 / 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:[
        dependents notNil ifTrue:[
            self changed:#removeFrom: with:(Array with:1 with:prevSize)
        ]
     ]

    "Modified: / 29.1.1998 / 10:53:28 / cg"
!

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

     |deletedObject|

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

    "Modified: / 29.1.1998 / 10:53:36 / cg"
!

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

    |deletedObjects|

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

    "Modified: / 29.1.1998 / 10:53:40 / 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.
    dependents notNil ifTrue:[
       self changed:#removeFrom: with:(Array with:startIndex with:stopIndex).
    ].
    ^ deletedObjects

    "Modified: / 29.1.1998 / 10:54:03 / cg"
!

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

     |deletedObject|

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

    "Modified: / 29.1.1998 / 10:54:15 / 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.
    dependents notNil ifTrue:[
        self changed:#removeFrom: with:(Array with:(stop - n + 1) with:stop).
    ].
    ^ deletedObjects

    "Modified: / 29.1.1998 / 10:54:25 / 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"

    aCollection size == 0 ifTrue:[
        dependents := nil
    ] ifFalse:[
        dependents := aCollection.
    ].

    "Created: / 14.2.1997 / 16:05:58 / cg"
    "Modified: / 29.1.1998 / 10:54:52 / cg"
!

nonWeakDependents
    "return a Collection of dependents - empty 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: / 30.1.1998 / 14:06:12 / 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.10 1998-02-02 18:07:57 cg Exp $'
! !