Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 55 additions & 20 deletions src/FSharp.Collections.Immutable/FlatList/FlatList.fs
Original file line number Diff line number Diff line change
Expand Up @@ -409,14 +409,52 @@ let collect (mapping : 'T -> FlatList<'U>) (array : FlatList<'T>) =

builder.DrainToImmutable ()

/// Joins flat lists into one, in their order. The flat lists are read twice, to add up their lengths and to copy them, so
/// the array must not change meanwhile. A default flat list throws ArgumentNullException for the parameter of the name.
let private joinFlatLists (name : string) (parts : FlatList<'T> array) =
let mutable total = 0L
let mutable nonEmpty = 0
let mutable last = empty

for part in parts do
checkNotDefault name part

if part.Length > 0 then
total <- total + int64 part.Length
nonEmpty <- nonEmpty + 1
last <- part

if nonEmpty <= 1 then
// Nothing to join: the only flat list with elements is the result, as append returns its other flat list
last
elif total > int64 Int32.MaxValue then
// The result does not fit an array. Array.concat reports it as it does for any arrays: it adds up the lengths in
// an int, which wraps around, so that it throws OverflowException or ArgumentException, depending on the total
Array.concat (Array.map (unwrapArray name) parts)
|> wrapArray
else
let result = Array.zeroCreate (int total)
let mutable offset = 0

for part in parts do
part.CopyTo (result, offset)
offset <- offset + part.Length

wrapArray result

[<CompiledName "Concat">]
let concat (arrays : seq<FlatList<'T>>) =
checkNotNullOrDefaultSeq (nameof arrays) arrays

arrays
|> Seq.map (fun array -> unwrapArray (nameof arrays) array)
|> Array.concat
|> wrapArray
// Array.concat reads an array of arrays in place and takes a snapshot of any other sequence, which is also what the
// two passes of the join need. The array under a flat list cannot change
let parts =
match arrays with
| :? FlatList<FlatList<'T>> as outer -> unwrapArray (nameof arrays) outer
| :? (FlatList<'T> array) as outer -> outer
| _ -> Seq.toArray arrays

joinFlatLists (nameof arrays) parts

[<CompiledName "Filter">]
let filter predicate (array : FlatList<'T>) =
Expand All @@ -436,13 +474,22 @@ let insertAt index (value : 'T) (source : FlatList<'T>) =
[<CompiledName "InsertManyAt">]
let insertManyAt index (values : seq<'T>) (source : FlatList<'T>) =
let array = unwrapArray (nameof source) source
let inRange = index >= 0 && index <= array.Length

// Array.insertManyAt checks the index before it enumerates the values, and it names the values source then, as
// Seq.toArray does; the check of the values comes after the one of the index and names the parameter of this function
if index >= 0 && index <= array.Length then
if inRange then
checkNotNullOrDefaultSeq (nameof values) values

Array.insertManyAt index values array |> wrapArray
match values with
// InsertRange copies the values straight into the result, where Array.insertManyAt copies them into a temporary array
// first. It reads the count of the values before it enumerates them, so only the values with a count that cannot
// change meanwhile go to it: an array and a flat list. They stay typed as a sequence on purpose: the overload of
// InsertRange for an array keeps the array of the caller instead of copying it when the flat list is empty. A flat
// list of values that goes into an empty flat list is returned as it is, without a copy
| :? ('T array)
| :? FlatList<'T> when inRange -> source.InsertRange (index, values)
| _ -> Array.insertManyAt index values array |> wrapArray

[<CompiledName "Map">]
let map (mapping : 'T -> 'U) (array : FlatList<'T>) =
Expand Down Expand Up @@ -1060,20 +1107,8 @@ module Parallel =
let collect (mapping : 'T -> FlatList<'U>) (array : FlatList<'T>) =
// As in Array.Parallel.collect, the mapping runs in parallel, and the results are copied in their order into one
// array of their total length. Array.Parallel.collect dereferences a null result; a default flat list from the
// mapping throws ArgumentNullException instead, as in the sequential collect (ADR 0001, D6).
let results = Array.Parallel.map mapping (unwrapArray (nameof array) array)
let mutable length = 0

for result in results do
checkNotDefault (nameof mapping) result
length <- length + result.Length

let builder = ImmutableArray.CreateBuilder<'U> length

for result in results do
builder.AddRange result

builder.MoveToImmutable ()
// mapping throws ArgumentNullException instead, as in the sequential collect.
joinFlatLists (nameof mapping) (Array.Parallel.map mapping (unwrapArray (nameof array) array))

[<CompiledName "Filter">]
let filter predicate (array : FlatList<'T>) =
Expand Down
12 changes: 9 additions & 3 deletions src/FSharp.Collections.Immutable/FlatList/FlatList.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -838,9 +838,12 @@ val collect : mapping : ('T -> FlatList<'U>) -> array : FlatList<'T> -> FlatList

/// <summary>Concatenates a sequence of flat lists.</summary>
/// <param name="arrays">The flat lists.</param>
/// <returns>The concatenation.</returns>
/// <returns>The concatenation, or the flat list itself when it is the only one with elements.</returns>
/// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="arrays"/> is <see langword="null"/> or a default flat list, or contains a default flat list.</exception>
/// <remarks>O(the total length).</remarks>
/// <remarks>
/// O(the number of the flat lists + the total length). A sequence that is not an array or a flat list is read once, to the
/// end, before the flat lists are joined.
/// </remarks>
[<CompiledName "Concat">]
val concat : arrays : seq<FlatList<'T>> -> FlatList<'T>

Expand Down Expand Up @@ -882,7 +885,10 @@ val insertAt : index : int -> value : 'T -> source : FlatList<'T> -> FlatList<'T
/// <returns>The flat list with the values.</returns>
/// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="values"/> is <see langword="null"/> or a default flat list.</exception>
/// <exception cref="T:System.ArgumentException">Thrown when the index is negative or greater than the length.</exception>
/// <remarks>O(n + m).</remarks>
/// <remarks>
/// O(n + m). When <paramref name="source"/> is empty and <paramref name="values"/> is a flat list of the same element
/// type, the function returns the values.
/// </remarks>
[<CompiledName "InsertManyAt">]
val insertManyAt : index : int -> values : seq<'T> -> source : FlatList<'T> -> FlatList<'T>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,26 @@ type FlatListParallelTests () =
"FlatList.Parallel.collect must name the mapping as the source of the default flat list"
)

[<TestMethod; DataRow 32768; DataRow 65537>]
member _.``collect throws for a total length that does not fit an array as Array.Parallel.collect does`` (count : int) =
// The mapping returns the same flat list for every element, so that the input is small and the output would not
// fit: the lengths add up to 2^31 for the first count and a little more than 2^32 for the second
let part = Array.zeroCreate<int> 65536
let flatList = FlatList.ofArray part

ExceptionAssert.Same (
id,
(fun () ->
Array.Parallel.collect (fun _ -> part) (Array.zeroCreate<int> count)
|> ignore
),
(fun () ->
FlatList.Parallel.collect (fun _ -> flatList) (FlatList.ofArray (Array.zeroCreate<int> count))
|> ignore
),
$"FlatList.Parallel.collect of %d{count} flat lists of 65536 elements must throw what Array.Parallel.collect throws"
)

[<TestMethod; DynamicData(nameof FlatListParallelTests.InputsAndPredicates)>]
member _.``filter keeps the elements that satisfy the predicate in their order``
(input : ParallelInput, predicate : ParallelPredicate)
Expand Down
Loading
Loading