Skip to content

Commit

Permalink
fix: distributor
Browse files Browse the repository at this point in the history
  • Loading branch information
pipinet committed Oct 10, 2023
1 parent f2f2a42 commit 168a50d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lang/src/main/java/com/qwlabs/lang/WeightDistributor.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,17 @@ private Map<Integer, Integer> calculateWeights(int left, Map<Integer, Integer> l
int scale = String.valueOf(left).length();
var totalRequires = BigDecimal.valueOf(leftRequires.values().stream().mapToInt(v -> v).sum());
Map<Integer, Integer> weights = new HashMap<>(leftRequires.size());
for (Map.Entry<Integer, Integer> entry : leftRequires.entrySet()) {
var orderedLefts = C2.stream(leftRequires.entrySet())
.sorted(Map.Entry.comparingByValue())
.toList();
for (Map.Entry<Integer, Integer> entry : orderedLefts) {
var weight = BigDecimal.valueOf(entry.getValue())
.divide(totalRequires, scale, RoundingMode.UP)
.multiply(tmpLeft)
.setScale(0, RoundingMode.UP)
.intValue();
weights.put(entry.getKey(), weight);
weights.put(entry.getKey(), Math.min(left, weight));
left -= weight;
}
return weights;
}
Expand Down
37 changes: 33 additions & 4 deletions lang/src/test/java/com/qwlabs/lang/WeightDistributorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ void should_single_total_6() {
var distributor = WeightDistributor.of(100, List.of(2, 1), true);
distributor.distribute();
assertThat(distributor.getLeft(), is(0));
assertThat(distributor.getDistributed(0), is(67));
assertThat(distributor.getDistributed(1), is(33));
assertThat(distributor.getDistributed(0), is(66));
assertThat(distributor.getDistributed(1), is(34));
assertThat(distributor.getDistributions().size(), is(2));
}

Expand All @@ -82,11 +82,40 @@ void should_single_total_8() {
distributor.distribute();
assertThat(distributor.getLeft(), is(0));
assertThat(distributor.getDistributed(0), is(4998));
assertThat(distributor.getDistributed(1), is(4998));
assertThat(distributor.getDistributed(2), is(4));
assertThat(distributor.getDistributed(1), is(4996));
assertThat(distributor.getDistributed(2), is(6));
assertThat(distributor.getDistributions().size(), is(3));
}

@Test
void should_single_total_9() {
var distributor = WeightDistributor.of(2001, List.of(1000, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), true);
distributor.distribute();
assertThat(distributor.getLeft(), is(0));
assertThat(distributor.getDistributed(0), is(1941));
assertThat(distributor.getDistributed(1), is(3));
assertThat(distributor.getDistributed(2), is(3));
assertThat(distributor.getDistributed(3), is(3));
assertThat(distributor.getDistributed(4), is(3));
assertThat(distributor.getDistributed(5), is(3));
assertThat(distributor.getDistributed(6), is(3));
assertThat(distributor.getDistributed(7), is(3));
assertThat(distributor.getDistributed(8), is(3));
assertThat(distributor.getDistributed(9), is(3));
assertThat(distributor.getDistributed(10), is(3));
assertThat(distributor.getDistributed(11), is(3));
assertThat(distributor.getDistributed(12), is(3));
assertThat(distributor.getDistributed(13), is(3));
assertThat(distributor.getDistributed(14), is(3));
assertThat(distributor.getDistributed(15), is(3));
assertThat(distributor.getDistributed(16), is(3));
assertThat(distributor.getDistributed(17), is(3));
assertThat(distributor.getDistributed(18), is(3));
assertThat(distributor.getDistributed(19), is(3));
assertThat(distributor.getDistributed(20), is(3));
assertThat(distributor.getDistributions().size(), is(21));
}

@Test
void should_multi_total_1() {
var distributor = WeightDistributor.of(List.of(0, 1), List.of(1));
Expand Down

0 comments on commit 168a50d

Please sign in to comment.