| 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.ArrayList; |
| 20 | import uk.me.parabola.imgfmt.app.ImgFileWriter; |
| 21 | |
| 22 | /** |
| 23 | * Table B contains offsets in NOD1 of neighbouring nodes |
| 24 | * outside the containing RouteCenter. |
| 25 | */ |
| 26 | public class TableB { |
| 27 | |
| 28 | private final ArrayList<RouteNode> nodes = new ArrayList<>(); |
| 29 | |
| 30 | private static final int ITEM_SIZE = 3; |
| 31 | |
| 32 | private int offset; |
| 33 | |
| 34 | /** |
| 35 | * Retrieve the size of the Table as an int. |
| 36 | * |
| 37 | * While Table B is limited in size (0x100 entries), |
| 38 | * we temporarily build larger tables while subdividing |
| 39 | * the network. |
| 40 | */ |
| 41 | public int size() { |
| 42 | return nodes.size(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Retrieve the size of the table as byte. |
| 47 | * |
| 48 | * This value is what should be written to the table |
| 49 | * header. When this is read, the table is assumed to |
| 50 | * be fit for writing, so at this point we check |
| 51 | * it isn't too large. |
| 52 | */ |
| 53 | public int getNumberOfItems() { |
| 54 | assert nodes.size() < 0x100 : "Table B too large."; |
| 55 | return nodes.size(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Add a node (in another RouteCenter) to this Table and return its index. |
| 60 | * |
| 61 | * This index may overflow while it isn't certain that the |
| 62 | * table fulfills the size constraint. |
| 63 | */ |
| 64 | public void addNode(RouteNode node) { |
| 65 | int i = nodes.indexOf(node); |
| 66 | if (i < 0) { |
| 67 | nodes.add(node); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Retrieve a nodes index. Checked for correct bounds. |
| 73 | */ |
| 74 | public int getIndex(RouteNode node) { |
| 75 | int i = nodes.indexOf(node); |
| 76 | assert i >= 0 : "Trying to read Table B index for non-registered node."; |
| 77 | assert i < 0x100 : "Table B index too large."; |
| 78 | return i; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Reserve space, since node offsets in other |
| 83 | * RoutingCenters need not be known yet. See writePost. |
| 84 | */ |
| 85 | public void write(ImgFileWriter writer) { |
| 86 | offset = writer.position(); |
| 87 | int size = nodes.size() * ITEM_SIZE; |
| 88 | for (int i = 0; i < size; i++) |
| 89 | writer.put1u(0); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Fill in node offsets. |
| 94 | */ |
| 95 | public void writePost(ImgFileWriter writer) { |
| 96 | writer.position(offset); |
| 97 | for (RouteNode node : nodes) |
| 98 | writer.put3u(node.getOffsetNod1()); |
| 99 | } |
| 100 | } |