1 // Copyright 2006-2009 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.
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
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.
46 // template <typename T, class P = FreeStoreAllocationPolicy> class List;
47 template <typename T, class P>
51 List() { Initialize(0); }
52 INLINE(explicit List(int capacity)) { Initialize(capacity); }
53 INLINE(~List()) { DeleteData(data_); }
55 // Deallocates memory used by the list and leaves the list in a consistent
62 INLINE(void* operator new(size_t size)) {
63 return P::New(static_cast<int>(size));
65 INLINE(void operator delete(void* p, size_t)) { return P::Delete(p); }
67 // 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).
70 inline T& operator[](int i) const {
75 inline T& at(int i) const { return operator[](i); }
76 inline T& last() const { return at(length_ - 1); }
77 inline T& first() const { return at(0); }
79 INLINE(bool is_empty() const) { return length_ == 0; }
80 INLINE(int length() const) { return length_; }
81 INLINE(int capacity() const) { return capacity_; }
83 Vector<T> ToVector() { return Vector<T>(data_, length_); }
85 Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); }
87 // Adds a copy of the given 'element' to the end of the list,
88 // expanding the list if necessary.
89 void Add(const T& element);
91 // Add all the elements from the argument list to this list.
92 void AddAll(const List<T, P>& other);
94 // Inserts the element at the specific index.
95 void InsertAt(int index, const T& element);
97 // Added 'count' elements with the value 'value' and returns a
98 // vector that allows access to the elements. The vector is valid
99 // until the next change is made to this list.
100 Vector<T> AddBlock(T value, int count);
102 // Removes the i'th element without deleting it even if T is a
103 // pointer type; moves all elements above i "down". Returns the
104 // removed element. This function's complexity is linear in the
108 // Remove the given element from the list. Returns whether or not
109 // the input is included in the list in the first place.
110 bool RemoveElement(const T& elm);
112 // Removes the last element without deleting it even if T is a
113 // pointer type. Returns the removed element.
114 INLINE(T RemoveLast()) { return Remove(length_ - 1); }
116 // Clears the list by setting the length to zero. Even if T is a
117 // pointer type, clearing the list doesn't delete the entries.
118 INLINE(void Clear());
120 // Drops all but the first 'pos' elements from the list.
121 INLINE(void Rewind(int pos));
123 // Drop the last 'count' elements from the list.
124 INLINE(void RewindBy(int count)) { Rewind(length_ - count); }
126 bool Contains(const T& elm) const;
127 int CountOccurrences(const T& elm, int start, int end) const;
129 // Iterate through all list entries, starting at index 0.
130 void Iterate(void (*callback)(T* x));
131 template<class Visitor>
132 void Iterate(Visitor* visitor);
134 // Sort all list entries (using QuickSort)
135 void Sort(int (*cmp)(const T* x, const T* y));
138 INLINE(void Initialize(int capacity));
145 INLINE(T* NewData(int n)) { return static_cast<T*>(P::New(n * sizeof(T))); }
146 INLINE(void DeleteData(T* data)) { P::Delete(data); }
148 // Increase the capacity of a full list, and add an element.
149 // List must be full already.
150 void ResizeAdd(const T& element);
152 // Inlined implementation of ResizeAdd, shared by inlined and
153 // non-inlined versions of ResizeAdd.
154 void ResizeAddInternal(const T& element);
157 void Resize(int new_capacity);
159 DISALLOW_COPY_AND_ASSIGN(List);
162 } } // namespace v8::internal