
    \
j8                     @    d Z  G d de          Z G d d          ZdS )aK  Memory allocation algorithm for vertex arrays and buffers.

The region allocator is used to allocate vertex indices within a vertex
domain's  multiple buffers.  ("Buffer" refers to any abstract buffer presented
by :py:mod:`pyglet.graphics.vertexbuffer`.
 
The allocator will at times request more space from the buffers. The current
policy is to double the buffer size when there is not enough room to fulfil an
allocation.  The buffer is never resized smaller.

The allocator maintains references to free space only; it is the caller's
responsibility to maintain the allocated regions.
c                       e Zd ZdZd ZdS )AllocatorMemoryExceptiona"  The buffer is not large enough to fulfil an allocation.

    Raised by `Allocator` methods when the operation failed due to
    lack of buffer space.  The buffer should be increased to at least
    requested_capacity and then the operation retried (guaranteed to
    pass second time).
    c                     || _         d S )N)requested_capacity)selfr   s     T/DATA/AppData/hermes/venv/lib/python3.11/site-packages/pyglet/graphics/allocation.py__init__z!AllocatorMemoryException.__init__S   s    "4    N)__name__
__module____qualname____doc__r    r	   r   r   r   J   s-         5 5 5 5 5r	   r   c                   ^    e Z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 Zd Zd ZdS )	Allocatorz'Buffer space allocation implementation.capacitystartssizesc                 0    || _         g | _        g | _        dS )zCreate an allocator for a buffer of the specified capacity.

        :Parameters:
            `capacity` : int
                Maximum size of the buffer.

        Nr   )r   r   s     r   r   zAllocator.__init__\   s     !$ 


r	   c                 .    || j         k    sJ || _         dS )zResize the maximum buffer size.
        
        The capaity cannot be reduced.

        :Parameters:
            `size` : int
                New maximum size of the buffer.

        N)r   )r   sizes     r   set_capacityzAllocator.set_capacityy   s"     dm####r	   c                 \   |dk    sJ |dk    rdS | j         sP|| j        k    r6| j                             d           | j                            |           dS t	          |          | j         d         |k    r8| j                             dd           | j                            d|           dS | j         d         | j        d         z   }t          t          | j         dd         | j        dd                             D ]i\  }\  }}||z
  }||k    r2| j        |xx         ||z   z  cc<   | j         |dz   = | j        |dz   = |c S ||k    r| j        |xx         |z  cc<   |c S ||z   }j| j        |z
  }||k    r| j        dxx         |z  cc<   |S t	          | j        |z   |z
            )a9  Allocate memory in the buffer.

        Raises `AllocatorMemoryException` if the allocation cannot be
        fulfilled.

        :Parameters:
            `size` : int
                Size of region to allocate.
               
        :rtype: int
        :return: Starting index of the allocated region.
               N)r   r   appendr   r   insert	enumeratezip)r   r   
free_startialloc_start
alloc_size	free_sizes          r   alloczAllocator.alloc   s    qyyyy1991 { 	5t}$$""1%%%
!!$'''q.t444 ;q>D  Kq!$$$Ja&&&1 [^djm3
,5c$+abb/4:VWVXVX>6Z6Z,[,[ 	2 	2(A(Z
 $j0ID  
1Z!77K!$JqsO!!!!T!! 
1%!!!!$z1JJ MJ.	JrNNNd"NNN&t}t';i'GHHHr	   c                 (   |dk    r|dk    sJ |dk    r|dk    r|                      ||           dS |dk    r|                     |          S ||k     r|                      ||z   ||z
             |S t          t          | j        | j        f           D ]\  }\  }}||z
  }|dk    r|||z
  k    r n|dk    r	|||z
  k    sVt          t          t          | j        | j                                       t          |||           t          |||           |dk    r	|||z
  k    s
J d            |||z
  k    r|t          | j                  dz
  k    }|s| j        |dz            ||z   z
  }	n| j	        ||z   z
  }	|	||z
  k    r@|s>| j        |xx         |	| j        |dz            z   z  cc<   | j        |dz   = | j        |dz   = |S |	||z
  k    r| j        |xx         ||z
  z  cc<   |S |                     |          }
|                      ||           |
S )a  Reallocate a region of the buffer.

        This is more efficient than separate `dealloc` and `alloc` calls, as
        the region can often be resized in-place.

        Raises `AllocatorMemoryException` if the allocation cannot be
        fulfilled.

        :Parameters:
            `start` : int
                Current starting index of the region.
            `size` : int
                Current size of the region.
            `new_size` : int
                New size of the region.

        r   Region not allocatedr   )
deallocr&   r   r    r   r   printlistlenr   )r   startr   new_sizer"   r#   r$   pis_final_blockr%   results              r   realloczAllocator.realloc   s   $ qyyX]]]*q==qyyUD)))1QYY::h'''
 d??LL)4(?;;;L -6cDK;T6U,V,V 	 	(A(Z#AAvv$*q.00Q4:>11$s4;
3344555%x(((![*---Avv$*q.0002H000:>!!#dk"2"2Q"66N! ; KA.%$,?		 MUT\:	
 HtO++N+ 
1TZA->!>>KA&Jq1u%X_,,
1D0 H%%UD!!!r	   c                 j   |dk    sJ |dk    rdS | j         sJ t          t          | j         | j        f           D ]\  }\  }}||z
  }|dk    r|||z
  k    r n|dk    r	|||z
  k    s
J d            |dk    r||k    r| j         |= | j        |= dS |dk    r,| j         |xx         |z  cc<   | j        |xx         |z  cc<   dS |||z
  k    r| j        |xx         |z  cc<   dS || j        |<   | j                             |dz   ||z              | j                            |dz   |||z   z
             dS )zFree a region of the buffer.

        :Parameters:
            `start` : int
                Starting index of the region.
            `size` : int
                Size of the region.

        r   Nr(   r   )r   r   r    r   r   )r   r-   r   r"   r#   r$   r/   s          r   r)   zAllocator.dealloc  s    qyyyy199F{{ -6cDK;T6U,V,V 	 	(A(Z#AAvv$*q.00 Avv$*q.0002H00066dj((A
