|
4 | 4 | import java.util.LinkedHashMap; |
5 | 5 | import java.util.Map; |
6 | 6 | import java.util.NavigableMap; |
| 7 | +import java.util.Objects; |
7 | 8 | import java.util.TreeMap; |
8 | 9 | import java.util.function.Function; |
9 | 10 |
|
10 | 11 | import ai.timefold.solver.core.impl.util.ConstantLambdaUtils; |
11 | 12 | import ai.timefold.solver.core.impl.util.MutableInt; |
12 | 13 |
|
13 | | -public final class MinMaxUndoableActionable<Result_, Property_> implements UndoableActionable<Result_, Result_> { |
14 | | - private final boolean isMin; |
15 | | - private final NavigableMap<Property_, Map<Result_, MutableInt>> propertyToItemCountMap; |
16 | | - private final Function<? super Result_, ? extends Property_> propertyFunction; |
17 | | - |
18 | | - private MinMaxUndoableActionable(boolean isMin, |
19 | | - NavigableMap<Property_, Map<Result_, MutableInt>> propertyToItemCountMap, |
20 | | - Function<? super Result_, ? extends Property_> propertyFunction) { |
21 | | - this.isMin = isMin; |
22 | | - this.propertyToItemCountMap = propertyToItemCountMap; |
23 | | - this.propertyFunction = propertyFunction; |
| 14 | +public final class MinMaxUndoableActionable<Result_, Property_> implements UndoableActionable<Result_> { |
| 15 | + public static final class State<Result_, Property_> { |
| 16 | + final boolean isMin; |
| 17 | + final NavigableMap<Property_, Map<Result_, MutableInt>> propertyToItemCountMap; |
| 18 | + final Function<? super Result_, ? extends Property_> propertyFunction; |
| 19 | + |
| 20 | + State(boolean isMin, NavigableMap<Property_, Map<Result_, MutableInt>> propertyToItemCountMap, |
| 21 | + Function<? super Result_, ? extends Property_> propertyFunction) { |
| 22 | + this.isMin = isMin; |
| 23 | + this.propertyToItemCountMap = propertyToItemCountMap; |
| 24 | + this.propertyFunction = propertyFunction; |
| 25 | + } |
| 26 | + |
| 27 | + public Result_ result() { |
| 28 | + if (propertyToItemCountMap.isEmpty()) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + return isMin ? getFirstKey(propertyToItemCountMap.firstEntry().getValue()) |
| 32 | + : getFirstKey(propertyToItemCountMap.lastEntry().getValue()); |
| 33 | + } |
| 34 | + |
| 35 | + private static <Key_> Key_ getFirstKey(Map<Key_, ?> map) { |
| 36 | + return map.keySet().iterator().next(); |
| 37 | + } |
24 | 38 | } |
25 | 39 |
|
26 | | - public static <Result extends Comparable<? super Result>> MinMaxUndoableActionable<Result, Result> minCalculator() { |
27 | | - return new MinMaxUndoableActionable<>(true, new TreeMap<>(), ConstantLambdaUtils.identity()); |
| 40 | + public static <Result extends Comparable<? super Result>> State<Result, Result> minState() { |
| 41 | + return new State<>(true, new TreeMap<>(), ConstantLambdaUtils.identity()); |
28 | 42 | } |
29 | 43 |
|
30 | | - public static <Result extends Comparable<? super Result>> MinMaxUndoableActionable<Result, Result> maxCalculator() { |
31 | | - return new MinMaxUndoableActionable<>(false, new TreeMap<>(), ConstantLambdaUtils.identity()); |
| 44 | + public static <Result extends Comparable<? super Result>> State<Result, Result> maxState() { |
| 45 | + return new State<>(false, new TreeMap<>(), ConstantLambdaUtils.identity()); |
32 | 46 | } |
33 | 47 |
|
34 | | - public static <Result> MinMaxUndoableActionable<Result, Result> minCalculator(Comparator<? super Result> comparator) { |
35 | | - return new MinMaxUndoableActionable<>(true, new TreeMap<>(comparator), ConstantLambdaUtils.identity()); |
| 48 | + public static <Result> State<Result, Result> minState(Comparator<? super Result> comparator) { |
| 49 | + return new State<>(true, new TreeMap<>(comparator), ConstantLambdaUtils.identity()); |
36 | 50 | } |
37 | 51 |
|
38 | | - public static <Result> MinMaxUndoableActionable<Result, Result> maxCalculator(Comparator<? super Result> comparator) { |
39 | | - return new MinMaxUndoableActionable<>(false, new TreeMap<>(comparator), ConstantLambdaUtils.identity()); |
| 52 | + public static <Result> State<Result, Result> maxState(Comparator<? super Result> comparator) { |
| 53 | + return new State<>(false, new TreeMap<>(comparator), ConstantLambdaUtils.identity()); |
40 | 54 | } |
41 | 55 |
|
42 | | - public static <Result, Property extends Comparable<? super Property>> MinMaxUndoableActionable<Result, Property> |
43 | | - minCalculator( |
44 | | - Function<? super Result, ? extends Property> propertyMapper) { |
45 | | - return new MinMaxUndoableActionable<>(true, new TreeMap<>(), propertyMapper); |
| 56 | + public static <Result, Property extends Comparable<? super Property>> State<Result, Property> minState( |
| 57 | + Function<? super Result, ? extends Property> propertyMapper) { |
| 58 | + return new State<>(true, new TreeMap<>(), propertyMapper); |
46 | 59 | } |
47 | 60 |
|
48 | | - public static <Result, Property extends Comparable<? super Property>> MinMaxUndoableActionable<Result, Property> |
49 | | - maxCalculator( |
50 | | - Function<? super Result, ? extends Property> propertyMapper) { |
51 | | - return new MinMaxUndoableActionable<>(false, new TreeMap<>(), propertyMapper); |
| 61 | + public static <Result, Property extends Comparable<? super Property>> State<Result, Property> maxState( |
| 62 | + Function<? super Result, ? extends Property> propertyMapper) { |
| 63 | + return new State<>(false, new TreeMap<>(), propertyMapper); |
| 64 | + } |
| 65 | + |
| 66 | + private final State<Result_, Property_> state; |
| 67 | + private Result_ cachedItem; |
| 68 | + private Property_ cachedKey; |
| 69 | + private Map<Result_, MutableInt> cachedInnerMap; |
| 70 | + private MutableInt cachedCount; |
| 71 | + |
| 72 | + public MinMaxUndoableActionable(State<Result_, Property_> state) { |
| 73 | + this.state = state; |
52 | 74 | } |
53 | 75 |
|
54 | 76 | @Override |
55 | | - public Runnable insert(Result_ item) { |
56 | | - Property_ key = propertyFunction.apply(item); |
57 | | - Map<Result_, MutableInt> itemCountMap = propertyToItemCountMap.computeIfAbsent(key, ignored -> new LinkedHashMap<>()); |
58 | | - MutableInt count = itemCountMap.computeIfAbsent(item, ignored -> new MutableInt()); |
59 | | - count.increment(); |
60 | | - |
61 | | - return () -> { |
62 | | - if (count.decrement() == 0) { |
63 | | - itemCountMap.remove(item); |
64 | | - if (itemCountMap.isEmpty()) { |
65 | | - propertyToItemCountMap.remove(key); |
66 | | - } |
67 | | - } |
68 | | - }; |
| 77 | + public void insert(Result_ item) { |
| 78 | + cachedItem = item; |
| 79 | + cachedKey = state.propertyFunction.apply(item); |
| 80 | + cachedInnerMap = state.propertyToItemCountMap.computeIfAbsent(cachedKey, ignored -> new LinkedHashMap<>()); |
| 81 | + cachedCount = cachedInnerMap.computeIfAbsent(item, ignored -> new MutableInt()); |
| 82 | + cachedCount.increment(); |
69 | 83 | } |
70 | 84 |
|
71 | 85 | @Override |
72 | | - public Result_ result() { |
73 | | - if (propertyToItemCountMap.isEmpty()) { |
74 | | - return null; |
| 86 | + public void update(Result_ item) { |
| 87 | + var newKey = state.propertyFunction.apply(item); |
| 88 | + if (Objects.equals(cachedKey, newKey)) { |
| 89 | + return; |
75 | 90 | } |
76 | | - return isMin ? getFirstKey(propertyToItemCountMap.firstEntry().getValue()) |
77 | | - : getFirstKey(propertyToItemCountMap.lastEntry().getValue()); |
| 91 | + retract(); |
| 92 | + cachedItem = item; |
| 93 | + cachedKey = newKey; |
| 94 | + cachedInnerMap = state.propertyToItemCountMap.computeIfAbsent(newKey, ignored -> new LinkedHashMap<>()); |
| 95 | + cachedCount = cachedInnerMap.computeIfAbsent(item, ignored -> new MutableInt()); |
| 96 | + cachedCount.increment(); |
78 | 97 | } |
79 | 98 |
|
80 | | - private static <Key_> Key_ getFirstKey(Map<Key_, ?> map) { |
81 | | - return map.keySet().iterator().next(); |
| 99 | + @Override |
| 100 | + public void retract() { |
| 101 | + if (cachedCount.decrement() == 0) { |
| 102 | + cachedInnerMap.remove(cachedItem); |
| 103 | + if (cachedInnerMap.isEmpty()) { |
| 104 | + state.propertyToItemCountMap.remove(cachedKey); |
| 105 | + } |
| 106 | + } |
82 | 107 | } |
83 | 108 | } |
0 commit comments