comparison gnv-artifacts/src/main/java/de/intevation/gnv/jfreechart/LevelOrderIndices.java @ 450:20a480753ff9

Render labels in vertical cross section charts. gnv-artifacts/trunk@498 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 04 Jan 2010 02:49:42 +0000
parents
children c4156275c1e1
comparison
equal deleted inserted replaced
449:c7ca2fce041f 450:20a480753ff9
1 package de.intevation.gnv.jfreechart;
2
3 import java.util.LinkedList;
4
5 /**
6 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
7 */
8 public class LevelOrderIndices
9 {
10 public interface Visitor {
11 Object visit(int index);
12 }
13
14 protected int from;
15 protected int to;
16
17 public LevelOrderIndices() {
18 }
19
20 public LevelOrderIndices(int to) {
21 this(0, to);
22 }
23
24 public LevelOrderIndices(int from, int to) {
25 this.from = Math.min(from, to);
26 this.to = Math.max(from, to);
27 }
28
29 public Object visit(Visitor visitor) {
30 LinkedList<int[]> queue = new LinkedList<int[]>();
31
32 queue.add(new int [] { from, to });
33
34 while (!queue.isEmpty()) {
35 int [] pair = queue.remove();
36
37 int mid = (pair[0] + pair[1]) >> 1;
38
39 Object result = visitor.visit(mid);
40
41 if (result != null) {
42 return result;
43 }
44
45 if (mid-1 >= pair[0]) {
46 queue.add(new int [] { pair[0], mid-1 });
47 }
48
49 if (mid+1 <= pair[1]) {
50 pair[0] = mid+1;
51 queue.add(pair);
52 }
53 }
54
55 return null;
56 }
57 }
58 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org