Debian Codegraph @

src/uk/me/parabola/imgfmt/app/net/TableA.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: 18-Jul-2008
16 */
17 package uk.me.parabola.imgfmt.app.net;
18
19 import java.util.HashMap;
20 import java.util.LinkedHashMap;
21
22 import uk.me.parabola.imgfmt.app.ImgFileWriter;
23 import uk.me.parabola.log.Logger;
24
25 /**
26 * Table A that contains road information for segments in one RouteCenter.
27 *
28 * Each arc starting from a node in the RouteCenter has an associated
29 * entry in Table A, shared by the inverse arc for internal arcs. This
30 * entry consists of some routing parameters and a link to the road in
31 * NET.
32 */
33 public class TableA {
34 private static final Logger log = Logger.getLogger(TableA.class);
35
36 private static final int ITEM_SIZE = 5;
37
38 // This table's start position relative to the start of NOD 1
39 private int offset;
40
41 // arcs for roundabouts
42 private final HashMap<RoadDef,Integer> roundaboutArcs = new LinkedHashMap<>();
43 // arcs for unpaved ways
44 private final HashMap<RoadDef,Integer> unpavedArcs = new LinkedHashMap<>();
45 // arcs for ferry ways
46 private final HashMap<RoadDef,Integer> ferryArcs = new LinkedHashMap<>();
47 // arcs for paved ways
48 private final HashMap<RoadDef,Integer> pavedArcs = new LinkedHashMap<>();
49
50 private static int count;
51
52 private boolean frozen ; // true when no more arcs should be added
53
54 public TableA() {
55 log.debug("creating TableA", count);
56 count++;
57 }
58
59 /**
60 * Add an arc to the table if not present and set its index.
61 *
62 * The value may overflow while it isn't certain that
63 * the table fulfils the size constraint.
64 */
65 public void addArc(RouteArc arc) {
66 assert !frozen : "trying to add arc to Table A after it has been frozen";
67 int i;
68 RoadDef rd = arc.getRoadDef();
69 if(rd.isRoundabout()) {
70 if (!roundaboutArcs.containsKey(rd)) {
71 i = roundaboutArcs.size();
72 roundaboutArcs.put(rd, i);
73 log.debug("added roundabout arc", count, rd, i);
74 }
75 }
76 else if(rd.ferry()) {
77 if (!ferryArcs.containsKey(rd)) {
78 i = ferryArcs.size();
79 ferryArcs.put(rd, i);
80 log.debug("added ferry arc", count, rd, i);
81 }
82 }
83 else if(rd.paved()) {
84 if (!pavedArcs.containsKey(rd)) {
85 i = pavedArcs.size();
86 pavedArcs.put(rd, i);
87 log.debug("added paved arc", count, rd, i);
88 }
89 }
90 else {
91 if (!unpavedArcs.containsKey(rd)) {
92 i = unpavedArcs.size();
93 unpavedArcs.put(rd, i);
94 log.debug("added unpaved arc", count, rd, i);
95 }
96 }
97 }
98
99 /**
100 * Retrieve an arc's index.
101 * Order in table A: roundabouts, unpaved, ferry, paved
102 */
103 public int getIndex(RouteArc arc) {
104 frozen = true; // don't allow any more arcs to be added
105 int i;
106 RoadDef rd = arc.getRoadDef();
107 if(rd.isRoundabout()) {
108 assert roundaboutArcs.containsKey(rd):
109 "Trying to read Table A index for non-registered arc: " + count + " " + rd;
110 i = roundaboutArcs.get(rd);
111 }
112 else if(rd.ferry()) {
113 assert ferryArcs.containsKey(rd):
114 "Trying to read Table A index for non-registered arc: " + count + " " + rd;
115 i = roundaboutArcs.size() + unpavedArcs.size() + ferryArcs.get(rd);
116 }
117 else if(rd.paved()) {
118 assert pavedArcs.containsKey(rd):
119 "Trying to read Table A index for non-registered arc: " + count + " " + rd;
120 i = roundaboutArcs.size() + unpavedArcs.size() + ferryArcs.size() + pavedArcs.get(rd);
121 }
122 else {
123 assert unpavedArcs.containsKey(rd):
124 "Trying to read Table A index for non-registered arc: " + count + " " + rd;
125 i = roundaboutArcs.size() + unpavedArcs.get(rd);
126 }
127 assert i < 0x100 : "Table A index too large: " + rd;
128 return i;
129 }
130
131 /**
132 * Retrieve the size of the Table as an int.
133 *
134 * While Table A is limited to byte size (0x100 entries),
135 * we temporarily build larger tables while subdividing
136 * the network.
137 */
138 public int size() {
139 return roundaboutArcs.size() + unpavedArcs.size() + ferryArcs.size() + pavedArcs.size();
140 }
141
142 public int numRoundaboutArcs() {
143 return roundaboutArcs.size();
144 }
145
146 public int numUnpavedArcs() {
147 return unpavedArcs.size();
148 }
149
150 public int numFerryArcs() {
151 return ferryArcs.size();
152 }
153
154 /**
155 * Retrieve the size of the table as byte.
156 *
157 * This value is what should be written to the table
158 * header. When this is read, the table is assumed to
159 * be fit for writing, so at this point we check
160 * it isn't too large.
161 */
162 public int getNumberOfItems() {
163 assert size() < 0x100 : "Table A too large";
164 return size();
165 }
166
167 /**
168 * This is called first to reserve enough space. It will be rewritten
169 * later.
170 */
171 public void write(ImgFileWriter writer) {
172 offset = writer.position();
173 int size = size() * ITEM_SIZE;
174 log.debug("tab a offset", offset, "tab a size", size);
175
176 for (int i = 0; i < size; i++)
177 writer.put1u(0);
178 }
179
180 /**
181 * Fill in the table once the NET offsets of the roads are known.
182 */
183 public void writePost(ImgFileWriter writer) {
184 writer.position(offset);
185 // unpaved arcs first
186 for (RoadDef rd: roundaboutArcs.keySet()) {
187 writePost(writer, rd);
188 }
189 for (RoadDef rd: unpavedArcs.keySet()) {
190 writePost(writer, rd);
191 }
192 // followed by the ferry arcs
193 for (RoadDef rd : ferryArcs.keySet()) {
194 writePost(writer, rd);
195 }
196 // followed by the paved arcs
197 for (RoadDef rd : pavedArcs.keySet()) {
198 writePost(writer, rd);
199 }
200 }
201
202 public void writePost(ImgFileWriter writer, RoadDef rd) {
203 // write the table A entries. Consists of a pointer to net
204 // followed by 2 bytes of class and speed flags and road restrictions.
205 int pos = rd.getOffsetNet1();
206 int access = rd.getTabAAccess();
207 // top bits of access go into net1 offset
208 final int ACCESS_TOP_BITS = 0xc000;
209 pos |= (access & ACCESS_TOP_BITS) << 8;
210 access &= ~ACCESS_TOP_BITS;
211 writer.put3u(pos);
212 writer.put1u(rd.getTabAInfo());
213 writer.put1u(access);
214 }
215 }