5 #ifndef V8_ZONE_ZONE_LIST_INL_H_ 6 #define V8_ZONE_ZONE_LIST_INL_H_ 8 #include "src/zone/zone.h" 10 #include "src/base/macros.h" 11 #include "src/base/platform/platform.h" 12 #include "src/utils.h" 18 void ZoneList<T>::Add(
const T& element, Zone* zone) {
19 if (length_ < capacity_) {
20 data_[length_++] = element;
22 ZoneList<T>::ResizeAdd(element, ZoneAllocationPolicy(zone));
27 void ZoneList<T>::AddAll(
const ZoneList<T>& other, Zone* zone) {
28 AddAll(other.ToVector(), zone);
32 void ZoneList<T>::AddAll(
const Vector<T>& other, Zone* zone) {
33 int result_length = length_ + other.length();
34 if (capacity_ < result_length)
35 Resize(result_length, ZoneAllocationPolicy(zone));
36 if (std::is_fundamental<T>()) {
37 memcpy(data_ + length_, other.start(),
sizeof(*data_) * other.length());
39 for (
int i = 0;
i < other.length();
i++) data_[length_ +
i] = other.at(
i);
41 length_ = result_length;
47 void ZoneList<T>::ResizeAdd(
const T& element, ZoneAllocationPolicy alloc) {
48 ResizeAddInternal(element, alloc);
52 void ZoneList<T>::ResizeAddInternal(
const T& element,
53 ZoneAllocationPolicy alloc) {
54 DCHECK(length_ >= capacity_);
57 int new_capacity = 1 + 2 * capacity_;
61 Resize(new_capacity, alloc);
62 data_[length_++] = temp;
66 void ZoneList<T>::Resize(
int new_capacity, ZoneAllocationPolicy alloc) {
67 DCHECK_LE(length_, new_capacity);
68 T* new_data = NewData(new_capacity, alloc);
69 MemCopy(new_data, data_, length_ *
sizeof(T));
70 ZoneList<T>::DeleteData(data_);
72 capacity_ = new_capacity;
76 Vector<T> ZoneList<T>::AddBlock(T value,
int count, Zone* zone) {
78 for (
int i = 0;
i < count;
i++) Add(value, zone);
79 return Vector<T>(&data_[start], count);
83 void ZoneList<T>::Set(
int index,
const T& elm) {
84 DCHECK(index >= 0 && index <= length_);
89 void ZoneList<T>::InsertAt(
int index,
const T& elm, Zone* zone) {
90 DCHECK(index >= 0 && index <= length_);
92 for (
int i = length_ - 1;
i > index; --
i) {
93 data_[
i] = data_[
i - 1];
99 T ZoneList<T>::Remove(
int i) {
102 while (
i < length_) {
103 data_[
i] = data_[
i + 1];
109 template <
typename T>
110 void ZoneList<T>::Clear() {
119 template <
typename T>
120 void ZoneList<T>::Rewind(
int pos) {
121 DCHECK(0 <= pos && pos <= length_);
125 template <
typename T>
126 template <
class Visitor>
127 void ZoneList<T>::Iterate(Visitor* visitor) {
128 for (
int i = 0;
i < length_;
i++) visitor->Apply(&data_[
i]);
131 template <
typename T>
132 bool ZoneList<T>::Contains(
const T& elm)
const {
133 for (
int i = 0;
i < length_;
i++) {
134 if (data_[
i] == elm)
return true;
139 template <
typename T>
140 template <
typename CompareFunction>
141 void ZoneList<T>::Sort(CompareFunction cmp) {
142 ToVector().Sort(cmp, 0, length_);
144 for (
int i = 1;
i < length_;
i++) {
145 DCHECK_LE(cmp(&data_[
i - 1], &data_[
i]), 0);
150 template <
typename T>
151 template <
typename CompareFunction>
152 void ZoneList<T>::StableSort(CompareFunction cmp,
size_t s,
size_t l) {
153 ToVector().StableSort(cmp, s, l);
155 for (
size_t i = s + 1;
i < l;
i++) {
156 DCHECK_LE(cmp(&data_[
i - 1], &data_[
i]), 0);
164 #endif // V8_ZONE_ZONE_LIST_INL_H_