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.
14 // ----------------------------------------------------------------------------
15 // The list is a template for very light-weight lists. We are not
16 // using the STL because we want full control over space and speed of
17 // the code. This implementation is based on code by Robert Griesemer
20 // The list is parameterized by the type of its elements (T) and by an
21 // allocation policy (P). The policy is used for allocating lists in
22 // the C free store or the zone; see zone.h.
25 // template <typename T,
26 // class AllocationPolicy = FreeStoreAllocationPolicy> class List;
27 template <typename T, class AllocationPolicy>
30 explicit List(AllocationPolicy allocator = AllocationPolicy()) {
31 Initialize(0, allocator);
33 INLINE(explicit List(int capacity,
34 AllocationPolicy allocator = AllocationPolicy())) {
35 Initialize(capacity, allocator);
37 INLINE(~List()) { DeleteData(data_); }
39 // Deallocates memory used by the list and leaves the list in a consistent
46 INLINE(void* operator new(size_t size,
47 AllocationPolicy allocator = AllocationPolicy())) {
48 return allocator.New(static_cast<int>(size));
50 INLINE(void operator delete(void* p)) {
51 AllocationPolicy::Delete(p);
54 // Please the MSVC compiler. We should never have to execute this.
55 INLINE(void operator delete(void* p, AllocationPolicy allocator)) {
59 // Returns a reference to the element at index i. This reference is
60 // not safe to use after operations that can change the list's
61 // backing store (e.g. Add).
62 inline T& operator[](int i) const {
64 SLOW_ASSERT(i < length_);
67 inline T& at(int i) const { return operator[](i); }
68 inline T& last() const { return at(length_ - 1); }
69 inline T& first() const { return at(0); }
72 inline iterator begin() const { return &data_[0]; }
73 inline iterator end() const { return &data_[length_]; }
75 INLINE(bool is_empty() const) { return length_ == 0; }
76 INLINE(int length() const) { return length_; }
77 INLINE(int capacity() const) { return capacity_; }
79 Vector<T> ToVector() const { return Vector<T>(data_, length_); }
81 Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); }
83 // Adds a copy of the given 'element' to the end of the list,
84 // expanding the list if necessary.
85 void Add(const T& element, AllocationPolicy allocator = AllocationPolicy());
87 // Add all the elements from the argument list to this list.
88 void AddAll(const List<T, AllocationPolicy>& other,
89 AllocationPolicy allocator = AllocationPolicy());
91 // Add all the elements from the vector to this list.
92 void AddAll(const Vector<T>& other,
93 AllocationPolicy allocator = AllocationPolicy());
95 // Inserts the element at the specific index.
96 void InsertAt(int index, const T& element,
97 AllocationPolicy allocator = AllocationPolicy());
99 // Overwrites the element at the specific index.
100 void Set(int index, const T& element);
102 // Added 'count' elements with the value 'value' and returns a
103 // vector that allows access to the elements. The vector is valid
104 // until the next change is made to this list.
105 Vector<T> AddBlock(T value, int count,
106 AllocationPolicy allocator = AllocationPolicy());
108 // Removes the i'th element without deleting it even if T is a
109 // pointer type; moves all elements above i "down". Returns the
110 // removed element. This function's complexity is linear in the
114 // Remove the given element from the list. Returns whether or not
115 // the input is included in the list in the first place.
116 bool RemoveElement(const T& elm);
118 // Removes the last element without deleting it even if T is a
119 // pointer type. Returns the removed element.
120 INLINE(T RemoveLast()) { return Remove(length_ - 1); }
122 // Deletes current list contents and allocates space for 'length' elements.
123 INLINE(void Allocate(int length,
124 AllocationPolicy allocator = AllocationPolicy()));
126 // Clears the list by setting the length to zero. Even if T is a
127 // pointer type, clearing the list doesn't delete the entries.
128 INLINE(void Clear());
130 // Drops all but the first 'pos' elements from the list.
131 INLINE(void Rewind(int pos));
133 // Drop the last 'count' elements from the list.
134 INLINE(void RewindBy(int count)) { Rewind(length_ - count); }
136 // Halve the capacity if fill level is less than a quarter.
137 INLINE(void Trim(AllocationPolicy allocator = AllocationPolicy()));
139 bool Contains(const T& elm) const;
140 int CountOccurrences(const T& elm, int start, int end) const;
142 // Iterate through all list entries, starting at index 0.
143 void Iterate(void (*callback)(T* x));
144 template<class Visitor>
145 void Iterate(Visitor* visitor);
147 // Sort all list entries (using QuickSort)
148 void Sort(int (*cmp)(const T* x, const T* y));
151 INLINE(void Initialize(int capacity,
152 AllocationPolicy allocator = AllocationPolicy()));
159 INLINE(T* NewData(int n, AllocationPolicy allocator)) {
160 return static_cast<T*>(allocator.New(n * sizeof(T)));
162 INLINE(void DeleteData(T* data)) {
163 AllocationPolicy::Delete(data);
166 // Increase the capacity of a full list, and add an element.
167 // List must be full already.
168 void ResizeAdd(const T& element, AllocationPolicy allocator);
170 // Inlined implementation of ResizeAdd, shared by inlined and
171 // non-inlined versions of ResizeAdd.
172 void ResizeAddInternal(const T& element, AllocationPolicy allocator);
175 void Resize(int new_capacity, AllocationPolicy allocator);
177 DISALLOW_COPY_AND_ASSIGN(List);
181 template<typename T, class P>
182 size_t GetMemoryUsedByList(const List<T, P>& list) {
183 return list.length() * sizeof(T) + sizeof(list);
188 template<class> class TypeImpl;
189 struct HeapTypeConfig;
190 typedef TypeImpl<HeapTypeConfig> HeapType;
192 template<typename T> class Handle;
193 typedef List<Map*> MapList;
194 typedef List<Code*> CodeList;
195 typedef List<Handle<Map> > MapHandleList;
196 typedef List<Handle<HeapType> > TypeHandleList;
197 typedef List<Handle<Code> > CodeHandleList;
199 // Perform binary search for an element in an already sorted
200 // list. Returns the index of the element of -1 if it was not found.
201 // |cmp| is a predicate that takes a pointer to an element of the List
202 // and returns +1 if it is greater, -1 if it is less than the element
204 template <typename T, class P>
205 int SortedListBSearch(const List<T>& list, P cmp);
206 template <typename T>
207 int SortedListBSearch(const List<T>& list, T elem);
210 } } // namespace v8::internal