1!VVKNNNd"NNNJqMMMT!MMMMMZ!^##JqMMMT!MMMMM& DJqMKq1uedl333Ja!eZ1t8%<=====r	   c                     | j         | j        fS )a  Get a list of (aggregate) allocated regions.

        The result of this method is ``(starts, sizes)``, where ``starts`` is
        a list of starting indices of the regions and ``sizes`` their
        corresponding lengths.

        :rtype: (list, list)
        )r   r   r   s    r   get_allocated_regionszAllocator.get_allocated_regionsR  s     {DJ&&r	   c                     | j         sdS d}| j         d         | j        d         z   }t          t          | j         dd         | j        dd                             D ]\  }\  }}|||z
  z  }||z   }|S )zmReturns the amount of space unused, not including the final
        free block.

        :rtype: int
        r   r   N)r   r   r   r    )r   
total_freer!   r"   r#   r$   s         r   get_fragmented_free_sizez"Allocator.get_fragmented_free_size^  s     { 	1 
[^djm3
,5c$+abb/4:VWVXVX>6Z6Z,[,[ 	2 	2(A(Z+
22J$z1JJr	   c                     | j         s| j        S | j        | j         d         | j        d         z   z
  }|                                 |z   S )zHReturn the amount of space unused.
        
        :rtype: int
        r   )r   r   r   r9   )r   free_ends     r   get_free_sizezAllocator.get_free_sizep  sJ    
 { 	!= =DKOdjn$DE,,..99r	   c                 Z    d|                                  t          | j                  z  z
  S )zXReturn fraction of capacity currently allocated.
        
        :rtype: float
        g      ?)r<   floatr   r5   s    r   	get_usagezAllocator.get_usage{  s*    
 D&&((5+?+????r	   c                     |                                  }|dk    rdS |                                 t          |                                            z  S )z]Return fraction of free space that is not expandable.
        
        :rtype: float
        r   g        )r<   r9   r>   )r   r%   s     r   get_fragmentationzAllocator.get_fragmentation  sN    
 &&((	>>2,,..t7I7I7K7K1L1LLLr	   c           	      p    dt          t          t          | j        | j                                      z   S )Nzallocs=)reprr+   r    r   r   r5   s    r   __str__zAllocator.__str__  s+    4Sdj%A%A B BCCCCr	   c                 B    d| j         j        dt          |           dS )N< >)	__class__r
   strr5   s    r   __repr__zAllocator.__repr__  s$      N333SYYYY??r	   N)r
   r   r   r   	__slots__r   r   r&   r2   r)   r6   r9   r<   r?   rA   rD   rK   r   r	   r   r   r   W   s        11-I  :  <I <I <I|Q Q Qf9> 9> 9>v
' 
' 
'  $	: 	: 	:@ @ @M M MD D D@ @ @ @ @r	   r   N)r   	Exceptionr   r   r   r	   r   <module>rN      s   H L
5 
5 
5 
5 
5y 
5 
5 
5y@ y@ y@ y@ y@ y@ y@ y@ y@ y@r	   