
    ^j0                    L    d Z ddlmZ  G d de          Z G d d          ZdS )aJ  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.
    )annotationsc                      e Zd ZdZd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).
    requested_capacityintreturnNonec                    || _         dS )z&Requested capacity failed to allocate.N)r   )selfr   s     U/home/agentuser/manim-venv/lib/python3.11/site-packages/pyglet/graphics/allocation.py__init__z!AllocatorMemoryException.__init__1   s    "4    N)r   r   r   r	   )__name__
__module____qualname____doc__r    r   r   r   r   (   s2         5 5 5 5 5 5r   r   c                      e Zd ZU dZded<   ded<   dZdd
ZddZddZd dZ	d!dZ
d"dZd#dZd#dZd$dZd$dZd%dZd%dZdS )&	Allocatorz'Buffer space allocation implementation.z	list[int]sizesstartscapacityr   r   r   r   r   r	   c                0    || _         g | _        g | _        dS )zHCreate an allocator for a buffer of the specified maximum capacity size.Nr   )r   r   s     r   r   zAllocator.__init__=   s     $ 


r   sizec                .    || j         k    sJ || _         dS )zQResize the maximum buffer size.

        The capacity cannot be reduced.
        N)r   )r   r   s     r   set_capacityzAllocator.set_capacityT   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
            )a  Allocate memory in the buffer.

        Raises `AllocatorMemoryException` if the allocation cannot be
        fulfilled.

        Args:
            size:
                Size of region to allocate.

        Returns:
            Starting index of the allocated region.
        r      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 { 	1t}$$""1%%%
!!$'''q*4000 ;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A&Jq1u%!!!!T!! 
1%!!!!$z1JJ MJ.	JrNNNd"NNN&t}t';i'GHHHr   startnew_sizec                (   |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.

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

        Returns:
            Starting index of the re-allocated region.
        r   Region not allocatedr   )
deallocr*   r#   r$   r   r   printlistlenr   )r   r+   r   r,   r&   r'   r(   pis_final_blockr)   results              r   realloczAllocator.realloc   s   ( qyyX]]]*q==qyyUD)))1199::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%8d?**
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.

        Args:
            start:
                Starting index of the region.
            size:
                Size of the region.

        r   Nr.   r   )r   r#   r$   r   r"   )r   r+   r   r&   r'   r(   r3   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   tuple[list, list]c                    | j         | j        fS )zGet 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.
        )r   r   r   s    r   get_allocated_regionszAllocator.get_allocated_regions+  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 )zGReturns the amount of space unused, not including the final free block.r   r   N)r   r   r#   r$   )r   
total_freer%   r&   r'   r(   s         r   get_fragmented_free_sizez"Allocator.get_fragmented_free_size4  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 )z"Return the amount of space unused.r    )r   r   r   r>   )r   free_ends     r   get_free_sizezAllocator.get_free_sizeB  sH    { 	!= =DKOdjn$DE,,..99r   floatc                Z    d|                                  t          | j                  z  z
  S )z0Return fraction of capacity currently allocated.g      ?)rA   rB   r   r:   s    r   	get_usagezAllocator.get_usageJ  s(    D&&((5+?+????r   c                    |                                  }|dk    rdS |                                 t          |                                            z  S )z5Return fraction of free space that is not expandable.r   g        )rA   r>   rB   )r   r)   s     r   get_fragmentationzAllocator.get_fragmentationN  sL    &&((	>>2,,..t7I7I7K7K1L1LLLr   strc           	     p    dt          t          t          | j        | j                                      z   S )Nzallocs=)reprr1   r$   r   r   r:   s    r   __str__zAllocator.__str__U  s+    4Sdj%A%A B BCCCCr   c                (    d| j         j         d| dS )N< >)	__class__r   r:   s    r   __repr__zAllocator.__repr__X  s     64>*66T6666r   N)r   r   r   r	   )r   r   r   r	   )r   r   r   r   )r+   r   r   r   r,   r   r   r   )r+   r   r   r   r   r	   )r   r8   )r   r   )r   rB   )r   rG   )r   r   r   r   __annotations__	__slots__r   r   r*   r6   r/   r;   r>   rA   rD   rF   rJ   rP   r   r   r   r   r   6   s@        11-I   .   <I <I <I <I|T T T Tl9> 9> 9> 9>v' ' ' '   : : : :@ @ @ @M M M MD D D D7 7 7 7 7 7r   r   N)r   
__future__r   	Exceptionr   r   r   r   r   <module>rU      s    H # " " " " "5 5 5 5 5y 5 5 5c7 c7 c7 c7 c7 c7 c7 c7 c7 c7r   