
    cwi,                         S r SSKJr  SSKJrJrJrJrJrJ	r	J
r
JrJrJrJr  SSKJrJrJrJr  / SQr\SS\4   rS	 rS
 rS rSS jrS\4S jrS r " S S\	\   5      r " S S\\\4   5      rg)a  
Functions and classes related to statistics.

Examples
--------
The following example shows how to add custom statistics and dump the
statistics in json format:

    >>> from json import dumps
    >>> from clingo.control import Control
    >>>
    >>> def on_statistics(step, accu):
    ...     accu["example"] = 42
    ...
    >>> ctl = Control(['--stats'])
    >>> ctl.add("base", [], "{a}.")
    >>> ctl.ground([("base", [])])
    >>> print(ctl.solve(on_statistics=on_statistics))
    SAT
    >>> print(dumps(ctl.statistics['user_accu'], sort_keys=True,
    ...             indent=4, separators=(',', ': ')))
    {
        "example": 42.0
    }
    >>> print(dumps(ctl.statistics['summary']['times'], sort_keys=True,
    ...             indent=4, separators=(',', ': ')))
    {
        "cpu": 0.000785999999999995,
        "sat": 7.867813110351562e-06,
        "solve": 2.288818359375e-05,
        "total": 0.0007848739624023438,
        "unsat": 0.0
    }
    )abc)Any	ItemsViewIterableKeysViewMutableMappingMutableSequenceSequenceTupleUnion
