blob: b84842910bc5e7462c70f343274ecb842b7c9cf9 [file] [log] [blame]
buzbee862a7602013-04-05 10:58:54 -07001/*
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 Geoffray818f2102014-02-18 16:43:35 +000017#ifndef ART_COMPILER_UTILS_GROWABLE_ARRAY_H_
18#define ART_COMPILER_UTILS_GROWABLE_ARRAY_H_
buzbee862a7602013-04-05 10:58:54 -070019
20#include <stdint.h>
21#include <stddef.h>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070022
23#include "arena_object.h"
buzbee862a7602013-04-05 10:58:54 -070024
25namespace art {
26
Vladimir Markoe39c54e2014-09-22 14:50:02 +010027// Deprecated
28// TODO: Replace all uses with ArenaVector<T>.
buzbee862a7602013-04-05 10:58:54 -070029template<typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070030class GrowableArray : public ArenaObject<kArenaAllocGrowableArray> {
buzbee862a7602013-04-05 10:58:54 -070031 public:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070032 GrowableArray(ArenaAllocator* arena, size_t init_length)
buzbee862a7602013-04-05 10:58:54 -070033 : arena_(arena),
buzbeea5abf702013-04-12 14:39:29 -070034 num_allocated_(init_length),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070035 num_used_(0) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070036 elem_list_ = static_cast<T*>(arena_->Alloc(sizeof(T) * init_length,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000037 kArenaAllocGrowableArray));
Andreas Gampec8ccf682014-09-29 20:07:43 -070038 }
buzbee862a7602013-04-05 10:58:54 -070039
Nicolas Geoffray86dde162015-01-26 12:54:33 +000040 GrowableArray(ArenaAllocator* arena, size_t init_length, T initial_data)
41 : arena_(arena),
42 num_allocated_(init_length),
43 num_used_(0) {
44 SetSize(init_length);
45 elem_list_ = static_cast<T*>(arena_->Alloc(sizeof(T) * init_length,
46 kArenaAllocGrowableArray));
47 for (size_t i = 0; i < init_length; ++i) {
48 elem_list_[i] = initial_data;
49 }
50 }
51
buzbee862a7602013-04-05 10:58:54 -070052
53 // Expand the list size to at least new length.
54 void Resize(size_t new_length) {
55 if (new_length <= num_allocated_) return;
buzbeea5abf702013-04-12 14:39:29 -070056 // If it's a small list double the size, else grow 1.5x.
57 size_t target_length =
58 (num_allocated_ < 128) ? num_allocated_ << 1 : num_allocated_ + (num_allocated_ >> 1);
buzbee862a7602013-04-05 10:58:54 -070059 if (new_length > target_length) {
60 target_length = new_length;
61 }
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070062 T* new_array = static_cast<T*>(arena_->Alloc(sizeof(T) * target_length,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000063 kArenaAllocGrowableArray));
buzbee862a7602013-04-05 10:58:54 -070064 memcpy(new_array, elem_list_, sizeof(T) * num_allocated_);
buzbee862a7602013-04-05 10:58:54 -070065 num_allocated_ = target_length;
66 elem_list_ = new_array;
Andreas Gampec8ccf682014-09-29 20:07:43 -070067 }
buzbee862a7602013-04-05 10:58:54 -070068
69 // NOTE: does not return storage, just resets use count.
70 void Reset() {
71 num_used_ = 0;
72 }
73
74 // Insert an element to the end of a list, resizing if necessary.
75 void Insert(T elem) {
76 if (num_used_ == num_allocated_) {
77 Resize(num_used_ + 1);
78 }
79 elem_list_[num_used_++] = elem;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000080 }
81
82 void InsertAt(size_t index, T elem) {
83 DCHECK(index <= Size());
84 Insert(elem);
85 for (size_t i = Size() - 1; i > index; --i) {
86 elem_list_[i] = elem_list_[i - 1];
87 }
88 elem_list_[index] = elem;
89 }
90
91 void Add(T elem) {
92 Insert(elem);
93 }
buzbee862a7602013-04-05 10:58:54 -070094
95 T Get(size_t index) const {
96 DCHECK_LT(index, num_used_);
97 return elem_list_[index];
Andreas Gampec8ccf682014-09-29 20:07:43 -070098 }
buzbee862a7602013-04-05 10:58:54 -070099
100 // Overwrite existing element at position index. List must be large enough.
101 void Put(size_t index, T elem) {
102 DCHECK_LT(index, num_used_);
103 elem_list_[index] = elem;
104 }
105
106 void Increment(size_t index) {
107 DCHECK_LT(index, num_used_);
108 elem_list_[index]++;
109 }
110
buzbeebd663de2013-09-10 15:41:31 -0700111 /*
112 * Remove an existing element from list. If there are more than one copy
113 * of the element, only the first one encountered will be deleted.
114 */
115 // TODO: consider renaming this.
buzbee862a7602013-04-05 10:58:54 -0700116 void Delete(T element) {
117 bool found = false;
118 for (size_t i = 0; i < num_used_ - 1; i++) {
119 if (!found && elem_list_[i] == element) {
120 found = true;
121 }
122 if (found) {
123 elem_list_[i] = elem_list_[i+1];
124 }
125 }
126 // We should either have found the element, or it was the last (unscanned) element.
127 DCHECK(found || (element == elem_list_[num_used_ - 1]));
128 num_used_--;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700129 }
buzbee862a7602013-04-05 10:58:54 -0700130
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100131 void DeleteAt(size_t index) {
132 for (size_t i = index; i < num_used_ - 1; i++) {
133 elem_list_[i] = elem_list_[i + 1];
134 }
135 num_used_--;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700136 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100137
buzbee862a7602013-04-05 10:58:54 -0700138 size_t GetNumAllocated() const { return num_allocated_; }
139
140 size_t Size() const { return num_used_; }
141
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000142 bool IsEmpty() const { return num_used_ == 0; }
143
144 T Pop() {
145 DCHECK_GE(num_used_, (size_t)0);
146 return elem_list_[--num_used_];
147 }
148
149 T Peek() const {
150 DCHECK_GE(num_used_, (size_t)0);
151 return elem_list_[num_used_ - 1];
152 }
153
buzbeebd663de2013-09-10 15:41:31 -0700154 void SetSize(size_t new_size) {
155 Resize(new_size);
156 num_used_ = new_size;
157 }
158
buzbee862a7602013-04-05 10:58:54 -0700159 T* GetRawStorage() const { return elem_list_; }
160
buzbee862a7602013-04-05 10:58:54 -0700161 private:
162 ArenaAllocator* const arena_;
163 size_t num_allocated_;
164 size_t num_used_;
buzbee862a7602013-04-05 10:58:54 -0700165 T* elem_list_;
166};
167
168} // namespace art
169
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000170#endif // ART_COMPILER_UTILS_GROWABLE_ARRAY_H_