Skip to content

Commit

Permalink
v0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Aug 5, 2023
1 parent f36b857 commit c392ab4
Show file tree
Hide file tree
Showing 12 changed files with 340 additions and 241 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: golangci-lint
on:
push:
branches:
- master
pull_request:
permissions:
contents: read
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.20' ]
steps:
- uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
cache: true
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.53.3
30 changes: 30 additions & 0 deletions .github/workflows/unit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: gotest-unit
on:
push:
branches:
- master
pull_request:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.20' ]
steps:
- uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
cache: true
- name: Install dependencies
run: go get .
- name: Test with Go
run: go test -json > TestResults-${{ matrix.go-version }}.json
- name: Upload Go test results
uses: actions/upload-artifact@v3
with:
name: Go-results-${{ matrix.go-version }}
path: TestResults-${{ matrix.go-version }}.json
32 changes: 32 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
run:
timeout: 1m
linters:
enable-all: true
disable:
# deprecated
- nosnakecase
- structcheck
- interfacer
- deadcode
- exhaustivestruct
- maligned
- ifshort
- varcheck
- golint
- scopelint
# not relevant
- varnamelen
linters-settings:
lll:
line-length: 160
gci:
sections:
- Standard
- Default
- Prefix(github.com/bavix)
depguard:
rules:
main:
allow:
- $gostd
- github.com
12 changes: 6 additions & 6 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package boxpacker3_test

import (
rand2 "crypto/rand"
"crypto/rand"
"math/big"
"testing"

Expand All @@ -11,13 +11,13 @@ import (
)

func BenchmarkPacker(b *testing.B) {
items := make(boxpacker3.ItemSlice, 0, 100)
items := make([]*boxpacker3.Item, 0, 100)

for x := 0; x < 100; x++ {
w, _ := rand2.Int(rand2.Reader, big.NewInt(150))
l, _ := rand2.Int(rand2.Reader, big.NewInt(150))
h, _ := rand2.Int(rand2.Reader, big.NewInt(150))
w2, _ := rand2.Int(rand2.Reader, big.NewInt(100))
w, _ := rand.Int(rand.Reader, big.NewInt(150))
l, _ := rand.Int(rand.Reader, big.NewInt(150))
h, _ := rand.Int(rand.Reader, big.NewInt(150))
w2, _ := rand.Int(rand.Reader, big.NewInt(100))

items = append(items, boxpacker3.NewItem(
uuid.New().String(),
Expand Down
91 changes: 54 additions & 37 deletions box.go
Original file line number Diff line number Diff line change
@@ -1,83 +1,83 @@
package boxpacker3

type Box struct {
ID string
Width float64
Height float64
Depth float64
MaxWeight float64
Volume float64
Items []*Item

id string
width float64
height float64
depth float64
maxWeight float64
volume float64
items []*Item
itemsVolume float64
itemsWeight float64
}

type BoxSlice []*Box
type boxSlice []*Box

func (bs BoxSlice) Len() int {
func (bs boxSlice) Len() int {
return len(bs)
}

func (bs BoxSlice) Less(i, j int) bool {
func (bs boxSlice) Less(i, j int) bool {
return bs[i].GetVolume() < bs[j].GetVolume()
}

func (bs BoxSlice) Swap(i, j int) {
func (bs boxSlice) Swap(i, j int) {
bs[i], bs[j] = bs[j], bs[i]
}

func NewBox(id string, w, h, d, mw float64) *Box {
//nolint:exhaustruct
return &Box{
ID: id,
Width: w,
Height: h,
Depth: d,
MaxWeight: mw,
Volume: w * h * d,
Items: make([]*Item, 0),
id: id,
width: w,
height: h,
depth: d,
maxWeight: mw,
volume: w * h * d,
items: nil,
}
}

func (b *Box) GetID() string {
return b.ID
return b.id
}

func (b *Box) GetWidth() float64 {
return b.Width
return b.width
}

func (b *Box) GetHeight() float64 {
return b.Height
return b.height
}

func (b *Box) GetDepth() float64 {
return b.Depth
return b.depth
}

func (b *Box) GetVolume() float64 {
return b.Volume
return b.volume
}

func (b *Box) GetMaxWeight() float64 {
return b.MaxWeight
return b.maxWeight
}

// PutItem Пытается поместить элемент в опорную точку p коробки b.
func (b *Box) PutItem(item *Item, p Pivot) bool {
fit := false
func (b *Box) GetItems() []*Item {
return b.items
}

if b.itemsVolume+item.GetVolume() > b.GetVolume() {
// PutItem Attempts to place an element at anchor point p of box b.
func (b *Box) PutItem(item *Item, p Pivot) bool {
if !b.canTryToPlace(item) {
return false
}

if b.itemsWeight+item.GetWeight() > b.GetMaxWeight() {
return false
}
fit := false
item.position = p

item.Position = p
for rt := RotationTypeWhd; rt <= RotationTypeWdh; rt++ {
item.RotationType = rt
item.rotationType = rt
d := item.GetDimension()

if b.GetWidth() < p[WidthAxis]+d[WidthAxis] || b.GetHeight() < p[HeightAxis]+d[HeightAxis] || b.GetDepth() < p[DepthAxis]+d[DepthAxis] {
Expand All @@ -86,7 +86,7 @@ func (b *Box) PutItem(item *Item, p Pivot) bool {

fit = true

for _, ib := range b.Items {
for _, ib := range b.items {
if ib.Intersect(item) {
fit = false

Expand All @@ -106,9 +106,26 @@ func (b *Box) PutItem(item *Item, p Pivot) bool {
return fit
}

func (b *Box) insert(item *Item) {
b.Items = append(b.Items, item)
func (b *Box) canTryToPlace(item *Item) bool {
if b.itemsVolume+item.GetVolume() > b.GetVolume() {
return false
}

if b.itemsWeight+item.GetWeight() > b.GetMaxWeight() {
return false
}

return true
}

func (b *Box) insert(item *Item) {
b.items = append(b.items, item)
b.itemsVolume += item.GetVolume()
b.itemsWeight += item.GetWeight()
}

func (b *Box) purge() {
b.items = []*Item{}
b.itemsVolume = 0
b.itemsWeight = 0
}
33 changes: 0 additions & 33 deletions clone_test.go

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/google/uuid v1.3.0
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b h1:r+vk0EmXNmekl0S0BascoeeoHk/L7wmaW2QF90K+kYI=
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
Loading

0 comments on commit c392ab4

Please sign in to comment.