Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: src/list.h

Issue 6265002: Adding SearchableList for implementing a list with fast search capability. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. 1 // Copyright 2006-2009 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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 void Iterate(void (*callback)(T* x)); 130 void Iterate(void (*callback)(T* x));
131 template<class Visitor> 131 template<class Visitor>
132 void Iterate(Visitor* visitor); 132 void Iterate(Visitor* visitor);
133 133
134 // Sort all list entries (using QuickSort) 134 // Sort all list entries (using QuickSort)
135 void Sort(int (*cmp)(const T* x, const T* y)); 135 void Sort(int (*cmp)(const T* x, const T* y));
136 void Sort(); 136 void Sort();
137 137
138 INLINE(void Initialize(int capacity)); 138 INLINE(void Initialize(int capacity));
139 139
140 protected:
141 INLINE(T* data() const) { return data_; }
142
140 private: 143 private:
141 T* data_; 144 T* data_;
142 int capacity_; 145 int capacity_;
143 int length_; 146 int length_;
144 147
145 INLINE(T* NewData(int n)) { return static_cast<T*>(P::New(n * sizeof(T))); } 148 INLINE(T* NewData(int n)) { return static_cast<T*>(P::New(n * sizeof(T))); }
146 INLINE(void DeleteData(T* data)) { P::Delete(data); } 149 INLINE(void DeleteData(T* data)) { P::Delete(data); }
147 150
148 // Increase the capacity of a full list, and add an element. 151 // Increase the capacity of a full list, and add an element.
149 // List must be full already. 152 // List must be full already.
150 void ResizeAdd(const T& element); 153 void ResizeAdd(const T& element);
151 154
152 // Inlined implementation of ResizeAdd, shared by inlined and 155 // Inlined implementation of ResizeAdd, shared by inlined and
153 // non-inlined versions of ResizeAdd. 156 // non-inlined versions of ResizeAdd.
154 void ResizeAddInternal(const T& element); 157 void ResizeAddInternal(const T& element);
155 158
156 // Resize the list. 159 // Resize the list.
157 void Resize(int new_capacity); 160 void Resize(int new_capacity);
158 161
159 DISALLOW_COPY_AND_ASSIGN(List); 162 DISALLOW_COPY_AND_ASSIGN(List);
160 }; 163 };
161 164
165
166 // The SearchableList is just like the List except that it keeps track of
167 // whether its elements are sorted. If sorted, the Contains method will
168 // take advantage of the list having being sorted and use a binary search on
169 // it. Otherwise, it will let the super class List do a linear look up.
170 //
171 // The SearchableList is not sorted automatically. The user must call Sort()
172 // when appropriate. This allows multiple Add()s to be called without
173 // repeatedly incurring the cost of sorting.
174 template <typename T, class P>
175 class SearchableList: public List<T, P> {
176 public:
177 explicit SearchableList(int (*cmp)(const T* x, const T* y))
178 : is_sorted_(false), comparator_(cmp) {}
179
180 // Adds a copy of the given 'element' to the end of the list,
181 // expanding the list if necessary.
182 void Add(const T& element);
183
184 // Add all the elements from the argument list to this list.
185 void AddAll(const List<T, P>& other);
186
187 // Added 'count' elements with the value 'value' and returns a
188 // vector that allows access to the elements. The vector is valid
189 // until the next change is made to this list.
190 Vector<T> AddBlock(T value, int count);
191
192 bool Contains(const T& elm);
193 bool Contains(const T& elm, bool ensure_sorted);
194
195 // Sort all list elements (using QuickSort). This function only performs
196 // a sort if the list is not already sorted to begin with.
197 void Sort();
198
199 bool is_sorted() const { return is_sorted_; }
200
201 private:
202 bool is_sorted_;
203 int (*comparator_)(const T* x, const T* y);
204
205 bool ContainsSorted(const T& elm);
206
207 DISALLOW_COPY_AND_ASSIGN(SearchableList);
208 };
209
210
162 } } // namespace v8::internal 211 } } // namespace v8::internal
163 212
164 #endif // V8_LIST_H_ 213 #endif // V8_LIST_H_
OLDNEW
« src/globals.h ('K') | « src/globals.h ('k') | src/list-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698