Skip to content

Commit 667405e

Browse files
committed
remove safety checks on hash-based access, cleanup
1 parent c1f01c3 commit 667405e

File tree

1 file changed

+71
-28
lines changed

1 file changed

+71
-28
lines changed

common-tools/clas-utils/src/main/java/org/jlab/utils/groups/IndexedTable.java

Lines changed: 71 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.List;
1010
import java.util.Map;
1111
import java.util.Set;
12+
import java.util.stream.Collectors;
1213
import javax.swing.JTable;
1314
import javax.swing.table.DefaultTableCellRenderer;
1415
import javax.swing.table.DefaultTableModel;
@@ -21,16 +22,15 @@
2122
public class IndexedTable extends DefaultTableModel {
2223

2324
public static final IndexGenerator DEFAULT_GENERATOR = new IndexGenerator();
25+
26+
protected Map<String,Integer> entryMap = new LinkedHashMap<>();
27+
protected Map<String,String> entryTypes = new LinkedHashMap<>();
2428

2529
private IndexedList<IndexedEntry> entries = null;
26-
private Map<String,Integer> entryMap = new LinkedHashMap<>();
27-
private Map<String,String> entryTypes = new LinkedHashMap<>();
2830
private List<String> entryNames = new ArrayList<>();
2931
private List<String> indexNames = new ArrayList<>();
3032
private String precisionFormat = "%.6f";
31-
3233
private Map<Integer,List<RowConstraint>> constrains = new HashMap<>();
33-
3434
private int DEBUG_MODE = 0;
3535

3636
public IndexedTable(int indexCount){
@@ -40,6 +40,18 @@ public IndexedTable(int indexCount){
4040
}
4141
}
4242

43+
/**
44+
* Clone the format of an existing IndexedTable.
45+
* @param it
46+
*/
47+
public IndexedTable(IndexedTable it) {
48+
entries = new IndexedList<>(it.indexNames.size());
49+
indexNames.addAll(it.indexNames);
50+
entryMap = it.entryMap;
51+
entryTypes = it.entryTypes;
52+
entryNames = it.entryNames;
53+
}
54+
4355
public IndexedTable(int indexCount,String format){
4456
entries = new IndexedList<>(indexCount);
4557
for(int i = 0; i < indexCount; i++){
@@ -119,7 +131,11 @@ public void setIntValue(Integer value, String item, int... index){
119131
}
120132
}
121133
}
122-
134+
135+
public void setIntValueByHash(Integer value, int column, long hash) {
136+
this.entries.getItemByHash(hash).setValue(column, value);
137+
}
138+
123139
public void setDoubleValue(Double value, String item, int... index){
124140
if(this.entries.hasItem(index)==false){
125141
if(DEBUG_MODE>0) System.out.println( "[IndexedTable] ---> error.. entry does not exist");
@@ -134,35 +150,31 @@ public void setDoubleValue(Double value, String item, int... index){
134150
}
135151

136152
public int getIntValueByHash(int index, long hash) {
137-
if (this.entries.hasItemByHash(hash))
138-
return this.entries.getItemByHash(hash).getValue(index).intValue();
139-
return 0;
153+
return entries.getItemByHash(hash).getValue(index).intValue();
140154
}
141-
155+
142156
public double getDoubleValueByHash(int index, long hash) {
143-
if (this.entries.hasItemByHash(hash))
144-
return this.entries.getItemByHash(hash).getValue(index).doubleValue();
145-
return 0;
157+
return entries.getItemByHash(hash).getValue(index).doubleValue();
146158
}
147159

148160
public int getIntValueByHash(String item, long hash) {
149-
if (this.entries.hasItemByHash(hash)) {
150-
if (this.entryMap.containsKey(item)) {
151-
int index = this.entryMap.get(item);
152-
return this.entries.getItemByHash(hash).getValue(index).intValue();
153-
}
154-
}
155-
return 0;
161+
return entries.getItemByHash(hash).getValue(entryMap.get(item)).intValue();
156162
}
157-
163+
158164
public double getDoubleValueByHash(String item, long hash) {
159-
if (this.entries.hasItemByHash(hash)) {
160-
if (this.entryMap.containsKey(item)) {
161-
int index = this.entryMap.get(item);
162-
return this.entries.getItemByHash(hash).getValue(index).doubleValue();
163-
}
164-
}
165-
return 0;
165+
return entries.getItemByHash(hash).getValue(entryMap.get(item)).doubleValue();
166+
}
167+
168+
public List<Number> getValuesByHash(long hash) {
169+
return this.entries.getItemByHash(hash).entryValues;
170+
}
171+
172+
public List<Integer> getIntegersByHash(long hash) {
173+
return getValuesByHash(hash).stream().map(x -> x.intValue()).collect(Collectors.toList());
174+
}
175+
176+
public List<Double> getDoublesByHash(long hash) {
177+
return getValuesByHash(hash).stream().map(x -> x.doubleValue()).collect(Collectors.toList());
166178
}
167179

168180
public int getIntValue(String item, int... index){
@@ -201,6 +213,10 @@ public IndexedList getList(){
201213
return this.entries;
202214
}
203215

216+
public Map<String,Integer> getEntryMap(){
217+
return this.entryMap;
218+
}
219+
204220
private void parseFormat(String format){
205221
String[] tokens = format.split(":");
206222
entryMap.clear();
@@ -250,7 +266,7 @@ public void show(){
250266
@Override
251267
public String toString(){
252268
StringBuilder str = new StringBuilder();
253-
str.append(String.format("IndexedList SIZE = %d\n", entryMap.size()));
269+
str.append(String.format("IndexedList SIZE = %d/%d\n", entryMap.size(),entries.getMap().size()));
254270
for(Map.Entry<String,Integer> entry : this.entryMap.entrySet()){
255271
str.append(String.format("* %-24s * %3s * \n",entry.getKey(),
256272
this.entryTypes.get(entry.getKey())));
@@ -453,4 +469,31 @@ public boolean conflicts(IndexedTable it) {
453469
return !conflicts.isEmpty();
454470
}
455471

472+
/**
473+
* Make one big table.
474+
* @param tables the tables to combine
475+
* @return
476+
*/
477+
public static IndexedTable add(List<IndexedTable> tables) {
478+
// create the new table:
479+
IndexedTable ret = new IndexedTable(tables.get(0));
480+
for (IndexedTable table : tables) {
481+
// loop over the input table rows:
482+
for (Object key : table.getList().getMap().keySet()) {
483+
// get the indexing for this row:
484+
int crate = IndexedTable.DEFAULT_GENERATOR.getIndex((long)key, 0);
485+
int slot = IndexedTable.DEFAULT_GENERATOR.getIndex((long)key, 1);
486+
int channel = IndexedTable.DEFAULT_GENERATOR.getIndex((long)key, 2);
487+
long hash = IndexedTable.DEFAULT_GENERATOR.hashCode(crate, slot, channel);
488+
// add row to the new table:
489+
ret.addEntry(crate,slot,channel);
490+
// set values for the new row:
491+
for (int column : table.entryMap.values()) {
492+
int value = table.getIntValueByHash(column, hash);
493+
ret.setIntValueByHash(value, column, hash);
494+
}
495+
}
496+
}
497+
return ret;
498+
}
456499
}

0 commit comments

Comments
 (0)