author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Thu, 29 Sep 2011 16:44:37 +0100 | |
branch | jv |
changeset 17869 | 9610c6c94e71 |
parent 17865 | 598963c6ff8e |
child 17892 | d86c8bd5ece3 |
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 |
||
154 | 14 |
LookupKey subclass:#Association |
1283 | 15 |
instanceVariableNames:'value' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Collections-Support' |
|
1 | 19 |
! |
20 |
||
68 | 21 |
!Association class methodsFor:'documentation'! |
1 | 22 |
|
82 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
379 | 26 |
All Rights Reserved |
82 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
68 | 37 |
documentation |
38 |
" |
|
92 | 39 |
Associations are a key-value pair; conceptionally, they are the elements of |
40 |
Dictionaries - storing value under the key. (However, the real implementation |
|
41 |
of Dictionary is different). |
|
154 | 42 |
Taken by itself, an association is not very useful. |
1 | 43 |
|
1283 | 44 |
[Instance variables:] |
68 | 45 |
|
1283 | 46 |
key <Object> the key (inherited) |
47 |
value <Object> the value |
|
48 |
||
1290 | 49 |
[author:] |
50 |
Claus Gittinger |
|
51 |
||
1283 | 52 |
[See also:] |
53 |
Dictionary IdentityDictionary |
|
68 | 54 |
" |
55 |
! ! |
|
1 | 56 |
|
57 |
!Association class methodsFor:'instance creation'! |
|
58 |
||
59 |
key:aKey value:aValue |
|
60 |
"return a new Association" |
|
61 |
||
62 |
^ self basicNew key:aKey value:aValue |
|
63 |
! ! |
|
64 |
||
65 |
!Association methodsFor:'accessing'! |
|
66 |
||
607 | 67 |
key:aKey value:aValue |
68 |
"set both the key and value of the receiver. |
|
69 |
Return the receiver" |
|
70 |
||
71 |
key := aKey. |
|
72 |
value := aValue |
|
73 |
! |
|
74 |
||
1 | 75 |
value |
76 |
"return the value of the association" |
|
77 |
||
78 |
^ value |
|
79 |
! |
|
80 |
||
81 |
value:anObject |
|
82 |
"set the value of the receiver to be anObject. |
|
83 |
Return the receiver" |
|
84 |
||
85 |
value := anObject |
|
86 |
! ! |
|
87 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
88 |
!Association methodsFor:'collection mimicri'! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
89 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
90 |
keysAndValuesDo:aBlock |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
91 |
"evaluate the argument, aBlock for each element in the collection. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
92 |
Pass both index and element to the block. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
93 |
Added here to allow for an association to be added via the comma operation |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
94 |
to an existing dictionary" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
95 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
96 |
aBlock value:key value:value. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
97 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
98 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
99 |
|d1 d2| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
100 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
101 |
d1 := Dictionary new. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
102 |
d1 at:#a put:'aaa'. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
103 |
d1 at:#b put:'bbb'. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
104 |
d2 := d1 , (#c -> 'ccc'). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
105 |
d2 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
106 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
107 |
! ! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
108 |
|
1 | 109 |
!Association methodsFor:'comparing'! |
110 |
||
111 |
= anAssociation |
|
68 | 112 |
"return true if the receiver equals the argument. |
154 | 113 |
Notice, that this compares both key AND value. |
114 |
Time will show if this is ok." |
|
43 | 115 |
|
154 | 116 |
self species == anAssociation species ifTrue:[ |
379 | 117 |
(anAssociation key = key) ifTrue:[ |
118 |
^ anAssociation value = value |
|
119 |
] |
|
1 | 120 |
]. |
121 |
^ false |
|
54 | 122 |
! |
123 |
||
124 |
hash |
|
125 |
"return an integer useful for hashing on the receiver; |
|
126 |
redefined since = is redefined here." |
|
127 |
||
128 |
^ (key hash) bitXor:(value hash) |
|
1 | 129 |
! ! |
130 |
||
131 |
!Association methodsFor:'printing & storing'! |
|
132 |
||
607 | 133 |
displayString |
134 |
"return a string containing a printable representation |
|
135 |
of the receiver for displaying - redefined to use display string on |
|
136 |
the components for nicer look" |
|
137 |
||
138 |
^ key displayString , '->' , value displayString |
|
139 |
! |
|
140 |
||
54 | 141 |
printOn:aStream |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
142 |
"append a printed representation of the receiver to aStream" |
1 | 143 |
|
54 | 144 |
key printOn:aStream. |
145 |
aStream nextPutAll:'->'. |
|
146 |
value printOn:aStream |
|
5234 | 147 |
! |
148 |
||
149 |
storeOn:aStream |
|
150 |
"append a string representing an expression to reconstruct the receiver |
|
151 |
to aStream." |
|
152 |
||
153 |
aStream nextPut:$(. |
|
154 |
key storeOn:aStream. |
|
155 |
aStream nextPutAll:'->'. |
|
156 |
value storeOn:aStream. |
|
157 |
aStream nextPut:$). |
|
158 |
||
159 |
"Created: / 3.2.2000 / 22:31:28 / cg" |
|
607 | 160 |
! ! |
1 | 161 |
|
1397 | 162 |
!Association methodsFor:'queries'! |
163 |
||
164 |
isAssociation |
|
165 |
"return true, if the receiver is some kind of association; |
|
166 |
true is returned here - the method is redefined from Object." |
|
167 |
||
168 |
^ true |
|
169 |
||
170 |
"Created: 14.5.1996 / 17:04:13 / cg" |
|
171 |
! ! |
|
172 |
||
629 | 173 |
!Association class methodsFor:'documentation'! |
174 |
||
175 |
version |
|
17869 | 176 |
^ '$Id: Association.st 10700 2011-09-29 15:44:37Z vranyj1 $' |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
177 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
178 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
179 |
version_CVS |
17845 | 180 |
^ '§Header: /cvs/stx/stx/libbasic/Association.st,v 1.23 2009/10/06 15:16:10 cg Exp §' |
17865 | 181 |
! |
182 |
||
183 |
version_SVN |
|
17869 | 184 |
^ '$Id: Association.st 10700 2011-09-29 15:44:37Z vranyj1 $' |
17846 | 185 |
! ! |