1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 // ----------------------------------------------------------------------------
38 // The list is a template for very light-weight lists. We are not
39 // using the STL because we want full control over space and speed of
40 // the code. This implementation is based on code by Robert Griesemer
43 // The list is parameterized by the type of its elements (T) and by an
44 // allocation policy (P). The policy is used for allocating lists in
45 // the C free store or the zone; see zone.h.
48 // template <typename T,
49 // class AllocationPolicy = FreeStoreAllocationPolicy> class List;
50 template <typename T, class AllocationPolicy>
51 class List : private AllocationPolicy::Deleter {
53 explicit List(AllocationPolicy allocator = AllocationPolicy())
54 : AllocationPolicy::Deleter(allocator) {
55 Initialize(0, allocator);
57 INLINE(explicit List(int capacity,
58 AllocationPolicy allocator = AllocationPolicy()))
59 : AllocationPolicy::Deleter(allocator) {
60 Initialize(capacity, allocator);
62 INLINE(~List()) { DeleteData(data_); }
64 // Deallocates memory used by the list and leaves the list in a consistent
71 INLINE(void* operator new(size_t size,
72 AllocationPolicy allocator = AllocationPolicy())) {
73 return allocator.New(static_cast<int>(size));
75 INLINE(void operator delete(void* p)) {
76 AllocationPolicy::Deleter::Delete(p);
79 // Please the MSVC compiler. We should never have to execute this.
80 INLINE(void operator delete(void* p, AllocationPolicy allocator)) {
84 // Delete via the instance Deleter
85 static void Delete(List* p) {
86 if (p == NULL) return;
88 p->AllocationPolicy::Deleter::Delete(p);
91 // Returns a reference to the element at index i. This reference is
92 // not safe to use after operations that can change the list's
93 // backing store (e.g. Add).
94 inline T& operator[](int i) const {
99 inline T& at(int i) const { return operator[](i); }
100 inline T& last() const { return at(length_ - 1); }
101 inline T& first() const { return at(0); }
103 INLINE(bool is_empty() const) { return length_ == 0; }
104 INLINE(int length() const) { return length_; }
105 INLINE(int capacity() const) { return capacity_; }
107 Vector<T> ToVector() const { return Vector<T>(data_, length_); }
109 Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); }
111 // Adds a copy of the given 'element' to the end of the list,
112 // expanding the list if necessary.
113 void Add(const T& element, AllocationPolicy allocator = AllocationPolicy());
115 // Add all the elements from the argument list to this list.
116 void AddAll(const List<T, AllocationPolicy>& other,
117 AllocationPolicy allocator = AllocationPolicy());
119 // Add all the elements from the vector to this list.
120 void AddAll(const Vector<T>& other,
121 AllocationPolicy allocator = AllocationPolicy());
123 // Inserts the element at the specific index.
124 void InsertAt(int index, const T& element,
125 AllocationPolicy allocator = AllocationPolicy());
127 // Overwrites the element at the specific index.
128 void Set(int index, const T& element);
130 // Added 'count' elements with the value 'value' and returns a
131 // vector that allows access to the elements. The vector is valid
132 // until the next change is made to this list.
133 Vector<T> AddBlock(T value, int count,
134 AllocationPolicy allocator = AllocationPolicy());
136 // Removes the i'th element without deleting it even if T is a
137 // pointer type; moves all elements above i "down". Returns the
138 // removed element. This function's complexity is linear in the
142 // Remove the given element from the list. Returns whether or not
143 // the input is included in the list in the first place.
144 bool RemoveElement(const T& elm);
146 // Removes the last element without deleting it even if T is a
147 // pointer type. Returns the removed element.
148 INLINE(T RemoveLast()) { return Remove(length_ - 1); }
150 // Deletes current list contents and allocates space for 'length' elements.
151 INLINE(void Allocate(int length,
152 AllocationPolicy allocator = AllocationPolicy()));
154 // Clears the list by setting the length to zero. Even if T is a
155 // pointer type, clearing the list doesn't delete the entries.
156 INLINE(void Clear());
158 // Drops all but the first 'pos' elements from the list.
159 INLINE(void Rewind(int pos));
161 // Drop the last 'count' elements from the list.
162 INLINE(void RewindBy(int count)) { Rewind(length_ - count); }
164 // Halve the capacity if fill level is less than a quarter.
165 INLINE(void Trim(AllocationPolicy allocator = AllocationPolicy()));
167 bool Contains(const T& elm) const;
168 int CountOccurrences(const T& elm, int start, int end) const;
170 // Iterate through all list entries, starting at index 0.
171 void Iterate(void (*callback)(T* x));
172 template<class Visitor>
173 void Iterate(Visitor* visitor);
175 // Sort all list entries (using QuickSort)
176 void Sort(int (*cmp)(const T* x, const T* y));
179 INLINE(void Initialize(int capacity,
180 AllocationPolicy allocator = AllocationPolicy()));
187 INLINE(T* NewData(int n, AllocationPolicy allocator)) {
188 return static_cast<T*>(allocator.New(n * sizeof(T)));
190 INLINE(void DeleteData(T* data)) {
191 this->AllocationPolicy::Deleter::Delete(data);
194 // Increase the capacity of a full list, and add an element.
195 // List must be full already.
196 void ResizeAdd(const T& element, AllocationPolicy allocator);
198 // Inlined implementation of ResizeAdd, shared by inlined and
199 // non-inlined versions of ResizeAdd.
200 void ResizeAddInternal(const T& element, AllocationPolicy allocator);
203 void Resize(int new_capacity, AllocationPolicy allocator);
205 DISALLOW_COPY_AND_ASSIGN(List);
210 template<typename T> class Handle;
211 typedef List<Map*> MapList;
212 typedef List<Code*> CodeList;
213 typedef List<Handle<Map> > MapHandleList;
214 typedef List<Handle<Code> > CodeHandleList;
216 // Perform binary search for an element in an already sorted
217 // list. Returns the index of the element of -1 if it was not found.
218 // |cmp| is a predicate that takes a pointer to an element of the List
219 // and returns +1 if it is greater, -1 if it is less than the element
221 template <typename T, class P>
222 int SortedListBSearch(const List<T>& list, P cmp);
223 template <typename T>
224 int SortedListBSearch(const List<T>& list, T elem);
227 } } // namespace v8::internal