package toffee

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Compute_grid.Cell_occupancySource

Cell occupancy tracking for CSS Grid auto-placement.

Tracks which grid cells are occupied during auto-placement using a dynamically expanding 2D matrix. Maintains counts of implicit and explicit tracks in both axes.

Coordinates use the OriginZero system where negative implicit tracks have negative indices, explicit tracks start at 0, and positive implicit tracks follow the explicit ones.

Grid Expansion

The matrix expands automatically in all four directions when items are placed outside current bounds. Track counts update accordingly to maintain correct coordinate mappings.

Key Invariants

  • Out-of-bounds cells are treated as unoccupied
  • Track counts must accurately reflect the matrix dimensions after expansion
  • Coordinate conversions depend on track counts being synchronized with matrix size
Sourcetype cell_occupancy_state =
  1. | Unoccupied
  2. | DefinitelyPlaced
  3. | AutoPlaced
Sourcetype t
Sourceval with_track_counts : Grid_track_counts.t -> Grid_track_counts.t -> t

with_track_counts columns rows creates an occupancy matrix with provisional track counts.

Sourceval row_count : t -> int

row_count t returns the number of rows in the matrix.

Sourceval col_count : t -> int

col_count t returns the number of columns in the matrix.

Sourceval is_area_in_range : t -> Geometry.Absolute_axis.t -> (int * int) -> (int * int) -> bool

is_area_in_range t primary_axis primary_range secondary_range checks if the specified area fits within current matrix bounds.

Ranges are (start, end) pairs in track indices.

Sourceval expand_to_fit_range : t -> (int * int) -> (int * int) -> unit

expand_to_fit_range t row_range col_range expands the matrix to accommodate the specified ranges, adding implicit tracks as needed.

Algorithm:

  1. Calculate required expansion in all four directions
  2. Allocate new matrix with expanded dimensions
  3. Copy existing cells with offset for negative expansion
  4. Update track counts to reflect new implicit tracks

Mutates t.inner, t.rows, and t.columns.

mark_area_as t primary_axis primary_span secondary_span value marks the specified area as occupied, expanding the matrix if necessary.

Spans are in OriginZero line coordinates. Automatically resolves to track ranges and expands the grid if the area extends beyond current bounds. Re-resolves coordinates after expansion since track indices may shift.

Sourceval track_area_is_unoccupied : t -> Geometry.Absolute_axis.t -> (int * int) -> (int * int) -> bool

track_area_is_unoccupied t primary_axis primary_range secondary_range checks if all cells in the area are unoccupied.

Ranges are (start, end) pairs in track indices. Out-of-bounds cells are treated as unoccupied.

Sourceval line_area_is_unoccupied : t -> Geometry.Absolute_axis.t -> int Geometry.Line.t -> int Geometry.Line.t -> bool

line_area_is_unoccupied t primary_axis primary_span secondary_span checks if all cells in the area are unoccupied.

Spans are in OriginZero line coordinates. Converts to track ranges before checking occupancy.

Sourceval row_is_occupied : t -> int -> bool

row_is_occupied t row_index checks if the specified row contains any occupied cells.

Returns false if row_index is out of bounds.

Sourceval column_is_occupied : t -> int -> bool

column_is_occupied t column_index checks if the specified column contains any occupied cells.

Returns false if column_index is out of bounds.

track_counts t track_type returns the track counts for the specified axis.

Sourceval last_of_type : t -> Geometry.Absolute_axis.t -> int -> cell_occupancy_state -> int option

last_of_type t track_type start_at kind searches backwards from the end of the specified track to find the last cell matching kind.

Returns the OriginZero line index of the matching cell, or None if no match is found or the track is out of bounds.

Sourceval to_string : t -> string

to_string t returns a debug representation showing track counts and cell states in compact 2D format.

States are represented as: _ = Unoccupied, D = DefinitelyPlaced, A = AutoPlaced.