Mojo function
compact_order
compact_order(shape: IntTuple[origin], order: IntTuple[origin]) -> IntTuple
Create a compact stride based on shape and order.
This function generates a stride tuple where lower order numbers imply faster varying strides. The resulting shape and stride form a bijective layout.
Performance: - Always inlined for optimal performance in tight loops. - Flattens inputs and re-nests results for consistent behavior.
Example:
```mojo
from layout import IntTuple
from layout.int_tuple import compact_order
# Create a compact layout with dimensions (2,3,4,5) and ordering (1,4,3,5)
var x = compact_order(IntTuple(2,3,4,5), IntTuple(1,4,3,5)) # returns (1,8,2,24)
# Create a compact layout with nested dimensions and corresponding ordering
var y = compact_order(IntTuple(2,IntTuple(3,4),5), IntTuple(1,IntTuple(2,3),4)) # returns (1,(2,6),24)
```
.
```mojo
from layout import IntTuple
from layout.int_tuple import compact_order
# Create a compact layout with dimensions (2,3,4,5) and ordering (1,4,3,5)
var x = compact_order(IntTuple(2,3,4,5), IntTuple(1,4,3,5)) # returns (1,8,2,24)
# Create a compact layout with nested dimensions and corresponding ordering
var y = compact_order(IntTuple(2,IntTuple(3,4),5), IntTuple(1,IntTuple(2,3),4)) # returns (1,(2,6),24)
```
.
Args:
- shape (
IntTuple[origin]
): The shape tuple defining dimensions. - order (
IntTuple[origin]
): The order tuple defining the relative ordering of dimensions.
Returns:
A stride tuple that creates a compact memory layout according to the specified order.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!