OLD | NEW |
1 | 1 |
2 /** | 2 /** |
3 * Instances of the class {@code NodeList} represent a list of AST nodes that ha
ve a common parent. | 3 * Instances of the class {@code NodeList} represent a list of AST nodes that ha
ve a common parent. |
4 */ | 4 */ |
5 class NodeList<E extends ASTNode> extends Object with ListMixin<E> { | 5 class NodeList<E extends ASTNode> extends Object with ListMixin<E> { |
6 /** | 6 /** |
7 * Create an empty list with the given owner. This is a convenience method tha
t allows the | 7 * Create an empty list with the given owner. This is a convenience method tha
t allows the |
8 * compiler to determine the correct value of the type argument [E] without ne
eding to | 8 * compiler to determine the correct value of the type argument [E] without ne
eding to |
9 * explicitly specify it. | 9 * explicitly specify it. |
10 * | 10 * |
(...skipping 18 matching lines...) Expand all Loading... |
29 * @param owner the node that is the parent of each of the elements in the lis
t | 29 * @param owner the node that is the parent of each of the elements in the lis
t |
30 */ | 30 */ |
31 NodeList(this.owner); | 31 NodeList(this.owner); |
32 | 32 |
33 /** | 33 /** |
34 * Use the given visitor to visit each of the nodes in this list. | 34 * Use the given visitor to visit each of the nodes in this list. |
35 * | 35 * |
36 * @param visitor the visitor to be used to visit the elements of this list | 36 * @param visitor the visitor to be used to visit the elements of this list |
37 */ | 37 */ |
38 accept(ASTVisitor visitor) { | 38 accept(ASTVisitor visitor) { |
39 for (E element in _elements) { | 39 var length = _elements.length; |
40 element.accept(visitor); | 40 for (var i = 0; i < length; i++) { |
| 41 _elements[i].accept(visitor); |
41 } | 42 } |
42 } | 43 } |
43 void add(E node) { | 44 void add(E node) { |
44 insert(length, node); | 45 insert(length, node); |
45 } | 46 } |
46 void insert(int index, E node) { | 47 void insert(int index, E node) { |
47 int length = _elements.length; | 48 int length = _elements.length; |
48 if (index < 0 || index > length) { | 49 if (index < 0 || index > length) { |
49 throw new RangeError("Index: ${index}, Size: ${_elements.length}"); | 50 throw new RangeError("Index: ${index}, Size: ${_elements.length}"); |
50 } | 51 } |
51 owner.becomeParentOf(node); | 52 owner.becomeParentOf(node); |
52 if (length == 0) { | 53 if (length == 0) { |
53 _elements = <E> [node]; | 54 _elements = <E> [node]; |
54 } else { | 55 } else { |
55 List<E> newElements = new List<E>(length + 1); | 56 _elements.insert(index, node); |
56 JavaSystem.arraycopy(_elements, 0, newElements, 0, index); | |
57 newElements[index] = node; | |
58 JavaSystem.arraycopy(_elements, index, newElements, index + 1, length - in
dex); | |
59 _elements = newElements; | |
60 } | 57 } |
61 } | 58 } |
62 bool addAll(Iterable<E> nodes) { | 59 bool addAll(Iterable<E> nodes) { |
63 if (nodes != null && !nodes.isEmpty) { | 60 if (nodes != null && !nodes.isEmpty) { |
64 int oldCount = _elements.length; | 61 _elements.addAll(nodes); |
65 int newCount = nodes.length; | |
66 List<E> newElements = new List<E>(oldCount + newCount); | |
67 JavaSystem.arraycopy(_elements, 0, newElements, 0, oldCount); | |
68 int index = oldCount; | |
69 for (E node in nodes) { | 62 for (E node in nodes) { |
70 owner.becomeParentOf(node); | 63 owner.becomeParentOf(node); |
71 newElements[index++] = node; | |
72 } | 64 } |
73 _elements = newElements; | |
74 return true; | 65 return true; |
75 } | 66 } |
76 return false; | 67 return false; |
77 } | 68 } |
78 E operator[](int index) { | 69 E operator[](int index) { |
79 if (index < 0 || index >= _elements.length) { | 70 if (index < 0 || index >= _elements.length) { |
80 throw new RangeError("Index: ${index}, Size: ${_elements.length}"); | 71 throw new RangeError("Index: ${index}, Size: ${_elements.length}"); |
81 } | 72 } |
82 return _elements[index] as E; | 73 return _elements[index] as E; |
83 } | 74 } |
(...skipping 24 matching lines...) Expand all Loading... |
108 E removeAt(int index) { | 99 E removeAt(int index) { |
109 if (index < 0 || index >= _elements.length) { | 100 if (index < 0 || index >= _elements.length) { |
110 throw new RangeError("Index: ${index}, Size: ${_elements.length}"); | 101 throw new RangeError("Index: ${index}, Size: ${_elements.length}"); |
111 } | 102 } |
112 E removedNode = _elements[index] as E; | 103 E removedNode = _elements[index] as E; |
113 int length = _elements.length; | 104 int length = _elements.length; |
114 if (length == 1) { | 105 if (length == 1) { |
115 _elements = ASTNode.EMPTY_ARRAY; | 106 _elements = ASTNode.EMPTY_ARRAY; |
116 return removedNode; | 107 return removedNode; |
117 } | 108 } |
118 List<E> newElements = new List<E>(length - 1); | 109 _elements.removeAt(index); |
119 JavaSystem.arraycopy(_elements, 0, newElements, 0, index); | |
120 JavaSystem.arraycopy(_elements, index + 1, newElements, index, length - inde
x - 1); | |
121 _elements = newElements; | |
122 return removedNode; | 110 return removedNode; |
123 } | 111 } |
124 void operator[]=(int index, E node) { | 112 void operator[]=(int index, E node) { |
125 if (index < 0 || index >= _elements.length) { | 113 if (index < 0 || index >= _elements.length) { |
126 throw new RangeError("Index: ${index}, Size: ${_elements.length}"); | 114 throw new RangeError("Index: ${index}, Size: ${_elements.length}"); |
127 } | 115 } |
128 _elements[index] as E; | |
129 owner.becomeParentOf(node); | 116 owner.becomeParentOf(node); |
130 _elements[index] = node; | 117 _elements[index] = node; |
131 } | 118 } |
132 int get length => _elements.length; | 119 int get length => _elements.length; |
133 } | 120 } |
OLD | NEW |