You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
introduce function collect_as for construction from an iterator (#48)
* introduce function `collect_as` for construction from an iterator
Makes constructing `FixedSizeArray`s more convenient!
Inspired by
JuliaLang/julia#36288
This currently ignores `Base.IteratorElType`, xref
https://discourse.julialang.org/t/i-dont-get-base-iteratoreltype/113604
The allocations in some code paths are probably excessive/could be
optimized. But I guess this is good for a start.
Fixes#20
* also test an iterator with `BigInt`-valued `size` and `length`
* remove the premature optimization for `AbstractArray`
* improve tests
* add to the Readme
* whitespace/formatting fix
* delete two useless `nothing` lines
One of these wasn't being recorded by code coverage (another Julia
coverage bug, I guess).
* simplify a bit
Copy file name to clipboardExpand all lines: README.md
+40
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,46 @@ Main differences between `FixedSizeArray` and `MArray` are:
13
13
*`FixedSizeArray` is based on the `Memory` type introduced in Julia v1.11, `MArray` is backed by tuples;
14
14
* the size of the array is part of the type parameters of `MArray`, this isn't the case for `FixedSizeArray`, where the size is only a constant field of the data structure.
15
15
16
+
FixedSizeArrays supports the usual array interfaces, so things like broadcasting, matrix
17
+
multiplication, other linear algebra operations, `similar`, `copyto!` or `map` should just work.
18
+
19
+
Use the constructors to convert from other array types. Use `collect_as` to convert from
20
+
arbitrary iterators.
21
+
22
+
```julia-repl
23
+
julia> arr = [10 20; 30 14]
24
+
2×2 Matrix{Int64}:
25
+
10 20
26
+
30 14
27
+
28
+
julia> iter = (i for i ∈ 7:9 if i≠8);
29
+
30
+
julia> using FixedSizeArrays
31
+
32
+
julia> FixedSizeArray(arr) # construct from an `AbstractArray` value
33
+
2×2 FixedSizeMatrix{Int64}:
34
+
10 20
35
+
30 14
36
+
37
+
julia> FixedSizeArray{Float64}(arr) # construct from an `AbstractArray` value while converting element type
38
+
2×2 FixedSizeMatrix{Float64}:
39
+
10.0 20.0
40
+
30.0 14.0
41
+
42
+
julia> const ca = FixedSizeArrays.collect_as
43
+
collect_as (generic function with 1 method)
44
+
45
+
julia> ca(FixedSizeArray, iter) # construct from an arbitrary iterator
46
+
2-element FixedSizeVector{Int64}:
47
+
7
48
+
9
49
+
50
+
julia> ca(FixedSizeArray{Float64}, iter) # construct from an arbitrary iterator while converting element type
51
+
2-element FixedSizeVector{Float64}:
52
+
7.0
53
+
9.0
54
+
```
55
+
16
56
Note: `FixedSizeArray`s are not guaranteed to be stack-allocated, in fact they will more likely *not* be stack-allocated.
17
57
However, in some *extremely* simple cases the compiler may be able to completely elide their allocations:
0 commit comments