blob: 2f8aa906f1d03d66d4349b45e449f345ffb8e2ae [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 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
31namespace v8 { namespace internal {
32
33
34// ----------------------------------------------------------------------------
35// The list is a template for very light-weight lists. We are not
36// using the STL because we want full control over space and speed of
37// the code. This implementation is based on code by Robert Griesemer
38// and Rob Pike.
39//
40// The list is parameterized by the type of its elements (T) and by an
41// allocation policy (P). The policy is used for allocating lists in
42// the C free store or the zone; see zone.h.
43
44// Forward defined as
45// template <typename T, class P = FreeStoreAllocationPolicy> class List;
46template <typename T, class P>
47class List {
48 public:
ager@chromium.orga74f0da2008-12-03 16:05:52 +000049
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050 INLINE(explicit List(int capacity)) { Initialize(capacity); }
51 INLINE(~List()) { DeleteData(data_); }
52
53 INLINE(void* operator new(size_t size)) { return P::New(size); }
54 INLINE(void operator delete(void* p, size_t)) { return P::Delete(p); }
55
56 inline T& operator[](int i) const {
57 ASSERT(0 <= i && i < length_);
58 return data_[i];
59 }
60 inline T& at(int i) const { return this->operator[](i); }
61 INLINE(const T& last() const) {
62 ASSERT(!is_empty());
63 return this->at(length_ - 1);
64 }
65
66 INLINE(bool is_empty() const) { return length_ == 0; }
67 INLINE(int length() const) { return length_; }
68
69 Vector<T> ToVector() { return Vector<T>(data_, length_); }
70
ager@chromium.orga74f0da2008-12-03 16:05:52 +000071 Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); }
72
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073 // Adds a copy of the given 'element' to the end of the list,
74 // expanding the list if necessary.
75 T& Add(const T& element);
76
77 // Added 'count' elements with the value 'value' and returns a
78 // vector that allows access to the elements. The vector is valid
79 // until the next change is made to this list.
80 Vector<T> AddBlock(const T& value, int count);
81
82 // Removes the i'th element without deleting it even if T is a
83 // pointer type; moves all elements above i "down". Returns the
84 // removed element.
85 T Remove(int i);
86
87 // Removes the last element without deleting it even if T is a
88 // pointer type. Returns the removed element.
89 INLINE(T RemoveLast()) { return Remove(length_ - 1); }
90
91 // Clears the list by setting the length to zero. Even if T is a
92 // pointer type, clearing the list doesn't delete the entries.
93 INLINE(void Clear());
94
95 // Drops all but the first 'pos' elements from the list.
96 INLINE(void Rewind(int pos));
97
ager@chromium.orga74f0da2008-12-03 16:05:52 +000098 bool Contains(const T& elm);
99
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000100 // Iterate through all list entries, starting at index 0.
101 void Iterate(void (*callback)(T* x));
102
103 // Sort all list entries (using QuickSort)
104 void Sort(int (*cmp)(const T* x, const T* y));
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000105 void Sort();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000106
107 INLINE(void Initialize(int capacity));
108
109 private:
110 T* data_;
111 int capacity_;
112 int length_;
113
114 INLINE(T* NewData(int n)) { return static_cast<T*>(P::New(n * sizeof(T))); }
115 INLINE(void DeleteData(T* data)) { P::Delete(data); }
116
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000117 DISALLOW_COPY_AND_ASSIGN(List);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118};
119
120
121} } // namespace v8::internal
122
123#endif // V8_LIST_H_