blob: 7fd4f5cd2d6f6af7b8e2675252347b560c95dbdd [file] [log] [blame]
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
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.
15//
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.
27
28#ifndef V8_LIST_H_
29#define V8_LIST_H_
30
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000031#include "utils.h"
32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035
36
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
41// and Rob Pike.
42//
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.
46
47// Forward defined as
rossberg@chromium.org400388e2012-06-06 09:29:22 +000048// template <typename T,
49// class AllocationPolicy = FreeStoreAllocationPolicy> class List;
50template <typename T, class AllocationPolicy>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051class List {
52 public:
rossberg@chromium.org400388e2012-06-06 09:29:22 +000053 explicit List(AllocationPolicy allocator = AllocationPolicy()) {
54 Initialize(0, allocator);
55 }
56 INLINE(explicit List(int capacity,
57 AllocationPolicy allocator = AllocationPolicy())) {
58 Initialize(capacity, allocator);
59 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000060 INLINE(~List()) { DeleteData(data_); }
61
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000062 // Deallocates memory used by the list and leaves the list in a consistent
63 // empty state.
64 void Free() {
65 DeleteData(data_);
66 Initialize(0);
67 }
68
rossberg@chromium.org400388e2012-06-06 09:29:22 +000069 INLINE(void* operator new(size_t size,
70 AllocationPolicy allocator = AllocationPolicy())) {
71 return allocator.New(static_cast<int>(size));
ager@chromium.orgc4c92722009-11-18 14:12:51 +000072 }
rossberg@chromium.org400388e2012-06-06 09:29:22 +000073 INLINE(void operator delete(void* p)) {
74 AllocationPolicy::Delete(p);
75 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000077 // Please the MSVC compiler. We should never have to execute this.
78 INLINE(void operator delete(void* p, AllocationPolicy allocator)) {
79 UNREACHABLE();
80 }
81
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000082 // Returns a reference to the element at index i. This reference is
83 // not safe to use after operations that can change the list's
ulan@chromium.org2efb9002012-01-19 15:36:35 +000084 // backing store (e.g. Add).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +000085 inline T& operator[](int i) const {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000086 ASSERT(0 <= i);
87 ASSERT(i < length_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088 return data_[i];
89 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +000090 inline T& at(int i) const { return operator[](i); }
ager@chromium.orga1645e22009-09-09 19:27:10 +000091 inline T& last() const { return at(length_ - 1); }
92 inline T& first() const { return at(0); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093
94 INLINE(bool is_empty() const) { return length_ == 0; }
95 INLINE(int length() const) { return length_; }
ager@chromium.org71daaf62009-04-01 07:22:49 +000096 INLINE(int capacity() const) { return capacity_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +000098 Vector<T> ToVector() const { return Vector<T>(data_, length_); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000100 Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); }
101
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102 // Adds a copy of the given 'element' to the end of the list,
103 // expanding the list if necessary.
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000104 void Add(const T& element, AllocationPolicy allocator = AllocationPolicy());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000106 // Add all the elements from the argument list to this list.
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000107 void AddAll(const List<T, AllocationPolicy>& other,
108 AllocationPolicy allocator = AllocationPolicy());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000109
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000110 // Add all the elements from the vector to this list.
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000111 void AddAll(const Vector<T>& other,
112 AllocationPolicy allocator = AllocationPolicy());
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000113
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000114 // Inserts the element at the specific index.
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000115 void InsertAt(int index, const T& element,
116 AllocationPolicy allocator = AllocationPolicy());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000117
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118 // Added 'count' elements with the value 'value' and returns a
119 // vector that allows access to the elements. The vector is valid
120 // until the next change is made to this list.
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000121 Vector<T> AddBlock(T value, int count,
122 AllocationPolicy allocator = AllocationPolicy());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123
124 // Removes the i'th element without deleting it even if T is a
125 // pointer type; moves all elements above i "down". Returns the
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000126 // removed element. This function's complexity is linear in the
127 // size of the list.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000128 T Remove(int i);
129
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000130 // Remove the given element from the list. Returns whether or not
131 // the input is included in the list in the first place.
132 bool RemoveElement(const T& elm);
133
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134 // Removes the last element without deleting it even if T is a
135 // pointer type. Returns the removed element.
136 INLINE(T RemoveLast()) { return Remove(length_ - 1); }
137
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000138 // Deletes current list contents and allocates space for 'length' elements.
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000139 INLINE(void Allocate(int length,
140 AllocationPolicy allocator = AllocationPolicy()));
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000141
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142 // Clears the list by setting the length to zero. Even if T is a
143 // pointer type, clearing the list doesn't delete the entries.
144 INLINE(void Clear());
145
146 // Drops all but the first 'pos' elements from the list.
147 INLINE(void Rewind(int pos));
148
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000149 // Drop the last 'count' elements from the list.
150 INLINE(void RewindBy(int count)) { Rewind(length_ - count); }
151
152 bool Contains(const T& elm) const;
153 int CountOccurrences(const T& elm, int start, int end) const;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000154
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155 // Iterate through all list entries, starting at index 0.
156 void Iterate(void (*callback)(T* x));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000157 template<class Visitor>
158 void Iterate(Visitor* visitor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159
160 // Sort all list entries (using QuickSort)
161 void Sort(int (*cmp)(const T* x, const T* y));
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000162 void Sort();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000164 INLINE(void Initialize(int capacity,
165 AllocationPolicy allocator = AllocationPolicy()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166
167 private:
168 T* data_;
169 int capacity_;
170 int length_;
171
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000172 INLINE(T* NewData(int n, AllocationPolicy allocator)) {
173 return static_cast<T*>(allocator.New(n * sizeof(T)));
174 }
175 INLINE(void DeleteData(T* data)) {
176 AllocationPolicy::Delete(data);
177 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178
ager@chromium.org5ec48922009-05-05 07:25:34 +0000179 // Increase the capacity of a full list, and add an element.
180 // List must be full already.
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000181 void ResizeAdd(const T& element, AllocationPolicy allocator);
ager@chromium.org5ec48922009-05-05 07:25:34 +0000182
183 // Inlined implementation of ResizeAdd, shared by inlined and
184 // non-inlined versions of ResizeAdd.
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000185 void ResizeAddInternal(const T& element, AllocationPolicy allocator);
ager@chromium.org5ec48922009-05-05 07:25:34 +0000186
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000187 // Resize the list.
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000188 void Resize(int new_capacity, AllocationPolicy allocator);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000189
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000190 DISALLOW_COPY_AND_ASSIGN(List);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191};
192
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000193class Map;
194class Code;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000195template<typename T> class Handle;
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000196typedef List<Map*> MapList;
197typedef List<Code*> CodeList;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000198typedef List<Handle<Map> > MapHandleList;
199typedef List<Handle<Code> > CodeHandleList;
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000200
lrn@chromium.org34e60782011-09-15 07:25:40 +0000201// Perform binary search for an element in an already sorted
202// list. Returns the index of the element of -1 if it was not found.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000203// |cmp| is a predicate that takes a pointer to an element of the List
204// and returns +1 if it is greater, -1 if it is less than the element
205// being searched.
206template <typename T, class P>
207int SortedListBSearch(const List<T>& list, P cmp);
lrn@chromium.org34e60782011-09-15 07:25:40 +0000208template <typename T>
209int SortedListBSearch(const List<T>& list, T elem);
210
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000211
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212} } // namespace v8::internal
213
lrn@chromium.org34e60782011-09-15 07:25:40 +0000214
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215#endif // V8_LIST_H_