Title: [156281] trunk
Revision
156281
Author
[email protected]
Date
2013-09-23 11:21:24 -0700 (Mon, 23 Sep 2013)

Log Message

ListHashSet should use std::reverse_iterator for reverse iterators
https://bugs.webkit.org/show_bug.cgi?id=121795

Reviewed by Antti Koivisto.

Source/WTF:

Get rid of the custom reverse iterators and just use std::reverse_iterator instead.

* wtf/ListHashSet.h:

Tools:

Add test for reverse iterators.

* TestWebKitAPI/Tests/WTF/ListHashSet.cpp:
(TestWebKitAPI::TEST):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (156280 => 156281)


--- trunk/Source/WTF/ChangeLog	2013-09-23 17:54:22 UTC (rev 156280)
+++ trunk/Source/WTF/ChangeLog	2013-09-23 18:21:24 UTC (rev 156281)
@@ -1,5 +1,16 @@
 2013-09-23  Anders Carlsson  <[email protected]>
 
+        ListHashSet should use std::reverse_iterator for reverse iterators
+        https://bugs.webkit.org/show_bug.cgi?id=121795
+
+        Reviewed by Antti Koivisto.
+
+        Get rid of the custom reverse iterators and just use std::reverse_iterator instead.
+
+        * wtf/ListHashSet.h:
+
+2013-09-23  Anders Carlsson  <[email protected]>
+
         De-indent ListHashSet in preparation of more changes.
 
         Rubber-stamped by Sam Weinig.

Modified: trunk/Source/WTF/wtf/ListHashSet.h (156280 => 156281)


--- trunk/Source/WTF/wtf/ListHashSet.h	2013-09-23 17:54:22 UTC (rev 156280)
+++ trunk/Source/WTF/wtf/ListHashSet.h	2013-09-23 18:21:24 UTC (rev 156281)
@@ -45,8 +45,6 @@
 
 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetIterator;
 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetConstIterator;
-template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetReverseIterator;
-template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetConstReverseIterator;
 
 template<typename ValueArg, size_t inlineCapacity> struct ListHashSetNode;
 template<typename ValueArg, size_t inlineCapacity> struct ListHashSetNodeAllocator;
@@ -77,9 +75,8 @@
     typedef ListHashSetConstIterator<ValueType, inlineCapacity, HashArg> const_iterator;
     friend class ListHashSetConstIterator<ValueType, inlineCapacity, HashArg>;
 
-    typedef ListHashSetReverseIterator<ValueType, inlineCapacity, HashArg> reverse_iterator;
-    typedef ListHashSetConstReverseIterator<ValueType, inlineCapacity, HashArg> const_reverse_iterator;
-    friend class ListHashSetConstReverseIterator<ValueType, inlineCapacity, HashArg>;
+    typedef std::reverse_iterator<iterator> reverse_iterator;
+    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
 
     typedef HashTableAddResult<iterator> AddResult;
 
@@ -99,10 +96,10 @@
     const_iterator begin() const;
     const_iterator end() const;
 
-    reverse_iterator rbegin();
-    reverse_iterator rend();
-    const_reverse_iterator rbegin() const;
-    const_reverse_iterator rend() const;
+    reverse_iterator rbegin() { return reverse_iterator(end()); }
+    reverse_iterator rend() { return reverse_iterator(begin()); }
+    const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
+    const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
 
     ValueType& first();
     const ValueType& first() const;
@@ -156,8 +153,6 @@
     
     iterator makeIterator(Node*);
     const_iterator makeConstIterator(Node*) const;
-    reverse_iterator makeReverseIterator(Node*);
-    const_reverse_iterator makeConstReverseIterator(Node*) const;
 
     friend void deleteAllValues<>(const ListHashSet&);
 
@@ -280,21 +275,25 @@
     typedef ListHashSetConstIterator<ValueArg, inlineCapacity, HashArg> const_iterator;
     typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
     typedef ValueArg ValueType;
