00001 // Copyright 2006-2008 the V8 project authors. All rights reserved. 00002 // Redistribution and use in source and binary forms, with or without 00003 // modification, are permitted provided that the following conditions are 00004 // met: 00005 // 00006 // * Redistributions of source code must retain the above copyright 00007 // notice, this list of conditions and the following disclaimer. 00008 // * Redistributions in binary form must reproduce the above 00009 // copyright notice, this list of conditions and the following 00010 // disclaimer in the documentation and/or other materials provided 00011 // with the distribution. 00012 // * Neither the name of Google Inc. nor the names of its 00013 // contributors may be used to endorse or promote products derived 00014 // from this software without specific prior written permission. 00015 // 00016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00017 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00018 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00019 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00020 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00021 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00026 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 00028 #ifndef V8_LIST_H_ 00029 #define V8_LIST_H_ 00030 00031 namespace v8 { namespace internal { 00032 00033 00034 // ---------------------------------------------------------------------------- 00035 // The list is a template for very light-weight lists. We are not 00036 // using the STL because we want full control over space and speed of 00037 // the code. This implementation is based on code by Robert Griesemer 00038 // and Rob Pike. 00039 // 00040 // The list is parameterized by the type of its elements (T) and by an 00041 // allocation policy (P). The policy is used for allocating lists in 00042 // the C free store or the zone; see zone.h. 00043 00044 // Forward defined as 00045 // template <typename T, class P = FreeStoreAllocationPolicy> class List; 00046 template <typename T, class P> 00047 class List { 00048 public: 00049 INLINE(explicit List(int capacity)) { Initialize(capacity); } 00050 INLINE(~List()) { DeleteData(data_); } 00051 00052 INLINE(void* operator new(size_t size)) { return P::New(size); } 00053 INLINE(void operator delete(void* p, size_t)) { return P::Delete(p); } 00054 00055 inline T& operator[](int i) const { 00056 ASSERT(0 <= i && i < length_); 00057 return data_[i]; 00058 } 00059 inline T& at(int i) const { return this->operator[](i); } 00060 INLINE(const T& last() const) { 00061 ASSERT(!is_empty()); 00062 return this->at(length_ - 1); 00063 } 00064 00065 INLINE(bool is_empty() const) { return length_ == 0; } 00066 INLINE(int length() const) { return length_; } 00067 00068 Vector<T> ToVector() { return Vector<T>(data_, length_); } 00069 00070 // Adds a copy of the given 'element' to the end of the list, 00071 // expanding the list if necessary. 00072 T& Add(const T& element); 00073 00074 // Added 'count' elements with the value 'value' and returns a 00075 // vector that allows access to the elements. The vector is valid 00076 // until the next change is made to this list. 00077 Vector<T> AddBlock(const T& value, int count); 00078 00079 // Removes the i'th element without deleting it even if T is a 00080 // pointer type; moves all elements above i "down". Returns the 00081 // removed element. 00082 T Remove(int i); 00083 00084 // Removes the last element without deleting it even if T is a 00085 // pointer type. Returns the removed element. 00086 INLINE(T RemoveLast()) { return Remove(length_ - 1); } 00087 00088 // Clears the list by setting the length to zero. Even if T is a 00089 // pointer type, clearing the list doesn't delete the entries. 00090 INLINE(void Clear()); 00091 00092 // Drops all but the first 'pos' elements from the list. 00093 INLINE(void Rewind(int pos)); 00094 00095 // Iterate through all list entries, starting at index 0. 00096 void Iterate(void (*callback)(T* x)); 00097 00098 // Sort all list entries (using QuickSort) 00099 void Sort(int (*cmp)(const T* x, const T* y)); 00100 00101 INLINE(void Initialize(int capacity)); 00102 00103 private: 00104 T* data_; 00105 int capacity_; 00106 int length_; 00107 00108 INLINE(T* NewData(int n)) { return static_cast<T*>(P::New(n * sizeof(T))); } 00109 INLINE(void DeleteData(T* data)) { P::Delete(data); } 00110 00111 DISALLOW_COPY_AND_ASSIGN(List); 00112 }; 00113 00114 00115 } } // namespace v8::internal 00116 00117 #endif // V8_LIST_H_