From 9ff2b7f4f20529a6acb2db87165660bf5c176afd Mon Sep 17 00:00:00 2001 From: Preetham Gujjula Date: Fri, 10 May 2024 02:46:23 -0700 Subject: [PATCH] [#10] Add mergeAll-based version of applyMerge Implement applyMerge in terms of mergeAll from data-ordlist, to provide a point of comparison for benchmarking. --- apply-merge.cabal | 5 +++++ bench/Main.hs | 2 ++ package.yaml | 2 ++ src/ApplyMerge/MergeAll.hs | 10 ++++++++++ test/Main.hs | 2 ++ test/Test/ApplyMerge/MergeAll.hs | 25 +++++++++++++++++++++++++ 6 files changed, 46 insertions(+) create mode 100644 src/ApplyMerge/MergeAll.hs create mode 100644 test/Test/ApplyMerge/MergeAll.hs diff --git a/apply-merge.cabal b/apply-merge.cabal index 1999f1c..325aa86 100644 --- a/apply-merge.cabal +++ b/apply-merge.cabal @@ -55,6 +55,7 @@ test-suite apply-merge-tests ApplyMerge.DoublyLinkedList ApplyMerge.IntMap ApplyMerge.IntSet + ApplyMerge.MergeAll Data.DoublyLinkedList.STRef Data.List.ApplyMerge Data.List.NonEmpty.ApplyMerge @@ -63,6 +64,7 @@ test-suite apply-merge-tests Test.ApplyMerge.DoublyLinkedList Test.ApplyMerge.IntMap Test.ApplyMerge.IntSet + Test.ApplyMerge.MergeAll Test.Data.DoublyLinkedList.STRef Test.Data.List.ApplyMerge Test.Data.PQueue.Prio.Min.Mutable @@ -73,6 +75,7 @@ test-suite apply-merge-tests build-depends: base >=4.16 && <4.17 || >=4.17 && <4.18 || >=4.18 && <4.19 || >=4.19 && <4.20 , containers ==0.6.* + , data-ordlist ==0.4.* , pqueue >=1.4 && <1.5 || >=1.5 && <1.6 , reflection ==2.1.* , tasty >=1.4 && <1.5 || >=1.5 && <1.6 @@ -90,6 +93,7 @@ benchmark apply-merge-benchmarks ApplyMerge.DoublyLinkedList ApplyMerge.IntMap ApplyMerge.IntSet + ApplyMerge.MergeAll Data.DoublyLinkedList.STRef Data.List.ApplyMerge Data.List.NonEmpty.ApplyMerge @@ -104,6 +108,7 @@ benchmark apply-merge-benchmarks build-depends: base >=4.16 && <4.17 || >=4.17 && <4.18 || >=4.18 && <4.19 || >=4.19 && <4.20 , containers ==0.6.* + , data-ordlist ==0.4.* , pqueue >=1.4 && <1.5 || >=1.5 && <1.6 , reflection ==2.1.* , tasty-bench ==0.3.* diff --git a/bench/Main.hs b/bench/Main.hs index 71c8c0a..88c0ac6 100644 --- a/bench/Main.hs +++ b/bench/Main.hs @@ -6,6 +6,7 @@ module Main (main) where import ApplyMerge.DoublyLinkedList qualified import ApplyMerge.IntMap qualified import ApplyMerge.IntSet qualified +import ApplyMerge.MergeAll qualified import Bench.Data.DoublyLinkedList.STRef qualified import Bench.PriorityQueue.MinPQueue qualified import Bench.PriorityQueue.MinPQueue.Mutable qualified @@ -20,6 +21,7 @@ main = ApplyMerge.DoublyLinkedList.applyMerge, benchCommon "IntMap" ApplyMerge.IntMap.applyMerge, benchCommon "IntSet" ApplyMerge.IntSet.applyMerge, + benchCommon "MergeAll" ApplyMerge.MergeAll.applyMerge, Bench.Data.DoublyLinkedList.STRef.benchmarks, Bench.PriorityQueue.MinPQueue.benchmarks, Bench.PriorityQueue.MinPQueue.Mutable.benchmarks diff --git a/package.yaml b/package.yaml index ae6ca97..e36e5e4 100644 --- a/package.yaml +++ b/package.yaml @@ -64,6 +64,7 @@ tests: main: Main.hs dependencies: - containers ^>= {0.6} + - data-ordlist ^>= {0.4} - pqueue ^>= {1.4, 1.5} - reflection ^>= {2.1} - tasty ^>= {1.4, 1.5} @@ -81,6 +82,7 @@ benchmarks: main: Main.hs dependencies: - containers ^>= {0.6} + - data-ordlist ^>= {0.4} - pqueue ^>= {1.4, 1.5} - reflection ^>= {2.1} - tasty-bench ^>= {0.3} diff --git a/src/ApplyMerge/MergeAll.hs b/src/ApplyMerge/MergeAll.hs new file mode 100644 index 0000000..d644da3 --- /dev/null +++ b/src/ApplyMerge/MergeAll.hs @@ -0,0 +1,10 @@ +-- SPDX-FileCopyrightText: Copyright Preetham Gujjula +-- SPDX-License-Identifier: BSD-3-Clause +module ApplyMerge.MergeAll (applyMerge) where + +import Data.List.Ordered (mergeAll) + +applyMerge :: (Ord c) => (a -> b -> c) -> [a] -> [b] -> [c] +applyMerge _ [] _ = [] +applyMerge f xs ys = + mergeAll (map (\y -> map (`f` y) xs) ys) diff --git a/test/Main.hs b/test/Main.hs index f274ae9..b31ea2f 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -6,6 +6,7 @@ module Main (main) where import Test.ApplyMerge.DoublyLinkedList qualified (tests) import Test.ApplyMerge.IntMap qualified (tests) import Test.ApplyMerge.IntSet qualified (tests) +import Test.ApplyMerge.MergeAll qualified (tests) import Test.Data.DoublyLinkedList.STRef qualified (tests) import Test.Data.List.ApplyMerge qualified (tests) import Test.Data.PQueue.Prio.Min.Mutable qualified (tests) @@ -21,6 +22,7 @@ tests = [ Test.ApplyMerge.DoublyLinkedList.tests, Test.ApplyMerge.IntMap.tests, Test.ApplyMerge.IntSet.tests, + Test.ApplyMerge.MergeAll.tests, Test.Data.List.ApplyMerge.tests, Test.Data.DoublyLinkedList.STRef.tests, Test.Data.PQueue.Prio.Min.Mutable.tests diff --git a/test/Test/ApplyMerge/MergeAll.hs b/test/Test/ApplyMerge/MergeAll.hs new file mode 100644 index 0000000..3fa6ec6 --- /dev/null +++ b/test/Test/ApplyMerge/MergeAll.hs @@ -0,0 +1,25 @@ +-- SPDX-FileCopyrightText: Copyright Preetham Gujjula +-- SPDX-License-Identifier: BSD-3-Clause +module Test.ApplyMerge.MergeAll + ( tests, + ) +where + +import ApplyMerge.MergeAll (applyMerge) +import Test.ApplyMerge.Common + ( basicTest, + blockTest, + maxTest, + skewedTest, + ) +import Test.Tasty (TestTree, testGroup) + +tests :: TestTree +tests = + testGroup + "ApplyMerge.MergeAll" + [ basicTest applyMerge, + skewedTest applyMerge, + blockTest applyMerge, + maxTest applyMerge + ]