author | Claus Gittinger <cg@exept.de> |
Sun, 05 Jan 2020 17:27:00 +0100 | |
changeset 25177 | 1c618f3d51f5 |
parent 21543 | 5be3bab4f1a8 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
379 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
5792 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
21543 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
154 | 16 |
LookupKey subclass:#Association |
1283 | 17 |
instanceVariableNames:'value' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Support' |
|
1 | 21 |
! |
22 |
||
68 | 23 |
!Association class methodsFor:'documentation'! |
1 | 24 |
|
82 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
379 | 28 |
All Rights Reserved |
82 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
68 | 39 |
documentation |
40 |
" |
|
92 | 41 |
Associations are a key-value pair; conceptionally, they are the elements of |
42 |
Dictionaries - storing value under the key. (However, the real implementation |
|
43 |
of Dictionary is different). |
|
154 | 44 |
Taken by itself, an association is not very useful. |
1 | 45 |
|
1283 | 46 |
[Instance variables:] |
68 | 47 |
|
1283 | 48 |
key <Object> the key (inherited) |
49 |
value <Object> the value |
|
50 |
||
1290 | 51 |
[author:] |
52 |
Claus Gittinger |
|
53 |
||
1283 | 54 |
[See also:] |
55 |
Dictionary IdentityDictionary |
|
68 | 56 |
" |
57 |
! ! |
|
1 | 58 |
|
59 |
!Association class methodsFor:'instance creation'! |
|
60 |
||
61 |
key:aKey value:aValue |
|
62 |
"return a new Association" |
|
63 |
||
64 |
^ self basicNew key:aKey value:aValue |
|
65 |
! ! |
|
66 |
||
67 |
!Association methodsFor:'accessing'! |
|
68 |
||
607 | 69 |
key:aKey value:aValue |
70 |
"set both the key and value of the receiver. |
|
71 |
Return the receiver" |
|
72 |
||
73 |
key := aKey. |
|
74 |
value := aValue |
|
75 |
! |
|
76 |
||
1 | 77 |
value |
78 |
"return the value of the association" |
|
79 |
||
80 |
^ value |
|
81 |
! |
|
82 |
||
83 |
value:anObject |
|
84 |
"set the value of the receiver to be anObject. |
|
85 |
Return the receiver" |
|
86 |
||
87 |
value := anObject |
|
88 |
! ! |
|
89 |
||
11908
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
90 |
!Association methodsFor:'collection mimicri'! |
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
91 |
|
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
92 |
keysAndValuesDo:aBlock |
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
93 |
"evaluate the argument, aBlock for each element in the collection. |
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
94 |
Pass both index and element to the block. |
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
95 |
Added here to allow for an association to be added via the comma operation |
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
96 |
to an existing dictionary" |
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
97 |
|
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
98 |
aBlock value:key value:value. |
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
99 |
|
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
100 |
" |
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
101 |
|d1 d2| |
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
102 |
|
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
103 |
d1 := Dictionary new. |
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
104 |
d1 at:#a put:'aaa'. |
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
105 |
d1 at:#b put:'bbb'. |
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
106 |
d2 := d1 , (#c -> 'ccc'). |
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
107 |
d2 |
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
108 |
" |
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
109 |
! ! |
1034accdce84
+ keysAndValuesDo: for collection mimicri
Claus Gittinger <cg@exept.de>
parents:
5793
diff
changeset
|
110 |
|
1 | 111 |
!Association methodsFor:'comparing'! |
112 |
||
113 |
= anAssociation |
|
68 | 114 |
"return true if the receiver equals the argument. |
154 | 115 |
Notice, that this compares both key AND value. |
116 |
Time will show if this is ok." |
|
43 | 117 |
|
16767 | 118 |
^ self species == anAssociation species |
119 |
and:[anAssociation key = key and:[anAssociation value = value]] |
|
54 | 120 |
! |
121 |
||
122 |
hash |
|
123 |
"return an integer useful for hashing on the receiver; |
|
124 |
redefined since = is redefined here." |
|
125 |
||
126 |
^ (key hash) bitXor:(value hash) |
|
1 | 127 |
! ! |
128 |
||
129 |
!Association methodsFor:'printing & storing'! |
|
130 |
||
14118
c4375f1867f6
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
12161
diff
changeset
|
131 |
displayOn:aGCOrStream |
c4375f1867f6
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
12161
diff
changeset
|
132 |
"Compatibility |
c4375f1867f6
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
12161
diff
changeset
|
133 |
append a printed desription on some stream (Dolphin, Squeak) |
c4375f1867f6
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
12161
diff
changeset
|
134 |
OR: |
c4375f1867f6
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
12161
diff
changeset
|
135 |
display the receiver in a graphicsContext at 0@0 (ST80). |
c4375f1867f6
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
12161
diff
changeset
|
136 |
This method allows for any object to be displayed in some view |
c4375f1867f6
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
12161
diff
changeset
|
137 |
(although the fallBack is to display its printString ...)" |
607 | 138 |
|
14118
c4375f1867f6
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
12161
diff
changeset
|
139 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
21543 | 140 |
"/ old ST80 means: draw-yourself on a GC. |
16740 | 141 |
(aGCOrStream isStream) ifFalse:[ |
14118
c4375f1867f6
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
12161
diff
changeset
|
142 |
^ super displayOn:aGCOrStream |
c4375f1867f6
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
12161
diff
changeset
|
143 |
]. |
c4375f1867f6
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
12161
diff
changeset
|
144 |
|
c4375f1867f6
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
12161
diff
changeset
|
145 |
key displayOn:aGCOrStream. |
c4375f1867f6
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
12161
diff
changeset
|
146 |
aGCOrStream nextPutAll:'->'. |
c4375f1867f6
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
12161
diff
changeset
|
147 |
value displayOn:aGCOrStream. |
21543 | 148 |
|
149 |
"Modified (comment): / 22-02-2017 / 16:52:13 / cg" |
|
607 | 150 |
! |
151 |
||
54 | 152 |
printOn:aStream |
12161 | 153 |
"append a printed representation of the receiver to aStream" |
1 | 154 |
|
54 | 155 |
key printOn:aStream. |
156 |
aStream nextPutAll:'->'. |
|
157 |
value printOn:aStream |
|
5234 | 158 |
! |
159 |
||
160 |
storeOn:aStream |
|
161 |
"append a string representing an expression to reconstruct the receiver |
|
162 |
to aStream." |
|
163 |
||
164 |
aStream nextPut:$(. |
|
165 |
key storeOn:aStream. |
|
166 |
aStream nextPutAll:'->'. |
|
167 |
value storeOn:aStream. |
|
168 |
aStream nextPut:$). |
|
169 |
||
170 |
"Created: / 3.2.2000 / 22:31:28 / cg" |
|
607 | 171 |
! ! |
1 | 172 |
|
1397 | 173 |
!Association methodsFor:'queries'! |
174 |
||
175 |
isAssociation |
|
176 |
"return true, if the receiver is some kind of association; |
|
177 |
true is returned here - the method is redefined from Object." |
|
178 |
||
179 |
^ true |
|
180 |
||
181 |
"Created: 14.5.1996 / 17:04:13 / cg" |
|
182 |
! ! |
|
183 |
||
629 | 184 |
!Association class methodsFor:'documentation'! |
185 |
||
186 |
version |
|
21543 | 187 |
^ '$Header$' |
12161 | 188 |
! |
189 |
||
190 |
version_CVS |
|
21543 | 191 |
^ '$Header$' |
629 | 192 |
! ! |
16740 | 193 |