Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/PrefixIndex.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2012.
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 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
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 * Holds an index of name prefixes to record numbers.
26 *
27 * Extends MdrSection, although is sometimes a subsection, not an actual section.
28 *
29 * @author Steve Ratcliffe
30 */
31 public class PrefixIndex extends MdrSection {
32 private final int prefixLength;
33 private int maxIndex;
34
35 // We use mdr8record for all similar indexes.
36 private final List<Mdr8Record> index = new ArrayList<>();
37
38 /**
39 * Sets the config and the prefix length for this index.
40 *
41 * Prefix length may differ depending on the amount of data, so will have
42 * to deal with that when it happens.
43 *
44 * @param config Configuration for sorting methods.
45 * @param prefixLength The prefix length for this index.
46 */
47 public PrefixIndex(MdrConfig config, int prefixLength) {
48 setConfig(config);
49 this.prefixLength = prefixLength;
50 }
51
52 /**
53 * We can create an index for any type that has a name.
54 * @param list A list of items that have a name.
55 * @param grouped used with MDR7 records
56 */
57 public void createFromList(List<? extends NamedRecord> list, boolean grouped) {
58 maxIndex = list.size();
59
60 // Prefixes are equal based on the primary unaccented character, so
61 // we need to use the collator to test for equality and not equals().
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; // record number of the input list
69 int outRecord = 0; // record number of the index
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 // Peek into the real type to support the mdr17 feature of indexes sorted on country.
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 * We can create an index for any type that has a name.
110 * @param list A list of items that have a name.
111 */
112 public void createFromList(List<? extends NamedRecord> list) {
113 createFromList(list, false);
114 }
115
116 /**
117 * Write the section or subsection.
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 }