Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/jfreechart/LevelOrderIndices.java @ 540:80630520e25a
merged gnv-artifacts/0.4
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:49 +0200 |
parents | 20a480753ff9 |
children | c4156275c1e1 |
comparison
equal
deleted
inserted
replaced
415:9f4a0b990d27 | 540:80630520e25a |
---|---|
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 : |