-    typedef ValueType& ReferenceType;
-    typedef ValueType* PointerType;
 
     friend class ListHashSet<ValueArg, inlineCapacity, HashArg>;
 
     ListHashSetIterator(const ListHashSetType* set, Node* position) : m_iterator(set, position) { }
 
 public:
+    typedef ptrdiff_t difference_type;
+    typedef ValueType value_type;
+    typedef ValueType* pointer;
+    typedef ValueType& reference;
+    typedef std::bidirectional_iterator_tag iterator_category;
+
     ListHashSetIterator() { }
 
     // default copy, assignment and destructor are OK
 
-    PointerType get() const { return const_cast<PointerType>(m_iterator.get()); }
-    ReferenceType operator*() const { return *get(); }
-    PointerType operator->() const { return get(); }
+    ValueType* get() const { return const_cast<ValueType*>(m_iterator.get()); }
+    ValueType& operator*() const { return *get(); }
+    ValueType* operator->() const { return get(); }
 
     iterator& operator++() { ++m_iterator; return *this; }
 
@@ -323,8 +322,6 @@
     typedef ListHashSetConstIterator<ValueArg, inlineCapacity, HashArg> const_iterator;
     typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
     typedef ValueArg ValueType;
-    typedef const ValueType& ReferenceType;
-    typedef const ValueType* PointerType;
 
     friend class ListHashSet<ValueArg, inlineCapacity, HashArg>;
     friend class ListHashSetIterator<ValueArg, inlineCapacity, HashArg>;
@@ -336,17 +333,24 @@
     }
 
 public:
+    typedef ptrdiff_t difference_type;
+    typedef const ValueType value_type;
+    typedef const ValueType* pointer;
+    typedef const ValueType& reference;
+    typedef std::bidirectional_iterator_tag iterator_category;
+
     ListHashSetConstIterator()
     {
     }
 
-    PointerType get() const
+    const ValueType* get() const
     {
         return &m_position->m_value;
     }
-    ReferenceType operator*() const { return *get(); }
-    PointerType operator->() const { return get(); }
 
+    const ValueType& operator*() const { return *get(); }
+    const ValueType* operator->() const { return get(); }
+
     const_iterator& operator++()
     {
         ASSERT(m_position != 0);
@@ -385,118 +389,6 @@
     Node* m_position;
 };
 
