1a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Copyright 2006-2009 the V8 project authors. All rights reserved.
2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Redistribution and use in source and binary forms, with or without
3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// modification, are permitted provided that the following conditions are
4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// met:
5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions of source code must retain the above copyright
7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       notice, this list of conditions and the following disclaimer.
8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions in binary form must reproduce the above
9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       copyright notice, this list of conditions and the following
10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       disclaimer in the documentation and/or other materials provided
11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       with the distribution.
12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Neither the name of Google Inc. nor the names of its
13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       contributors may be used to endorse or promote products derived
14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       from this software without specific prior written permission.
15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
28a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#ifndef V8_LIST_INL_H_
29a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#define V8_LIST_INL_H_
30a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
31a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#include "list.h"
32a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
33a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocknamespace v8 {
34a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocknamespace internal {
35a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
36a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
37a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocktemplate<typename T, class P>
38a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvoid List<T, P>::Add(const T& element) {
39a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  if (length_ < capacity_) {
40a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    data_[length_++] = element;
41a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  } else {
42a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    List<T, P>::ResizeAdd(element);
43a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
44a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
45a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
46a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
47a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocktemplate<typename T, class P>
48a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvoid List<T, P>::AddAll(const List<T, P>& other) {
49a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  int result_length = length_ + other.length_;
50a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  if (capacity_ < result_length) Resize(result_length);
51a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  for (int i = 0; i < other.length_; i++) {
52a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    data_[length_ + i] = other.data_[i];
53a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
54a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  length_ = result_length;
55a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
56a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
57a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
58a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Use two layers of inlining so that the non-inlined function can
59a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// use the same implementation as the inlined version.
60a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocktemplate<typename T, class P>
61a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvoid List<T, P>::ResizeAdd(const T& element) {
62a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  ResizeAddInternal(element);
63a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
64a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
65a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
66a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocktemplate<typename T, class P>
67a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvoid List<T, P>::ResizeAddInternal(const T& element) {
68a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  ASSERT(length_ >= capacity_);
69a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Grow the list capacity by 50%, but make sure to let it grow
70a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // even when the capacity is zero (possible initial case).
71a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  int new_capacity = 1 + capacity_ + (capacity_ >> 1);
72a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Since the element reference could be an element of the list, copy
73a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // it out of the old backing storage before resizing.
74a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  T temp = element;
75a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Resize(new_capacity);
76a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  data_[length_++] = temp;
77a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
78a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
79a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
80a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocktemplate<typename T, class P>
81a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvoid List<T, P>::Resize(int new_capacity) {
82a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  T* new_data = List<T, P>::NewData(new_capacity);
83a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  memcpy(new_data, data_, capacity_ * sizeof(T));
84a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  List<T, P>::DeleteData(data_);
85a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  data_ = new_data;
86a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  capacity_ = new_capacity;
87a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
88a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
89a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
90a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocktemplate<typename T, class P>
91a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockVector<T> List<T, P>::AddBlock(T value, int count) {
92a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  int start = length_;
93a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  for (int i = 0; i < count; i++) Add(value);
94a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  return Vector<T>(&data_[start], count);
95a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
96a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
97a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
98a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocktemplate<typename T, class P>
99a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockT List<T, P>::Remove(int i) {
100a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  T element = at(i);
101a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  length_--;
102a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  while (i < length_) {
103a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    data_[i] = data_[i + 1];
104a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    i++;
105a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
106a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  return element;
107a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
108a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
109a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
110a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocktemplate<typename T, class P>
111a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvoid List<T, P>::Clear() {
112a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  DeleteData(data_);
113a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Initialize(0);
114a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
115a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
116a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
117a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocktemplate<typename T, class P>
118a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvoid List<T, P>::Rewind(int pos) {
119a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  length_ = pos;
120a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
121a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
122a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
123a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocktemplate<typename T, class P>
124a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvoid List<T, P>::Iterate(void (*callback)(T* x)) {
125a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  for (int i = 0; i < length_; i++) callback(&data_[i]);
126a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
127a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
128a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
129a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocktemplate<typename T, class P>
130a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbool List<T, P>::Contains(const T& elm) {
131a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  for (int i = 0; i < length_; i++) {
132a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    if (data_[i] == elm)
133a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      return true;
134a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
135a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  return false;
136a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
137a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
138a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
139a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocktemplate<typename T, class P>
140a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvoid List<T, P>::Sort(int (*cmp)(const T* x, const T* y)) {
141a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  ToVector().Sort(cmp);
142a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#ifdef DEBUG
143a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  for (int i = 1; i < length_; i++)
144a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    ASSERT(cmp(&data_[i - 1], &data_[i]) <= 0);
145a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#endif
146a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
147a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
148a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
149a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocktemplate<typename T, class P>
150a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvoid List<T, P>::Sort() {
151a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Sort(PointerValueCompare<T>);
152a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
153a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
154a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
155a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocktemplate<typename T, class P>
156a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvoid List<T, P>::Initialize(int capacity) {
157a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  ASSERT(capacity >= 0);
158a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  data_ = (capacity > 0) ? NewData(capacity) : NULL;
159a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  capacity_ = capacity;
160a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  length_ = 0;
161a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
162a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
163a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
164a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} }  // namespace v8::internal
165a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
166a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#endif  // V8_LIST_INL_H_
167