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: allocate on stack. | 5 // - GrowableArray: allocate on stack. |
6 // - ZoneGrowableArray: allocated in the zone. | 6 // - ZoneGrowableArray: allocated in the zone. |
7 | 7 |
8 #ifndef VM_GROWABLE_ARRAY_H_ | 8 #ifndef VM_GROWABLE_ARRAY_H_ |
9 #define VM_GROWABLE_ARRAY_H_ | 9 #define VM_GROWABLE_ARRAY_H_ |
10 | 10 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 ASSERT(length_ <= capacity_); | 52 ASSERT(length_ <= capacity_); |
53 return data_[index]; | 53 return data_[index]; |
54 } | 54 } |
55 | 55 |
56 T& Last() const { | 56 T& Last() const { |
57 ASSERT(length_ > 0); | 57 ASSERT(length_ > 0); |
58 return operator[](length_ - 1); | 58 return operator[](length_ - 1); |
59 } | 59 } |
60 | 60 |
61 void AddArray(const BaseGrowableArray<T, B>& src) { | 61 void AddArray(const BaseGrowableArray<T, B>& src) { |
62 for (int i = 0; i < src.length(); i++) { | 62 for (intptr_t i = 0; i < src.length(); i++) { |
63 Add(src[i]); | 63 Add(src[i]); |
64 } | 64 } |
65 } | 65 } |
66 | 66 |
67 void Clear() { | 67 void Clear() { |
68 length_ = 0; | 68 length_ = 0; |
69 } | 69 } |
70 | 70 |
71 void RemoveFirst() { | 71 void RemoveFirst() { |
72 ASSERT(length_ > 0); | 72 ASSERT(length_ > 0); |
73 length_--; | 73 length_--; |
74 for (int i = 0; i < length_; i++) { | 74 for (int i = 0; i < length_; i++) { |
75 data_[i] = data_[i + 1]; | 75 data_[i] = data_[i + 1]; |
76 } | 76 } |
77 } | 77 } |
78 | 78 |
| 79 void InsertAt(intptr_t idx, const T& value) { |
| 80 Resize(length() + 1); |
| 81 for (intptr_t i = length_ - 2; i >= idx; i--) { |
| 82 data_[i + 1] = data_[i]; |
| 83 } |
| 84 data_[idx] = value; |
| 85 } |
| 86 |
79 // Sort the array in place. | 87 // Sort the array in place. |
80 inline void Sort(int compare(const T*, const T*)); | 88 inline void Sort(int compare(const T*, const T*)); |
81 | 89 |
82 private: | 90 private: |
83 int length_; | 91 int length_; |
84 int capacity_; | 92 int capacity_; |
85 T* data_; | 93 T* data_; |
86 BaseZone* zone_; // Zone in which we are allocating the array. | 94 BaseZone* zone_; // Zone in which we are allocating the array. |
87 | 95 |
88 void Resize(int new_length); | 96 void Resize(int new_length); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 initial_capacity, | 144 initial_capacity, |
137 Isolate::Current()->current_zone()->GetBaseZone()) {} | 145 Isolate::Current()->current_zone()->GetBaseZone()) {} |
138 ZoneGrowableArray() : | 146 ZoneGrowableArray() : |
139 BaseGrowableArray<T, ZoneAllocated>( | 147 BaseGrowableArray<T, ZoneAllocated>( |
140 Isolate::Current()->current_zone()->GetBaseZone()) {} | 148 Isolate::Current()->current_zone()->GetBaseZone()) {} |
141 }; | 149 }; |
142 | 150 |
143 } // namespace dart | 151 } // namespace dart |
144 | 152 |
145 #endif // VM_GROWABLE_ARRAY_H_ | 153 #endif // VM_GROWABLE_ARRAY_H_ |
OLD | NEW |