Add utility function to allow easy reverse range-based iteration of a container
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 14 Mar 2019 02:46:16 +0000 (02:46 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 14 Mar 2019 02:46:16 +0000 (02:46 +0000)
https://bugs.webkit.org/show_bug.cgi?id=195542

Patch by Sam Weinig <sam@webkit.org> on 2019-03-13
Reviewed by Antti Koivisto.

Source/WTF:

Add functions to create an IteratorRange<T> that will iterate a container backwards. It
works with any container that is compatible with std::rbegin() and std::rend(). It is
expected to be used in conjunction with range-based for-loops like so:

for (auto& value : WTF::makeReversedRange(myContainer))
    ...

* wtf/IteratorRange.h:
(WTF::makeReversedRange):

Tools:

* TestWebKitAPI/CMakeLists.txt:
* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WTF/IteratorRange.cpp: Added.
(TestWebKitAPI::TEST):
Add test to ensure WTF::makeReversedRange() works correctly and uses the correct types.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@242932 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WTF/ChangeLog
Source/WTF/wtf/IteratorRange.h
Tools/ChangeLog
Tools/TestWebKitAPI/CMakeLists.txt
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WTF/IteratorRange.cpp [new file with mode: 0644]

index 9ae117b..99bf757 100644 (file)
@@ -1,3 +1,20 @@
+2019-03-13  Sam Weinig  <sam@webkit.org>
+
+        Add utility function to allow easy reverse range-based iteration of a container
+        https://bugs.webkit.org/show_bug.cgi?id=195542
+
+        Reviewed by Antti Koivisto.
+
+        Add functions to create an IteratorRange<T> that will iterate a container backwards. It
+        works with any container that is compatible with std::rbegin() and std::rend(). It is
+        expected to be used in conjunction with range-based for-loops like so:
+
+        for (auto& value : WTF::makeReversedRange(myContainer)) 
+            ...
+
+        * wtf/IteratorRange.h:
+        (WTF::makeReversedRange):
+
 2019-03-13  Keith Rollin  <krollin@apple.com>
 
         Add support for new StagedFrameworks layout
index 393fceb..5d8fe4d 100644 (file)
@@ -25,6 +25,8 @@
 
 #pragma once
 
+#include <iterator>
+
 namespace WTF {
 
 template<typename Iterator>
@@ -50,6 +52,18 @@ IteratorRange<Iterator> makeIteratorRange(Iterator&& begin, Iterator&& end)
     return IteratorRange<Iterator>(std::forward<Iterator>(begin), std::forward<Iterator>(end));
 }
 
+template<typename Container>
+IteratorRange<typename Container::reverse_iterator> makeReversedRange(Container& container)
+{
+    return makeIteratorRange(std::rbegin(container), std::rend(container));
+}
+
+template<typename Container>
+IteratorRange<typename Container::const_reverse_iterator> makeReversedRange(const Container& container)
+{
+    return makeIteratorRange(std::crbegin(container), std::crend(container));
+}
+
 template<typename Container, typename Iterator>
 class SizedIteratorRange {
 public:
index 2acffdb..ecf888a 100644 (file)
@@ -1,3 +1,16 @@
+2019-03-13  Sam Weinig  <sam@webkit.org>
+
+        Add utility function to allow easy reverse range-based iteration of a container
+        https://bugs.webkit.org/show_bug.cgi?id=195542
+
+        Reviewed by Antti Koivisto.
+
+        * TestWebKitAPI/CMakeLists.txt:
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WTF/IteratorRange.cpp: Added.
+        (TestWebKitAPI::TEST):
+        Add test to ensure WTF::makeReversedRange() works correctly and uses the correct types.
+
 2019-03-13  Chris Dumez  <cdumez@apple.com>
 
         Use a ServiceWorker process per registrable domain
index 6843d03..4509208 100644 (file)
@@ -129,6 +129,7 @@ set(TestWTF_SOURCES
     ${TESTWEBKITAPI_DIR}/Tests/WTF/HashSet.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WTF/Hasher.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WTF/IntegerToStringConversion.cpp
+    ${TESTWEBKITAPI_DIR}/Tests/WTF/IteratorRange.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WTF/JSONValue.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WTF/LEBDecoder.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WTF/LifecycleLogger.cpp
index 765c99c..00fd99a 100644 (file)
                7CCE7F2E1A411B1000447C4C /* WKBrowsingContextGroupTest.mm in Sources */ = {isa = PBXBuildFile; fileRef = BC3C4C7D14587AA60025FB62 /* WKBrowsingContextGroupTest.mm */; };
                7CCE7F2F1A411B1000447C4C /* WKBrowsingContextLoadDelegateTest.mm in Sources */ = {isa = PBXBuildFile; fileRef = BC3C4C7014575B6A0025FB62 /* WKBrowsingContextLoadDelegateTest.mm */; };
                7CD4C26E1E2C0E6E00929470 /* StringConcatenate.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CD4C26C1E2C0E6E00929470 /* StringConcatenate.cpp */; };
+               7CEB62AB223609DE0069CBB0 /* IteratorRange.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CEB62A92236086C0069CBB0 /* IteratorRange.cpp */; };
                7CEFA9661AC0B9E200B910FD /* _WKUserContentExtensionStore.mm in Sources */ = {isa = PBXBuildFile; fileRef = 7CEFA9641AC0B9E200B910FD /* _WKUserContentExtensionStore.mm */; };
                7CFBCAE51743238F00B2BFCF /* WillLoad_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CFBCAE31743238E00B2BFCF /* WillLoad_Bundle.cpp */; };
                830F2E0C209A6A8E00D36FF1 /* WebContentProcessDidTerminate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 830F2E0B209A6A7400D36FF1 /* WebContentProcessDidTerminate.mm */; };
                7CCE7EA31A4115CB00447C4C /* TestWebKitAPILibrary.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = TestWebKitAPILibrary.xcconfig; sourceTree = "<group>"; };
                7CD0D5AA1D5534DE000CC9E1 /* Variant.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Variant.cpp; sourceTree = "<group>"; };
                7CD4C26C1E2C0E6E00929470 /* StringConcatenate.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StringConcatenate.cpp; sourceTree = "<group>"; };
+               7CEB62A92236086C0069CBB0 /* IteratorRange.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = IteratorRange.cpp; sourceTree = "<group>"; };
                7CEFA9641AC0B9E200B910FD /* _WKUserContentExtensionStore.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = _WKUserContentExtensionStore.mm; sourceTree = "<group>"; };
                7CFBCADD1743234F00B2BFCF /* WillLoad.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WillLoad.cpp; sourceTree = "<group>"; };
                7CFBCAE31743238E00B2BFCF /* WillLoad_Bundle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WillLoad_Bundle.cpp; sourceTree = "<group>"; };
                                0BCD833414857CE400EA2003 /* HashMap.cpp */,
                                26B2DFF815BDE599004F691D /* HashSet.cpp */,
                                266FAFD215E5775200F61D5B /* IntegerToStringConversion.cpp */,
