Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/jfreechart/LevelOrderIndices.java @ 1119:7c4f81f74c47
merged gnv-artifacts
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:00 +0200 |
parents | f953c9a559d8 |
children |
comparison
equal
deleted
inserted
replaced
1027:fca4b5eb8d2f | 1119:7c4f81f74c47 |
---|---|
1 /* | |
2 * Copyright (c) 2010 by Intevation GmbH | |
3 * | |
4 * This program is free software under the LGPL (>=v2.1) | |
5 * Read the file LGPL.txt coming with the software for details | |
6 * or visit http://www.gnu.org/licenses/ if it does not exist. | |
7 */ | |
8 | |
9 package de.intevation.gnv.jfreechart; | |
10 | |
11 import java.util.LinkedList; | |
12 | |
13 /** | |
14 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> | |
15 */ | |
16 public class LevelOrderIndices | |
17 { | |
18 public interface Visitor { | |
19 | |
20 Object visit(int index); | |
21 } | |
22 | |
23 | |
24 protected int from; | |
25 | |
26 protected int to; | |
27 | |
28 | |
29 public LevelOrderIndices() { | |
30 } | |
31 | |
32 | |
33 public LevelOrderIndices(int to) { | |
34 this(0, to); | |
35 } | |
36 | |
37 | |
38 public LevelOrderIndices(int from, int to) { | |
39 this.from = Math.min(from, to); | |
40 this.to = Math.max(from, to); | |
41 } | |
42 | |
43 | |
44 public Object visit(Visitor visitor) { | |
45 LinkedList<int[]> queue = new LinkedList<int[]>(); | |
46 | |
47 queue.add(new int [] { from, to }); | |
48 | |
49 while (!queue.isEmpty()) { | |
50 int [] pair = queue.remove(); | |
51 | |
52 int mid = (pair[0] + pair[1]) >> 1; | |
53 | |
54 Object result = visitor.visit(mid); | |
55 | |
56 if (result != null) { | |
57 return result; | |
58 } | |
59 | |
60 if (mid-1 >= pair[0]) { | |
61 queue.add(new int [] { pair[0], mid-1 }); | |
62 } | |
63 | |
64 if (mid+1 <= pair[1]) { | |
65 pair[0] = mid+1; | |
66 queue.add(pair); | |
67 } | |
68 } | |
69 | |
70 return null; | |
71 } | |
72 } | |
73 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |