1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_LIST_H_
6 #define V8_LIST_H_
7
8 #include "src/utils.h"
9
10 namespace v8 {
11 namespace internal {
12
13 template<typename T> class Vector;
14
15 // ----------------------------------------------------------------------------
16 // The list is a template for very light-weight lists. We are not
17 // using the STL because we want full control over space and speed of
18 // the code. This implementation is based on code by Robert Griesemer
19 // and Rob Pike.
20 //
21 // The list is parameterized by the type of its elements (T) and by an
22 // allocation policy (P). The policy is used for allocating lists in
23 // the C free store or the zone; see zone.h.
24
25 // Forward defined as
26 // template <typename T,
27 // class AllocationPolicy = FreeStoreAllocationPolicy> class List;
28 template <typename T, class AllocationPolicy>
29 class List {
30 public:
31 explicit List(AllocationPolicy allocator = AllocationPolicy()) {
32 Initialize(0, allocator);
33 }
34 INLINE(explicit List(int capacity,
35 AllocationPolicy allocator = AllocationPolicy())) {
36 Initialize(capacity, allocator);
37 }
INLINE(~List ())38 INLINE(~List()) { DeleteData(data_); }
39
40 // Deallocates memory used by the list and leaves the list in a consistent
41 // empty state.
Free()42 void Free() {
43 DeleteData(data_);
44 Initialize(0);
45 }
46
47 INLINE(void* operator new(size_t size,
48 AllocationPolicy allocator = AllocationPolicy())) {
49 return allocator.New(static_cast<int>(size));
50 }
INLINE(void operator delete (void * p))51 INLINE(void operator delete(void* p)) {
52 AllocationPolicy::Delete(p);
53 }
54
55 // Please the MSVC compiler. We should never have to execute this.
INLINE(void operator delete (void * p,AllocationPolicy allocator))56 INLINE(void operator delete(void* p, AllocationPolicy allocator)) {
57 UNREACHABLE();
58 }
59
60 // Returns a reference to the element at index i. This reference is
61 // not safe to use after operations that can change the list's
62 // backing store (e.g. Add).
63 inline T& operator[](int i) const {
64 ASSERT(0 <= i);
65 SLOW_ASSERT(i < length_);
66 return data_[i];
67 }
at(int i)68 inline T& at(int i) const { return operator[](i); }
last()69 inline T& last() const { return at(length_ - 1); }
first()70 inline T& first() const { return at(0); }
71
72 typedef T* iterator;
begin()73 inline iterator begin() const { return &data_[0]; }
end()74 inline iterator end() const { return &data_[length_]; }
75
INLINE(bool is_empty ()const)76 INLINE(bool is_empty() const) { return length_ == 0; }
INLINE(int length ()const)77 INLINE(int length() const) { return length_; }
INLINE(int capacity ()const)78 INLINE(int capacity() const) { return capacity_; }
79
ToVector()80 Vector<T> ToVector() const { return Vector<T>(data_, length_); }
81
ToConstVector()82 Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); }
83
84 // Adds a copy of the given 'element' to the end of the list,
85 // expanding the list if necessary.
86 void Add(const T& element, AllocationPolicy allocator = AllocationPolicy());
87
88 // Add all the elements from the argument list to this list.
89 void AddAll(const List<T, AllocationPolicy>& other,
90 AllocationPolicy allocator = AllocationPolicy());
91
92 // Add all the elements from the vector to this list.
93 void AddAll(const Vector<T>& other,
94 AllocationPolicy allocator = AllocationPolicy());
95
96 // Inserts the element at the specific index.
97 void InsertAt(int index, const T& element,
98 AllocationPolicy allocator = AllocationPolicy());
99
100 // Overwrites the element at the specific index.
101 void Set(int index, const T& element);
102
103 // Added 'count' elements with the value 'value' and returns a
104 // vector that allows access to the elements. The vector is valid
105 // until the next change is made to this list.
106 Vector<T> AddBlock(T value, int count,
107 AllocationPolicy allocator = AllocationPolicy());
108
109 // Removes the i'th element without deleting it even if T is a
110 // pointer type; moves all elements above i "down". Returns the
111 // removed element. This function's complexity is linear in the
112 // size of the list.
113 T Remove(int i);
114
115 // Remove the given element from the list. Returns whether or not
116 // the input is included in the list in the first place.
117 bool RemoveElement(const T& elm);
118
119 // Removes the last element without deleting it even if T is a
120 // pointer type. Returns the removed element.
INLINE(T RemoveLast ())121 INLINE(T RemoveLast()) { return Remove(length_ - 1); }
122
123 // Deletes current list contents and allocates space for 'length' elements.
124 INLINE(void Allocate(int length,
125 AllocationPolicy allocator = AllocationPolicy()));
126
127 // Clears the list by setting the length to zero. Even if T is a
128 // pointer type, clearing the list doesn't delete the entries.
129 INLINE(void Clear());
130
131 // Drops all but the first 'pos' elements from the list.
132 INLINE(void Rewind(int pos));
133
134 // Drop the last 'count' elements from the list.
INLINE(void RewindBy (int count))135 INLINE(void RewindBy(int count)) { Rewind(length_ - count); }
136
137 // Halve the capacity if fill level is less than a quarter.
138 INLINE(void Trim(AllocationPolicy allocator = AllocationPolicy()));
139
140 bool Contains(const T& elm) const;
141 int CountOccurrences(const T& elm, int start, int end) const;
142
143 // Iterate through all list entries, starting at index 0.
144 void Iterate(void (*callback)(T* x));
145 template<class Visitor>
146 void Iterate(Visitor* visitor);
147
148 // Sort all list entries (using QuickSort)
149 void Sort(int (*cmp)(const T* x, const T* y));
150 void Sort();
151
152 INLINE(void Initialize(int capacity,
153 AllocationPolicy allocator = AllocationPolicy()));
154
155 private:
156 T* data_;
157 int capacity_;
158 int length_;
159
INLINE(T * NewData (int n,AllocationPolicy allocator))160 INLINE(T* NewData(int n, AllocationPolicy allocator)) {
161 return static_cast<T*>(allocator.New(n * sizeof(T)));
162 }
INLINE(void DeleteData (T * data))163 INLINE(void DeleteData(T* data)) {
164 AllocationPolicy::Delete(data);
165 }
166
167 // Increase the capacity of a full list, and add an element.
168 // List must be full already.
169 void ResizeAdd(const T& element, AllocationPolicy allocator);
170
171 // Inlined implementation of ResizeAdd, shared by inlined and
172 // non-inlined versions of ResizeAdd.
173 void ResizeAddInternal(const T& element, AllocationPolicy allocator);
174
175 // Resize the list.
176 void Resize(int new_capacity, AllocationPolicy allocator);
177
178 DISALLOW_COPY_AND_ASSIGN(List);
179 };
180
181
182 template<typename T, class P>
GetMemoryUsedByList(const List<T,P> & list)183 size_t GetMemoryUsedByList(const List<T, P>& list) {
184 return list.length() * sizeof(T) + sizeof(list);
185 }
186
187
188 class Map;
189 template<class> class TypeImpl;
190 struct HeapTypeConfig;
191 typedef TypeImpl<HeapTypeConfig> HeapType;
192 class Code;
193 template<typename T> class Handle;
194 typedef List<Map*> MapList;
195 typedef List<Code*> CodeList;
196 typedef List<Handle<Map> > MapHandleList;
197 typedef List<Handle<HeapType> > TypeHandleList;
198 typedef List<Handle<Code> > CodeHandleList;
199
200 // Perform binary search for an element in an already sorted
201 // list. Returns the index of the element of -1 if it was not found.
202 // |cmp| is a predicate that takes a pointer to an element of the List
203 // and returns +1 if it is greater, -1 if it is less than the element
204 // being searched.
205 template <typename T, class P>
206 int SortedListBSearch(const List<T>& list, P cmp);
207 template <typename T>
208 int SortedListBSearch(const List<T>& list, T elem);
209
210
211 } } // namespace v8::internal
212
213
214 #endif // V8_LIST_H_
215