f7947d4fe3481eaa1bee792a5363d2b67df75a13
java/com.sap.sse.common/src/com/sap/sse/common/scalablevalue/KadaneExtremeSubarraysFinder.java
| ... | ... | @@ -0,0 +1,86 @@ |
| 1 | +package com.sap.sse.common.scalablevalue; |
|
| 2 | + |
|
| 3 | +import java.io.Serializable; |
|
| 4 | +import java.util.LinkedList; |
|
| 5 | +import java.util.List; |
|
| 6 | + |
|
| 7 | +/** |
|
| 8 | + * In a sequence of {@link ComparableScalableValueWithDistance} objects, tells the contiguous sub-sequence with the |
|
| 9 | + * greatest and the contiguous sub-sequence with the least sum, according to the |
|
| 10 | + * {@link ComparableScalableValueWithDistance#add(ScalableValue) add} and the |
|
| 11 | + * {@link ComparableScalableValueWithDistance#compareTo(Object) compareTo} methods. |
|
| 12 | + * <p> |
|
| 13 | + * |
|
| 14 | + * The sequence is mutable. In particular, elements can be added at any position, also before the start or after the |
|
| 15 | + * end, and elements can be removed at least from the beginning of the sequence. Updating the sub-sequences with minimal |
|
| 16 | + * and maximal sums happens with complexity O(1) when adding to the end of the sequence, so with constant effort |
|
| 17 | + * regardless the size of the sequence. When inserting into or removing from the sequence at arbitrary positions, |
|
| 18 | + * constant effort can no longer be guaranteed as changes may need to get propagated onwards to following elements. |
|
| 19 | + * <p> |
|
| 20 | + * |
|
| 21 | + * See also <a href="https://en.wikipedia.org/wiki/Maximum_subarray_problem">here</a> for a description of the |
|
| 22 | + * algorithm. |
|
| 23 | + * |
|
| 24 | + * @author Axel Uhl (d043530) |
|
| 25 | + * |
|
| 26 | + */ |
|
| 27 | +public class KadaneExtremeSubarraysFinder<ValueType, AveragesTo extends Comparable<AveragesTo>, T extends ComparableScalableValueWithDistance<ValueType, AveragesTo>> |
|
| 28 | +implements Serializable { |
|
| 29 | + private static final long serialVersionUID = 2109193559337714286L; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * The elements constituting the full sequence in which to find the contiguous sub-sequences |
|
| 33 | + */ |
|
| 34 | + private final List<T> sequence; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The element at index <tt>i</tt> holds the maximum value of the sum of any contiguous sub-sequence ending at index |
|
| 38 | + * <tt>i</tt>. |
|
| 39 | + */ |
|
| 40 | + private final List<AveragesTo> maxSumEndingAt; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The maximum of the sums of any contiguous sub-sequence |
|
| 44 | + */ |
|
| 45 | + private AveragesTo maxSum; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Index of the first element in {@link #sequence} of the contiguous sub-sequence having the maximum sum |
|
| 49 | + */ |
|
| 50 | + private int startIndexInclusiveOfMaxSumSequence; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Index of the element after the last element in {@link #sequence} of the contiguous sub-sequence having the maximum sum |
|
| 54 | + */ |
|
| 55 | + private int endIndexExclusiveOfMaxSumSequence; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The element at index <tt>i</tt> holds the minimum value of the sum of any contiguous sub-sequence ending at index |
|
| 59 | + * <tt>i</tt>. |
|
| 60 | + */ |
|
| 61 | + private final List<AveragesTo> minSumEndingAt; |
|
| 62 | + |
|
| 63 | + private AveragesTo minSum; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Index of the first element in {@link #sequence} of the contiguous sub-sequence having the minium sum |
|
| 67 | + */ |
|
| 68 | + private int startIndexInclusiveOfMinSumSequence; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Index of the element after the last element in {@link #sequence} of the contiguous sub-sequence having the minimum sum |
|
| 72 | + */ |
|
| 73 | + private int endIndexExclusiveOfMinSumSequence; |
|
| 74 | + |
|
| 75 | + public KadaneExtremeSubarraysFinder() { |
|
| 76 | + sequence = new LinkedList<>(); |
|
| 77 | + maxSumEndingAt = new LinkedList<>(); |
|
| 78 | + minSumEndingAt = new LinkedList<>(); |
|
| 79 | + maxSum = null; |
|
| 80 | + minSum = null; |
|
| 81 | + startIndexInclusiveOfMaxSumSequence = -1; |
|
| 82 | + endIndexExclusiveOfMaxSumSequence = -1; |
|
| 83 | + startIndexInclusiveOfMinSumSequence = -1; |
|
| 84 | + endIndexExclusiveOfMinSumSequence = -1; |
|
| 85 | + } |
|
| 86 | +} |