-template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetReverseIterator {
-private:
-    typedef ListHashSet<ValueArg, inlineCapacity, HashArg> ListHashSetType;
-    typedef ListHashSetReverseIterator<ValueArg, inlineCapacity, HashArg> reverse_iterator;
-    typedef ListHashSetConstReverseIterator<ValueArg, inlineCapacity, HashArg> const_reverse_iterator;
-    typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
-    typedef ValueArg ValueType;
-    typedef ValueType& ReferenceType;
-    typedef ValueType* PointerType;
-
-    friend class ListHashSet<ValueArg, inlineCapacity, HashArg>;
-
-    ListHashSetReverseIterator(const ListHashSetType* set, Node* position) : m_iterator(set, position) { }
-
-public:
-    ListHashSetReverseIterator() { }
-
-    // default copy, assignment and destructor are OK
-
-    PointerType get() const { return const_cast<PointerType>(m_iterator.get()); }
-    ReferenceType operator*() const { return *get(); }
-    PointerType operator->() const { return get(); }
-
-    reverse_iterator& operator++() { ++m_iterator; return *this; }
-
-    // postfix ++ intentionally omitted
-
-    reverse_iterator& operator--() { --m_iterator; return *this; }
-
-    // postfix -- intentionally omitted
-
-    // Comparison.
-    bool operator==(const reverse_iterator& other) const { return m_iterator == other.m_iterator; }
-    bool operator!=(const reverse_iterator& other) const { return m_iterator != other.m_iterator; }
-
-    operator const_reverse_iterator() const { return m_iterator; }
-
-private:
-    Node* node() { return m_iterator.node(); }
-
-    const_reverse_iterator m_iterator;
-};
-
-template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetConstReverseIterator {
-private:
-    typedef ListHashSet<ValueArg, inlineCapacity, HashArg> ListHashSetType;
-    typedef ListHashSetReverseIterator<ValueArg, inlineCapacity, HashArg> reverse_iterator;
-    typedef ListHashSetConstReverseIterator<ValueArg, inlineCapacity, HashArg> const_reverse_iterator;
-    typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
-    typedef ValueArg ValueType;
-    typedef const ValueType& ReferenceType;
-    typedef const ValueType* PointerType;
-
-    friend class ListHashSet<ValueArg, inlineCapacity, HashArg>;
-    friend class ListHashSetReverseIterator<ValueArg, inlineCapacity, HashArg>;
-
-    ListHashSetConstReverseIterator(const ListHashSetType* set, Node* position)
-        : m_set(set)
-        , m_position(position)
-    {
-    }
-
-public:
-    ListHashSetConstReverseIterator()
-    {
-    }
-
-    PointerType get() const
-    {
-        return &m_position->m_value;
-    }
-    ReferenceType operator*() const { return *get(); }
-    PointerType operator->() const { return get(); }
-
-    const_reverse_iterator& operator++()
-    {
-        ASSERT(m_position != 0);
-        m_position = m_position->m_prev;
-        return *this;
-    }
-
-    // postfix ++ intentionally omitted
-
-    const_reverse_iterator& operator--()
-    {
-        ASSERT(m_position != m_set->m_tail);
-        if (!m_position)
-            m_position = m_set->m_head;
-        else
-            m_position = m_position->m_next;
-        return *this;
-    }
-
-    // postfix -- intentionally omitted
-
-    // Comparison.
-    bool operator==(const const_reverse_iterator& other) const
-    {
-        return m_position == other.m_position;
-    }
-    bool operator!=(const const_reverse_iterator& other) const
-    {
-        return m_position != other.m_position;
-    }
-
-private:
-    Node* node() { return m_position; }
-
-    const ListHashSetType* m_set;
-    Node* m_position;
-};
-
 template<typename HashFunctions>
 struct ListHashSetTranslator {
     template<typename T> static unsigned hash(const T& key) { return HashFunctions::hash(key); }
@@ -592,30 +484,6 @@
 }
 
 template<typename T, size_t inlineCapacity, typename U>
-inline typename ListHashSet<T, inlineCapacity, U>::reverse_iterator ListHashSet<T, inlineCapacity, U>::rbegin()
-{
-    return makeReverseIterator(m_tail); 
-}
-
-template<typename T, size_t inlineCapacity, typename U>
-inline typename ListHashSet<T, inlineCapacity, U>::reverse_iterator ListHashSet<T, inlineCapacity, U>::rend()
-{
-    return makeReverseIterator(0);
-}
-
-template<typename T, size_t inlineCapacity, typename U>
-inline typename ListHashSet<T, inlineCapacity, U>::const_reverse_iterator ListHashSet<T, inlineCapacity, U>::rbegin() const
-{
-    return makeConstReverseIterator(m_tail); 
-}
-
-template<typename T, size_t inlineCapacity, typename U>
-inline typename ListHashSet<T, inlineCapacity, U>::const_reverse_iterator ListHashSet<T, inlineCapacity, U>::rend() const
-{
-    return makeConstReverseIterator(0); 
-}
-
-template<typename T, size_t inlineCapacity, typename U>
 inline T& ListHashSet<T, inlineCapacity, U>::first()
 {
     ASSERT(!isEmpty());
@@ -888,18 +756,6 @@
 }
 
 template<typename T, size_t inlineCapacity, typename U>
