AutoResizingOrderedCollectionWithDefault.st
author Claus Gittinger <cg@exept.de>
Tue, 25 Jun 2019 14:28:51 +0200
changeset 5050 44fa8672d102
parent 4331 6b08efe6d794
permissions -rw-r--r--
#DOCUMENTATION by cg class: SharedQueue comment/format in: #next #nextWithTimeout:

"
 COPYRIGHT (c) 2016 by Claus Gittinger / 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.
"
"{ Package: 'stx:libbasic2' }"

"{ NameSpace: Smalltalk }"

AutoResizingOrderedCollection subclass:#AutoResizingOrderedCollectionWithDefault
	instanceVariableNames:'defaultValue'
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Sequenceable'
!

!AutoResizingOrderedCollectionWithDefault class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2016 by Claus Gittinger / 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
"
    I am an ordered collection which automatically resizes if elements
    are added beyond the size. 
    I.e. if #at:put: is sent for indexes > the current size, the receiver grows to
    the required index and missing fields are implicitly filled with a default value.
    Queries for non-existing elements are anwered with the default value.

    [see also:]
        OrderedCollection
        Array
        SparseArray
"
!

examples
"
    |coll|

    coll := AutoResizingOrderedCollectionWithDefault newWithDefaultValue:99.
    coll at:4 put:'four'.
    coll at:8 put:'eight'.
    coll at:9
"
! !

!AutoResizingOrderedCollectionWithDefault methodsFor:'accessing'!

at:index
    "fetch an object at index.
     If index is beyond the actual size, return the default value
     (i.e. this is an alias for at:index ifAbsent:[default value])"

    index > self size ifTrue:[^ defaultValue].
    ^ super at:index
!

at:index put:anObject
    "store an object at index.
     If required, grow the receiver to ensure that index is valid"

    |oldSize|
    
    index > (oldSize := self size) ifTrue:[
        self grow:index.
        self from:oldSize+1 to:index put:defaultValue.
    ].
    super at:index put:anObject
! !

!AutoResizingOrderedCollectionWithDefault methodsFor:'initialization'!

setDefaultValue:v
    defaultValue := v
! !

!AutoResizingOrderedCollectionWithDefault class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !