buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 17 | #ifndef ART_COMPILER_UTILS_GROWABLE_ARRAY_H_ |
| 18 | #define ART_COMPILER_UTILS_GROWABLE_ARRAY_H_ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <stddef.h> |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 22 | |
| 23 | #include "arena_object.h" |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 27 | // Deprecated |
| 28 | // TODO: Replace all uses with ArenaVector<T>. |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 29 | template<typename T> |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 30 | class GrowableArray : public ArenaObject<kArenaAllocGrowableArray> { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 31 | public: |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 32 | GrowableArray(ArenaAllocator* arena, size_t init_length) |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 33 | : arena_(arena), |
buzbee | a5abf70 | 2013-04-12 14:39:29 -0700 | [diff] [blame] | 34 | num_allocated_(init_length), |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 35 | num_used_(0) { |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 36 | elem_list_ = static_cast<T*>(arena_->Alloc(sizeof(T) * init_length, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 37 | kArenaAllocGrowableArray)); |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 38 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 39 | |
| 40 | |
| 41 | // Expand the list size to at least new length. |
| 42 | void Resize(size_t new_length) { |
| 43 | if (new_length <= num_allocated_) return; |
buzbee | a5abf70 | 2013-04-12 14:39:29 -0700 | [diff] [blame] | 44 | // If it's a small list double the size, else grow 1.5x. |
| 45 | size_t target_length = |
| 46 | (num_allocated_ < 128) ? num_allocated_ << 1 : num_allocated_ + (num_allocated_ >> 1); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 47 | if (new_length > target_length) { |
| 48 | target_length = new_length; |
| 49 | } |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 50 | T* new_array = static_cast<T*>(arena_->Alloc(sizeof(T) * target_length, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 51 | kArenaAllocGrowableArray)); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 52 | memcpy(new_array, elem_list_, sizeof(T) * num_allocated_); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 53 | num_allocated_ = target_length; |
| 54 | elem_list_ = new_array; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 55 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 56 | |
| 57 | // NOTE: does not return storage, just resets use count. |
| 58 | void Reset() { |
| 59 | num_used_ = 0; |
| 60 | } |
| 61 | |
| 62 | // Insert an element to the end of a list, resizing if necessary. |
| 63 | void Insert(T elem) { |
| 64 | if (num_used_ == num_allocated_) { |
| 65 | Resize(num_used_ + 1); |
| 66 | } |
| 67 | elem_list_[num_used_++] = elem; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void InsertAt(size_t index, T elem) { |
| 71 | DCHECK(index <= Size()); |
| 72 | Insert(elem); |
| 73 | for (size_t i = Size() - 1; i > index; --i) { |
| 74 | elem_list_[i] = elem_list_[i - 1]; |
| 75 | } |
| 76 | elem_list_[index] = elem; |
| 77 | } |
| 78 | |
| 79 | void Add(T elem) { |
| 80 | Insert(elem); |
| 81 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 82 | |
| 83 | T Get(size_t index) const { |
| 84 | DCHECK_LT(index, num_used_); |
| 85 | return elem_list_[index]; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 86 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 87 | |
| 88 | // Overwrite existing element at position index. List must be large enough. |
| 89 | void Put(size_t index, T elem) { |
| 90 | DCHECK_LT(index, num_used_); |
| 91 | elem_list_[index] = elem; |
| 92 | } |
| 93 | |
| 94 | void Increment(size_t index) { |
| 95 | DCHECK_LT(index, num_used_); |
| 96 | elem_list_[index]++; |
| 97 | } |
| 98 | |
buzbee | bd663de | 2013-09-10 15:41:31 -0700 | [diff] [blame] | 99 | /* |
| 100 | * Remove an existing element from list. If there are more than one copy |
| 101 | * of the element, only the first one encountered will be deleted. |
| 102 | */ |
| 103 | // TODO: consider renaming this. |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 104 | void Delete(T element) { |
| 105 | bool found = false; |
| 106 | for (size_t i = 0; i < num_used_ - 1; i++) { |
| 107 | if (!found && elem_list_[i] == element) { |
| 108 | found = true; |
| 109 | } |
| 110 | if (found) { |
| 111 | elem_list_[i] = elem_list_[i+1]; |
| 112 | } |
| 113 | } |
| 114 | // We should either have found the element, or it was the last (unscanned) element. |
| 115 | DCHECK(found || (element == elem_list_[num_used_ - 1])); |
| 116 | num_used_--; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 117 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 118 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 119 | void DeleteAt(size_t index) { |
| 120 | for (size_t i = index; i < num_used_ - 1; i++) { |
| 121 | elem_list_[i] = elem_list_[i + 1]; |
| 122 | } |
| 123 | num_used_--; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 124 | } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 125 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 126 | size_t GetNumAllocated() const { return num_allocated_; } |
| 127 | |
| 128 | size_t Size() const { return num_used_; } |
| 129 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 130 | bool IsEmpty() const { return num_used_ == 0; } |
| 131 | |
| 132 | T Pop() { |
| 133 | DCHECK_GE(num_used_, (size_t)0); |
| 134 | return elem_list_[--num_used_]; |
| 135 | } |
| 136 | |
| 137 | T Peek() const { |
| 138 | DCHECK_GE(num_used_, (size_t)0); |
| 139 | return elem_list_[num_used_ - 1]; |
| 140 | } |
| 141 | |
buzbee | bd663de | 2013-09-10 15:41:31 -0700 | [diff] [blame] | 142 | void SetSize(size_t new_size) { |
| 143 | Resize(new_size); |
| 144 | num_used_ = new_size; |
| 145 | } |
| 146 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 147 | T* GetRawStorage() const { return elem_list_; } |
| 148 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 149 | private: |
| 150 | ArenaAllocator* const arena_; |
| 151 | size_t num_allocated_; |
| 152 | size_t num_used_; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 153 | T* elem_list_; |
| 154 | }; |
| 155 | |
| 156 | } // namespace art |
| 157 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 158 | #endif // ART_COMPILER_UTILS_GROWABLE_ARRAY_H_ |