Skip to content

Commit 6803eba

Browse files
committed
add ListPacker + tests
1 parent 31c6c23 commit 6803eba

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

pyiceberg/utils/bin_packing.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,29 @@ def remove_bin(self) -> Bin[T]:
104104
return bin_
105105
else:
106106
return self.bins.pop(0)
107+
108+
109+
class ListPacker(Generic[T]):
110+
_target_weight: int
111+
_lookback: int
112+
_largest_bin_first: bool
113+
114+
def __init__(self, target_weight: int, lookback: int, largest_bin_first: bool) -> None:
115+
self._target_weight = target_weight
116+
self._lookback = lookback
117+
self._largest_bin_first = largest_bin_first
118+
119+
def pack(self, items: List[T], weight_func: Callable[[T], int]) -> List[List[T]]:
120+
return list(
121+
PackingIterator(
122+
items=items,
123+
target_weight=self._target_weight,
124+
lookback=self._lookback,
125+
weight_func=weight_func,
126+
largest_bin_first=self._largest_bin_first,
127+
)
128+
)
129+
130+
def pack_end(self, items: List[T], weight_func: Callable[[T], int]) -> List[List[T]]:
131+
packed = self.pack(items=list(reversed(items)), weight_func=weight_func)
132+
return [list(reversed(bin_items)) for bin_items in reversed(packed)]

tests/utils/test_bin_packing.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
import pytest
2222

23-
from pyiceberg.utils.bin_packing import PackingIterator
23+
from pyiceberg.utils.bin_packing import ListPacker, PackingIterator
24+
25+
INT_MAX = 2147483647
2426

2527

2628
@pytest.mark.parametrize(
@@ -83,4 +85,46 @@ def test_bin_packing_lookback(
8385
def weight_func(x: int) -> int:
8486
return x
8587

88+
packer: ListPacker[int] = ListPacker(target_weight, lookback, largest_bin_first)
89+
8690
assert list(PackingIterator(splits, target_weight, lookback, weight_func, largest_bin_first)) == expected_lists
91+
assert list(packer.pack(splits, weight_func)) == expected_lists
92+
93+
94+
@pytest.mark.parametrize(
95+
"splits, target_weight, lookback, largest_bin_first, expected_lists",
96+
[
97+
# Single Lookback Tests
98+
([1, 2, 3, 4, 5], 3, 1, False, [[1, 2], [3], [4], [5]]),
99+
([1, 2, 3, 4, 5], 4, 1, False, [[1, 2], [3], [4], [5]]),
100+
([1, 2, 3, 4, 5], 5, 1, False, [[1], [2, 3], [4], [5]]),
101+
([1, 2, 3, 4, 5], 6, 1, False, [[1, 2, 3], [4], [5]]),
102+
([1, 2, 3, 4, 5], 7, 1, False, [[1, 2], [3, 4], [5]]),
103+
([1, 2, 3, 4, 5], 8, 1, False, [[1, 2], [3, 4], [5]]),
104+
([1, 2, 3, 4, 5], 9, 1, False, [[1, 2, 3], [4, 5]]),
105+
([1, 2, 3, 4, 5], 11, 1, False, [[1, 2, 3], [4, 5]]),
106+
([1, 2, 3, 4, 5], 12, 1, False, [[1, 2], [3, 4, 5]]),
107+
([1, 2, 3, 4, 5], 14, 1, False, [[1], [2, 3, 4, 5]]),
108+
([1, 2, 3, 4, 5], 15, 1, False, [[1, 2, 3, 4, 5]]),
109+
# Unlimited Lookback Tests
110+
([1, 2, 3, 4, 5], 3, INT_MAX, False, [[1, 2], [3], [4], [5]]),
111+
([1, 2, 3, 4, 5], 4, INT_MAX, False, [[2], [1, 3], [4], [5]]),
112+
([1, 2, 3, 4, 5], 5, INT_MAX, False, [[2, 3], [1, 4], [5]]),
113+
([1, 2, 3, 4, 5], 6, INT_MAX, False, [[3], [2, 4], [1, 5]]),
114+
([1, 2, 3, 4, 5], 7, INT_MAX, False, [[1], [3, 4], [2, 5]]),
115+
([1, 2, 3, 4, 5], 8, INT_MAX, False, [[1, 2, 4], [3, 5]]),
116+
([1, 2, 3, 4, 5], 9, INT_MAX, False, [[1, 2, 3], [4, 5]]),
117+
([1, 2, 3, 4, 5], 10, INT_MAX, False, [[2, 3], [1, 4, 5]]),
118+
([1, 2, 3, 4, 5], 11, INT_MAX, False, [[1, 3], [2, 4, 5]]),
119+
([1, 2, 3, 4, 5], 12, INT_MAX, False, [[1, 2], [3, 4, 5]]),
120+
([1, 2, 3, 4, 5], 13, INT_MAX, False, [[2], [1, 3, 4, 5]]),
121+
([1, 2, 3, 4, 5], 14, INT_MAX, False, [[1], [2, 3, 4, 5]]),
122+
([1, 2, 3, 4, 5], 15, INT_MAX, False, [[1, 2, 3, 4, 5]]),
123+
],
124+
)
125+
def test_reverse_bin_packing_lookback(
126+
splits: List[int], target_weight: int, lookback: int, largest_bin_first: bool, expected_lists: List[List[int]]
127+
) -> None:
128+
packer: ListPacker[int] = ListPacker(target_weight, lookback, largest_bin_first)
129+
result = packer.pack_end(splits, lambda x: x)
130+
assert result == expected_lists

0 commit comments

Comments
 (0)