Title: Fast 1d Bin Packing
Version: 0.2.0
Description: Implements the First Fit Decreasing algorithm to achieve one dimensional heuristic bin packing. Runtime is of order O(n log(n)) where n is the number of items to pack. See "The Art of Computer Programming Vol. 1" by Donald E. Knuth (1997, ISBN: 0201896834) for more details.
License: GPL (≥ 3)
Encoding: UTF-8
RoxygenNote: 7.3.3
LinkingTo: cpp11
Suggests: testthat (≥ 3.0.0), hedgehog (≥ 0.1), BBmisc (≥ 1.13)
Config/testthat/edition: 3
URL: https://github.com/lschneiderbauer/binpackr, https://lschneiderbauer.github.io/binpackr/
BugReports: https://github.com/lschneiderbauer/binpackr/issues
NeedsCompilation: yes
Packaged: 2026-01-12 19:54:31 UTC; lukas
Author: Lukas Schneiderbauer [aut, cre, cph]
Maintainer: Lukas Schneiderbauer <lukas.schneiderbauer@gmail.com>
Repository: CRAN
Date/Publication: 2026-01-12 20:10:02 UTC

1D bin packing "First Fit (Decreasing)" algorithm

Description

1D bin packing "First Fit (Decreasing)" algorithm

Usage

bin_pack_ffd(x, cap, sort = TRUE)

Arguments

x

[numeric()] A numeric vector of item sizes to be fit into bins. Each value represents the size of an atomic item. If a value is NA it is ignored and the corresponding result will also be NA.

cap

[numeric(1)] A scalar value representing the bin capacity in units of values in x. If an individual item size is above cap a single bin is reserved for this item.

sort

[logical(1)] Determines whether the input vector should be sorted in decreasing order before applying the "First Fit" algorithm ("First Fit Decreasing").

Details

See Wikipedia for a concise introduction or "The Art of Computer Programming Vol. 1" by Donald E. Knuth (1997, ISBN: 0201896834) for more details.

Value

[integer()] An integer vector of labels of the same length as x. The integer label at position i determines the assignment of the ith item with size x[i] to a bin. If the value x[i] is NA the result label at position i will also be NA.

Examples

# Generate a vector of item sizes
x <- sample(100, 1000, replace = TRUE)

# Pack those items into bins of capacity 130
bins <- bin_pack_ffd(x, cap = 130)

# Number of bins needed to pack the items
print(length(unique(bins)))