説明を見る。00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef V8_LIST_INL_H_
00029 #define V8_LIST_INL_H_
00030
00031 #include "list.h"
00032
00033 namespace v8 { namespace internal {
00034
00035
00036 template<typename T, class P>
00037 T& List<T, P>::Add(const T& element) {
00038 if (length_ >= capacity_) {
00039
00040
00041 int new_capacity = 1 + capacity_ + (capacity_ >> 1);
00042 T* new_data = NewData(new_capacity);
00043 memcpy(new_data, data_, capacity_ * sizeof(T));
00044 DeleteData(data_);
00045 data_ = new_data;
00046 capacity_ = new_capacity;
00047 }
00048 return data_[length_++] = element;
00049 }
00050
00051
00052 template<typename T, class P>
00053 Vector<T> List<T, P>::AddBlock(const T& element, int count) {
00054 int start = length_;
00055 for (int i = 0; i < count; i++)
00056 Add(element);
00057 return Vector<T>(&data_[start], count);
00058 }
00059
00060
00061 template<typename T, class P>
00062 T List<T, P>::Remove(int i) {
00063 T element = at(i);
00064 length_--;
00065 while (i < length_) {
00066 data_[i] = data_[i + 1];
00067 i++;
00068 }
00069 return element;
00070 }
00071
00072
00073 template<typename T, class P>
00074 void List<T, P>::Clear() {
00075 DeleteData(data_);
00076 Initialize(0);
00077 }
00078
00079
00080 template<typename T, class P>
00081 void List<T, P>::Rewind(int pos) {
00082 length_ = pos;
00083 }
00084
00085
00086 template<typename T, class P>
00087 void List<T, P>::Iterate(void (*callback)(T* x)) {
00088 for (int i = 0; i < length_; i++) callback(&data_[i]);
00089 }
00090
00091
00092 template<typename T, class P>
00093 void List<T, P>::Sort(int (*cmp)(const T* x, const T* y)) {
00094 qsort(data_,
00095 length_,
00096 sizeof(T),
00097 reinterpret_cast<int (*)(const void*, const void*)>(cmp));
00098 #ifdef DEBUG
00099 for (int i = 1; i < length_; i++)
00100 ASSERT(cmp(&data_[i - 1], &data_[i]) <= 0);
00101 #endif
00102 }
00103
00104
00105 template<typename T, class P>
00106 void List<T, P>::Initialize(int capacity) {
00107 ASSERT(capacity >= 0);
00108 data_ = (capacity > 0) ? NewData(capacity) : NULL;
00109 capacity_ = capacity;
00110 length_ = 0;
00111 }
00112
00113
00114 } }
00115
00116 #endif // V8_LIST_INL_H_