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

Unified 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 side-by-side diff with in-line comments
Download patch
« src/globals.h ('K') | « src/globals.h ('k') | src/list-inl.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/list.h
===================================================================
--- src/list.h (revision 6372)
+++ src/list.h (working copy)
@@ -137,6 +137,9 @@
INLINE(void Initialize(int capacity));
+ protected:
+ INLINE(T* data() const) { return data_; }
+
private:
T* data_;
int capacity_;
@@ -159,6 +162,52 @@
DISALLOW_COPY_AND_ASSIGN(List);
};
+
+// The SearchableList is just like the List except that it keeps track of
+// whether its elements are sorted. If sorted, the Contains method will
+// take advantage of the list having being sorted and use a binary search on
+// it. Otherwise, it will let the super class List do a linear look up.
+//
+// The SearchableList is not sorted automatically. The user must call Sort()
+// when appropriate. This allows multiple Add()s to be called without
+// repeatedly incurring the cost of sorting.
+template <typename T, class P>
+class SearchableList: public List<T, P> {
+ public:
+ explicit SearchableList(int (*cmp)(const T* x, const T* y))
+ : is_sorted_(false), comparator_(cmp) {}
+
+ // Adds a copy of the given 'element' to the end of the list,
+ // expanding the list if necessary.
+ void Add(const T& element);
+
+ // Add all the elements from the argument list to this list.
+ void AddAll(const List<T, P>& other);
+
+ // Added 'count' elements with the value 'value' and returns a
+ // vector that allows access to the elements. The vector is valid
+ // until the next change is made to this list.
+ Vector<T> AddBlock(T value, int count);
+
+ bool Contains(const T& elm);
+ bool Contains(const T& elm, bool ensure_sorted);
+
+ // Sort all list elements (using QuickSort). This function only performs
+ // a sort if the list is not already sorted to begin with.
+ void Sort();
+
+ bool is_sorted() const { return is_sorted_; }
+
+ private:
+ bool is_sorted_;
+ int (*comparator_)(const T* x, const T* y);
+
+ bool ContainsSorted(const T& elm);
+
+ DISALLOW_COPY_AND_ASSIGN(SearchableList);
+};
+
+
} } // namespace v8::internal
#endif // V8_LIST_H_
« 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