Skip to content

[RFC] when should Colon : keep offset information #246

Open
@johnnychen94

Description

@johnnychen94

In a normal 1-based indexing array world, this is clear: : serves as a length placeholder.

During #220 #228 and #245, I've realized that we haven't yet had a clear and consistent definition of the role of : in the OffsetArray world. Let's take reshape as an example, it might also apply to all other operations where : is allowed, e.g., getindex, setindex!.

I propose the rule of thumb is to keep offset information if it's unambiguous. It comes with one and only one extra rule: if all inds inputs are range type, keep offset information for the corresponding dimension where : is placed at.

A = OffsetArray(rand(4, 4, 4), -1, -2, -3)

reshape(A, :) # (0:64, )

reshape(A, 1:8, :) # (1:8, -1:6)
reshape(A, :, 1:8) # (0:7, 1:8)

reshape(A, 1:8, 1:8, :) # (1:8, 1:8, -2:-2)
reshape(A, 1:8, :, 1:8) # (1:8, -1:-1, 1:8)
reshape(A, :, 1:8, 1:8) # (0:0, 1:8, 1:8)

reshape(A, 1:8, 1:2, :) # (1:8, 1:2, -2:1)
reshape(A, 1:8, :, 1:2) # (1:8, -1:2, 1:2)
reshape(A, :, 1:8, 1:2) # (0:3, 1:8, 1:2)

All other cases should be consistent with the Base case. For example:

reshape(A, 8, :) # (1:8, 1:8)

In this case, it's ambiguous whether : is used as a length placeholder or axes placeholder so we should stick to the Base case; otherwise, I can foresee a lot of type piracy involved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions