Documentation ¶
Index ¶
- func CopyPtr[T any](original *T) *T
- func CopySlicePtr[T any](data []*T) []*T
- type Axis
- type Box
- func (b *Box) GetDepth() float64
- func (b *Box) GetHeight() float64
- func (b *Box) GetID() string
- func (b *Box) GetItems() []*Item
- func (b *Box) GetMaxWeight() float64
- func (b *Box) GetVolume() float64
- func (b *Box) GetWidth() float64
- func (b *Box) PutItem(item *Item, p Pivot) bool
- func (b *Box) Reset()
- type Dimension
- type Item
- func (i *Item) GetDepth() float64
- func (i *Item) GetDimension() Dimension
- func (i *Item) GetHeight() float64
- func (i *Item) GetID() string
- func (i *Item) GetPosition() Pivot
- func (i *Item) GetVolume() float64
- func (i *Item) GetWeight() float64
- func (i *Item) GetWidth() float64
- func (i *Item) Intersect(it *Item) bool
- type Packer
- type Pivot
- type Result
- type RotationType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CopyPtr ¶
func CopyPtr[T any](original *T) *T
CopyPtr creates a copy of a pointer.
It takes a pointer to a generic type T as an argument and returns a new pointer to a copy of the original value. If the original pointer is nil, it returns nil.
func CopySlicePtr ¶
func CopySlicePtr[T any](data []*T) []*T
CopySlicePtr creates a copy of a slice of pointers.
It takes a slice of pointers as an argument and returns a new slice with the same elements, but with each element being a copy of the original.
Types ¶
type Box ¶
type Box struct {
// contains filtered or unexported fields
}
Box represents a box that can hold items.
It has fields for the box's dimensions and maximum weight. It also has fields for tracking the box's current items and their volume and weight.
func NewBox ¶
NewBox creates a new Box with the given id, dimensions, and maximum weight.
Parameters: - id: a unique identifier for the box. - w: the width of the box. - h: the height of the box. - d: the depth of the box. - mw: the maximum weight the box can hold.
Returns: - A pointer to the newly created Box.
func (*Box) GetItems ¶
GetItems returns a slice of pointers to the items currently in the box.
The slice is a copy and not a reference to the original slice, so modifying the slice returned by this function will not affect the contents of the box.
func (*Box) GetMaxWeight ¶
GetMaxWeight returns the maximum weight the box can hold.
func (*Box) PutItem ¶
PutItem Attempts to place the given item at the specified anchor point within the box.
Attempts to place the given item at the specified anchor point within the box.
It tries to place the item at the given anchor point by iterating through each rotation type (Whd, Hwd, Hdw, Dhw, Dwh, Wdh) and checks if the item can be placed within the box without intersecting with any of the other items in the box. If the item can be placed, it inserts the item into the box and returns true. If the item cannot be placed, it returns false.
Parameters:
- item: The item to be placed in the box.
- p: The anchor point at which to attempt placing the item within the box.
Returns:
- bool: True if the item was successfully placed within the box, false otherwise.
type Item ¶
type Item struct {
// contains filtered or unexported fields
}
Item represents an item that can be packed into a box.
func (*Item) GetDimension ¶
GetDimension returns the dimensions of the item based on its current rotation type.
The dimensions are returned as a Dimension struct.
Returns:
- Dimension: The dimensions of the item.
func (*Item) GetPosition ¶
GetPosition returns the position of the item.
func (*Item) Intersect ¶
Intersect tests for intersections between two items.
It checks for intersections between the current item and the given item. It does this by getting the dimensions of the current item and the given item and then calling the intersect method with the appropriate parameters.
Parameters:
- it: The item to check for intersections with.
Returns:
- bool: True if the items intersect, false otherwise.
type Packer ¶
type Packer struct{}
Packer is a struct that packs items into boxes.
It sorts input boxes and items by volume and weight. It then selects the box with the largest volume and weight that can accommodate the items. If there are still items left after packing the boxes, it sets them as unfit items.
func NewPacker ¶
func NewPacker() *Packer
NewPacker creates a new instance of Packer.
Returns: - a pointer to a Packer struct.
func (*Packer) Pack ¶
Pack packs items into boxes.
This function sorts input boxes and items by volume and weight. It selects the box with the largest volume and weight that can accommodate the items. If there are still items left after packing the boxes, they will be set as unfit items.
Parameters: - inputBoxes: a list of boxes. - inputItems: a list of items.
Returns: - a Result struct that contains two slices:
- Boxes: a list of boxes with items.
- UnfitItems: a list of items that didn't fit into boxes.
func (*Packer) PackCtx ¶
func (p *Packer) PackCtx(ctx context.Context, inputBoxes []*Box, inputItems []*Item) (*Result, error)
PackCtx packs items into boxes asynchronously and handles the context.
This function sorts input boxes and items by volume and weight. It selects the box with the largest volume and weight that can accommodate the items. If there are still items left after packing the boxes, they will be set as unfit items.
Parameters: - ctx: the context.Context to use. - inputBoxes: a list of boxes. - inputItems: a list of items.
Returns:
- *Result: the result of packing items into boxes.
- error: If the context is done before the packing process is complete, an error will be returned. nil will be returned otherwise.
This function is useful when you want to pack items into boxes asynchronously and handle the context at the same time.
type Result ¶
type Result struct { // UnfitItems is a list of items that didn't fit into boxes. UnfitItems itemSlice // Boxes is a list of boxes with items. Boxes boxSlice }
Result represents the result of packing items into boxes.
It is a struct that contains two slices: - UnfitItems: a list of items that didn't fit into boxes. - Boxes: a list of boxes with items.
type RotationType ¶
type RotationType int
RotationType represents the type of rotation for an item.
const ( RotationTypeWhd RotationType = iota // RotationTypeHwd represents the rotation type where the height is the longest dimension. RotationTypeHwd // RotationTypeHdw represents the rotation type where the depth is the longest dimension. RotationTypeHdw // RotationTypeDhw represents the rotation type where the depth is the longest dimension. RotationTypeDhw // RotationTypeDwh represents the rotation type where the width is the longest dimension. RotationTypeDwh // RotationTypeWdh represents the rotation type where the height is the longest dimension. RotationTypeWdh )
RotationTypeWhd represents the rotation type where the width is the longest dimension.