OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // Defines growable array classes, that differ where they are allocated: | 4 // Defines growable array classes, that differ where they are allocated: |
5 // - GrowableArray: allocated on stack. | 5 // - GrowableArray: allocated on stack. |
6 // - ZoneGrowableArray: allocated in the zone. | 6 // - ZoneGrowableArray: allocated in the zone. |
7 // - MallocGrowableArray: allocates using malloc/realloc; free is only called | 7 // - MallocGrowableArray: allocates using malloc/realloc; free is only called |
8 // at destruction. | 8 // at destruction. |
9 | 9 |
10 #ifndef VM_GROWABLE_ARRAY_H_ | 10 #ifndef VM_GROWABLE_ARRAY_H_ |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 void Swap(intptr_t i, intptr_t j) { | 103 void Swap(intptr_t i, intptr_t j) { |
104 ASSERT(i >= 0); | 104 ASSERT(i >= 0); |
105 ASSERT(j >= 0); | 105 ASSERT(j >= 0); |
106 ASSERT(i < length_); | 106 ASSERT(i < length_); |
107 ASSERT(j < length_); | 107 ASSERT(j < length_); |
108 T temp = data_[i]; | 108 T temp = data_[i]; |
109 data_[i] = data_[j]; | 109 data_[i] = data_[j]; |
110 data_[j] = temp; | 110 data_[j] = temp; |
111 } | 111 } |
112 | 112 |
| 113 // NOTE: Does not preserve array order. |
| 114 void RemoveAt(intptr_t i) { |
| 115 ASSERT(i >= 0); |
| 116 ASSERT(i < length_); |
| 117 intptr_t last = length_ - 1; |
| 118 if (i < last) { |
| 119 Swap(i, last); |
| 120 } |
| 121 RemoveLast(); |
| 122 } |
| 123 |
113 // The content is uninitialized after calling it. | 124 // The content is uninitialized after calling it. |
114 void SetLength(intptr_t new_length); | 125 void SetLength(intptr_t new_length); |
115 | 126 |
116 // Sort the array in place. | 127 // Sort the array in place. |
117 inline void Sort(int compare(const T*, const T*)); | 128 inline void Sort(int compare(const T*, const T*)); |
118 | 129 |
119 private: | 130 private: |
120 intptr_t length_; | 131 intptr_t length_; |
121 intptr_t capacity_; | 132 intptr_t capacity_; |
122 T* data_; | 133 T* data_; |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 public: | 288 public: |
278 explicit MallocGrowableArray(intptr_t initial_capacity) | 289 explicit MallocGrowableArray(intptr_t initial_capacity) |
279 : BaseGrowableArray<T, EmptyBase, Malloc>(initial_capacity, NULL) {} | 290 : BaseGrowableArray<T, EmptyBase, Malloc>(initial_capacity, NULL) {} |
280 MallocGrowableArray() | 291 MallocGrowableArray() |
281 : BaseGrowableArray<T, EmptyBase, Malloc>(NULL) {} | 292 : BaseGrowableArray<T, EmptyBase, Malloc>(NULL) {} |
282 }; | 293 }; |
283 | 294 |
284 } // namespace dart | 295 } // namespace dart |
285 | 296 |
286 #endif // VM_GROWABLE_ARRAY_H_ | 297 #endif // VM_GROWABLE_ARRAY_H_ |
OLD | NEW |