
    AHj                        U d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	 d dl
mZ d dlmZmZ  e	d      ZdZd	ed
<    G d dee         Zy)    )annotations)defaultdict)product)GenericIterableTypeVar)	TypeAlias)OffsetRegion	ValueTypeztuple[int, int]r	   GridCoordinatec                  :    e Zd ZdZdddZd	dZ	 	 	 	 d
dZddZy)
SpatialMapa  A spatial map allows for data to be associated with rectangular regions
    in Euclidean space, and efficiently queried.

    When the SpatialMap is populated, a reference to each value is placed into one or
    more buckets associated with a regular grid that covers 2D space.

    The SpatialMap is able to quickly retrieve the values under a given "window" region
    by combining the values in the grid squares under the visible area.
    c                j    ||f| _         t               | _        t        t              | _        g | _        y)zCreate a spatial map with the given grid size.

        Args:
            grid_width: Width of a grid square.
            grid_height: Height of a grid square.
        N)
_grid_sizer   total_regionr   list_map_fixed)self
grid_widthgrid_heights      L/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/_spatial_map.py__init__zSpatialMap.__init__   s-     &{3"HBMdBS	')    c                    |\  }}}}||z   dz
  }||z   dz
  }| j                   \  }}	t        t        ||z  ||z  dz         t        ||	z  ||	z  dz               S )zGet the grid squares under a region.

        Args:
            region: A region.

        Returns:
            Iterable of grid coordinates (tuple of 2 values).
           )r   r   range)
r   regionx1y1widthheightx2y2r   r   s
             r   _region_to_grid_coordinatesz&SpatialMap._region_to_grid_coordinates&   sy     !'Bv%Z!^&[1_"&//
K"
"B*$4q$89"#R;%6%:;
 	
r   c                >   | j                   j                  }| j                  j                  }| j                  }| j
                  }|D ]M  \  }}}}	}
|r	 ||
       |	s|j                  |      } |||z         D ]  } ||      j                  |
        O || _        y)a?  Insert values into the Spatial map.

        Values are associated with their region in Euclidean space, and a boolean that
        indicates fixed regions. Fixed regions don't scroll and are always visible.

        Args:
            regions_and_values: An iterable of (REGION, OFFSET, FIXED, OVERLAY, VALUE).
        N)r   appendr   __getitem__r&   r   union)r   regions_and_valuesappend_fixedget_grid_list_region_to_gridr   r   offsetfixedoverlayvaluegrids               r   insertzSpatialMap.insert;   s     {{))		--::((5G 	61FFE7EU##/#5#5f#=L+FVO< 6D!$'..u56	6 )r   c                   | j                   j                         }|j                  }| j                  j                  }| j                  |      D ]  } ||      }| ||        t        t        j                  |            }|S )zGet a superset of all the values that intersect with a given region.

        Note that this may return false positives.

        Args:
            region: A region.

        Returns:
            Values under the region.
        )	r   copyextendr   getr&   r   dictfromkeys)r   r   resultsadd_resultsget_grid_valuesgrid_coordinategrid_valuesunique_valuess           r   get_values_in_regionzSpatialMap.get_values_in_regionT   sy     $(;;#3#3#5nn))--#??G 	)O)/:K&K(	) T]]734r   N)d      )r   intr   rD   returnNone)r   r   rE   zIterable[GridCoordinate])r+   z6Iterable[tuple[Region, Offset, bool, bool, ValueType]]rE   rF   )r   r   rE   zlist[ValueType])__name__
__module____qualname____doc__r   r&   r4   rA    r   r   r   r      s+    
*
*)"X)	)2r   r   N)
__future__r   collectionsr   	itertoolsr   typingr   r   r   typing_extensionsr	   textual.geometryr
   r   r   r   __annotations__r   rK   r   r   <module>rS      sA    " #  - - ' +K 	-	 -X# Xr   