OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 class _Expando<T> implements Expando<T> { | |
6 final String name; | |
7 | |
8 const _Expando([String this.name]); | |
9 | |
10 T operator[](Object object) { | |
11 checkType(object); | |
12 var weak_property = find(this); | |
13 var do_compact = false; | |
14 var result = null; | |
15 var list = weak_property.value; | |
16 for (int i = 0; i < list.length; ++i) { | |
17 if (list[i] !== null) { | |
Ivan Posva
2012/08/21 23:19:40
How can this ever be false?
cshapiro
2012/08/22 02:37:35
This was needed before I added compaction. Since
| |
18 var key = list[i].key; | |
19 if (key === object) { | |
20 result = list[i].value; | |
21 break; | |
22 } | |
23 if (key === null) { | |
24 do_compact = true; | |
25 list[i] = null; | |
26 } | |
27 } | |
28 } | |
29 if (do_compact) { | |
30 weak_property.value = list.filter((e) => (e === null)); | |
31 } | |
32 return result; | |
33 } | |
34 | |
35 void operator[]=(Object object, T value) { | |
36 checkType(object); | |
37 var weak_property = find(this); | |
38 var do_compact = false; | |
39 var list = weak_property.value; | |
40 int i = 0; | |
41 for (; i < list.length; ++i) { | |
42 if (list[i] !== null) { | |
43 var key = list[i].key; | |
44 if (key === object) { | |
45 break; | |
46 } | |
47 if (key === null) { | |
48 do_compact = true; | |
49 list[i] = null; | |
50 } | |
51 } | |
52 } | |
53 if (i !== list.length && value === null) { | |
54 do_compact = true; | |
55 list[i] = null; | |
56 } else { | |
57 var weak_property = new WeakProperty(object, value); | |
Ivan Posva
2012/08/21 23:19:40
It is confusing to be shadowing the outer weak_pro
cshapiro
2012/08/22 02:37:35
I removed this as part of an unrelated simplificat
| |
58 if (i !== list.length) { | |
59 list[i] = weak_property; | |
Ivan Posva
2012/08/21 23:19:40
Why not "list[i].value = value"?
cshapiro
2012/08/22 02:37:35
While I added a setter for the value field, my pre
| |
60 } else { | |
61 list.add(weak_property); | |
62 } | |
63 } | |
64 if (do_compact) { | |
65 weak_property.value = list.filter((e) => (e === null)); | |
66 } | |
67 } | |
68 | |
69 String toString() => "Expando:$name"; | |
70 | |
71 static checkType(object) { | |
72 if (object === null) { | |
73 throw new NullPointerException(); | |
74 } | |
75 if (object is bool || object is num || object is String) { | |
76 throw new IllegalArgumentException(object); | |
77 } | |
78 } | |
79 | |
80 static find(expando) { | |
81 if (data === null) data = new List(); | |
82 var do_compact = false; | |
83 int i = 0; | |
84 for (; i < data.length; ++i) { | |
85 if (data[i] !== null) { | |
86 var key = data[i].key; | |
87 if (key == expando) { | |
88 break; | |
89 } | |
90 if (key === null) { | |
91 do_compact = true; | |
92 data[i] = null; | |
93 } | |
94 } | |
95 } | |
96 if (i == data.length) { | |
97 data.add(new WeakProperty(expando, new List())); | |
98 } | |
99 var result = data[i]; | |
100 if (do_compact) { | |
101 data = data.filter((e) => (e === null)); | |
102 } | |
103 return result; | |
104 } | |
105 | |
106 static List data; | |
107 } | |
OLD | NEW |