AutoResizingOrderedCollectionWithDefault.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 4331 6b08efe6d794
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4331
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
     1
"
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
     2
 COPYRIGHT (c) 2016 by Claus Gittinger / eXept Software AG
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
     3
              All Rights Reserved
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
     4
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
     5
 This software is furnished under a license and may be used
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
     6
 only in accordance with the terms of that license and with the
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
     8
 be provided or otherwise made available to, or used by, any
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
     9
 other person.  No title to or ownership of the software is
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
    10
 hereby transferred.
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
    11
"
3690
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
"{ NameSpace: Smalltalk }"
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
AutoResizingOrderedCollection subclass:#AutoResizingOrderedCollectionWithDefault
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	instanceVariableNames:'defaultValue'
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	classVariableNames:''
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	poolDictionaries:''
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
	category:'Collections-Sequenceable'
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
!
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
!AutoResizingOrderedCollectionWithDefault class methodsFor:'documentation'!
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
4331
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
    25
copyright
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
    26
"
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
    27
 COPYRIGHT (c) 2016 by Claus Gittinger / eXept Software AG
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
    28
              All Rights Reserved
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
    29
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
    30
 This software is furnished under a license and may be used
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
    31
 only in accordance with the terms of that license and with the
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
    33
 be provided or otherwise made available to, or used by, any
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
    34
 other person.  No title to or ownership of the software is
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
    35
 hereby transferred.
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
    36
"
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
    37
!
6b08efe6d794 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4136
diff changeset
    38
3690
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
documentation
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
"
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
    I am an ordered collection which automatically resizes if elements
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
    are added beyond the size. 
4136
fad79a4ef0fc #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3699
diff changeset
    43
    I.e. if #at:put: is sent for indexes > the current size, the receiver grows to
3690
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
    the required index and missing fields are implicitly filled with a default value.
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
    Queries for non-existing elements are anwered with the default value.
4136
fad79a4ef0fc #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3699
diff changeset
    46
fad79a4ef0fc #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3699
diff changeset
    47
    [see also:]
fad79a4ef0fc #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3699
diff changeset
    48
        OrderedCollection
fad79a4ef0fc #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3699
diff changeset
    49
        Array
fad79a4ef0fc #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3699
diff changeset
    50
        SparseArray
3690
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
"
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
!
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
examples
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
"
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
    |coll|
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
    coll := AutoResizingOrderedCollectionWithDefault newWithDefaultValue:99.
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
    coll at:4 put:'four'.
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
    coll at:8 put:'eight'.
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
    coll at:9
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
"
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
! !
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
!AutoResizingOrderedCollectionWithDefault methodsFor:'accessing'!
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
at:index
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
    "fetch an object at index.
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
     If index is beyond the actual size, return the default value
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
     (i.e. this is an alias for at:index ifAbsent:[default value])"
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
    index > self size ifTrue:[^ defaultValue].
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
    ^ super at:index
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
!
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
at:index put:anObject
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
    "store an object at index.
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
     If required, grow the receiver to ensure that index is valid"
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
    |oldSize|
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
    
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
    index > (oldSize := self size) ifTrue:[
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
        self grow:index.
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
        self from:oldSize+1 to:index put:defaultValue.
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
    ].
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
    super at:index put:anObject
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
! !
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
!AutoResizingOrderedCollectionWithDefault methodsFor:'initialization'!
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
setDefaultValue:v
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
    defaultValue := v
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
! !
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
!AutoResizingOrderedCollectionWithDefault class methodsFor:'documentation'!
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
version
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
    ^ '$Header$'
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
!
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
version_CVS
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
    ^ '$Header$'
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
! !
759c80ab346d initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104