From 7182c2bfea5374e2140aaeb60308be2df921383d Mon Sep 17 00:00:00 2001 From: Vesa Karvonen Date: Wed, 26 Jun 2024 10:49:39 +0300 Subject: [PATCH] Add `copy_as ~padded` This is convenient for making padding optional and has use cases in many other libraries. --- CHANGES.md | 4 ++++ src/Multicore_magic.mli | 4 ++++ src/padding.ml | 3 +++ 3 files changed, 11 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 139caf6..5e35bf2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +## Next version + +- Add `copy_as ~padded` for convenient optional padding (@polytypic) + ## 2.2.0 - Add (unboxed) `Atomic_array` (@polytypic) diff --git a/src/Multicore_magic.mli b/src/Multicore_magic.mli index 45a3e68..35e3b5b 100644 --- a/src/Multicore_magic.mli +++ b/src/Multicore_magic.mli @@ -38,6 +38,10 @@ val copy_as_padded : 'a -> 'a Padding changes the length of an array. If you need to pad an array, use {!make_padded_array}. *) +val copy_as : ?padded:bool -> 'a -> 'a +(** [copy_as x] by default simply returns [x]. When explicitly specified + [~padded:true], returns {{!copy_as_padded} [copy_as_padded x]}. *) + val make_padded_array : int -> 'a -> 'a array (** Creates a padded array. The length of the returned array includes padding. Use {!length_of_padded_array} to get the unpadded length. *) diff --git a/src/padding.ml b/src/padding.ml index e3255c3..22ece15 100644 --- a/src/padding.ml +++ b/src/padding.ml @@ -21,6 +21,9 @@ let copy_as_padded (o : 'a) : 'a = end else Obj.obj o +let copy_as ?padded x = + match padded with None | Some false -> x | Some true -> copy_as_padded x + let make_padded_array n x = let a = Array.make (n + num_padding_words) x in if Obj.is_block (Obj.repr x) then