Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: runtime/vm/growable_array.h

Issue 324203003: Add SetLength to GrowableArray and use it in code coverage. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/coverage.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 } 76 }
77 77
78 void InsertAt(intptr_t idx, const T& value) { 78 void InsertAt(intptr_t idx, const T& value) {
79 Resize(length() + 1); 79 Resize(length() + 1);
80 for (intptr_t i = length_ - 2; i >= idx; i--) { 80 for (intptr_t i = length_ - 2; i >= idx; i--) {
81 data_[i + 1] = data_[i]; 81 data_[i + 1] = data_[i];
82 } 82 }
83 data_[idx] = value; 83 data_[idx] = value;
84 } 84 }
85 85
86 // The content is uninitialized after calling it.
87 void SetLength(intptr_t new_length);
88
86 // Sort the array in place. 89 // Sort the array in place.
87 inline void Sort(int compare(const T*, const T*)); 90 inline void Sort(int compare(const T*, const T*));
88 91
89 private: 92 private:
90 intptr_t length_; 93 intptr_t length_;
91 intptr_t capacity_; 94 intptr_t capacity_;
92 T* data_; 95 T* data_;
93 Zone* zone_; // Zone in which we are allocating the array. 96 Zone* zone_; // Zone in which we are allocating the array.
94 97
98 // Used for growing the array.
95 void Resize(intptr_t new_length); 99 void Resize(intptr_t new_length);
96 100
97 DISALLOW_COPY_AND_ASSIGN(BaseGrowableArray); 101 DISALLOW_COPY_AND_ASSIGN(BaseGrowableArray);
98 }; 102 };
99 103
100 104
101 template<typename T, typename B> 105 template<typename T, typename B>
102 inline void BaseGrowableArray<T, B>::Sort( 106 inline void BaseGrowableArray<T, B>::Sort(
103 int compare(const T*, const T*)) { 107 int compare(const T*, const T*)) {
104 typedef int (*CompareFunction)(const void*, const void*); 108 typedef int (*CompareFunction)(const void*, const void*);
105 qsort(data_, length_, sizeof(T), reinterpret_cast<CompareFunction>(compare)); 109 qsort(data_, length_, sizeof(T), reinterpret_cast<CompareFunction>(compare));
106 } 110 }
107 111
108 112
109 template<typename T, typename B> 113 template<typename T, typename B>
110 void BaseGrowableArray<T, B>::Resize(intptr_t new_length) { 114 void BaseGrowableArray<T, B>::Resize(intptr_t new_length) {
111 if (new_length > capacity_) { 115 if (new_length > capacity_) {
112 intptr_t new_capacity = Utils::RoundUpToPowerOfTwo(new_length); 116 intptr_t new_capacity = Utils::RoundUpToPowerOfTwo(new_length);
113 T* new_data = zone_->Realloc<T>(data_, capacity_, new_capacity); 117 T* new_data = zone_->Realloc<T>(data_, capacity_, new_capacity);
114 ASSERT(new_data != NULL); 118 ASSERT(new_data != NULL);
115 data_ = new_data; 119 data_ = new_data;
116 capacity_ = new_capacity; 120 capacity_ = new_capacity;
117 } 121 }
118 length_ = new_length; 122 length_ = new_length;
119 } 123 }
120 124
121 125
126 template<typename T, typename B>
127 void BaseGrowableArray<T, B>::SetLength(intptr_t new_length) {
128 if (new_length > capacity_) {
129 T* new_data = zone_->Alloc<T>(new_length);
130 ASSERT(new_data != NULL);
131 data_ = new_data;
132 capacity_ = new_length;
133 }
134 length_ = new_length;
135 }
136
137
122 template<typename T> 138 template<typename T>
123 class GrowableArray : public BaseGrowableArray<T, ValueObject> { 139 class GrowableArray : public BaseGrowableArray<T, ValueObject> {
124 public: 140 public:
125 GrowableArray(Isolate* isolate, intptr_t initial_capacity) 141 GrowableArray(Isolate* isolate, intptr_t initial_capacity)
126 : BaseGrowableArray<T, ValueObject>( 142 : BaseGrowableArray<T, ValueObject>(
127 initial_capacity, isolate->current_zone()) {} 143 initial_capacity, isolate->current_zone()) {}
128 explicit GrowableArray(intptr_t initial_capacity) 144 explicit GrowableArray(intptr_t initial_capacity)
129 : BaseGrowableArray<T, ValueObject>( 145 : BaseGrowableArray<T, ValueObject>(
130 initial_capacity, Isolate::Current()->current_zone()) {} 146 initial_capacity, Isolate::Current()->current_zone()) {}
131 GrowableArray() 147 GrowableArray()
(...skipping 13 matching lines...) Expand all
145 initial_capacity, 161 initial_capacity,
146 Isolate::Current()->current_zone()) {} 162 Isolate::Current()->current_zone()) {}
147 ZoneGrowableArray() : 163 ZoneGrowableArray() :
148 BaseGrowableArray<T, ZoneAllocated>( 164 BaseGrowableArray<T, ZoneAllocated>(
149 Isolate::Current()->current_zone()) {} 165 Isolate::Current()->current_zone()) {}
150 }; 166 };
151 167
152 } // namespace dart 168 } // namespace dart
153 169
154 #endif // VM_GROWABLE_ARRAY_H_ 170 #endif // VM_GROWABLE_ARRAY_H_
OLDNEW
« no previous file with comments | « runtime/vm/coverage.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698