v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
list.h
Go to the documentation of this file.
1 // Copyright 2011 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
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 
31 #include "utils.h"
32 
33 namespace v8 {
34 namespace internal {
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,
49 // class AllocationPolicy = FreeStoreAllocationPolicy> class List;
50 template <typename T, class AllocationPolicy>
51 class List {
52  public:
53  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  }
60  INLINE(~List()) { DeleteData(data_); }
61 
62  // 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 
69  INLINE(void* operator new(size_t size,
70  AllocationPolicy allocator = AllocationPolicy())) {
71  return allocator.New(static_cast<int>(size));
72  }
73  INLINE(void operator delete(void* p)) {
74  AllocationPolicy::Delete(p);
75  }
76 
77  // Please the MSVC compiler. We should never have to execute this.
78  INLINE(void operator delete(void* p, AllocationPolicy allocator)) {
79  UNREACHABLE();
80  }
81 
82  // 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
84  // backing store (e.g. Add).
85  inline T& operator[](int i) const {
86  ASSERT(0 <= i);
87  SLOW_ASSERT(i < length_);
88  return data_[i];
89  }
90  inline T& at(int i) const { return operator[](i); }
91  inline T& last() const { return at(length_ - 1); }
92  inline T& first() const { return at(0); }
93 
94  typedef T* iterator;
95  inline iterator begin() const { return &data_[0]; }
96  inline iterator end() const { return &data_[length_]; }
97 
98  INLINE(bool is_empty() const) { return length_ == 0; }
99  INLINE(int length() const) { return length_; }
100  INLINE(int capacity() const) { return capacity_; }
101 
102  Vector<T> ToVector() const { return Vector<T>(data_, length_); }
103 
104  Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); }
105 
106  // Adds a copy of the given 'element' to the end of the list,
107  // expanding the list if necessary.
108  void Add(const T& element, AllocationPolicy allocator = AllocationPolicy());
109 
110  // Add all the elements from the argument list to this list.
111  void AddAll(const List<T, AllocationPolicy>& other,
112  AllocationPolicy allocator = AllocationPolicy());
113 
114  // Add all the elements from the vector to this list.
115  void AddAll(const Vector<T>& other,
116  AllocationPolicy allocator = AllocationPolicy());
117 
118  // Inserts the element at the specific index.
119  void InsertAt(int index, const T& element,
120  AllocationPolicy allocator = AllocationPolicy());
121 
122  // Overwrites the element at the specific index.
123  void Set(int index, const T& element);
124 
125  // Added 'count' elements with the value 'value' and returns a
126  // vector that allows access to the elements. The vector is valid
127  // until the next change is made to this list.
128  Vector<T> AddBlock(T value, int count,
129  AllocationPolicy allocator = AllocationPolicy());
130 
131  // Removes the i'th element without deleting it even if T is a
132  // pointer type; moves all elements above i "down". Returns the
133  // removed element. This function's complexity is linear in the
134  // size of the list.
135  T Remove(int i);
136 
137  // Remove the given element from the list. Returns whether or not
138  // the input is included in the list in the first place.
139  bool RemoveElement(const T& elm);
140 
141  // Removes the last element without deleting it even if T is a
142  // pointer type. Returns the removed element.
143  INLINE(T RemoveLast()) { return Remove(length_ - 1); }
144 
145  // Deletes current list contents and allocates space for 'length' elements.
146  INLINE(void Allocate(int length,
147  AllocationPolicy allocator = AllocationPolicy()));
148 
149  // Clears the list by setting the length to zero. Even if T is a
150  // pointer type, clearing the list doesn't delete the entries.
151  INLINE(void Clear());
152 
153  // Drops all but the first 'pos' elements from the list.
154  INLINE(void Rewind(int pos));
155 
156  // Drop the last 'count' elements from the list.
157  INLINE(void RewindBy(int count)) { Rewind(length_ - count); }
158 
159  // Halve the capacity if fill level is less than a quarter.
160  INLINE(void Trim(AllocationPolicy allocator = AllocationPolicy()));
161 
162  bool Contains(const T& elm) const;
163  int CountOccurrences(const T& elm, int start, int end) const;
164 
165  // Iterate through all list entries, starting at index 0.
166  void Iterate(void (*callback)(T* x));
167  template<class Visitor>
168  void Iterate(Visitor* visitor);
169 
170  // Sort all list entries (using QuickSort)
171  void Sort(int (*cmp)(const T* x, const T* y));
172  void Sort();
173 
174  INLINE(void Initialize(int capacity,
175  AllocationPolicy allocator = AllocationPolicy()));
176 
177  private:
178  T* data_;
179  int capacity_;
180  int length_;
181 
182  INLINE(T* NewData(int n, AllocationPolicy allocator)) {
183  return static_cast<T*>(allocator.New(n * sizeof(T)));
184  }
185  INLINE(void DeleteData(T* data)) {
186  AllocationPolicy::Delete(data);
187  }
188 
189  // Increase the capacity of a full list, and add an element.
190  // List must be full already.
191  void ResizeAdd(const T& element, AllocationPolicy allocator);
192 
193  // Inlined implementation of ResizeAdd, shared by inlined and
194  // non-inlined versions of ResizeAdd.
195  void ResizeAddInternal(const T& element, AllocationPolicy allocator);
196 
197  // Resize the list.
198  void Resize(int new_capacity, AllocationPolicy allocator);
199 
200  DISALLOW_COPY_AND_ASSIGN(List);
201 };
202 
203 
204 template<typename T, class P>
205 size_t GetMemoryUsedByList(const List<T, P>& list) {
206  return list.length() * sizeof(T) + sizeof(list);
207 }
208 
209 
210 class Map;
211 template<class> class TypeImpl;
214 class Code;
215 template<typename T> class Handle;
216 typedef List<Map*> MapList;
221 
222 // Perform binary search for an element in an already sorted
223 // list. Returns the index of the element of -1 if it was not found.
224 // |cmp| is a predicate that takes a pointer to an element of the List
225 // and returns +1 if it is greater, -1 if it is less than the element
226 // being searched.
227 template <typename T, class P>
228 int SortedListBSearch(const List<T>& list, P cmp);
229 template <typename T>
230 int SortedListBSearch(const List<T>& list, T elem);
231 
232 
233 } } // namespace v8::internal
234 
235 
236 #endif // V8_LIST_H_
#define SLOW_ASSERT(condition)
Definition: checks.h:306
void InsertAt(int index, const T &element, AllocationPolicy allocator=AllocationPolicy())
Definition: list-inl.h:114
bool Contains(const T &elm) const
Definition: list-inl.h:196
Vector< const T > ToConstVector()
Definition: list.h:104
iterator end() const
Definition: list.h:96
T & at(int i) const
Definition: list.h:90
List< Handle< Map > > MapHandleList
Definition: list.h:218
#define ASSERT(condition)
Definition: checks.h:329
INLINE(T RemoveLast())
Definition: list.h:143
#define UNREACHABLE()
Definition: checks.h:52
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object size
Definition: flags.cc:211
T & last() const
Definition: list.h:91
INLINE(bool is_empty() const)
Definition: list.h:98
iterator begin() const
Definition: list.h:95
Vector< T > ToVector() const
Definition: list.h:102
List(AllocationPolicy allocator=AllocationPolicy())
Definition: list.h:53
T Remove(int i)
Definition: list-inl.h:125
INLINE(explicit List(int capacity, AllocationPolicy allocator=AllocationPolicy()))
Definition: list.h:56
List< Code * > CodeList
Definition: list.h:217
List< Handle< HeapType > > TypeHandleList
Definition: list.h:219
INLINE(void *operator new(size_t size, AllocationPolicy allocator=AllocationPolicy()))
Definition: list.h:69
#define T(name, string, precedence)
Definition: token.cc:48
List< Handle< Code > > CodeHandleList
Definition: list.h:220
void Set(int index, const T &element)
Definition: list-inl.h:107
int CountOccurrences(const T &elm, int start, int end) const
Definition: list-inl.h:206
INLINE(~List())
Definition: list.h:60
size_t GetMemoryUsedByList(const List< T, P > &list)
Definition: list.h:205
INLINE(int length() const)
Definition: list.h:99
T & first() const
Definition: list.h:92
List< Map * > MapList
Definition: list.h:215
int SortedListBSearch(const List< T > &list, P cmp)
Definition: list-inl.h:241
T & operator[](int i) const
Definition: list.h:85
INLINE(void operator delete(void *p, AllocationPolicy allocator))
Definition: list.h:78
void Add(const T &element, AllocationPolicy allocator=AllocationPolicy())
Definition: list-inl.h:39
void Iterate(void(*callback)(T *x))
Definition: list-inl.h:183
TypeImpl< HeapTypeConfig > HeapType
Definition: list.h:212
INLINE(void RewindBy(int count))
Definition: list.h:157
Vector< T > AddBlock(T value, int count, AllocationPolicy allocator=AllocationPolicy())
Definition: list-inl.h:99
bool RemoveElement(const T &elm)
Definition: list-inl.h:137
INLINE(void operator delete(void *p))
Definition: list.h:73
void AddAll(const List< T, AllocationPolicy > &other, AllocationPolicy allocator=AllocationPolicy())
INLINE(int capacity() const)
Definition: list.h:100
void Free()
Definition: list.h:64