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.
8 #include "src/checks.h"
14 template<typename T> class Vector;
16 // ----------------------------------------------------------------------------
17 // The list is a template for very light-weight lists. We are not
18 // using the STL because we want full control over space and speed of
19 // the code. This implementation is based on code by Robert Griesemer
22 // The list is parameterized by the type of its elements (T) and by an
23 // allocation policy (P). The policy is used for allocating lists in
24 // the C free store or the zone; see zone.h.
27 // template <typename T,
28 // class AllocationPolicy = FreeStoreAllocationPolicy> class List;
29 template <typename T, class AllocationPolicy>
32 explicit List(AllocationPolicy allocator = AllocationPolicy()) {
33 Initialize(0, allocator);
35 INLINE(explicit List(int capacity,
36 AllocationPolicy allocator = AllocationPolicy())) {
37 Initialize(capacity, allocator);
39 INLINE(~List()) { DeleteData(data_); }
41 // Deallocates memory used by the list and leaves the list in a consistent
48 INLINE(void* operator new(size_t size,
49 AllocationPolicy allocator = AllocationPolicy())) {
50 return allocator.New(static_cast<int>(size));
52 INLINE(void operator delete(void* p)) {
53 AllocationPolicy::Delete(p);
56 // Please the MSVC compiler. We should never have to execute this.
57 INLINE(void operator delete(void* p, AllocationPolicy allocator)) {
61 // Returns a reference to the element at index i. This reference is
62 // not safe to use after operations that can change the list's
63 // backing store (e.g. Add).
64 inline T& operator[](int i) const {
66 SLOW_DCHECK(static_cast<unsigned>(i) < static_cast<unsigned>(length_));
69 inline T& at(int i) const { return operator[](i); }
70 inline T& last() const { return at(length_ - 1); }
71 inline T& first() const { return at(0); }
74 inline iterator begin() const { return &data_[0]; }
75 inline iterator end() const { return &data_[length_]; }
77 INLINE(bool is_empty() const) { return length_ == 0; }
78 INLINE(int length() const) { return length_; }
79 INLINE(int capacity() const) { return capacity_; }
81 Vector<T> ToVector() const { return Vector<T>(data_, length_); }
83 Vector<const T> ToConstVector() const {
84 return Vector<const T>(data_, length_);
87 // Adds a copy of the given 'element' to the end of the list,
88 // expanding the list if necessary.
89 void Add(const T& element, AllocationPolicy allocator = AllocationPolicy());
91 // Add all the elements from the argument list to this list.
92 void AddAll(const List<T, AllocationPolicy>& other,
93 AllocationPolicy allocator = AllocationPolicy());
95 // Add all the elements from the vector to this list.
96 void AddAll(const Vector<T>& other,
97 AllocationPolicy allocator = AllocationPolicy());
99 // Inserts the element at the specific index.
100 void InsertAt(int index, const T& element,
101 AllocationPolicy allocator = AllocationPolicy());
103 // Overwrites the element at the specific index.
104 void Set(int index, const T& element);
106 // Added 'count' elements with the value 'value' and returns a
107 // vector that allows access to the elements. The vector is valid
108 // until the next change is made to this list.
109 Vector<T> AddBlock(T value, int count,
110 AllocationPolicy allocator = AllocationPolicy());
112 // Removes the i'th element without deleting it even if T is a
113 // pointer type; moves all elements above i "down". Returns the
114 // removed element. This function's complexity is linear in the
118 // Remove the given element from the list. Returns whether or not
119 // the input is included in the list in the first place.
120 bool RemoveElement(const T& elm);
122 // Removes the last element without deleting it even if T is a
123 // pointer type. Returns the removed element.
124 INLINE(T RemoveLast()) { return Remove(length_ - 1); }
126 // Deletes current list contents and allocates space for 'length' elements.
127 INLINE(void Allocate(int length,
128 AllocationPolicy allocator = AllocationPolicy()));
130 // Clears the list by setting the length to zero. Even if T is a
131 // pointer type, clearing the list doesn't delete the entries.
132 INLINE(void Clear());
134 // Drops all but the first 'pos' elements from the list.
135 INLINE(void Rewind(int pos));
137 // Drop the last 'count' elements from the list.
138 INLINE(void RewindBy(int count)) { Rewind(length_ - count); }
140 // Halve the capacity if fill level is less than a quarter.
141 INLINE(void Trim(AllocationPolicy allocator = AllocationPolicy()));
143 bool Contains(const T& elm) const;
144 int CountOccurrences(const T& elm, int start, int end) const;
146 // Iterate through all list entries, starting at index 0.
147 void Iterate(void (*callback)(T* x));
148 template<class Visitor>
149 void Iterate(Visitor* visitor);
151 // Sort all list entries (using QuickSort)
152 void Sort(int (*cmp)(const T* x, const T* y));
155 INLINE(void Initialize(int capacity,
156 AllocationPolicy allocator = AllocationPolicy()));
163 INLINE(T* NewData(int n, AllocationPolicy allocator)) {
164 return static_cast<T*>(allocator.New(n * sizeof(T)));
166 INLINE(void DeleteData(T* data)) {
167 AllocationPolicy::Delete(data);
170 // Increase the capacity of a full list, and add an element.
171 // List must be full already.
172 void ResizeAdd(const T& element, AllocationPolicy allocator);
174 // Inlined implementation of ResizeAdd, shared by inlined and
175 // non-inlined versions of ResizeAdd.
176 void ResizeAddInternal(const T& element, AllocationPolicy allocator);
179 void Resize(int new_capacity, AllocationPolicy allocator);
181 DISALLOW_COPY_AND_ASSIGN(List);
185 template<typename T, class P>
186 size_t GetMemoryUsedByList(const List<T, P>& list) {
187 return list.length() * sizeof(T) + sizeof(list);
192 template<class> class TypeImpl;
193 struct HeapTypeConfig;
194 typedef TypeImpl<HeapTypeConfig> HeapType;
196 template<typename T> class Handle;
197 typedef List<Map*> MapList;
198 typedef List<Code*> CodeList;
199 typedef List<Handle<Map> > MapHandleList;
200 typedef List<Handle<HeapType> > TypeHandleList;
201 typedef List<Handle<Code> > CodeHandleList;
203 // Perform binary search for an element in an already sorted
204 // list. Returns the index of the element of -1 if it was not found.
205 // |cmp| is a predicate that takes a pointer to an element of the List
206 // and returns +1 if it is greater, -1 if it is less than the element
208 template <typename T, class P>
209 int SortedListBSearch(const List<T>& list, P cmp);
210 template <typename T>
211 int SortedListBSearch(const List<T>& list, T elem);
214 } } // namespace v8::internal