B
    d
                 @   sT   d Z dZddlmZ ddlmZ G dd deZG dd deZ	G d	d
 d
e	Z
dS )z-
This module provides bin packing utilities.
z"Kipp Cannon <kipp.cannon@ligo.org>   )date)versionc               @   sd   e Zd Z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eZdS )BinaJ  
	Bin object for use in packing algorithm implementations.  A Bin
	instance has two attributes:  size, which is the total "size" of
	the contents of the Bin, and objects, which is a list of the Bins
	contents.

	Example:

	>>> strings = Bin()
	>>> s = "hello"
	>>> strings.add(s, len(s))
	>>> s.objects
	['hello']
	>>> s.size
	5
	c             C   s   g | _ d| _dS )z$
		Initialize a new Bin instance.
		    N)objectssize)self r	   ]/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/lalburst/packing.py__init__C   s    zBin.__init__c             C   s   | j | |  j|7  _| S )z9
		Add the object, whose size is as given, to the bin.
		)r   appendr   )r   objr   r	   r	   r
   addJ   s    zBin.addc             C   s"   | j |j  |  j|j7  _| S )z9
		Add the contents of another Bin object to this one.
		)r   extendr   )r   otherr	   r	   r
   __iadd__R   s    zBin.__iadd__c             C   s   | j |j k S )N)r   )r   r   r	   r	   r
   __lt__^   s    z
Bin.__lt__c             C   s   | j |j kS )N)r   )r   r   r	   r	   r
   __le__a   s    z
Bin.__le__c             C   s   | j |j kS )N)r   )r   r   r	   r	   r
   __eq__d   s    z
Bin.__eq__c             C   s   | j |j kS )N)r   )r   r   r	   r	   r
   __ne__g   s    z
Bin.__ne__c             C   s   | j |j kS )N)r   )r   r   r	   r	   r
   __ge__j   s    z
Bin.__ge__c             C   s   | j |j kS )N)r   )r   r   r	   r	   r
   __gt__m   s    z
Bin.__gt__c             C   s   dt | jt | jf S )z)
		A representation of the Bin object.
		zBin(size=%s, %s))strr   r   )r   r	   r	   r
   __repr__p   s    zBin.__repr__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   __str__r	   r	   r	   r
   r   2   s   r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	Packerz
	Parent class for packing algorithms.  Specific packing algorithms
	should sub-class this, providing implementations of the pack() and
	packlist() methods.
	c             C   s
   || _ dS )z4
		Set the list of bins on which we shall operate
		N)bins)r   r    r	   r	   r
   r      s    zPacker.__init__c             C   s   t dS )z1
		Pack an object of given size into the bins.
		N)NotImplementedError)r   r   r   r	   r	   r
   pack   s    zPacker.packc             C   s"   x|D ]\}}|  || qW dS )z
		Pack a list of (size, object) tuples into the bins.

		Default implementation invokes self.pack() on each pair in
		the the order given.
		N)r"   )r   size_object_pairsr   r   r	   r	   r
   packlist   s    zPacker.packlistN)r   r   r   r   r   r"   r$   r	   r	   r	   r
   r      s   r   c               @   s    e Zd ZdZdd Zdd ZdS )BiggestIntoEmptiestz3
	Packs the biggest object into the emptiest bin.
	c             C   s   t | j|| d S )N)minr    r   )r   r   r   r	   r	   r
   r"      s    zBiggestIntoEmptiest.packc             C   s*   x$t |ddD ]\}}| || qW d S )NT)reverse)sortedr"   )r   r#   r   r   r	   r	   r
   r$      s    zBiggestIntoEmptiest.packlistN)r   r   r   r   r"   r$   r	   r	   r	   r
   r%      s   r%   N)r   
__author__Zgit_versionr   __date__r   __version__objectr   r   r%   r	   r	   r	   r
   <module>   s   R