blob: 5a08212b1e048f772bcda57db26bfd768d1ef245 [file] [log] [blame]
ager@chromium.org71daaf62009-04-01 07:22:49 +00001// Copyright 2006-2009 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
kasperl@chromium.org71affb52009-05-26 05:44:31 +000031namespace v8 {
32namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033
34
35// ----------------------------------------------------------------------------
36// The list is a template for very light-weight lists. We are not
37// using the STL because we want full control over space and speed of
38// the code. This implementation is based on code by Robert Griesemer
39// and Rob Pike.
40//
41// The list is parameterized by the type of its elements (T) and by an
42// allocation policy (P). The policy is used for allocating lists in
43// the C free store or the zone; see zone.h.
44
45// Forward defined as
46// template <typename T, class P = FreeStoreAllocationPolicy> class List;
47template <typename T, class P>
48class List {
49 public:
ager@chromium.orga74f0da2008-12-03 16:05:52 +000050
ager@chromium.org3811b432009-10-28 14:53:37 +000051 List() { Initialize(0); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052 INLINE(explicit List(int capacity)) { Initialize(capacity); }
53 INLINE(~List()) { DeleteData(data_); }
54
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000055 // Deallocates memory used by the list and leaves the list in a consistent
56 // empty state.
57 void Free() {
58 DeleteData(data_);
59 Initialize(0);
60 }
61
ager@chromium.orgc4c92722009-11-18 14:12:51 +000062 INLINE(void* operator new(size_t size)) {
63 return P::New(static_cast<int>(size));
64 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065 INLINE(void operator delete(void* p, size_t)) { return P::Delete(p); }
66
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000067 // Returns a reference to the element at index i. This reference is
68 // not safe to use after operations that can change the list's
69 // backing store (eg, Add).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +000070 inline T& operator[](int i) const {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000071 ASSERT(0 <= i);
72 ASSERT(i < length_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073 return data_[i];
74 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +000075 inline T& at(int i) const { return operator[](i); }
ager@chromium.orga1645e22009-09-09 19:27:10 +000076 inline T& last() const { return at(length_ - 1); }
77 inline T& first() const { return at(0); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078
79 INLINE(bool is_empty() const) { return length_ == 0; }
80 INLINE(int length() const) { return length_; }
ager@chromium.org71daaf62009-04-01 07:22:49 +000081 INLINE(int capacity() const) { return capacity_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000082
83 Vector<T> ToVector() { return Vector<T>(data_, length_); }
84
ager@chromium.orga74f0da2008-12-03 16:05:52 +000085 Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); }
86
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000087 // Adds a copy of the given 'element' to the end of the list,
88 // expanding the list if necessary.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000089 void Add(const T& element);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000090
kasperl@chromium.org71affb52009-05-26 05:44:31 +000091 // Add all the elements from the argument list to this list.
92 void AddAll(const List<T, P>& other);
93
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094 // Added 'count' elements with the value 'value' and returns a
95 // vector that allows access to the elements. The vector is valid
96 // until the next change is made to this list.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000097 Vector<T> AddBlock(T value, int count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098
99 // Removes the i'th element without deleting it even if T is a
100 // pointer type; moves all elements above i "down". Returns the
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000101 // removed element. This function's complexity is linear in the
102 // size of the list.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103 T Remove(int i);
104
105 // Removes the last element without deleting it even if T is a
106 // pointer type. Returns the removed element.
107 INLINE(T RemoveLast()) { return Remove(length_ - 1); }
108
109 // Clears the list by setting the length to zero. Even if T is a
110 // pointer type, clearing the list doesn't delete the entries.
111 INLINE(void Clear());
112
113 // Drops all but the first 'pos' elements from the list.
114 INLINE(void Rewind(int pos));
115
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000116 bool Contains(const T& elm);
117
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118 // Iterate through all list entries, starting at index 0.
119 void Iterate(void (*callback)(T* x));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000120 template<class Visitor>
121 void Iterate(Visitor* visitor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122
123 // Sort all list entries (using QuickSort)
124 void Sort(int (*cmp)(const T* x, const T* y));
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000125 void Sort();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126
127 INLINE(void Initialize(int capacity));
128
129 private:
130 T* data_;
131 int capacity_;
132 int length_;
133
134 INLINE(T* NewData(int n)) { return static_cast<T*>(P::New(n * sizeof(T))); }
135 INLINE(void DeleteData(T* data)) { P::Delete(data); }
136
ager@chromium.org5ec48922009-05-05 07:25:34 +0000137 // Increase the capacity of a full list, and add an element.
138 // List must be full already.
139 void ResizeAdd(const T& element);
140
141 // Inlined implementation of ResizeAdd, shared by inlined and
142 // non-inlined versions of ResizeAdd.
143 void ResizeAddInternal(const T& element);
144
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000145 // Resize the list.
146 void Resize(int new_capacity);
147
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000148 DISALLOW_COPY_AND_ASSIGN(List);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149};
150
ager@chromium.org5ec48922009-05-05 07:25:34 +0000151class FrameElement;
152
153// Add() is inlined, ResizeAdd() called by Add() is inlined except for
154// Lists of FrameElements, and ResizeAddInternal() is inlined in ResizeAdd().
155template <>
156void List<FrameElement,
157 FreeStoreAllocationPolicy>::ResizeAdd(const FrameElement& element);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158
159} } // namespace v8::internal
160
161#endif // V8_LIST_H_