| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.tree.linked; |
| 9 |
|
| 10 |
import java.util.Arrays; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public class LineNumberMap { |
| 21 |
|
| 22 |
private int[] sequenceNumbers; |
| 23 |
private int[] lineNumbers; |
| 24 |
private int[] columnNumbers; |
| 25 |
private int allocated; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public LineNumberMap() { |
| 32 |
sequenceNumbers = new int[200]; |
| 33 |
lineNumbers = new int[200]; |
| 34 |
columnNumbers = new int[200]; |
| 35 |
allocated = 0; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public void setLineAndColumn(int sequence, int line, int column) { |
| 47 |
if (sequenceNumbers.length <= allocated + 1) { |
| 48 |
sequenceNumbers = Arrays.copyOf(sequenceNumbers, allocated * 2); |
| 49 |
lineNumbers = Arrays.copyOf(lineNumbers, allocated * 2); |
| 50 |
columnNumbers = Arrays.copyOf(columnNumbers, allocated * 2); |
| 51 |
} |
| 52 |
sequenceNumbers[allocated] = sequence; |
| 53 |
lineNumbers[allocated] = line; |
| 54 |
columnNumbers[allocated] = column; |
| 55 |
allocated++; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public int getLineNumber(int sequence) { |
| 66 |
if (sequenceNumbers.length > allocated) { |
| 67 |
condense(); |
| 68 |
} |
| 69 |
int index = Arrays.binarySearch(sequenceNumbers, sequence); |
| 70 |
if (index < 0) { |
| 71 |
index = -index - 1; |
| 72 |
if (index > lineNumbers.length - 1) { |
| 73 |
index = lineNumbers.length - 1; |
| 74 |
} |
| 75 |
} |
| 76 |
return lineNumbers[index]; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public int getColumnNumber(int sequence) { |
| 87 |
if (sequenceNumbers.length > allocated) { |
| 88 |
condense(); |
| 89 |
} |
| 90 |
int index = Arrays.binarySearch(sequenceNumbers, sequence); |
| 91 |
if (index < 0) { |
| 92 |
index = -index - 1; |
| 93 |
if (index >= columnNumbers.length) { |
| 94 |
index = columnNumbers.length - 1; |
| 95 |
} |
| 96 |
} |
| 97 |
return columnNumbers[index]; |
| 98 |
} |
| 99 |
|
| 100 |
private synchronized void condense() { |
| 101 |
sequenceNumbers = Arrays.copyOf(sequenceNumbers, allocated); |
| 102 |
lineNumbers = Arrays.copyOf(lineNumbers, allocated); |
| 103 |
columnNumbers = Arrays.copyOf(columnNumbers, allocated); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
} |
| 108 |
|