Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience constructors for OneElement #356

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented Apr 5, 2024

This adds convenience constructors that provide a similar signature to construct 1D and nD arrays.

After this,

  1. If one argument is provided, it is interpreted as the index. The value is chosen to be 1 and the size is set to equal the index. This is new.
  2. If two arguments are provided, they are interpreted as the index and the size. The value is chosen to be 1. This was already the case for 1D arrays, and this is now extended to nD arrays.
  3. If three arguments are provided, they are interpreted as (value, index, size) as before. This doesn't change.
julia> OneElement(2, 3)
3-element OneElement{Int64, 1, Tuple{Int64}, Tuple{Base.OneTo{Int64}}}:
 
 1
 

julia> OneElement((2,2), (3,3)) # New in this PR, where the value is implicitly assumed to be 1
3×3 OneElement{Int64, 2, Tuple{Int64, Int64}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}:
     
   1  
     

julia> OneElement(2) # new in this PR, where the size is equal to the last index
2-element OneElement{Int64, 1, Tuple{Int64}, Tuple{Base.OneTo{Int64}}}:
 
 1

julia> OneElement((2,2)) # Analog of the above for nD arrays
2×2 OneElement{Int64, 2, Tuple{Int64, Int64}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}:
   
   1

show for 1D and nD are also merged to display the constructor with at least the index and the size. The value is displayed if it is not zero, and the eltype is displayed if the value is 1 but not an Int.

julia> show(OneElement(1.0, 2, 2))
OneElement{Float64}(2, 2)

julia> show(OneElement(2))
OneElement(2, 2)

julia> show(OneElement(2.0, (2,2), (2,2)))
OneElement(2.0, (2, 2), (2, 2))

julia> show(OneElement((2,2), (2,2)))
OneElement((2, 2), (2, 2))

julia> show(OneElement((2,2)))
OneElement((2, 2), (2, 2))

julia> show(OneElement{Int8}((2,2), (2,2)))
OneElement{Int8}((2, 2), (2, 2))

Copy link

codecov bot commented Apr 5, 2024

Codecov Report

Attention: Patch coverage is 95.00000% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 99.81%. Comparing base (002f56c) to head (ba819b8).

Files Patch % Lines
src/oneelement.jl 95.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #356      +/-   ##
==========================================
- Coverage   99.90%   99.81%   -0.10%     
==========================================
  Files           8        8              
  Lines        1043     1057      +14     
==========================================
+ Hits         1042     1055      +13     
- Misses          1        2       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant