3690
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
"{ NameSpace: Smalltalk }"
|
|
4 |
|
|
5 |
AutoResizingOrderedCollection subclass:#AutoResizingOrderedCollectionWithDefault
|
|
6 |
instanceVariableNames:'defaultValue'
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'Collections-Sequenceable'
|
|
10 |
!
|
|
11 |
|
|
12 |
!AutoResizingOrderedCollectionWithDefault class methodsFor:'documentation'!
|
|
13 |
|
|
14 |
documentation
|
|
15 |
"
|
|
16 |
I am an ordered collection which automatically resizes if elements
|
|
17 |
are added beyond the size.
|
|
18 |
I.e. if at:put: is sent for indexes > the current size, the receiver grows to
|
|
19 |
the required index and missing fields are implicitly filled with a default value.
|
|
20 |
Queries for non-existing elements are anwered with the default value.
|
|
21 |
"
|
|
22 |
!
|
|
23 |
|
|
24 |
examples
|
|
25 |
"
|
|
26 |
|coll|
|
|
27 |
|
|
28 |
coll := AutoResizingOrderedCollectionWithDefault newWithDefaultValue:99.
|
|
29 |
coll at:4 put:'four'.
|
|
30 |
coll at:8 put:'eight'.
|
|
31 |
coll at:9
|
|
32 |
"
|
|
33 |
! !
|
|
34 |
|
|
35 |
!AutoResizingOrderedCollectionWithDefault methodsFor:'accessing'!
|
|
36 |
|
|
37 |
at:index
|
|
38 |
"fetch an object at index.
|
|
39 |
If index is beyond the actual size, return the default value
|
|
40 |
(i.e. this is an alias for at:index ifAbsent:[default value])"
|
|
41 |
|
|
42 |
index > self size ifTrue:[^ defaultValue].
|
|
43 |
^ super at:index
|
|
44 |
!
|
|
45 |
|
|
46 |
at:index put:anObject
|
|
47 |
"store an object at index.
|
|
48 |
If required, grow the receiver to ensure that index is valid"
|
|
49 |
|
|
50 |
|oldSize|
|
|
51 |
|
|
52 |
index > (oldSize := self size) ifTrue:[
|
|
53 |
self grow:index.
|
|
54 |
self from:oldSize+1 to:index put:defaultValue.
|
|
55 |
].
|
|
56 |
super at:index put:anObject
|
|
57 |
! !
|
|
58 |
|
|
59 |
!AutoResizingOrderedCollectionWithDefault methodsFor:'initialization'!
|
|
60 |
|
|
61 |
setDefaultValue:v
|
|
62 |
defaultValue := v
|
|
63 |
! !
|
|
64 |
|
|
65 |
!AutoResizingOrderedCollectionWithDefault class methodsFor:'documentation'!
|
|
66 |
|
|
67 |
version
|
|
68 |
^ '$Header$'
|
|
69 |
!
|
|
70 |
|
|
71 |
version_CVS
|
|
72 |
^ '$Header$'
|
|
73 |
! !
|
|
74 |
|