Skip to content

Commit

Permalink
Merge branch 'master' of github.com:skjolber/3d-bin-container-packing…
Browse files Browse the repository at this point in the history
… into support
  • Loading branch information
skjolber committed Jul 23, 2024
2 parents d763e2e + b6625e9 commit 6839759
Show file tree
Hide file tree
Showing 41 changed files with 1,301 additions and 1,171 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
with:
distribution: 'temurin'
java-version: 11
- uses: actions/cache@v3
- uses: actions/cache@v4
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-v2-${{ hashFiles('**/pom.xml') }}
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
![Build Status](https://github.com/skjolber/3d-bin-container-packing/actions/workflows/maven.yml/badge.svg)
[![Maven Central](https://img.shields.io/maven-central/v/com.github.skjolber.3d-bin-container-packing/parent.svg)](https://mvnrepository.com/artifact/com.github.skjolber.3d-bin-container-packing)

# 3d-bin-container-packing

This library does 3D rectangular bin packing; it attempts to match a set of 3D items to one or more in a set of 3D containers. The result can be constrained to a maximum number of containers.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.github.skjolber.packing.api;

import java.io.Closeable;

/**
* Fit boxes into container, i.e. perform bin packing to a single container.
*
* Thread-safe implementation.
*/

public interface Packager<B extends PackagerResultBuilder<B>> {
public interface Packager<B extends PackagerResultBuilder<B>> extends Closeable {

B newResultBuilder();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
package com.github.skjolber.packing.deadline;

public class DeadlineCheckPackagerInterruptSupplier implements PackagerInterruptSupplier {
import java.io.Closeable;
import java.util.concurrent.ScheduledFuture;

protected final long deadline;
public class DeadlineCheckPackagerInterruptSupplier implements PackagerInterruptSupplier, Runnable, Closeable {

public DeadlineCheckPackagerInterruptSupplier(long deadline) {
super();
this.deadline = deadline;
// this is not entirely accurate for multi-threading, but close enough
// (should have been volatile)
protected boolean expired = false;
protected ScheduledFuture<?> future;

public DeadlineCheckPackagerInterruptSupplier() {
}

@Override
public boolean getAsBoolean() {
return System.currentTimeMillis() > deadline;
return expired;
}

@Override
public void run() {
this.expired = true;
}

public void close() {
future.cancel(true);
}

public void setFuture(ScheduledFuture<?> future) {
this.future = future;
}

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
package com.github.skjolber.packing.deadline;

import java.io.Closeable;
import java.util.concurrent.ScheduledFuture;
import java.util.function.BooleanSupplier;

public class DelegateDeadlineCheckPackagerInterruptSupplier implements PackagerInterruptSupplier {
public class DelegateDeadlineCheckPackagerInterruptSupplier implements PackagerInterruptSupplier, Runnable, Closeable {

// this is not entirely accurate for multi-threading, but close enough
// (should have been volatile)
protected boolean expired = false;
protected ScheduledFuture<?> future;
protected final BooleanSupplier delegate;
protected final long deadline;

public DelegateDeadlineCheckPackagerInterruptSupplier(long deadline, BooleanSupplier delegate) {

public DelegateDeadlineCheckPackagerInterruptSupplier(BooleanSupplier delegate) {
super();
this.deadline = deadline;
this.delegate = delegate;
}

@Override
public boolean getAsBoolean() {
return delegate.getAsBoolean() || System.currentTimeMillis() > deadline;
return expired || delegate.getAsBoolean();
}

@Override
public void run() {
this.expired = true;
}

public void close() {
future.cancel(true);
}

public void setFuture(ScheduledFuture<?> future) {
this.future = future;
}

}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.github.skjolber.packing.deadline;

import java.io.Closeable;

@FunctionalInterface
public interface PackagerInterruptSupplier {
public interface PackagerInterruptSupplier extends Closeable {

/**
* Gets a result.
*
* @return a result
*/
boolean getAsBoolean();

default void close() {
}
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,68 @@
package com.github.skjolber.packing.deadline;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.BooleanSupplier;

public class PackagerInterruptSupplierBuilder {

public static final NegativePackagerInterruptSupplier NOOP = new NegativePackagerInterruptSupplier();
public static final NegativePackagerInterruptSupplier NEGATIVE = new NegativePackagerInterruptSupplier();
public static final PositivePackagerInterruptSupplier POSITIVE = new PositivePackagerInterruptSupplier();

private long deadline = Long.MAX_VALUE;
private int checkpointsPerDeadlineCheck = 1;
private BooleanSupplier interrupt = null;
private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;

public static PackagerInterruptSupplierBuilder builder() {
return new PackagerInterruptSupplierBuilder();
}

public PackagerInterruptSupplierBuilder withDeadline(long deadline, int checkpointsPerDeadlineCheck) {
public PackagerInterruptSupplierBuilder withDeadline(long deadline) {
this.deadline = deadline;
this.checkpointsPerDeadlineCheck = checkpointsPerDeadlineCheck;
return this;
}

public PackagerInterruptSupplierBuilder withInterrupt(BooleanSupplier interrupt) {
this.interrupt = interrupt;
return this;
}

public PackagerInterruptSupplierBuilder withScheduledThreadPoolExecutor(ScheduledThreadPoolExecutor executor) {
this.scheduledThreadPoolExecutor = executor;
return this;
}

public PackagerInterruptSupplier build() {
if(checkpointsPerDeadlineCheck == Integer.MAX_VALUE || checkpointsPerDeadlineCheck == -1 || deadline == Long.MAX_VALUE || deadline == -1L) {

if(deadline == Long.MAX_VALUE || deadline == -1L) {
// no deadline
if(interrupt != null) {
return new DefaultPackagerInterrupt(interrupt);
}
return NOOP;
return NEGATIVE;
}

if(checkpointsPerDeadlineCheck == 1) {
if(interrupt == null) {
return new DeadlineCheckPackagerInterruptSupplier(deadline);
}
return new DelegateDeadlineCheckPackagerInterruptSupplier(deadline, interrupt);
long delay = deadline - System.currentTimeMillis();
if(delay <= 0) {
return POSITIVE; // i.e. time is already up
}

if(scheduledThreadPoolExecutor == null) {
throw new IllegalStateException("Expected scheduler");
}

if(interrupt == null) {
return new NthDeadlineCheckPackagerInterruptSupplier(deadline, checkpointsPerDeadlineCheck);
DeadlineCheckPackagerInterruptSupplier supplier = new DeadlineCheckPackagerInterruptSupplier();
ScheduledFuture<?> schedule = scheduledThreadPoolExecutor.schedule(supplier, delay, TimeUnit.MILLISECONDS);
supplier.setFuture(schedule);
return supplier;
}
return new DelegateNthDeadlineCheckPackagerInterruptSupplier(deadline, checkpointsPerDeadlineCheck, interrupt);

DelegateDeadlineCheckPackagerInterruptSupplier supplier = new DelegateDeadlineCheckPackagerInterruptSupplier(interrupt);
ScheduledFuture<?> schedule = scheduledThreadPoolExecutor.schedule(supplier, delay, TimeUnit.MILLISECONDS);
supplier.setFuture(schedule);
return supplier;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.skjolber.packing.deadline;

public class PositivePackagerInterruptSupplier implements PackagerInterruptSupplier {

@Override
public boolean getAsBoolean() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public interface PermutationRotationIterator {
/**
* Get current permutations
*
* @return
* @return current permutations array
*/

int[] getPermutations();
Expand All @@ -49,7 +49,7 @@ public interface PermutationRotationIterator {
*
* Get current length
*
* @return
* @return current length of permutations array
*/

int length();
Expand Down Expand Up @@ -112,7 +112,7 @@ public interface PermutationRotationIterator {
*
* @param state previously saved state
* @param length number of items
* @return
* @return current permutations + rotations
*/

List<PermutationRotation> get(PermutationRotationState state, int length);
Expand Down
Loading

0 comments on commit 6839759

Please sign in to comment.