+                               7CEB62A92236086C0069CBB0 /* IteratorRange.cpp */,
                                7A0509401FB9F04400B33FB8 /* JSONValue.cpp */,
                                531C1D8D1DF8EF72006E979F /* LEBDecoder.cpp */,
                                A57D54F71F3397B400A97AA7 /* LifecycleLogger.cpp */,
                                7C83DED21D0A590C00FEBCF3 /* HashMap.cpp in Sources */,
                                7C83DED41D0A590C00FEBCF3 /* HashSet.cpp in Sources */,
                                7C83DEE01D0A590C00FEBCF3 /* IntegerToStringConversion.cpp in Sources */,
+                               7CEB62AB223609DE0069CBB0 /* IteratorRange.cpp in Sources */,
                                7A0509411FB9F06400B33FB8 /* JSONValue.cpp in Sources */,
                                531C1D8E1DF8EF72006E979F /* LEBDecoder.cpp in Sources */,
                                A57D54F91F3397B400A97AA7 /* LifecycleLogger.cpp in Sources */,
diff --git a/Tools/TestWebKitAPI/Tests/WTF/IteratorRange.cpp b/Tools/TestWebKitAPI/Tests/WTF/IteratorRange.cpp
new file mode 100644 (file)
index 0000000..086df6e
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2019 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include <array>
+#include <wtf/IteratorRange.h>
+#include <wtf/Vector.h>
+
+namespace TestWebKitAPI {
+
+TEST(WTF_IteratorRange, MakeReversedRange)
+{
+    Vector<int> intVector { 10, 11, 12, 13 };
+
+    auto reversedRange = WTF::makeReversedRange(intVector);
+
+    static_assert(std::is_same<decltype(reversedRange.begin()), typename Vector<int>::reverse_iterator>::value, "IteratorRange has correct begin() iterator type");
+    static_assert(std::is_same<decltype(reversedRange.end()), typename Vector<int>::reverse_iterator>::value, "IteratorRange has correct end() iterator type");
+
+    EXPECT_EQ(reversedRange.begin(), intVector.rbegin());
+    EXPECT_EQ(reversedRange.end(), intVector.rend());
+
+    std::array<int, 4> expectedResults { { 13, 12, 11, 10 } };
+    size_t index = 0;
+
+    for (auto& value : reversedRange)
+        EXPECT_EQ(value, expectedResults[index++]);
+}
+
+TEST(WTF_IteratorRange, MakeConstReversedRange)
+{
+    const Vector<int> intVector { 10, 11, 12, 13 };
+
+    auto reversedRange = WTF::makeReversedRange(intVector);
+
+    static_assert(std::is_same<decltype(reversedRange.begin()), typename Vector<int>::const_reverse_iterator>::value, "IteratorRange has correct begin() iterator type");
+    static_assert(std::is_same<decltype(reversedRange.end()), typename Vector<int>::const_reverse_iterator>::value, "IteratorRange has correct end() iterator type");
+
+    EXPECT_EQ(reversedRange.begin(), intVector.rbegin());
+    EXPECT_EQ(reversedRange.end(), intVector.rend());
+
+    std::array<int, 4> expectedResults { { 13, 12, 11, 10 } };
+    size_t index = 0;
+
+    for (auto& value : reversedRange)
+        EXPECT_EQ(value, expectedResults[index++]);
+}
+
+} // namespace TestWebKitAPI