
    =wg0                         d Z ddlmZ ddlZddlZddlmZmZmZ ddl	m
Z
mZ 	 ddlmZ d Z G d	 d
e      ZddZy# e$ r d ZY w xY w)zJ
This module implements a general external merge sort for Python objects.
    )with_statementN)heapifyheappopheapreplace)dumploadmergec                     t        |  S Nr	   )	iterabless    J/var/www/horilla/myenv/lib/python3.12/site-packages/whoosh/externalsort.pyimerger   9   s    i      c              #   z  K   t         t        t        }}}g }|j                  }t	        t        t        |             D ]"  \  }}	 |j                  } | |       ||g       $ t        |       	 	 	 |d   x\  }	}}}
|	  |       |
d<    |||
       ## |$ r Y ]w xY w# |$ r  ||       Y nt        $ r Y y w xY wNw)Nr   )
r   r   StopIterationappend	enumeratemapiternextr   
IndexError)r   _hpop	_hreplace_Stophh_appenditnumitnxvss              r   r   r   <   s     #*K%y88"3tY#78 	IE2WW"$r*+	 	
	'(t+LAub1G4AaDaO	     a  sT   A B;BB;.$B BB;BB;B7*B;,B74B;6B77B;c                   f    e Zd ZdZ	 	 ddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd ZddZy)SortingPoola  This object implements a general K-way external merge sort for Python
    objects.

    >>> pool = MergePool()
    >>> # Add an unlimited number of items in any order
    >>> for item in my_items:
    ...     pool.add(item)
    ...
    >>> # Get the items back in sorted order
    >>> for item in pool.items():
    ...     print(item)

    This class uses the `marshal` module to write the items to temporary files,
    so you can only sort marshal-able types (generally: numbers, strings,
    tuples, lists, and dicts).
    Nc                 ~    || _         |dk  rt        d|z        || _        || _        || _        g | _        g | _        y)ah  
        :param maxsize: the maximum number of items to keep in memory at once.
        :param tempdir: the path of a directory to use for temporary file
            storage. The default is to use the system's temp directory.
        :param prefix: a prefix to add to temporary filenames.
        :param suffix: a suffix to add to temporary filenames.
           zmaxsize=%s must be >= 1N)tempdir
ValueErrormaxsizeprefixsuffixcurrentruns)selfr)   r'   r*   r+   s        r   __init__zSortingPool.__init__g   sF     Q;6@AA	r   c                     t        j                  | j                  | j                  | j                        \  }}t        j                  |d      }||fS )N)r*   r+   dirwb)tempfilemkstempr*   r+   r'   osfdopen)r.   fdpathfs       r   _new_runzSortingPool._new_run|   sB    ##4;;t{{(,6DIIb$Qwr   c                     t        |d      S )Nrb)openr.   r8   s     r   	_open_runzSortingPool._open_run   s    D$r   c                 .    t        j                  |       y r   )r5   remover>   s     r   _remove_runzSortingPool._remove_run   s    
		$r   c              #      K   | j                  |      }	 	 t        |       # t        $ r$ Y |j                          | j	                  |       y w xY w# |j                          | j	                  |       w xY wwr   )r?   r   EOFErrorcloserB   )r.   r8   r9   s      r   	_read_runzSortingPool._read_run   sl     NN4 	#1g  	GGIT"		 GGIT"s0   A:$ 	AA "A:AA #A77A:c              #   z   K   |D cg c]  }| j                  |       }}t        |      D ]  }|  y c c}w wr   )rF   r   )r.   pathsr8   itersitems        r   _merge_runszSortingPool._merge_runs   s?     278$%885M 	DJ	 9s   ;6;c                     t        | j                        | j                  k\  r| j                          | j                  j	                  |       y)z.Adds `item` to the pool to be sorted.
        N)lenr,   r)   saver   )r.   rJ   s     r   addzSortingPool.add   s4     t||,IIKD!r   c                 L    |D ]  }t        ||d        |j                          y )N   )r   rE   )r.   r9   itemsrJ   s       r   
_write_runzSortingPool._write_run   s&     	Dq!			r   c                 :    | j                   j                  |       y r   )r-   r   )r.   filenames     r   _add_runzSortingPool._add_run   s    		"r   c                     | j                   }|rN|j                          | j                         \  }}| j                  ||       | j	                  |       g | _         y y r   )r,   sortr:   rS   rV   )r.   r,   r8   r9   s       r   rN   zSortingPool.save   sL    ,,LLNmmoGD!OOAw'MM$DL r   c                 p    | j                   D ]  }	 t        j                  |        y # t        $ r Y 'w xY wr   )r-   r5   rA   OSErrorr>   s     r   cleanupzSortingPool.cleanup   s9    II 	D		$	  s   )	55c                    |dk  rt        d|z        |dk  rt        d|z        | j                  }t        |      |kD  r| j                         \  }}g }|r>t        |      |k  r0|j	                  |j                                |rt        |      |k  r0| j                  || j                  |             |j                  d|       t        |      |kD  ry y )NrQ   zk=%s must be > 2r&   ztarget=%s must be >= 1r   )	r(   r-   rM   r:   r   poprS   rK   insert)r.   targetkr-   newpathr9   tomerges          r   	reduce_tozSortingPool.reduce_to   s     q5/!344A:5>??yy$i& JGQG3w<!+txxz* 3w<!+ OOAt//89KK7# $i& r   c                 &   |dk  rt        d|z        | j                  st        | j                        S | j	                          |t        | j                        k  r| j                  ||       | j                  }g | _        | j                  |      S )zReturns a sorted list or iterator of the items in the pool.

        :param maxfiles: maximum number of files to open at once.
        rQ   zmaxfiles=%s must be >= 2)r(   r-   sortedr,   rN   rM   rc   rK   )r.   maxfilesr-   s      r   rR   zSortingPool.items   s     a<7(BCCyy $,,''		 c$))n$NN8X. yy	%%r   )i@B N z.run)   )__name__
__module____qualname____doc__r/   r:   r?   rB   rF   rK   rO   rS   rV   rN   r[   rc   rR    r   r   r$   r$   U   sQ    " >@* 	#
"
#$$&r   r$   c                 p    t        ||      }| D ]  }|j                  |        |j                  |      S )aL  Sorts the given items using an external merge sort.

    :param tempdir: the path of a directory to use for temporary file
        storage. The default is to use the system's temp directory.
    :param maxsize: the maximum number of items to keep in memory at once.
    :param maxfiles: maximum number of files to open at once.
    )r)   r'   )rf   )r$   rO   rR   )rR   r)   r'   rf   prJ   s         r   rX   rX      s;     	GW5A 	d77H7%%r   )i Nrh   )rl   
__future__r   r5   r3   heapqr   r   r   whoosh.compatr   r   r
   r   ImportErrorobjectr$   rX   rm   r   r   <module>ru      sS   8 &  / / $"!8L&& L&^&S  s   	= AA