-inline ListHashSetReverseIterator<T, inlineCapacity, U> ListHashSet<T, inlineCapacity, U>::makeReverseIterator(Node* position) 
-{
-    return ListHashSetReverseIterator<T, inlineCapacity, U>(this, position); 
-}
-
-template<typename T, size_t inlineCapacity, typename U>
-inline ListHashSetConstReverseIterator<T, inlineCapacity, U> ListHashSet<T, inlineCapacity, U>::makeConstReverseIterator(Node* position) const
-{ 
-    return ListHashSetConstReverseIterator<T, inlineCapacity, U>(this, position); 
-}
-
-template<typename T, size_t inlineCapacity, typename U>
 inline ListHashSetIterator<T, inlineCapacity, U> ListHashSet<T, inlineCapacity, U>::makeIterator(Node* position) 
 {
     return ListHashSetIterator<T, inlineCapacity, U>(this, position); 

Modified: trunk/Tools/ChangeLog (156280 => 156281)


--- trunk/Tools/ChangeLog	2013-09-23 17:54:22 UTC (rev 156280)
+++ trunk/Tools/ChangeLog	2013-09-23 18:21:24 UTC (rev 156281)
@@ -1,3 +1,15 @@
+2013-09-23  Anders Carlsson  <[email protected]>
+
+        ListHashSet should use std::reverse_iterator for reverse iterators
+        https://bugs.webkit.org/show_bug.cgi?id=121795
+
+        Reviewed by Antti Koivisto.
+
+        Add test for reverse iterators.
+
+        * TestWebKitAPI/Tests/WTF/ListHashSet.cpp:
+        (TestWebKitAPI::TEST):
+
 2013-09-23  Krzysztof Czech  <[email protected]>
 
         [EFL] accessibility/aria-invalid is failing

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/ListHashSet.cpp (156280 => 156281)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/ListHashSet.cpp	2013-09-23 17:54:22 UTC (rev 156280)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/ListHashSet.cpp	2013-09-23 18:21:24 UTC (rev 156281)
@@ -29,7 +29,7 @@
 
 namespace TestWebKitAPI {
 
-TEST(WTF, ListHashSetRemoveFirst)
+TEST(WTF_ListHashSet, RemoveFirst)
 {
     ListHashSet<int> list;
     list.add(1);
@@ -48,7 +48,7 @@
     ASSERT_TRUE(list.isEmpty());
 }
 
-TEST(WTF, ListHashSetAppendOrMoveToLastNewItems)
+TEST(WTF_ListHashSet, AppendOrMoveToLastNewItems)
 {
     ListHashSet<int> list;
     ListHashSet<int>::AddResult result = list.appendOrMoveToLast(1);
@@ -70,7 +70,7 @@
     ++iterator;
 }
 
-TEST(WTF, ListHashSetAppendOrMoveToLastWithDuplicates)
+TEST(WTF_ListHashSet, AppendOrMoveToLastWithDuplicates)
 {
     ListHashSet<int> list;
 
@@ -109,7 +109,7 @@
     ++iterator;
 }
 
-TEST(WTF, ListHashSetPrependOrMoveToLastNewItems)
+TEST(WTF_ListHashSet, PrependOrMoveToLastNewItems)
 {
     ListHashSet<int> list;
     ListHashSet<int>::AddResult result = list.prependOrMoveToFirst(1);
@@ -131,7 +131,7 @@
     ++iterator;
 }
 
-TEST(WTF, ListHashSetPrependOrMoveToLastWithDuplicates)
+TEST(WTF_ListHashSet, PrependOrMoveToLastWithDuplicates)
 {
     ListHashSet<int> list;
 
@@ -170,4 +170,33 @@
     ++iterator;
 }
 
+TEST(WTF_ListHashSet, ReverseIterator)
+{
+    ListHashSet<int> list;
+
+    list.add(1);
+    list.add(2);
+    list.add(3);
+
+    auto it = list.rbegin();
+    ASSERT_EQ(3, *it);
+    ++it;
+    ASSERT_EQ(2, *it);
+    ++it;
+    ASSERT_EQ(1, *it);
+    ++it;
+    ASSERT_TRUE(it == list.rend());
+
+    const auto& listHashSet = list;
+
+    auto constIt = listHashSet.rbegin();
+    ASSERT_EQ(3, *constIt);
+    ++constIt;
+    ASSERT_EQ(2, *constIt);
+    ++constIt;
+    ASSERT_EQ(1, *constIt);
+    ++constIt;
+    ASSERT_TRUE(constIt == listHashSet.rend());
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to