Skip to content

Commit

Permalink
#13 - Fixed bigger test with random add and remove. Rest issues are s…
Browse files Browse the repository at this point in the history
…eparated in extra issue #20
  • Loading branch information
HolgerSchneider committed Apr 25, 2023
1 parent 860297e commit 84e8d65
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 17 deletions.
37 changes: 31 additions & 6 deletions src/main/java/util/collection/IndexedArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.ArrayList;
import java.util.Collection;

/**
/**
* Copyright (c) 2012-2023 Holger Schneider
* All rights reserved.
*
Expand All @@ -15,7 +15,7 @@
* - Entries have the index of their position in the list
* - Adding or Removing will not shift entry positions
* - Reuse of empty slots
*
*
* @author hschneid
*
* @param <E>
Expand All @@ -28,8 +28,10 @@ public class IndexedArrayList<E extends Indexable> extends ArrayList<E> {
private int freeIndexCursor = -1;
private int length = 0;

private int lastUsedIndex = 0;

/**
*
*
*/
public IndexedArrayList() {
super();
Expand All @@ -48,10 +50,13 @@ public boolean add(E e) {
e.setIdx(insertPos);
super.set(insertPos, e);
return true;
}
}

e.setIdx(super.size());
return super.add(e);
var added = super.add(e);
if(super.size() > lastUsedIndex) lastUsedIndex = super.size();

return added;
}

/*
Expand All @@ -67,6 +72,8 @@ public void add(int index, E e) {
}
e.setIdx(index);
super.set(index, e);

if(index > lastUsedIndex) lastUsedIndex = index;
}

/*
Expand Down Expand Up @@ -101,6 +108,8 @@ public E set(int index, E e) {
e.setIdx(index);
super.set(index, e);

if(index > lastUsedIndex) lastUsedIndex = index;

return oldEntry;
}

Expand All @@ -127,6 +136,15 @@ public E remove(int index) {
}
freeIndexArr[freeIndexCursor] = index;

if(index >= lastUsedIndex) {
for (int i = lastUsedIndex; i >= 0; i--) {
if (get(i) != null) {
lastUsedIndex = i;
break;
}
}
}

return e;
}

Expand Down Expand Up @@ -157,10 +175,16 @@ public boolean removeAll(Collection<?> c) {
public int length() {
return super.size();
}

@Override
public int size() {
return length;
}

public int getLastUsedIndex() {
return lastUsedIndex;
}

/*
* (non-Javadoc)
* @see java.util.ArrayList#clear()
Expand All @@ -171,6 +195,7 @@ public void clear() {
length = 0;
freeIndexArr = new int[10];
freeIndexCursor = -1;
lastUsedIndex = 0;
}

private void expand(int nbr) {
Expand Down
24 changes: 18 additions & 6 deletions src/main/java/xf/xflp/base/container/AddRemoveContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class AddRemoveContainer extends ContainerBase implements Container
private final LPListMap<Position, Position> posFollowerMap = new LPListMap<>();
private final Map<Position, Position> posAncestorMap = new HashMap<>();

/* Position -> Item */
/* Position -> Item - Which item is responsible, that this position was created. */
private final Map<Position, Item> positionItemMap = new HashMap<>();

private final Set<String> uniquePositionKeys = new HashSet<>();
Expand Down Expand Up @@ -118,10 +118,21 @@ public int add(Item item, Position pos, boolean isRotated) {
updateBearingCapacity(List.of(item));

history.add(item);

return item.index;
}

private void check() {
for (Map.Entry<Position, Position> e : posAncestorMap.entrySet()) {
if(e.getValue() == null)
System.out.println("MISS_ANC: " + e.getKey());
}

for (Position e : posFollowerMap.keySet()) {
if(posFollowerMap.get(e) == null)
System.out.println("MISS_FOL: " + e.getKey());
}
}

