| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package uk.me.parabola.imgfmt.app.mdr; |
| 14 |
|
| 15 |
import java.text.Collator; |
| 16 |
import java.util.ArrayList; |
| 17 |
import java.util.List; |
| 18 |
|
| 19 |
import uk.me.parabola.imgfmt.Utils; |
| 20 |
import uk.me.parabola.imgfmt.app.ImgFileWriter; |
| 21 |
import uk.me.parabola.imgfmt.app.srt.Sort; |
| 22 |
import uk.me.parabola.imgfmt.app.srt.Sort.SrtCollator; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class PrefixIndex extends MdrSection { |
| 32 |
private final int prefixLength; |
| 33 |
private int maxIndex; |
| 34 |
|
| 35 |
|
| 36 |
private final List<Mdr8Record> index = new ArrayList<>(); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public PrefixIndex(MdrConfig config, int prefixLength) { |
| 48 |
setConfig(config); |
| 49 |
this.prefixLength = prefixLength; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public void createFromList(List<? extends NamedRecord> list, boolean grouped) { |
| 58 |
maxIndex = list.size(); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
Sort sort = getConfig().getSort(); |
| 63 |
Sort.SrtCollator collator = (SrtCollator) sort.getCollator(); |
| 64 |
collator.setStrength(Collator.PRIMARY); |
| 65 |
|
| 66 |
String lastCountryName = null; |
| 67 |
char[] lastPrefix = "".toCharArray(); |
| 68 |
int inRecord = 0; |
| 69 |
int outRecord = 0; |
| 70 |
int lastMdr22SortPos = -1; |
| 71 |
for (NamedRecord r : list) { |
| 72 |
inRecord++; |
| 73 |
String name; |
| 74 |
if (r instanceof Mdr7Record) { |
| 75 |
name = ((Mdr7Record) r).getPartialName(); |
| 76 |
if (grouped) { |
| 77 |
int mdr22SortPos = ((Mdr7Record) r).getCity().getMdr22SortPos(); |
| 78 |
if (mdr22SortPos != lastMdr22SortPos) |
| 79 |
lastPrefix = "".toCharArray(); |
| 80 |
lastMdr22SortPos = mdr22SortPos; |
| 81 |
} |
| 82 |
} |
| 83 |
else |
| 84 |
name = r.getName(); |
| 85 |
char[] prefix = sort.getPrefix(name, prefixLength); |
| 86 |
int cmp = collator.compareOneStrengthWithLength(prefix, lastPrefix, Collator.PRIMARY, prefixLength); |
| 87 |
if (cmp > 0) { |
| 88 |
outRecord++; |
| 89 |
index.add(new Mdr8Record(prefix, inRecord)); |
| 90 |
|
| 91 |
lastPrefix = prefix; |
| 92 |
|
| 93 |
if (grouped) { |
| 94 |
|
| 95 |
Mdr5Record city = ((Mdr7Record) r).getCity(); |
| 96 |
if (city != null) { |
| 97 |
String countryName = city.getCountryName(); |
| 98 |
if (!countryName.equals(lastCountryName)) { |
| 99 |
city.getMdrCountry().getMdr29().setMdr17(outRecord); |
| 100 |
lastCountryName = countryName; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public void createFromList(List<? extends NamedRecord> list) { |
| 113 |
createFromList(list, false); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public void writeSectData(ImgFileWriter writer) { |
| 120 |
int size = Utils.numberToPointerSize(maxIndex); |
| 121 |
for (Mdr8Record s : index) { |
| 122 |
for (int i = 0; i< prefixLength; i++) { |
| 123 |
writer.put1u(s.getPrefix()[i]); |
| 124 |
} |
| 125 |
writer.putNu(size, s.getRecordNumber()); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
public int getItemSize() { |
| 130 |
return prefixLength + Utils.numberToPointerSize(maxIndex); |
| 131 |
} |
| 132 |
|
| 133 |
protected int numberOfItems() { |
| 134 |
return index.size(); |
| 135 |
} |
| 136 |
|
| 137 |
public int getPrefixLength() { |
| 138 |
return prefixLength; |
| 139 |
} |
| 140 |
} |