ó
    Ð]jA  ã                  ó‚   • S r SSKJr  SSKrSSKrSSKJr  SSKJr  SS jr	SS jr
SS jrSS	 jrSS
 jrSS jrSS jrg)aÒ  
Functions to manipulate packed binary representations of number sets.

To save space, coverage stores sets of line numbers in SQLite using a packed
binary representation called a numbits.  A numbits is a set of positive
integers.

A numbits is stored as a blob in the database.  The exact meaning of the bytes
in the blobs should be considered an implementation detail that might change in
the future.  Use these functions to work with those binary blobs of data.

é    )ÚannotationsN)ÚIterable)Úzip_longestc                ó®   •  [        U 5      S-  S-   n[        U5      nU  H  nX#S-  ==   SUS-  -  -  ss'   M     [        U5      $ ! [         a     gf = f)zŽConvert `nums` into a numbits.

Arguments:
    nums: a reusable iterable of integers, the line numbers to store.

Returns:
    A binary blob.
é   é   ó    )ÚmaxÚ
ValueErrorÚ	bytearrayÚbytes)ÚnumsÚnbytesÚbÚnums       ÚM/home/mande/repo/quber/.venv/lib/python3.13/site-packages/coverage/numbits.pyÚnums_to_numbitsr      sd   € ðÜ�T“˜a‘ !Ñ#ˆô 	�&Ó€AÛˆØ	�‰(‹�q˜C !™G‘|Ñ#�ñ ä�‹8€Oøô ó áðús   ‚A Á
AÁAc                ó¢   • / n[        U 5       H=  u  p#[        S5       H)  nUSU-  -  (       d  M  UR                  US-  U-   5        M+     M?     U$ )a  Convert a numbits into a list of numbers.

Arguments:
    numbits: a binary blob, the packed number set.

Returns:
    A list of ints.

When registered as a SQLite function by :func:`register_sqlite_functions`,
this returns a string, a JSON-encoded list of ints.

r   r   )Ú	enumerateÚrangeÚappend)Únumbitsr   Úbyte_iÚbyteÚbit_is        r   Únumbits_to_numsr   -   sS   € ð €DÜ! 'Ö*‰ˆÜ˜1–XˆEØ�q˜E‘z×"Ñ"Ø—‘˜F Q™J¨Ñ.Ö/ó ñ +ð €Kr	   c                ó:   • [        XSS9n[        S U 5       5      $ )zgCompute the union of two numbits.

Returns:
    A new numbits, the union of `numbits1` and `numbits2`.
r   ©Ú	fillvaluec              3  ó.   #   • U  H  u  pX-  v •  M     g 7f©N© ©Ú.0Úb1Úb2s      r   Ú	<genexpr>Ú numbits_union.<locals>.<genexpr>I   s   é € Ð2¢z™V˜R�–¢zùó   ‚)r   r   ©Únumbits1Únumbits2Ú
byte_pairss      r   Únumbits_unionr.   B   s!   € ô ˜X¸1Ñ=€JÜÑ2¡zÓ2Ó2Ð2r	   c                ó\   • [        XSS9n[        S U 5       5      nUR                  S5      $ )zrCompute the intersection of two numbits.

Returns:
    A new numbits, the intersection `numbits1` and `numbits2`.
r   r   c              3  ó.   #   • U  H  u  pX-  v •  M     g 7fr!   r"   r#   s      r   r'   Ú'numbits_intersection.<locals>.<genexpr>S   s   é € Ð@²Z©6¨2˜ržw²Zùr)   ó    )r   r   Úrstrip)r+   r,   r-   Úintersection_bytess       r   Únumbits_intersectionr5   L   s2   € ô ˜X¸1Ñ=€JÜÑ@±ZÓ@Ó@ÐØ×$Ñ$ UÓ+Ð+r	   c                ó:   • [        XSS9n[        S U 5       5      $ )zùIs there any number that appears in both numbits?

Determine whether two number sets have a non-empty intersection. This is
faster than computing the intersection.

Returns:
    A bool, True if there is any number in both `numbits1` and `numbits2`.
r   r   c              3  ó.   #   • U  H  u  pX-  v •  M     g 7fr!   r"   r#   s      r   r'   Ú+numbits_any_intersection.<locals>.<genexpr>a   s   é € Ð0¢Z™6˜2ˆrŽw¢Zùr)   )r   Úanyr*   s      r   Únumbits_any_intersectionr:   W   s!   € ô ˜X¸1Ñ=€JÜÑ0¡ZÓ0Ó0Ð0r	   c                ód   • [        U S5      u  p#U[        U5      :¼  a  g[        X   SU-  -  5      $ )zjDoes the integer `num` appear in `numbits`?

Returns:
    A bool, True if `num` is a member of `numbits`.
r   Fr   )ÚdivmodÚlenÚbool)r   r   ÚnbyteÚnbits       r   Únum_in_numbitsrA   d   s6   € ô ˜˜a“.�K€EØ”�G“ÓØÜ�‘ ! t¡)Ñ,Ó-Ð-r	   c                óä   • U R                  SS[        5        U R                  SS[        5        U R                  SS[        5        U R                  SS[        5        U R                  SSS 5        g	)
av  
Define numbits functions in a SQLite connection.

This defines these functions for use in SQLite statements:

* :func:`numbits_union`
* :func:`numbits_intersection`
* :func:`numbits_any_intersection`
* :func:`num_in_numbits`
* :func:`numbits_to_nums`

`connection` is a :class:`sqlite3.Connection <python:sqlite3.Connection>`
object.  After creating the connection, pass it to this function to
register the numbits functions.  Then you can use numbits functions in your
queries::

    import sqlite3
    from coverage.numbits import register_sqlite_functions

    conn = sqlite3.connect("example.db")
    register_sqlite_functions(conn)
    c = conn.cursor()
    # Kind of a nonsense query:
    # Find all the files and contexts that executed line 47 in any file:
    c.execute(
        "select file_id, context_id from line_bits where num_in_numbits(?, numbits)",
        (47,)
    )
r.   é   r5   r:   rA   r   r   c                ó@   • [         R                  " [        U 5      5      $ r!   )ÚjsonÚdumpsr   )r   s    r   Ú<lambda>Ú+register_sqlite_functions.<locals>.<lambda>’   s   € ¼t¿zºzÌ/ÐZ[ÓJ\Ô?]r	   N)Úcreate_functionr.   r5   r:   rA   )Ú
connections    r   Úregister_sqlite_functionsrK   p   se   € ð< ×Ñ˜°´=ÔAØ×ÑÐ5°qÔ:NÔOØ×ÑÐ9¸1Ô>VÔWØ×ÑÐ/°´NÔCØ×ÑÐ0°!Ñ5]Õ^r	   )r   zIterable[int]Úreturnr   )r   r   rL   z	list[int])r+   r   r,   r   rL   r   )r+   r   r,   r   rL   r>   )r   Úintr   r   rL   r>   )rJ   zsqlite3.ConnectionrL   ÚNone)Ú__doc__Ú
__future__r   rE   Úsqlite3Úcollections.abcr   Ú	itertoolsr   r   r   r.   r5   r:   rA   rK   r"   r	   r   Ú<module>rT      s?   ðñõ #ã Û Ý $Ý !ôô(ô*3ô,ô
1ô	.õ"_r	   