private void output() {
// OUTPUT
System.out.println("ITM\n"+itemList.stream()
Expand Down Expand Up @@ -224,8 +235,10 @@ public void remove(Item item) {
Position newPosition = Position.of(
pos.idx(), pos.x(), ((lowerItem != null) ? lowerItem.yl : 0), pos.z(), pos.type()
);
replacePosition(pos, newPosition);
recreateSpaces(newPosition);
if(!pos.equals(newPosition)) {
replacePosition(pos, newPosition);
recreateSpaces(newPosition);
}
}

// L�sche die alte Position, wenn deren Elter noch aktiv ist und selbst seine Nachfolger alle weg sind
Expand All @@ -239,7 +252,6 @@ public void remove(Item item) {
spaceService.getDominatingSpaces(
spacePositions.values().stream().flatMap(Collection::stream).toList()
);

history.add(item);
}

Expand Down Expand Up @@ -425,7 +437,7 @@ private void checkPosition(Position pos) {

if(
// Wenn die Position keine Nachfolger mehr hat, weil durch CheckTreeAndRemove gel�scht wurde und
(!posFollowerMap.containsKey(pos) || posFollowerMap.get(pos).isEmpty())
(!posFollowerMap.containsKey(pos) || posFollowerMap.get(pos) == null || posFollowerMap.get(pos).isEmpty())
// Wenn Vorg�nger (der die Position erzeugt hat) frei ist und
&& activePosList.contains(ancestor)
// die Position nicht der Root ist, dann l�sche die Position
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xf.xflp.base.container.constraints;

import util.collection.IndexedArrayList;
import xf.xflp.base.container.ContainerBase;
import xf.xflp.base.item.Item;
import xf.xflp.base.item.Tools;
Expand All @@ -20,7 +21,7 @@
public class LoadBearingChecker {

public void update(ContainerBase container, List<Item> initialItems) {
float[] bearingWeights = new float[container.getItems().size()];
float[] bearingWeights = new float[((IndexedArrayList<Item>)container.getItems()).getLastUsedIndex()];

// Collect the bearing weight per item - top-down
List<Item> floorItems = collectBearingWeight(initialItems, bearingWeights, container);
Expand All @@ -29,7 +30,7 @@ public void update(ContainerBase container, List<Item> initialItems) {
}

private List<Item> collectBearingWeight(List<Item> initialItems, float[] bearingWeights, ContainerBase container) {
BearingWeightQueue queue = new BearingWeightQueue(container.getItems().size());
BearingWeightQueue queue = new BearingWeightQueue(((IndexedArrayList<Item>)container.getItems()).getLastUsedIndex());

// Add all initial items to queue
for (Item initialItem : initialItems) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/xf/xflp/base/fleximport/ContainerData.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package xf.xflp.base.fleximport;

import xf.xflp.base.XFLPParameter;
import xf.xflp.base.container.AddRemoveContainer;
import xf.xflp.base.container.AddContainer;
import xf.xflp.base.container.AddRemoveContainer;
import xf.xflp.base.container.Container;
import xf.xflp.exception.XFLPException;
import xf.xflp.exception.XFLPExceptionType;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/xf/xflp/base/position/PositionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import util.collection.IndexedArrayList;
import util.collection.LPListMap;
import xf.xflp.base.container.AddRemoveContainer;
import xf.xflp.base.container.AddContainer;
import xf.xflp.base.container.AddRemoveContainer;
import xf.xflp.base.container.Container;
import xf.xflp.base.container.ParameterType;
import xf.xflp.base.container.constraints.StackingChecker;
Expand Down
2 changes: 1 addition & 1 deletion src/test/groovy/helper/Helper.groovy
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package helper

import xf.xflp.base.container.AddRemoveContainer
import xf.xflp.base.container.AddContainer
import xf.xflp.base.container.AddRemoveContainer
import xf.xflp.base.container.Container
import xf.xflp.base.container.GroundContactRule
import xf.xflp.base.fleximport.ContainerData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class ContainerRemoveSpec extends Specification {
unloadedItems.removeAll(loadedItems)

for (i in 0..<100) {
println 'AAA '+i
// Remove 1 loaded item
var unloadItemIdx = rand.nextInt(loadedItems.size())
var unloadItem = loadedItems[unloadItemIdx]
Expand Down

0 comments on commit 84e8d65

Please sign in to comment.