Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / v8 / src / list.h
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_LIST_H_
6 #define V8_LIST_H_
7
8 #include "utils.h"
9
10 namespace v8 {
11 namespace internal {
12
13
14 // ----------------------------------------------------------------------------
15 // The list is a template for very light-weight lists. We are not
16 // using the STL because we want full control over space and speed of
17 // the code. This implementation is based on code by Robert Griesemer
18 // and Rob Pike.
19 //
20 // The list is parameterized by the type of its elements (T) and by an
21 // allocation policy (P). The policy is used for allocating lists in
22 // the C free store or the zone; see zone.h.
23
24 // Forward defined as
25 // template <typename T,
26 //           class AllocationPolicy = FreeStoreAllocationPolicy> class List;
27 template <typename T, class AllocationPolicy>
28 class List {
29  public:
30   explicit List(AllocationPolicy allocator = AllocationPolicy()) {
31     Initialize(0, allocator);
32   }
33   INLINE(explicit List(int capacity,
34                        AllocationPolicy allocator = AllocationPolicy())) {
35     Initialize(capacity, allocator);
36   }
37   INLINE(~List()) { DeleteData(data_); }
38
39   // Deallocates memory used by the list and leaves the list in a consistent
40   // empty state.
41   void Free() {
42     DeleteData(data_);
43     Initialize(0);
44   }
45
46   INLINE(void* operator new(size_t size,
47                             AllocationPolicy allocator = AllocationPolicy())) {
48     return allocator.New(static_cast<int>(size));
49   }
50   INLINE(void operator delete(void* p)) {
51     AllocationPolicy::Delete(p);
52   }
53
54   // Please the MSVC compiler.  We should never have to execute this.
55   INLINE(void operator delete(void* p, AllocationPolicy allocator)) {
56     UNREACHABLE();
57   }
58
59   // Returns a reference to the element at index i.  This reference is
60   // not safe to use after operations that can change the list's
61   // backing store (e.g. Add).
62   inline T& operator[](int i) const {
63     ASSERT(0 <= i);
64     SLOW_ASSERT(i < length_);
65     return data_[i];
66   }
67   inline T& at(int i) const { return operator[](i); }
68   inline T& last() const { return at(length_ - 1); }
69   inline T& first() const { return at(0); }
70
71   typedef T* iterator;
72   inline iterator begin() const { return &data_[0]; }
73   inline iterator end() const { return &data_[length_]; }
74
75   INLINE(bool is_empty() const) { return length_ == 0; }
76   INLINE(int length() const) { return length_; }
77   INLINE(int capacity() const) { return capacity_; }
78
79   Vector<T> ToVector() const { return Vector<T>(data_, length_); }
80
81   Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); }
82
83   // Adds a copy of the given 'element' to the end of the list,
84   // expanding the list if necessary.
85   void Add(const T& element, AllocationPolicy allocator = AllocationPolicy());
86
87   // Add all the elements from the argument list to this list.
88   void AddAll(const List<T, AllocationPolicy>& other,
89               AllocationPolicy allocator = AllocationPolicy());
90
91   // Add all the elements from the vector to this list.
92   void AddAll(const Vector<T>& other,
93               AllocationPolicy allocator = AllocationPolicy());
94
95   // Inserts the element at the specific index.
96   void InsertAt(int index, const T& element,
97                 AllocationPolicy allocator = AllocationPolicy());
98
99   // Overwrites the element at the specific index.
100   void Set(int index, const T& element);
101
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.
105   Vector<T> AddBlock(T value, int count,
106                      AllocationPolicy allocator = AllocationPolicy());
107
108   // Removes the i'th element without deleting it even if T is a
109   // pointer type; moves all elements above i "down". Returns the
110   // removed element.  This function's complexity is linear in the
111   // size of the list.
112   T Remove(int i);
113
114   // Remove the given element from the list. Returns whether or not
115   // the input is included in the list in the first place.
116   bool RemoveElement(const T& elm);
117
118   // Removes the last element without deleting it even if T is a
119   // pointer type. Returns the removed element.
120   INLINE(T RemoveLast()) { return Remove(length_ - 1); }
121
122   // Deletes current list contents and allocates space for 'length' elements.
123   INLINE(void Allocate(int length,
124                        AllocationPolicy allocator = AllocationPolicy()));
125
126   // Clears the list by setting the length to zero. Even if T is a
127   // pointer type, clearing the list doesn't delete the entries.
128   INLINE(void Clear());
129
130   // Drops all but the first 'pos' elements from the list.
131   INLINE(void Rewind(int pos));
132
133   // Drop the last 'count' elements from the list.
134   INLINE(void RewindBy(int count)) { Rewind(length_ - count); }
135
136   // Halve the capacity if fill level is less than a quarter.
137   INLINE(void Trim(AllocationPolicy allocator = AllocationPolicy()));
138
139   bool Contains(const T& elm) const;
140   int CountOccurrences(const T& elm, int start, int end) const;
141
142   // Iterate through all list entries, starting at index 0.
143   void Iterate(void (*callback)(T* x));
144   template<class Visitor>
145   void Iterate(Visitor* visitor);
146
147   // Sort all list entries (using QuickSort)
148   void Sort(int (*cmp)(const T* x, const T* y));
149   void Sort();
150
151   INLINE(void Initialize(int capacity,
152                          AllocationPolicy allocator = AllocationPolicy()));
153
154  private:
155   T* data_;
156   int capacity_;
157   int length_;
158
159   INLINE(T* NewData(int n, AllocationPolicy allocator))  {
160     return static_cast<T*>(allocator.New(n * sizeof(T)));
161   }
162   INLINE(void DeleteData(T* data))  {
163     AllocationPolicy::Delete(data);
164   }
165
166   // Increase the capacity of a full list, and add an element.
167   // List must be full already.
168   void ResizeAdd(const T& element, AllocationPolicy allocator);
169
170   // Inlined implementation of ResizeAdd, shared by inlined and
171   // non-inlined versions of ResizeAdd.
172   void ResizeAddInternal(const T& element, AllocationPolicy allocator);
173
174   // Resize the list.
175   void Resize(int new_capacity, AllocationPolicy allocator);
176
177   DISALLOW_COPY_AND_ASSIGN(List);
178 };
179
180
181 template<typename T, class P>
182 size_t GetMemoryUsedByList(const List<T, P>& list) {
183   return list.length() * sizeof(T) + sizeof(list);
184 }
185
186
187 class Map;
188 template<class> class TypeImpl;
189 struct HeapTypeConfig;
190 typedef TypeImpl<HeapTypeConfig> HeapType;
191 class Code;
192 template<typename T> class Handle;
193 typedef List<Map*> MapList;
194 typedef List<Code*> CodeList;
195 typedef List<Handle<Map> > MapHandleList;
196 typedef List<Handle<HeapType> > TypeHandleList;
197 typedef List<Handle<Code> > CodeHandleList;
198
199 // Perform binary search for an element in an already sorted
200 // list. Returns the index of the element of -1 if it was not found.
201 // |cmp| is a predicate that takes a pointer to an element of the List
202 // and returns +1 if it is greater, -1 if it is less than the element
203 // being searched.
204 template <typename T, class P>
205 int SortedListBSearch(const List<T>& list, P cmp);
206 template <typename T>
207 int SortedListBSearch(const List<T>& list, T elem);
208
209
210 } }  // namespace v8::internal
211
212
213 #endif  // V8_LIST_H_