OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 28 matching lines...) Expand all Loading... |
39 // | 39 // |
40 // The list is parameterized by the type of its elements (T) and by an | 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 | 41 // allocation policy (P). The policy is used for allocating lists in |
42 // the C free store or the zone; see zone.h. | 42 // the C free store or the zone; see zone.h. |
43 | 43 |
44 // Forward defined as | 44 // Forward defined as |
45 // template <typename T, class P = FreeStoreAllocationPolicy> class List; | 45 // template <typename T, class P = FreeStoreAllocationPolicy> class List; |
46 template <typename T, class P> | 46 template <typename T, class P> |
47 class List { | 47 class List { |
48 public: | 48 public: |
| 49 |
49 INLINE(explicit List(int capacity)) { Initialize(capacity); } | 50 INLINE(explicit List(int capacity)) { Initialize(capacity); } |
50 INLINE(~List()) { DeleteData(data_); } | 51 INLINE(~List()) { DeleteData(data_); } |
51 | 52 |
52 INLINE(void* operator new(size_t size)) { return P::New(size); } | 53 INLINE(void* operator new(size_t size)) { return P::New(size); } |
53 INLINE(void operator delete(void* p, size_t)) { return P::Delete(p); } | 54 INLINE(void operator delete(void* p, size_t)) { return P::Delete(p); } |
54 | 55 |
55 inline T& operator[](int i) const { | 56 inline T& operator[](int i) const { |
56 ASSERT(0 <= i && i < length_); | 57 ASSERT(0 <= i && i < length_); |
57 return data_[i]; | 58 return data_[i]; |
58 } | 59 } |
59 inline T& at(int i) const { return this->operator[](i); } | 60 inline T& at(int i) const { return this->operator[](i); } |
60 INLINE(const T& last() const) { | 61 INLINE(const T& last() const) { |
61 ASSERT(!is_empty()); | 62 ASSERT(!is_empty()); |
62 return this->at(length_ - 1); | 63 return this->at(length_ - 1); |
63 } | 64 } |
64 | 65 |
65 INLINE(bool is_empty() const) { return length_ == 0; } | 66 INLINE(bool is_empty() const) { return length_ == 0; } |
66 INLINE(int length() const) { return length_; } | 67 INLINE(int length() const) { return length_; } |
67 | 68 |
68 Vector<T> ToVector() { return Vector<T>(data_, length_); } | 69 Vector<T> ToVector() { return Vector<T>(data_, length_); } |
69 | 70 |
| 71 Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); } |
| 72 |
70 // Adds a copy of the given 'element' to the end of the list, | 73 // Adds a copy of the given 'element' to the end of the list, |
71 // expanding the list if necessary. | 74 // expanding the list if necessary. |
72 T& Add(const T& element); | 75 T& Add(const T& element); |
73 | 76 |
74 // Added 'count' elements with the value 'value' and returns a | 77 // Added 'count' elements with the value 'value' and returns a |
75 // vector that allows access to the elements. The vector is valid | 78 // vector that allows access to the elements. The vector is valid |
76 // until the next change is made to this list. | 79 // until the next change is made to this list. |
77 Vector<T> AddBlock(const T& value, int count); | 80 Vector<T> AddBlock(const T& value, int count); |
78 | 81 |
79 // Removes the i'th element without deleting it even if T is a | 82 // Removes the i'th element without deleting it even if T is a |
80 // pointer type; moves all elements above i "down". Returns the | 83 // pointer type; moves all elements above i "down". Returns the |
81 // removed element. | 84 // removed element. |
82 T Remove(int i); | 85 T Remove(int i); |
83 | 86 |
84 // Removes the last element without deleting it even if T is a | 87 // Removes the last element without deleting it even if T is a |
85 // pointer type. Returns the removed element. | 88 // pointer type. Returns the removed element. |
86 INLINE(T RemoveLast()) { return Remove(length_ - 1); } | 89 INLINE(T RemoveLast()) { return Remove(length_ - 1); } |
87 | 90 |
88 // Clears the list by setting the length to zero. Even if T is a | 91 // Clears the list by setting the length to zero. Even if T is a |
89 // pointer type, clearing the list doesn't delete the entries. | 92 // pointer type, clearing the list doesn't delete the entries. |
90 INLINE(void Clear()); | 93 INLINE(void Clear()); |
91 | 94 |
92 // Drops all but the first 'pos' elements from the list. | 95 // Drops all but the first 'pos' elements from the list. |
93 INLINE(void Rewind(int pos)); | 96 INLINE(void Rewind(int pos)); |
94 | 97 |
| 98 bool Contains(const T& elm); |
| 99 |
95 // Iterate through all list entries, starting at index 0. | 100 // Iterate through all list entries, starting at index 0. |
96 void Iterate(void (*callback)(T* x)); | 101 void Iterate(void (*callback)(T* x)); |
97 | 102 |
98 // Sort all list entries (using QuickSort) | 103 // Sort all list entries (using QuickSort) |
99 void Sort(int (*cmp)(const T* x, const T* y)); | 104 void Sort(int (*cmp)(const T* x, const T* y)); |
| 105 void Sort(); |
100 | 106 |
101 INLINE(void Initialize(int capacity)); | 107 INLINE(void Initialize(int capacity)); |
102 | 108 |
103 private: | 109 private: |
104 T* data_; | 110 T* data_; |
105 int capacity_; | 111 int capacity_; |
106 int length_; | 112 int length_; |
107 | 113 |
108 INLINE(T* NewData(int n)) { return static_cast<T*>(P::New(n * sizeof(T))); } | 114 INLINE(T* NewData(int n)) { return static_cast<T*>(P::New(n * sizeof(T))); } |
109 INLINE(void DeleteData(T* data)) { P::Delete(data); } | 115 INLINE(void DeleteData(T* data)) { P::Delete(data); } |
110 | 116 |
111 DISALLOW_COPY_AND_ASSIGN(List); | 117 DISALLOW_COPY_AND_ASSIGN(List); |
112 }; | 118 }; |
113 | 119 |
114 | 120 |
115 } } // namespace v8::internal | 121 } } // namespace v8::internal |
116 | 122 |
117 #endif // V8_LIST_H_ | 123 #endif // V8_LIST_H_ |
OLD | NEW |