comparison gnv-artifacts/src/main/java/de/intevation/gnv/jfreechart/LevelOrderIndices.java @ 875:5e9efdda6894

merged gnv-artifacts/1.0
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:56 +0200
parents 22c18083225e
children f953c9a559d8
comparison
equal deleted inserted replaced
722:bb3ffe7d719e 875:5e9efdda6894
1 package de.intevation.gnv.jfreechart;
2
3 import java.util.LinkedList;
4
5 /**
6 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
7 */
8 public class LevelOrderIndices
9 {
10 public interface Visitor {
11
12 Object visit(int index);
13 }
14
15
16 protected int from;
17
18 protected int to;
19
20
21 public LevelOrderIndices() {
22 }
23
24
25 public LevelOrderIndices(int to) {
26 this(0, to);
27 }
28
29
30 public LevelOrderIndices(int from, int to) {
31 this.from = Math.min(from, to);
32 this.to = Math.max(from, to);
33 }
34
35
36 public Object visit(Visitor visitor) {
37 LinkedList<int[]> queue = new LinkedList<int[]>();
38
39 queue.add(new int [] { from, to });
40
41 while (!queue.isEmpty()) {
42 int [] pair = queue.remove();
43
44 int mid = (pair[0] + pair[1]) >> 1;
45
46 Object result = visitor.visit(mid);
47
48 if (result != null) {
49 return result;
50 }
51
52 if (mid-1 >= pair[0]) {
53 queue.add(new int [] { pair[0], mid-1 });
54 }
55
56 if (mid+1 <= pair[1]) {
57 pair[0] = mid+1;
58 queue.add(pair);
59 }
60 }
61
62 return null;
63 }
64 }
65 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org