Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALBurst 2.0.7.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Macros Modules Pages
packing.py
Go to the documentation of this file.
1# Copyright (C) 2006,2016,2017,2020 Kipp Cannon
2#
3# This program is free software; you can redistribute it and/or modify it
4# under the terms of the GNU General Public License as published by the
5# Free Software Foundation; either version 2 of the License, or (at your
6# option) any later version.
7#
8# This program is distributed in the hope that it will be useful, but
9# WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11# Public License for more details.
12#
13# You should have received a copy of the GNU General Public License along
14# with this program; if not, write to the Free Software Foundation, Inc.,
15# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
17
18#
19# =============================================================================
20#
21# Preamble
22#
23# =============================================================================
24#
25
26
27"""
28This module provides bin packing utilities.
29"""
30
31
32__author__ = "Kipp Cannon <kipp.cannon@ligo.org>"
33from .git_version import date as __date__
34from .git_version import version as __version__
35
36
37#
38# =============================================================================
39#
40# Bins
41#
42# =============================================================================
43#
44
45
46# FIXME: this should probably be a subclass of list, but no doubt that will
47# break things so make this change some time it's convenient.
48
49
50class Bin(object):
51 """
52 Bin object for use in packing algorithm implementations. A Bin
53 instance has two attributes: size, which is the total "size" of
54 the contents of the Bin, and objects, which is a list of the Bins
55 contents.
56
57 Example:
58
59 >>> strings = Bin()
60 >>> s = "hello"
61 >>> strings.add(s, len(s))
62 >>> s.objects
63 ['hello']
64 >>> s.size
65 5
66 """
67 def __init__(self):
68 """
69 Initialize a new Bin instance.
70 """
71 self.objects = []
72 self.size = 0
73
74 def add(self, obj, size):
75 """
76 Add the object, whose size is as given, to the bin.
77 """
78 self.objects.append(obj)
79 self.size += size
80 return self
81
82 def __iadd__(self, other):
83 """
84 Add the contents of another Bin object to this one.
85 """
86 self.objects.extend(other.objects)
87 self.size += other.size
88 return self
89
90 #
91 # Compare two Bin objects by their sizes.
92 #
93
94 def __lt__(self, other):
95 return self.size < other.size
96
97 def __le__(self, other):
98 return self.size <= other.size
99
100 def __eq__(self, other):
101 return self.size == other.size
102
103 def __ne__(self, other):
104 return self.size != other.size
105
106 def __ge__(self, other):
107 return self.size >= other.size
108
109 def __gt__(self, other):
110 return self.size > other.size
111
112 def __repr__(self):
113 """
114 A representation of the Bin object.
115 """
116 return "Bin(size=%s, %s)" % (str(self.size), str(self.objects))
117
118 __str__ = __repr__
119
120
121#
122# =============================================================================
123#
124# Packing Algorithms
125#
126# =============================================================================
127#
128
129
130# FIXME: this should probably also be a subclass of list.
131
132class Packer(object):
133 """
134 Parent class for packing algorithms. Specific packing algorithms
135 should sub-class this, providing implementations of the pack() and
136 packlist() methods.
137 """
138 def __init__(self, bins):
139 """
140 Set the list of bins on which we shall operate
141 """
142 self.bins = bins
143
144 def pack(self, size, obj):
145 """
146 Pack an object of given size into the bins.
147 """
148 raise NotImplementedError
149
150 def packlist(self, size_object_pairs):
151 """
152 Pack a list of (size, object) tuples into the bins.
153
154 Default implementation invokes self.pack() on each pair in
155 the the order given.
156 """
157 for size, obj in size_object_pairs:
158 self.pack(size, obj)
159
160
162 """
163 Packs the biggest object into the emptiest bin.
164 """
165 def pack(self, size, obj):
166 min(self.bins).add(obj, size)
167
168 def packlist(self, size_object_pairs):
169 for size, obj in sorted(size_object_pairs, reverse = True):
170 self.packpack(size, obj)
static double min(double a, double b)
Definition: EPFilters.c:42
Packs the biggest object into the emptiest bin.
Definition: packing.py:164
def pack(self, size, obj)
Pack an object of given size into the bins.
Definition: packing.py:165
def packlist(self, size_object_pairs)
Pack a list of (size, object) tuples into the bins.
Definition: packing.py:168
Bin object for use in packing algorithm implementations.
Definition: packing.py:66
def __eq__(self, other)
Definition: packing.py:100
def __gt__(self, other)
Definition: packing.py:109
def add(self, obj, size)
Add the object, whose size is as given, to the bin.
Definition: packing.py:77
def __lt__(self, other)
Definition: packing.py:94
def __iadd__(self, other)
Add the contents of another Bin object to this one.
Definition: packing.py:85
def __ge__(self, other)
Definition: packing.py:106
def __repr__(self)
A representation of the Bin object.
Definition: packing.py:115
def __le__(self, other)
Definition: packing.py:97
def __ne__(self, other)
Definition: packing.py:103
Parent class for packing algorithms.
Definition: packing.py:137
def pack(self, size, obj)
Pack an object of given size into the bins.
Definition: packing.py:147
def packlist(self, size_object_pairs)
Pack a list of (size, object) tuples into the bins.
Definition: packing.py:156