Skip to content

Commit

Permalink
Release 0.2.0 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Jan 14, 2023
1 parent 72b66b0 commit 9ddccbc
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 42 deletions.
6 changes: 5 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
0.2.0
Changed: Packer::sort and ::fit now support to children type for generic

0.1.0
Added: Packer, GrowingPacker
Added: Packer, FixedPacker, GrowingPacker
Added: PackerFitPos, PackerRegionSize, PackerRegion
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ org.gradle.jvmargs=-Dfile.encoding=UTF-8
projGroupId=io.github.over-run
projArtifactId=bin-packing
projName=bin-packing
projVersion=0.1.0
projVersion=0.2.0
projDesc=The Java port of jakesgordon/bin-packing.
projVcs=Over-Run/bin-packing
projBranch=0.x
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pluginManagement {
repositories {
mavenCentral()
gradlePluginPortal()
maven { url 'https://maven.aliyun.com/repository/public'}
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
}
}
Expand Down
16 changes: 1 addition & 15 deletions src/main/java/org/overrun/binpacking/FixedPacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public FixedPacker(int width, int height) {
}

@Override
public <T> void fit(List<PackerRegion<T>> regions) {
public void fit(List<? extends PackerRegion<?>> regions) {
PackerNode node;
for (var region : regions) {
if ((node = findNode(root, region.width(), region.height())) != null) {
Expand All @@ -93,20 +93,6 @@ public int height() {
return root.height();
}

private PackerNode findNode(PackerNode root, int w, int h) {
if (root.used()) {
PackerNode node = findNode(root.right(), w, h);
if (node != null) {
return node;
}
return findNode(root.down(), w, h);
}
if (w <= root.width() && h <= root.height()) {
return root;
}
return null;
}

private PackerNode splitNode(PackerNode node, int w, int h) {
return node.markUsed()
.setDown(new PackerNode()
Expand Down
16 changes: 1 addition & 15 deletions src/main/java/org/overrun/binpacking/GrowingPacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public GrowingPacker() {
}

@Override
public <T> void fit(List<PackerRegion<T>> regions) {
public void fit(List<? extends PackerRegion<?>> regions) {
PackerNode node;
boolean isNotEmpty = regions.size() > 0;
int w = isNotEmpty ? regions.get(0).width() : 0;
Expand All @@ -108,20 +108,6 @@ public int height() {
return root.height();
}

private PackerNode findNode(PackerNode root, int w, int h) {
if (root.used()) {
PackerNode node = findNode(root.right(), w, h);
if (node != null) {
return node;
}
return findNode(root.down(), w, h);
}
if (w <= root.width() && h <= root.height()) {
return root;
}
return null;
}

private PackerNode splitNode(PackerNode node, int w, int h) {
return node.markUsed()
.setDown(new PackerNode()
Expand Down
29 changes: 23 additions & 6 deletions src/main/java/org/overrun/binpacking/Packer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

package org.overrun.binpacking;

import org.overrun.binpacking.internal.PackerNode;

import java.util.Arrays;
import java.util.List;

Expand All @@ -40,11 +42,12 @@ public sealed abstract class Packer implements PackerRegionSize permits FixedPac
* This method mutates the given regions.
*
* @param regions the regions.
* @param <T> the type of the userdata.
* @param <U> the type of the userdata.
* @param <E> the type of elements in the list.
* @return the sorted regions wrapped in List.
*/
@SafeVarargs
public static <T> List<PackerRegion<T>> sort(PackerRegion<T>... regions) {
public static <U, E extends PackerRegion<? extends U>> List<E> sort(E... regions) {
Arrays.sort(regions);
return List.of(regions);
}
Expand All @@ -55,21 +58,35 @@ public static <T> List<PackerRegion<T>> sort(PackerRegion<T>... regions) {
* This method mutates the given regions.
*
* @param regions the regions.
* @param <T> the type of the userdata.
* @param <U> the type of the userdata.
* @param <E> the type of elements in the list.
* @return the sorted regions.
*/
public static <T> List<PackerRegion<T>> sort(List<PackerRegion<T>> regions) {
public static <U, E extends PackerRegion<? extends U>> List<E> sort(List<E> regions) {
regions.sort(null);
return regions;
}

PackerNode findNode(PackerNode root, int w, int h) {
if (root.used()) {
PackerNode node = findNode(root.right(), w, h);
if (node != null) {
return node;
}
return findNode(root.down(), w, h);
}
if (w <= root.width() && h <= root.height()) {
return root;
}
return null;
}

/**
* Fits this packer with the given regions.
*
* @param regions the regions.
* @param <T> the type of the userdata.
*/
public abstract <T> void fit(List<PackerRegion<T>> regions);
public abstract void fit(List<? extends PackerRegion<?>> regions);

/**
* Gets the width of the root node of this packer.
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/overrun/binpacking/test/PackerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @since 0.1.0
*/
public final class PackerTest {
private static List<PackerRegion<Object>> generateData() {
private static List<PackerRegion<?>> generateData() {
return Packer.sort(
PackerRegion.sized(100, 300),
PackerRegion.sized(300, 300),
Expand All @@ -47,7 +47,7 @@ private static List<PackerRegion<Object>> generateData() {
);
}

private static void testFixed(List<PackerRegion<Object>> testData) {
private static void testFixed(List<PackerRegion<?>> testData) {
var packer = new FixedPacker(400, 500);
packer.fit(testData);
System.out.println(packer.width() + ", " + packer.height());
Expand All @@ -58,7 +58,7 @@ private static void testFixed(List<PackerRegion<Object>> testData) {
);
}

private static void testGrowing(List<PackerRegion<Object>> testData) {
private static void testGrowing(List<PackerRegion<?>> testData) {
var packer = new GrowingPacker();
packer.fit(testData);
System.out.println(packer.width() + ", " + packer.height());
Expand Down

0 comments on commit 9ddccbc

Please sign in to comment.