Debian Codegraph @

src/uk/me/parabola/imgfmt/app/net/TableC.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, Robert Vollmert
15 * Create date: 18-Jul-2008
16 */
17 package uk.me.parabola.imgfmt.app.net;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import uk.me.parabola.imgfmt.FormatException;
23 import uk.me.parabola.imgfmt.app.ImgFileWriter;
24
25 /**
26 * @author Steve Ratcliffe, Robert Vollmert
27 */
28 public class TableC {
29 // size of the table, excluding the size field
30 private int size;
31
32 private final TableA tabA;
33
34 private final List<RouteRestriction> restrictions = new ArrayList<>();
35
36 public TableC(TableA tabA) {
37 this.tabA = tabA;
38 }
39
40 /**
41 * Write the table including size field.
42 */
43 public void write(ImgFileWriter writer, int tablesOffset) {
44 if (!restrictions.isEmpty()) {
45 if (size < 0x100)
46 writer.put1u(size);
47 else
48 writer.put2u(size);
49 for (RouteRestriction restr : restrictions)
50 restr.write(writer, tablesOffset);
51 }
52 if(tabA.numRoundaboutArcs() > 0)
53 writer.put1u(tabA.numRoundaboutArcs());
54 if(tabA.numUnpavedArcs() > 0)
55 writer.put1u(tabA.numUnpavedArcs());
56 if(tabA.numFerryArcs() > 0)
57 writer.put1u(tabA.numFerryArcs());
58 }
59
60 /**
61 * Add a restriction.
62 *
63 * @param restr A new restriction.
64 * @return The offset into Table C at which the restriction will
65 * be written.
66 */
67 public int addRestriction(RouteRestriction restr) {
68 int offset = size;
69 restrictions.add(restr);
70 size += restr.getSize();
71 return offset;
72 }
73
74 /**
75 * The size of restriction indices in the nodes area.
76 */
77 private int getOffsetSize() {
78 if (size < 0x80)
79 return 1; // allows 7 bit index (8th bit is flag)
80 else if (size < 0x8000)
81 return 2; // allows 15 bit index (16th bit is flag)
82 else
83 // XXX: haven't seen larger than 2, may well be possible
84 throw new FormatException("too many restrictions");
85 }
86
87 public void propagateSizeBytes() {
88 int b = getOffsetSize();
89 for (RouteRestriction restr : restrictions)
90 restr.setOffsetSize(b);
91 }
92
93 public int getFormat() {
94 // Table C format bitmask
95 // 0x01 = 1-255 bytes of restrictions
96 // 0x02 = 256-65535 bytes of restrictions
97 // 0x08 = unpaved roads count present
98 // 0x10 = ferry count present
99 int format = 0;
100 if(size > 0) {
101 ++format;
102 if(size > 0xff)
103 ++format;
104 }
105 if(tabA.numRoundaboutArcs() > 0)
106 format |= 0x04;
107 if(tabA.numUnpavedArcs() > 0)
108 format |= 0x08;
109 if(tabA.numFerryArcs() > 0)
110 format |= 0x10;
111 return format;
112 }
113 }