blob: 6af4853e0979b3f1a55d760587d872c1510adb44 [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),
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000043 num_used_(init_length) {
Nicolas Geoffray86dde162015-01-26 12:54:33 +000044 elem_list_ = static_cast<T*>(arena_->Alloc(sizeof(T) * init_length,
45 kArenaAllocGrowableArray));
46 for (size_t i = 0; i < init_length; ++i) {
47 elem_list_[i] = initial_data;
48 }
49 }
50
buzbee862a7602013-04-05 10:58:54 -070051
52 // Expand the list size to at least new length.
53 void Resize(size_t new_length) {
54 if (new_length <= num_allocated_) return;
buzbeea5abf702013-04-12 14:39:29 -070055 // If it's a small list double the size, else grow 1.5x.
56 size_t target_length =
57 (num_allocated_ < 128) ? num_allocated_ << 1 : num_allocated_ + (num_allocated_ >> 1);
buzbee862a7602013-04-05 10:58:54 -070058 if (new_length > target_length) {
59 target_length = new_length;
60 }
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070061 T* new_array = static_cast<T*>(arena_->Alloc(sizeof(T) * target_length,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000062 kArenaAllocGrowableArray));
buzbee862a7602013-04-05 10:58:54 -070063 memcpy(new_array, elem_list_, sizeof(T) * num_allocated_);
buzbee862a7602013-04-05 10:58:54 -070064 num_allocated_ = target_length;
65 elem_list_ = new_array;
Andreas Gampec8ccf682014-09-29 20:07:43 -070066 }
buzbee862a7602013-04-05 10:58:54 -070067
68 // NOTE: does not return storage, just resets use count.
69 void Reset() {
70 num_used_ = 0;
71 }
72
73 // Insert an element to the end of a list, resizing if necessary.
74 void Insert(T elem) {
75 if (num_used_ == num_allocated_) {
76 Resize(num_used_ + 1);
77 }
78 elem_list_[num_used_++] = elem;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000079 }
80
81 void InsertAt(size_t index, T elem) {
82 DCHECK(index <= Size());
83 Insert(elem);
84 for (size_t i = Size() - 1; i > index; --i) {
85 elem_list_[i] = elem_list_[i - 1];
86 }
87 elem_list_[index] = elem;
88 }
89
90 void Add(T elem) {
91 Insert(elem);
92 }
buzbee862a7602013-04-05 10:58:54 -070093
94 T Get(size_t index) const {
95 DCHECK_LT(index, num_used_);
96 return elem_list_[index];
Andreas Gampec8ccf682014-09-29 20:07:43 -070097 }
buzbee862a7602013-04-05 10:58:54 -070098
99 // Overwrite existing element at position index. List must be large enough.
100 void Put(size_t index, T elem) {
101 DCHECK_LT(index, num_used_);
102 elem_list_[index] = elem;
103 }
104
105 void Increment(size_t index) {
106 DCHECK_LT(index, num_used_);
107 elem_list_[index]++;
108 }
109
buzbeebd663de2013-09-10 15:41:31 -0700110 /*
111 * Remove an existing element from list. If there are more than one copy
112 * of the element, only the first one encountered will be deleted.
113 */
114 // TODO: consider renaming this.
buzbee862a7602013-04-05 10:58:54 -0700115 void Delete(T element) {
116 bool found = false;
117 for (size_t i = 0; i < num_used_ - 1; i++) {
118 if (!found && elem_list_[i] == element) {
119 found = true;
120 }
121 if (found) {
122 elem_list_[i] = elem_list_[i+1];
123 }
124 }
125 // We should either have found the element, or it was the last (unscanned) element.
126 DCHECK(found || (element == elem_list_[num_used_ - 1]));
127 num_used_--;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700128 }
buzbee862a7602013-04-05 10:58:54 -0700129
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100130 void DeleteAt(size_t index) {
131 for (size_t i = index; i < num_used_ - 1; i++) {
132 elem_list_[i] = elem_list_[i + 1];
133 }
134 num_used_--;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700135 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100136
buzbee862a7602013-04-05 10:58:54 -0700137 size_t GetNumAllocated() const { return num_allocated_; }
138
139 size_t Size() const { return num_used_; }
140
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000141 bool IsEmpty() const { return num_used_ == 0; }
142
143 T Pop() {
144 DCHECK_GE(num_used_, (size_t)0);
145 return elem_list_[--num_used_];
146 }
147
148 T Peek() const {
149 DCHECK_GE(num_used_, (size_t)0);
150 return elem_list_[num_used_ - 1];
151 }
152
buzbeebd663de2013-09-10 15:41:31 -0700153 void SetSize(size_t new_size) {
154 Resize(new_size);
155 num_used_ = new_size;
156 }
157
buzbee862a7602013-04-05 10:58:54 -0700158 T* GetRawStorage() const { return elem_list_; }
159
buzbee862a7602013-04-05 10:58:54 -0700160 private:
161 ArenaAllocator* const arena_;
162 size_t num_allocated_;
163 size_t num_used_;
buzbee862a7602013-04-05 10:58:54 -0700164 T* elem_list_;
165};
166
167} // namespace art
168
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000169#endif // ART_COMPILER_UTILS_GROWABLE_ARRAY_H_