fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // 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.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 31 | #include "utils.h" |
| 32 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | |
| 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 |
| 48 | // template <typename T, class P = FreeStoreAllocationPolicy> class List; |
| 49 | template <typename T, class P> |
| 50 | class List { |
| 51 | public: |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 52 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 53 | List() { Initialize(0); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 54 | INLINE(explicit List(int capacity)) { Initialize(capacity); } |
| 55 | INLINE(~List()) { DeleteData(data_); } |
| 56 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 57 | // Deallocates memory used by the list and leaves the list in a consistent |
| 58 | // empty state. |
| 59 | void Free() { |
| 60 | DeleteData(data_); |
| 61 | Initialize(0); |
| 62 | } |
| 63 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 64 | INLINE(void* operator new(size_t size)) { |
| 65 | return P::New(static_cast<int>(size)); |
| 66 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 67 | INLINE(void operator delete(void* p, size_t)) { return P::Delete(p); } |
| 68 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 69 | // Returns a reference to the element at index i. This reference is |
| 70 | // not safe to use after operations that can change the list's |
| 71 | // backing store (eg, Add). |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 72 | inline T& operator[](int i) const { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 73 | ASSERT(0 <= i); |
| 74 | ASSERT(i < length_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 75 | return data_[i]; |
| 76 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 77 | inline T& at(int i) const { return operator[](i); } |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 78 | inline T& last() const { return at(length_ - 1); } |
| 79 | inline T& first() const { return at(0); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 80 | |
| 81 | INLINE(bool is_empty() const) { return length_ == 0; } |
| 82 | INLINE(int length() const) { return length_; } |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 83 | INLINE(int capacity() const) { return capacity_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 84 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 85 | Vector<T> ToVector() const { return Vector<T>(data_, length_); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 86 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 87 | Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); } |
| 88 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 89 | // Adds a copy of the given 'element' to the end of the list, |
| 90 | // expanding the list if necessary. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 91 | void Add(const T& element); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 92 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 93 | // Add all the elements from the argument list to this list. |
| 94 | void AddAll(const List<T, P>& other); |
| 95 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 96 | // Add all the elements from the vector to this list. |
| 97 | void AddAll(const Vector<T>& other); |
| 98 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 99 | // Inserts the element at the specific index. |
| 100 | void InsertAt(int index, const T& element); |
| 101 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 105 | Vector<T> AddBlock(T value, int count); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 106 | |
| 107 | // Removes the i'th element without deleting it even if T is a |
| 108 | // pointer type; moves all elements above i "down". Returns the |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 109 | // removed element. This function's complexity is linear in the |
| 110 | // size of the list. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 111 | T Remove(int i); |
| 112 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 113 | // Remove the given element from the list. Returns whether or not |
| 114 | // the input is included in the list in the first place. |
| 115 | bool RemoveElement(const T& elm); |
| 116 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | // Removes the last element without deleting it even if T is a |
| 118 | // pointer type. Returns the removed element. |
| 119 | INLINE(T RemoveLast()) { return Remove(length_ - 1); } |
| 120 | |
| 121 | // Clears the list by setting the length to zero. Even if T is a |
| 122 | // pointer type, clearing the list doesn't delete the entries. |
| 123 | INLINE(void Clear()); |
| 124 | |
| 125 | // Drops all but the first 'pos' elements from the list. |
| 126 | INLINE(void Rewind(int pos)); |
| 127 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 128 | // Drop the last 'count' elements from the list. |
| 129 | INLINE(void RewindBy(int count)) { Rewind(length_ - count); } |
| 130 | |
| 131 | bool Contains(const T& elm) const; |
| 132 | int CountOccurrences(const T& elm, int start, int end) const; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 133 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 134 | // Iterate through all list entries, starting at index 0. |
| 135 | void Iterate(void (*callback)(T* x)); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 136 | template<class Visitor> |
| 137 | void Iterate(Visitor* visitor); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 138 | |
| 139 | // Sort all list entries (using QuickSort) |
| 140 | void Sort(int (*cmp)(const T* x, const T* y)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 141 | void Sort(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 142 | |
| 143 | INLINE(void Initialize(int capacity)); |
| 144 | |
| 145 | private: |
| 146 | T* data_; |
| 147 | int capacity_; |
| 148 | int length_; |
| 149 | |
| 150 | INLINE(T* NewData(int n)) { return static_cast<T*>(P::New(n * sizeof(T))); } |
| 151 | INLINE(void DeleteData(T* data)) { P::Delete(data); } |
| 152 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 153 | // Increase the capacity of a full list, and add an element. |
| 154 | // List must be full already. |
| 155 | void ResizeAdd(const T& element); |
| 156 | |
| 157 | // Inlined implementation of ResizeAdd, shared by inlined and |
| 158 | // non-inlined versions of ResizeAdd. |
| 159 | void ResizeAddInternal(const T& element); |
| 160 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 161 | // Resize the list. |
| 162 | void Resize(int new_capacity); |
| 163 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 164 | DISALLOW_COPY_AND_ASSIGN(List); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 167 | class Map; |
| 168 | class Code; |
| 169 | typedef List<Map*> MapList; |
| 170 | typedef List<Code*> CodeList; |
| 171 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 172 | } } // namespace v8::internal |
| 173 | |
| 174 | #endif // V8_LIST_H_ |