1 | package geniusweb.bidspace;
|
---|
2 |
|
---|
3 | import java.math.BigInteger;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Map;
|
---|
8 | import java.util.stream.Collectors;
|
---|
9 |
|
---|
10 | import org.eclipse.jdt.annotation.NonNull;
|
---|
11 |
|
---|
12 | import geniusweb.issuevalue.Bid;
|
---|
13 | import geniusweb.issuevalue.Domain;
|
---|
14 | import geniusweb.issuevalue.Value;
|
---|
15 | import tudelft.utilities.immutablelist.AbstractImmutableList;
|
---|
16 | import tudelft.utilities.immutablelist.ImmutableList;
|
---|
17 | import tudelft.utilities.immutablelist.Outer;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * A list containing all complete bids in the space. This is an
|
---|
21 | * {@link ImmutableList} so it can contain all bids without pre-computing them.
|
---|
22 | *
|
---|
23 | */
|
---|
24 | public class AllBidsList extends AbstractImmutableList<@NonNull Bid> {
|
---|
25 |
|
---|
26 | private final @NonNull List<@NonNull String> issues = new ArrayList<>();
|
---|
27 | private final @NonNull Outer<@NonNull ? extends Value> allValuePermutations;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * This object contains s list containing all bids in the space. This is an
|
---|
31 | * ImmutableList so it can contain all bids without pre-computing them.
|
---|
32 | *
|
---|
33 | * @param domain the {@link Domain}
|
---|
34 | *
|
---|
35 | */
|
---|
36 | public AllBidsList(@NonNull Domain domain) {
|
---|
37 | if (domain == null)
|
---|
38 | throw new IllegalArgumentException("domain=null");
|
---|
39 | issues.addAll(domain.getIssuesValues());
|
---|
40 |
|
---|
41 | //#PY values = [domain.getValues(issue) for issue in issues]
|
---|
42 | final @NonNull List<@NonNull ImmutableList<@NonNull Value>> values = issues
|
---|
43 | .stream().map(issue -> domain.getValues(issue))
|
---|
44 | .collect(Collectors.toList());
|
---|
45 |
|
---|
46 | allValuePermutations = new Outer<@NonNull Value>(values);
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public @NonNull Bid get(@NonNull BigInteger index) {
|
---|
51 | final @NonNull ImmutableList<@NonNull ? extends Value> nextValues = allValuePermutations
|
---|
52 | .get(index);
|
---|
53 |
|
---|
54 | final @NonNull Map<@NonNull String, @NonNull Value> issueValues = new HashMap<>();
|
---|
55 | for (int n = 0; n < issues.size(); n = n + 1) {
|
---|
56 | issueValues.put(issues.get(n),
|
---|
57 | nextValues.get(BigInteger.valueOf(n)));
|
---|
58 | }
|
---|
59 | return new Bid(issueValues);
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public @NonNull BigInteger size() {
|
---|
64 | return allValuePermutations.size();
|
---|
65 | }
|
---|
66 |
|
---|
67 | }
|
---|