From 46422e8d980694ab68f7713ec1595524b4b354b0 Mon Sep 17 00:00:00 2001 From: Guillaume Bagan Date: Tue, 31 Oct 2023 12:20:59 +0100 Subject: [PATCH 1/4] Add copy to Data.Array.ST --- src/Data/Array/ST.js | 2 +- src/Data/Array/ST.purs | 12 ++++++++++++ test/Test/Data/Array/ST.purs | 7 +++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Data/Array/ST.js b/src/Data/Array/ST.js index 2795f00..da87b7f 100644 --- a/src/Data/Array/ST.js +++ b/src/Data/Array/ST.js @@ -45,7 +45,7 @@ export const unsafeFreezeImpl = unsafeFreezeThawImpl; export const unsafeThawImpl = unsafeFreezeThawImpl; -function copyImpl(xs) { +export function copyImpl(xs) { return xs.slice(); } diff --git a/src/Data/Array/ST.purs b/src/Data/Array/ST.purs index 5ff3c1a..84cde8e 100644 --- a/src/Data/Array/ST.purs +++ b/src/Data/Array/ST.purs @@ -24,6 +24,7 @@ module Data.Array.ST , sortWith , freeze , thaw + , copy , unsafeFreeze , unsafeThaw , toAssocArray @@ -97,6 +98,17 @@ foreign import thawImpl :: forall h a. STFn1 (Array a) h (STArray h a) -- | Sort a mutable array in place. Sorting is stable: the order of equal -- | elements is preserved. + + +-- | Make a mutable copy of a mutable array. +copy + :: forall h a + . STArray h a + -> ST h (STArray h a) +copy = runSTFn1 copyImpl + +foreign import copyImpl :: forall h a. STFn1 (STArray h a) h (STArray h a) + sort :: forall a h. Ord a => STArray h a -> ST h (STArray h a) sort = sortBy compare diff --git a/test/Test/Data/Array/ST.purs b/test/Test/Data/Array/ST.purs index 6e42fc2..9887ba6 100644 --- a/test/Test/Data/Array/ST.purs +++ b/test/Test/Data/Array/ST.purs @@ -45,6 +45,13 @@ testArrayST = do arr <- STA.thaw [1, 2, 3] STA.freeze arr) == [1, 2, 3] + log "copy should produce a shallow copy of an STArray" + + assert $ ST.run (do + arr <- STA.thaw [1, 2, 3] + arr2 <- STA.copy arr + STA.freeze arr2) == [1, 2, 3] + log "unsafeThaw should produce an STArray from a standard array" assert $ STA.run (STA.unsafeThaw [1, 2, 3]) == [1, 2, 3] From 3d20814ec6bd295774f81753113d830c72f2b794 Mon Sep 17 00:00:00 2001 From: Guillaume Bagan Date: Tue, 31 Oct 2023 12:40:15 +0100 Subject: [PATCH 2/4] rename copy with clone --- src/Data/Array/ST.js | 4 +++- src/Data/Array/ST.purs | 14 ++++++-------- test/Test/Data/Array/ST.purs | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Data/Array/ST.js b/src/Data/Array/ST.js index da87b7f..60f250c 100644 --- a/src/Data/Array/ST.js +++ b/src/Data/Array/ST.js @@ -45,7 +45,7 @@ export const unsafeFreezeImpl = unsafeFreezeThawImpl; export const unsafeThawImpl = unsafeFreezeThawImpl; -export function copyImpl(xs) { +function copyImpl(xs) { return xs.slice(); } @@ -53,6 +53,8 @@ export const freezeImpl = copyImpl; export const thawImpl = copyImpl; +export const cloneImpl = copyImpl; + export const sortByImpl = (function () { function mergeFromTo(compare, fromOrdering, xs1, xs2, from, to) { var mid; diff --git a/src/Data/Array/ST.purs b/src/Data/Array/ST.purs index 84cde8e..158e410 100644 --- a/src/Data/Array/ST.purs +++ b/src/Data/Array/ST.purs @@ -24,7 +24,7 @@ module Data.Array.ST , sortWith , freeze , thaw - , copy + , clone , unsafeFreeze , unsafeThaw , toAssocArray @@ -96,19 +96,17 @@ thaw = runSTFn1 thawImpl -- | Create a mutable copy of an immutable array. foreign import thawImpl :: forall h a. STFn1 (Array a) h (STArray h a) --- | Sort a mutable array in place. Sorting is stable: the order of equal --- | elements is preserved. - - -- | Make a mutable copy of a mutable array. -copy +clone :: forall h a . STArray h a -> ST h (STArray h a) -copy = runSTFn1 copyImpl +clone = runSTFn1 cloneImpl -foreign import copyImpl :: forall h a. STFn1 (STArray h a) h (STArray h a) +foreign import cloneImpl :: forall h a. STFn1 (STArray h a) h (STArray h a) +-- | Sort a mutable array in place. Sorting is stable: the order of equal +-- | elements is preserved. sort :: forall a h. Ord a => STArray h a -> ST h (STArray h a) sort = sortBy compare diff --git a/test/Test/Data/Array/ST.purs b/test/Test/Data/Array/ST.purs index 9887ba6..2d93529 100644 --- a/test/Test/Data/Array/ST.purs +++ b/test/Test/Data/Array/ST.purs @@ -45,11 +45,11 @@ testArrayST = do arr <- STA.thaw [1, 2, 3] STA.freeze arr) == [1, 2, 3] - log "copy should produce a shallow copy of an STArray" + log "clone should produce a shallow copy of an STArray" assert $ ST.run (do arr <- STA.thaw [1, 2, 3] - arr2 <- STA.copy arr + arr2 <- STA.clone arr STA.freeze arr2) == [1, 2, 3] log "unsafeThaw should produce an STArray from a standard array" From 85f6f4260716d20ac2f478196088fddf9e5765ec Mon Sep 17 00:00:00 2001 From: Guillaume Bagan Date: Wed, 1 Nov 2023 09:56:48 +0100 Subject: [PATCH 3/4] modify the original array in the clone test to prove that the cloned array is a copy --- test/Test/Data/Array/ST.purs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/Test/Data/Array/ST.purs b/test/Test/Data/Array/ST.purs index 2d93529..d408ec1 100644 --- a/test/Test/Data/Array/ST.purs +++ b/test/Test/Data/Array/ST.purs @@ -50,6 +50,7 @@ testArrayST = do assert $ ST.run (do arr <- STA.thaw [1, 2, 3] arr2 <- STA.clone arr + _ <- STA.poke 0 4 arr STA.freeze arr2) == [1, 2, 3] log "unsafeThaw should produce an STArray from a standard array" From 0f236d17ae7b23d3bf19498fb0ecafcdf001f238 Mon Sep 17 00:00:00 2001 From: Guillaume Bagan Date: Thu, 2 Nov 2023 17:04:36 +0100 Subject: [PATCH 4/4] update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9540992..4626209 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based Breaking changes: New features: +- Add `ST.clone` (#243 by @Bgbagan) Bugfixes: