L iAtdZddlmZddlZddlZddlmZddlmZd dZ ddZ ddZ dd Z dd Z dd Zdd Zy)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 t|dzdz}t|}|D]}||dzxxd|dzzzcc<t|S#t$rYywxYw)zConvert `nums` into a numbits. Arguments: nums: a reusable iterable of integers, the line numbers to store. Returns: A binary blob. )max ValueError bytearraybytes)numsnbytesbnums V/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/coverage/numbits.pynums_to_numbitsrskTa!# &A$ #( qC!G|# $ 8O sA AAcg}t|D]5\}}tdD]"}|d|zzs |j|dz|z$7|S)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. rr) enumeraterangeappend)numbitsrbyte_ibytebit_is rnumbits_to_numsr-s\ D!'*0 1X 0EqEz" FQJ./ 00 Kr cBt||d}td|DS)zsCompute the union of two numbits. Returns: A new numbits, the union of `numbits1` and `numbits2`. r fillvaluec3,K|] \}}||zywN.0b1b2s r z numbits_union..Is2VRb2)rr numbits1numbits2 byte_pairss r numbits_unionr-Bs# Xx1=J 2z2 22r cdt||d}td|D}|jdS)z~Compute the intersection of two numbits. Returns: A new numbits, the intersection `numbits1` and `numbits2`. rrc3,K|] \}}||zywr!r"r#s rr'z'numbits_intersection..Ss@62rrBw@r()rr rstrip)r*r+r,intersection_bytess rnumbits_intersectionr3Ls4 Xx1=J@Z@@  $ $U ++r cBt||d}td|DS)a 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`. rrc3,K|] \}}||zywr!r"r#s rr'z+numbits_any_intersection..as062rrBw0r()ranyr)s rnumbits_any_intersectionr7Ws#Xx1=J 0Z0 00r cft|d\}}|t|k\ryt||d|zzS)zvDoes the integer `num` appear in `numbits`? Returns: A bool, True if `num` is a member of `numbits`. rFr)divmodlenbool)rrnbytenbits rnum_in_numbitsr>ds: a.KE4 G  !t), --r c|jddt|jddt|jddt|jddt|jdddy ) a 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 ` 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-r3r7r>rrc>tjt|Sr!)jsondumpsr)rs rz+register_sqlite_functions..stzz/Z[J\?]r N)create_functionr-r3r7r>) connections rregister_sqlite_functionsrGpse<=A5q:NO91>VW/NC0!5]^r )rz Iterable[int]returnr )rr rHz list[int])r*r r+r rHr )r*r r+r rHr;)rintrr rHr;)rFzsqlite3.ConnectionrHNone)__doc__ __future__rrBsqlite3collections.abcr itertoolsrrrr-r3r7r>rGr"r rrPs? # $!(*3, 1 ."_r