提交 a9b3b5c3 编写于 作者: K kohsuke

bug fix, and additional convenience methods.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@35604 71c3de6d-444a-0410-be80-ed276b4c234a
上级 70ba4fca
......@@ -37,15 +37,26 @@ import static java.lang.Math.*;
* as a map from "starting time of a range" to "value of q(t)".
*/
final class Timeline {
// int[] is always length=1
private final TreeMap<Long, int[]> data = new TreeMap<Long, int[]>();
/**
* Obtains q(t) for the given t.
*/
private int at(long t) {
SortedMap<Long, int[]> head = data.headMap(t);
SortedMap<Long, int[]> head = data.subMap(t,Long.MAX_VALUE);
if (head.isEmpty()) return 0;
return data.get(head.lastKey())[0];
return data.get(head.firstKey())[0];
}
/**
* Finds smallest t' > t where q(t')!=q(t)
*
* If there's no such t' this method returns null.
*/
private Long next(long t) {
SortedMap<Long, int[]> x = data.tailMap(t + 1);
return x.isEmpty() ? null : x.firstKey();
}
/**
......@@ -75,4 +86,39 @@ final class Timeline {
}
return peak;
}
/**
* Finds a "valley" in this timeline that fits the given duration.
* <p>
* More formally, find smallest x that:
* <ul>
* <li>x >= start
* <li>q(t) <= n for all t \in [x,x+duration)
* </ul>
*
* @return null
* if no such x was found.
*/
Long fit(long start, long duration, int n) {
OUTER:
while (true) {
long t = start;
// check if 'start' satisfies the two conditions by moving t across [start,start+duration)
while ((t-start)<duration) {
if (at(t)>n) {
// value too big. what's the next t that's worth trying?
Long nxt = next(t);
if (nxt==null) return null;
start = nxt;
continue OUTER;
} else {
Long nxt = next(t);
if (nxt==null) t = Long.MAX_VALUE;
else t = nxt;
}
}
// q(t) looks good at the entire [start,start+duration)
return start;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册