-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to multiplatform setup, and many other things
- Loading branch information
Showing
36 changed files
with
557 additions
and
413 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package binpack | ||
|
||
interface Container { | ||
val boxes: Collection<PlacedBox> | ||
val size: Int | ||
var ci: Int | ||
val hasAccessibleSpace: Boolean | ||
val freeSpace: Int | ||
|
||
fun add(placedBox: PlacedBox) | ||
fun clone(): Container | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package binpack | ||
|
||
class SegmentContainer( | ||
override var ci: Int, | ||
override val size: Int, | ||
override val boxes: MutableList<PlacedBox> = mutableListOf(), | ||
val segmentsX: MutableList<Segment> = mutableListOf(Segment(0, 0)), | ||
val segmentsY: MutableList<Segment> = mutableListOf(Segment(0, 0)), | ||
freeSpaceInit: Int = -1, | ||
accessibleSpaceInit: Int = -1 | ||
) : Container { | ||
override val hasAccessibleSpace: Boolean | ||
get() = segmentsX.any { it.value < size && it.start < size } | ||
|
||
override var freeSpace: Int = freeSpaceInit | ||
get() { | ||
if(field < 0) | ||
field = size * size - boxes.sumOf { it.area } | ||
return field | ||
} | ||
|
||
var accessibleSpace: Int = accessibleSpaceInit | ||
get() { | ||
if(field < 0) | ||
field = segmentsX.mapIndexed { idx, seg -> | ||
val width = size - seg.value | ||
val height = if(idx == segmentsX.size-1) size - seg.start else segmentsX[idx+1].start - seg.start | ||
width * height | ||
}.sum() | ||
return field | ||
} | ||
|
||
override fun clone() = SegmentContainer(ci, size, boxes.toMutableList(), segmentsX.toMutableList(), segmentsY.toMutableList(), freeSpace, accessibleSpace) | ||
|
||
override fun add(box: PlacedBox) { | ||
boxes.add(box) | ||
freeSpace -= box.area | ||
invalidateAccessible() | ||
|
||
updateSegments(segmentsX, box.y, box.h, box.x, box.w) | ||
updateSegments(segmentsY, box.x, box.w, box.y, box.h) | ||
} | ||
|
||
fun getRelevantSegments(segs: List<Segment>, boxStart: Int, boxMeasurement: Int): List<Segment> { | ||
val boxEnd = boxStart + boxMeasurement | ||
val lastSegmentIndex = normalizeBinarySearchIndex(segs.binarySearchBy(boxEnd - 1){ it.start }) | ||
val firstSegmentIndex = normalizeBinarySearchIndex(segs.binarySearchBy(boxStart){ it.start }) | ||
return segs.subList(firstSegmentIndex, lastSegmentIndex + 1) | ||
} | ||
|
||
fun swap(a: Int, b: Int) { | ||
val tmp = boxes[a] | ||
boxes[a] = boxes[b] | ||
boxes[b] = tmp | ||
} | ||
|
||
private fun invalidateAccessible() { | ||
accessibleSpace = -1 | ||
} | ||
|
||
private fun updateSegments(segs: MutableList<Segment>, boxStart: Int, boxMeasurement: Int, value: Int, boxValue: Int) { | ||
val boxEnd = boxStart + boxMeasurement | ||
val lastSegmentIndex = normalizeBinarySearchIndex(segs.binarySearchBy(boxEnd){ it.start }) | ||
val lastUsedSegment = segs[lastSegmentIndex] | ||
|
||
val segStart = getRelevantSegments(segs, boxStart, boxMeasurement).firstOrNull { it.value <= value }?.start | ||
?: return | ||
|
||
if(segStart >= boxEnd) | ||
return | ||
|
||
segs.removeAll { it.start in segStart until boxEnd } | ||
val insertIndex = -(segs.binarySearchBy(segStart){ it.start } + 1) | ||
segs.add(insertIndex, Segment(segStart, value + boxValue)) | ||
|
||
if(lastUsedSegment.start < boxEnd) | ||
segs.add(insertIndex+1, Segment(boxEnd, lastUsedSegment.value)) | ||
} | ||
|
||
private fun normalizeBinarySearchIndex(index: Int) = if(index < 0) (-(index+1)-1) else index | ||
} | ||
|
||
data class Segment(val start: Int, val value: Int) { | ||
override fun toString() = "(s: $start, v: $value)" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package binpack | ||
|
||
class SpaceContainer( | ||
override var ci: Int, | ||
override val size: Int, | ||
override val boxes: MutableList<PlacedBox> = mutableListOf(), | ||
val spaces: MutableList<PlacedBox> = mutableListOf(PlacedBox(size, size, 0, 0)) | ||
) : Container { | ||
override val hasAccessibleSpace: Boolean | ||
get() = spaces.isNotEmpty() | ||
|
||
override val freeSpace: Int | ||
get() = spaces.sumOf { it.area } | ||
|
||
override fun clone() = SpaceContainer(ci, size, boxes.toMutableList(), spaces.toMutableList()) | ||
|
||
override fun add(box: PlacedBox) { | ||
val space = spaces.firstOrNull { it.intersects(box) }!! | ||
add(box, space) | ||
} | ||
|
||
fun add(box: Box, space: PlacedBox) { | ||
var box = box | ||
if(!space.fits(box) && space.fits(box.rotate())) | ||
box = box.rotate() | ||
else if(!space.fits(box)) | ||
throw Exception("Space $space does not fit box $box") | ||
|
||
boxes.add(box.asPlaced(space.x, space.y)) | ||
spaces.remove(space) | ||
spaces.addAll(shatter(space, box)) | ||
} | ||
|
||
fun remove(box: PlacedBox) { | ||
boxes.remove(box) | ||
spaces.add(box) | ||
} | ||
|
||
private fun shatter(space: PlacedBox, box: Box): List<PlacedBox> { | ||
val newSpaces = mutableListOf<PlacedBox>() | ||
if(space.w > box.w) | ||
newSpaces.add(PlacedBox(space.w - box.w, space.h, space.x + box.w, space.y)) | ||
if(space.h > box.h) | ||
newSpaces.add(PlacedBox(box.w, space.h - box.h, space.x, space.y + box.h)) | ||
return newSpaces | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.