ValuesViewcast   )_c_call_handle_error_lib_to_str)StatisticsArrayStatisticsMapStatisticsValuer   r   c                 8    [        S[        R                  X5      $ )Nclingo_statistics_type_t)r   r   clingo_statistics_typestatskeys     K/home/james-whalen/.local/lib/python3.13/site-packages/clingo/statistics.py_statistics_typer   :   s    -t/J/JEWW    c                    [        X5      nU[        R                  :X  a  [        S[        R                  X5      $ U[        R
                  :X  ad  / n[        [        S[        R                  X5      5       H8  nUR                  [        U [        S[        R                  XU5      5      5        M:     U$ U[        R                  :X  d   e0 n[        [        S[        R                  X5      5       HR  n[        S[        R                  XU5      n[        S[        R                  XU5      n[        X5      U[        U5      '   MT     U$ )z1
Transform clingo's statistics into python type.
doublesize_tuint64_tchar*)r   r   clingo_statistics_type_valuer   clingo_statistics_value_getclingo_statistics_type_arrayrangeclingo_statistics_array_sizeappend_statisticsclingo_statistics_array_atclingo_statistics_type_mapclingo_statistics_map_size!clingo_statistics_map_subkey_nameclingo_statistics_map_atr   )r   r   type_retinamesubkeys          r   r+   r+   >   s    U(E111x!A!A5NN111Hd??L
A JJJ(G(GUVW
 
D33333
C78T%D%DeQRw F FTUVT%B%BEPTU(7GDM S Jr   c                    [        U [        5      (       a  [        S5      e[        U [        R                  5      (       a  [
        R                  $ [        U [        R                  5      (       a  [
        R                  $ [
        R                  $ )Nzunexpected string)

isinstancestr	TypeErrorr   r
   r   r'   Mappingr-   r%   )values    r   _mutable_statistics_value_typer<   ]   sa    %+,,%&&000%%%...,,,r   returnc                 j    [        S[        R                  U 5      n[        [        [        X5      5      $ Nr#   )r   r   clingo_statistics_rootr   r   _mutable_statistics_getr   s     r   _mutable_statisticsrB   g   s)    
*d995
AC6uBCCr   c                     [        X5      nU[        R                  :X  a  [        X5      $ U[        R                  :X  a  [        X5      $ U[        R                  :X  d   e[        S[        R                  X5      $ Nr!   )	r   r   r'   r   r-   r   r%   r   r&   )r   r   r1   s      r   rA   rA   l   sg    U(E111u**///U((D555558T==uJJr   c                    U[         R                  :X  a  [        X5      R                  U5        g U[         R                  :X  a  [        X5      R                  U5        g U[         R                  :X  d   e[        U5      (       a2  U(       a"  U" [        S[         R                  X5      5      nOU" S 5      nOUn[        [         R                  " XU5      5        g rD   )r   r-   r   updater'   r   r%   callabler   r&   r   clingo_statistics_value_set)r   r   r1   r;   rF   uvals         r   _mutable_statistics_setrJ   y   s    ///e!((/	$33	3#**5199999E??Hd&F&FS T{Dd66u4HIr   c                       \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS\SS4S jrS\\   SS4S jrS\\   SS4S jrSrg)r      zv
Class to modify statistics stored in an array.

Notes
-----
Only inplace concatenation and no deletion is supported.
c                     Xl         X l        g N_rep_keyselfrepr   s      r   __init__StatisticsArray.__init__       		r   c                 b    [        S[        R                  U R                  U R                  5      $ Nr"   )r   r   r)   rP   rQ   rS   s    r   __len__StatisticsArray.__len__   s&    d77DII
 	
r   c                     US:  d  U[        U 5      :  a  [        S5      e[        S[        R                  U R
                  U R                  U5      n[        U R
                  U5      $ )Nr   zinvalid indexr#   )len
IndexErrorr   r   r,   rP   rQ   rA   )rS   indexr   s      r   __getitem__StatisticsArray.__getitem__   sU    19T*_--77DIIu
 'tyy#66r   c                     [        S[        R                  U R                  U R                  U5      n[        U R                  U5      n[        U R                  X4US5        g )Nr#   T)r   r   r,   rP   rQ   r   rJ   )rS   r`   r;   r   r1   s        r   __setitem__StatisticsArray.__setitem__   sI    77DIIu
 !C0		3udCr   c                     [        S5      e)z
This method is not supported.
zinsertion is not supportedNotImplementedError)rS   r`   r;   s      r   insertStatisticsArray.insert   s     "">??r   c                     [        S5      eNzdeletion is not supportedrg   rS   r`   s     r   __delitem__StatisticsArray.__delitem__       !"=>>r   c                     [        S5      e)Nz'only inplace concatenation is supportedrg   )rS   others     r   __add__StatisticsArray.__add__   s    !"KLLr   c                 :    U H  nU R                  U5        M     U $ rN   )r*   )rS   rr   xs      r   __iadd__StatisticsArray.__iadd__   s    AKKN r   r;   r=   Nc                     [        U5      n[        S[        R                  U R                  U R
                  U5      n[        U R                  X2US5        g)zr
Append a value.

Parameters
----------
value
    A nested structure composed of floats, sequences, and mappings.
r#   FN)r<   r   r   clingo_statistics_array_pushrP   rQ   rJ   )rS   r;   r1   r   s       r   r*   StatisticsArray.append   sF     /u599499diiQV
 	 		3ueDr   valuesc                     X-  n g)z
Extend the statistics array with the given values.

Paremeters
----------
values
    A sequence of nested structures composed of floats, sequences, and
    mappings.
N )rS   r|   s     r   extendStatisticsArray.extend   s     	r   c                 z    [        U 5      n[        U5       H!  u  p4X2:  a  X@U'   M  U R                  U5        M#     g)aF  
Update a statistics array.

Parameters
----------
values
    A sequence of nested structures composed of floats, callable,
    sequences, and mappings. A callable can be used to update an
    existing value, it receives the previous numeric value (or None if
    absent) as argument and must return an updated numeric value.
N)r^   	enumerater*   )rS   r|   nidxvals        r   rF   StatisticsArray.update   s6     I!&)HCwS	C 	 *r   rQ   rP   )__name__
__module____qualname____firstlineno____doc__rU   r[   ra   rd   ri   rn   rs   rw   r   r*   r   r   r
   rF   __static_attributes__r~   r   r   r   r      sv    

7D@?M
EC ED E
Xc] 
t 
!Xc] !t !r   c                       \ rS rSrSrS rS rS\4S jrS\S\	4S jr
S	 rS
 rS rS\\\4   4S jrS\\   4S jrS rS\\   4S jrSrg)r      z[
Object to capture statistics stored in a map.

This class does not support item deletion.
c                     Xl         X l        g rN   rO   rR   s      r   rU   StatisticsMap.__init__   rW   r   c                 b    [        S[        R                  U R                  U R                  5      $ rY   )r   r   r.   rP   rQ   rZ   s    r   r[   StatisticsMap.__len__   s!    x!@!@$))TYYWWr   r4   c                     X;  a  [        S5      e[        S[        R                  U R                  U R
                  UR                  5       5      n[        U R                  U5      $ )Nzkey not foundr#   )KeyErrorr   r   r0   rP   rQ   encoderA   )rS   r4   r   s      r   ra   StatisticsMap.__getitem__   sR    |?++))IIIIKKM
 'tyy#66r   r;   c                    X;   nU(       aV  [        S[        R                  U R                  U R                  UR                  5       5      n[        U R                  U5      nOK[        U5      n[        S[        R                  U R                  U R                  UR                  5       U5      n[        U R                  XEX#5        g r?   )
r   r   r0   rP   rQ   r   r   r<    clingo_statistics_map_add_subkeyrJ   )rS   r4   r;   has_keyr   r1   s         r   rd   StatisticsMap.__setitem__  s    ,--				C %TYY4E259E55				C 	 		3uFr   c                     [        S5      erl   rg   rm   s     r   rn   StatisticsMap.__delitem__  rp   r   c                     [        U[        5      (       d  g[        S[        R                  U R
                  U R                  UR                  5       5      $ )NFbool)r7   r8   r   r    clingo_statistics_map_has_subkeyrP   rQ   r   )rS   r4   s     r   __contains__StatisticsMap.__contains__!  sB    $$$11IIIIKKM
 	
r   c                 4    [        U R                  5       5      $ rN   )iterkeysrZ   s    r   __iter__StatisticsMap.__iter__-  s    DIIK  r   r=   c                    / n[        [        U 5      5       H  n[        S[        R                  U R
                  U R                  U5      n[        S[        R                  U R
                  U R                  U5      nUR                  [        U5      [        U R
                  U5      45        M     [        [        [        [        4   U5      $ )z
Return the items of the map.
r$   r#   )r(   r^   r   r   r/   rP   rQ   r0   r*   r   rA   r   r   r8   r   rS   r2   r3   r4   r   s        r   itemsStatisticsMap.items0  s     s4y!A??DIIWXD D99499diiQUC JJ'>tyy#'NOP " Ic?23S99r   c                     / n[        [        U 5      5       HL  nUR                  [        [	        S[
        R                  U R                  U R                  U5      5      5        MN     [        [        [           U5      $ )z
Return the keys of the map.
r$   )r(   r^   r*   r   r   r   r/   rP   rQ   r   r   r8   )rS   r2   r3   s      r   r   StatisticsMap.keys@  sh     s4y!AJJ>>				
 " HSM3''r   c                 >    UR                  5        H	  u  p#X0U'   M     g)aP  
Update the map with the given values.

Parameters
----------
values
    A mapping of nested structures composed of floats, callable,
    sequences, and mappings. A callable can be used to update an
    existing value, it receives the previous numeric value (or None if
    absent) as argument and must return an updated numeric value.
N)r   )rS   r|   r   r;   s       r   rF   StatisticsMap.updateT  s     !,,.JCI )r   c                 x   / n[        [        U 5      5       H  n[        S[        R                  U R
                  U R                  U5      n[        S[        R                  U R
                  U R                  U5      nUR                  [        U R
                  U5      5        M     [        [        [           U5      $ )z
Return the values of the map.
r$   r#   )r(   r^   r   r   r/   rP   rQ   r0   r*   rA   r   r   r   r   s        r   r|   StatisticsMap.valuesc  s     s4y!A??DIIWXD D99499diiQUC JJ.tyy#>? " J/55r   r   N)r   r   r   r   r   rU   r[   r8   ra   r   rd   rn   r   r   r   r   r   r   r   rF   r   r|   r   r~   r   r   r   r      s    X
7 
7G GC G.?

!:yo!56 : (hsm ((6
?3 6r   N)r=   r   ) r   collectionsr   typingr   r   r   r   r   r	   r
   r   r   r   r   	_internalr   r   r   r   __all__floatr   r   r+   r<   rB   rA   rJ   r   r8   r   r~   r   r   <module>r      s   !F     = <
A)?EABX>-D

K? 
KJ&^!oo6 ^!BD6N3#78 D6r   