1113 | | This is implemented by putting all items that are deleted during iteration |
1114 | | are not actually deleted, but only *marked* for deletion. Note, however, |
1115 | | that they behave like items that actually *are* deleted. E.g., it is not |
1116 | | possible to delete the same item twice:: |
1117 | | |
1118 | | sage: class Val: pass |
1119 | | sage: V = [Val() for n in range(3)] |
1120 | | sage: D = sage.misc.weak_dict.WeakValueDictionary(enumerate(V)) |
1121 | | sage: for k,v in D.iteritems(): |
1122 | | ....: print k in D.keys() |
1123 | | ....: del D[k] |
1124 | | ....: print k in D.keys() |
1125 | | ....: try: |
1126 | | ....: del D[k] |
1127 | | ....: except KeyError: |
1128 | | ....: print "due error" |
1129 | | True |
1130 | | False |
1131 | | due error |
1132 | | True |
1133 | | False |
1134 | | due error |
1135 | | True |
1136 | | False |
1137 | | due error |
| 1113 | This is implemented by only marking entries for deletion if their values |
| 1114 | become deallocated while an iterator is active. Once the iterator finishes, |
| 1115 | the keys are deallocated. Note that because the weakrefs will be dead, the |
| 1116 | keys in question do not appear to be in the dictionary anymore:: |
| 1117 | |
| 1118 | sage: from sage.misc.weak_dict import WeakValueDictionary |
| 1119 | sage: K = [frozenset([i]) for i in range(11)] |
| 1120 | sage: D = WeakValueDictionary((K[i],K[i+1]) for i in range(10)) |
| 1121 | sage: k = K[10] |
| 1122 | sage: del K |
| 1123 | sage: i = D.iterkeys(); d = i.next(); del d |
| 1124 | sage: len(D.keys()) |
| 1125 | 10 |
| 1126 | sage: del k |
| 1127 | |
| 1128 | At this point, the entry for `k` appears to have disappeared out of `D`, |
| 1129 | but a reference to `k` is still kept, so the other entries in `D` survive:: |
1153 | | sage: import sage.misc.weak_dict |
1154 | | sage: class Val: pass |
1155 | | sage: V = [Val() for n in range(3)] |
1156 | | sage: D = sage.misc.weak_dict.WeakValueDictionary(enumerate(V)) |
1157 | | sage: for k,v in D.iteritems(): # indirect doctest |
1158 | | ....: k in D.keys() |
1159 | | ....: del D[k] |
1160 | | ....: k in D.keys() |
1161 | | True |
1162 | | False |
1163 | | True |
1164 | | False |
1165 | | True |
1166 | | False |
| 1155 | sage: from sage.misc.weak_dict import WeakValueDictionary |
| 1156 | sage: K = [frozenset([i]) for i in range(11)] |
| 1157 | sage: D = WeakValueDictionary((K[i],K[i+1]) for i in range(10)) |
| 1158 | sage: k = K[10] |
| 1159 | sage: del K |
| 1160 | sage: i = D.iterkeys(); d = i.next(); del d |
| 1161 | sage: len(D.keys()) |
| 1162 | 10 |
| 1163 | sage: del k |
| 1164 | sage: len(D.keys()) |
| 1165 | 9 |
| 1166 | sage: del i |
| 1167 | sage: len(D.keys()) |
| 1168 | 0 |
1178 | | sage: import sage.misc.weak_dict |
1179 | | sage: class Val: pass |
1180 | | sage: V = [Val() for n in range(3)] |
1181 | | sage: D = sage.misc.weak_dict.WeakValueDictionary(enumerate(V)) |
1182 | | sage: for k,v in D.iteritems(): # indirect doctest |
1183 | | ....: k in D.keys() |
1184 | | ....: del D[k] |
1185 | | ....: k in D.keys() |
1186 | | True |
1187 | | False |
1188 | | True |
1189 | | False |
1190 | | True |
1191 | | False |
| 1180 | sage: from sage.misc.weak_dict import WeakValueDictionary |
| 1181 | sage: K = [frozenset([i]) for i in range(11)] |
| 1182 | sage: D = WeakValueDictionary((K[i],K[i+1]) for i in range(10)) |
| 1183 | sage: k = K[10] |
| 1184 | sage: del K |
| 1185 | sage: i = D.iterkeys(); d = i.next(); del d |
| 1186 | sage: len(D.keys()) |
| 1187 | 10 |
| 1188 | sage: del k |
| 1189 | sage: len(D.keys()) |
| 1190 | 9 |
| 1191 | sage: del i |
| 1192 | sage: len(D.keys()) |
| 1193 | 0 |
1205 | | sage: import sage.misc.weak_dict |
1206 | | sage: class Val: pass |
1207 | | sage: V = [Val() for n in range(3)] |
1208 | | sage: D = sage.misc.weak_dict.WeakValueDictionary(enumerate(V)) |
1209 | | sage: for k,v in D.iteritems(): # indirect doctest |
1210 | | ....: k in D.keys() |
1211 | | ....: del D[k] |
1212 | | ....: k in D.keys() |
1213 | | True |
1214 | | False |
1215 | | True |
1216 | | False |
1217 | | True |
1218 | | False |
| 1207 | sage: from sage.misc.weak_dict import WeakValueDictionary |
| 1208 | sage: K = [frozenset([i]) for i in range(11)] |
| 1209 | sage: D = WeakValueDictionary((K[i],K[i+1]) for i in range(10)) |
| 1210 | sage: k = K[10] |
| 1211 | sage: del K |
| 1212 | sage: i = D.iterkeys(); d = i.next(); del d |
| 1213 | sage: len(D.keys()) |
| 1214 | 10 |
| 1215 | sage: del k |
| 1216 | sage: len(D.keys()) |
| 1217 | 9 |
| 1218 | sage: del i |
| 1219 | sage: len(D.keys()) |
| 1220 | 0 |