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.
13 template<typename T> class Vector;
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
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.
26 // template <typename T,
27 // class AllocationPolicy = FreeStoreAllocationPolicy> class List;
28 template <typename T, class AllocationPolicy>
31 explicit List(AllocationPolicy allocator = AllocationPolicy()) {
32 Initialize(0, allocator);
34 INLINE(explicit List(int capacity,
35 AllocationPolicy allocator = AllocationPolicy())) {
36 Initialize(capacity, allocator);
38 INLINE(~List()) { DeleteData(data_); }
40 // Deallocates memory used by the list and leaves the list in a consistent
47 INLINE(void* operator new(size_t size,
48 AllocationPolicy allocator = AllocationPolicy())) {
49 return allocator.New(static_cast<int>(size));
51 INLINE(void operator delete(void* p)) {
52 AllocationPolicy::Delete(p);
55 // Please the MSVC compiler. We should never have to execute this.
56 INLINE(void operator delete(void* p, AllocationPolicy allocator)) {
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 {
65 SLOW_ASSERT(i < length_);
68 inline T& at(int i) const { return operator[](i); }
69 inline T& last() const { return at(length_ - 1); }
70 inline T& first() const { return at(0); }
73 inline iterator begin() const { return &data_[0]; }
74 inline iterator end() const { return &data_[length_]; }
76 INLINE(bool is_empty() const) { return length_ == 0; }
77 INLINE(int length() const) { return length_; }
78 INLINE(int capacity() const) { return capacity_; }
80 Vector<T> ToVector() const { return Vector<T>(data_, length_); }
82 Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); }
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());
88 // Add all the elements from the argument list to this list.
89 void AddAll(const List<T, AllocationPolicy>& other,
90 AllocationPolicy allocator = AllocationPolicy());
92 // Add all the elements from the vector to this list.
93 void AddAll(const Vector<T>& other,
94 AllocationPolicy allocator = AllocationPolicy());
96 // Inserts the element at the specific index.
97 void InsertAt(int index, const T& element,
98 AllocationPolicy allocator = AllocationPolicy());
100 // Overwrites the element at the specific index.
101 void Set(int index, const T& element);
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());
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
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);
119 // Removes the last element without deleting it even if T is a
120 // pointer type. Returns the removed element.
121 INLINE(T RemoveLast()) { return Remove(length_ - 1); }
123 // Deletes current list contents and allocates space for 'length' elements.
124 INLINE(void Allocate(int length,
125 AllocationPolicy allocator = AllocationPolicy()));
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());
131 // Drops all but the first 'pos' elements from the list.
132 INLINE(void Rewind(int pos));
134 // Drop the last 'count' elements from the list.
135 INLINE(void RewindBy(int count)) { Rewind(length_ - count); }
137 // Halve the capacity if fill level is less than a quarter.
138 INLINE(void Trim(AllocationPolicy allocator = AllocationPolicy()));
140 bool Contains(const T& elm) const;
141 int CountOccurrences(const T& elm, int start, int end) const;
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);
148 // Sort all list entries (using QuickSort)
149 void Sort(int (*cmp)(const T* x, const T* y));
152 INLINE(void Initialize(int capacity,
153 AllocationPolicy allocator = AllocationPolicy()));
160 INLINE(T* NewData(int n, AllocationPolicy allocator)) {
161 return static_cast<T*>(allocator.New(n * sizeof(T)));
163 INLINE(void DeleteData(T* data)) {
164 AllocationPolicy::Delete(data);
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);
171 // Inlined implementation of ResizeAdd, shared by inlined and
172 // non-inlined versions of ResizeAdd.
173 void ResizeAddInternal(const T& element, AllocationPolicy allocator);
176 void Resize(int new_capacity, AllocationPolicy allocator);
178 DISALLOW_COPY_AND_ASSIGN(List);
182 template<typename T, class P>
183 size_t GetMemoryUsedByList(const List<T, P>& list) {
184 return list.length() * sizeof(T) + sizeof(list);
189 template<class> class TypeImpl;
190 struct HeapTypeConfig;
191 typedef TypeImpl<HeapTypeConfig> HeapType;
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;
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
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);
211 } } // namespace v8::internal