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 geniusweb.issuevalue.Bid;
|
---|
11 | import geniusweb.issuevalue.Domain;
|
---|
12 | import geniusweb.issuevalue.Value;
|
---|
13 | import geniusweb.issuevalue.ValueSet;
|
---|
14 | import tudelft.utilities.immutablelist.AbstractImmutableList;
|
---|
15 | import tudelft.utilities.immutablelist.ImmutableList;
|
---|
16 | import tudelft.utilities.immutablelist.Outer;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * A list containing all bids, both complete and partial, in the space. This is
|
---|
20 | * an {@link ImmutableList} so it contains all bids without pre-computing them.
|
---|
21 | * Constructing this is very cheap.
|
---|
22 | *
|
---|
23 | */
|
---|
24 | public class AllPartialBidsList extends AbstractImmutableList<Bid> {
|
---|
25 |
|
---|
26 | private final List<String> issues;
|
---|
27 | private final Outer<? extends Value> allValuePermutations;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * A list containing all partial bids (this includes complete bids) in the
|
---|
31 | * space. This is an ImmutableList so it can contain all bids without
|
---|
32 | * pre-computing them.
|
---|
33 | *
|
---|
34 | * @param domain the {@link Domain}
|
---|
35 | */
|
---|
36 | public AllPartialBidsList(Domain domain) {
|
---|
37 | if (domain == null)
|
---|
38 | throw new IllegalArgumentException("domain=null");
|
---|
39 | issues = new ArrayList<>(domain.getIssues());
|
---|
40 |
|
---|
41 | /*#PY
|
---|
42 | * values = [ ValueSetWithNull(domain.getValues(issue))
|
---|
43 | * for issue in issues ]
|
---|
44 | */
|
---|
45 | List<ImmutableList<Value>> values = issues.stream()
|
---|
46 | .map(issue -> new ValueSetWithNull(domain.getValues(issue)))
|
---|
47 | .collect(Collectors.toList());
|
---|
48 |
|
---|
49 | allValuePermutations = new Outer<Value>(values);
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public Bid get(BigInteger index) {
|
---|
54 | ImmutableList<? extends Value> nextValues = allValuePermutations
|
---|
55 | .get(index);
|
---|
56 |
|
---|
57 | Map<String, Value> issueValues = new HashMap<>();
|
---|
58 | for (int n = 0; n < issues.size(); n = n + 1) {
|
---|
59 | Value value = nextValues.get(BigInteger.valueOf(n));
|
---|
60 | if (value != null) {
|
---|
61 | issueValues.put(issues.get(n), value);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | return new Bid(issueValues);
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public BigInteger size() {
|
---|
69 | return allValuePermutations.size();
|
---|
70 | }
|
---|
71 |
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Extends a ValueSet so that the first element is null. Of course null is not a
|
---|
76 | * good value, we catch it to use it as 'value not set'.
|
---|
77 | *
|
---|
78 | */
|
---|
79 | class ValueSetWithNull extends AbstractImmutableList<Value>
|
---|
80 | implements ValueSet {
|
---|
81 |
|
---|
82 | private ValueSet valueset;
|
---|
83 |
|
---|
84 | public ValueSetWithNull(ValueSet valueset) {
|
---|
85 | this.valueset = valueset;
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public Value get(BigInteger index) {
|
---|
90 | if (BigInteger.ZERO.equals(index)) {
|
---|
91 | return null;
|
---|
92 | }
|
---|
93 | return valueset.get(index.subtract(BigInteger.ONE));
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public BigInteger size() {
|
---|
98 | return BigInteger.ONE.add(valueset.size());
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public Boolean contains(Value value) {
|
---|
103 | if (value == null)
|
---|
104 | return true;
|
---|
105 | return valueset.contains(value);
|
---|
106 | }
|
---|
107 |
|
---|
108 | }
|
---|