@@ -80,9 +80,9 @@ public String toString() {
80
80
/** index by Integer ID in STS file, return String name */
81
81
public Map <Integer , Chare > entryChares = new TreeMap <Integer , Chare >();
82
82
/** keys are indexes into flat arrays, values are the IDs given in STS file */
83
- private Map <Integer , Integer > entryFlatToID = new TreeMap < Integer , Integer >() ;
83
+ private Map <Integer , Integer > entryFlatToID = null ;
84
84
/** keys are the IDs given in STS file, values are indexes into flat arrays */
85
- private Map <Integer , Integer > entryIDToFlat = new TreeMap < Integer , Integer >() ;
85
+ private Map <Integer , Integer > entryIDToFlat = null ;
86
86
87
87
88
88
@@ -224,8 +224,19 @@ public StsReader(String FileName)
224
224
ChareID = Integer .parseInt (st .nextToken ());
225
225
st .nextToken (); // msgid
226
226
227
- entryFlatToID .put (entryIndex , ID );
228
- entryIDToFlat .put (ID ,entryIndex );
227
+ // In general, Entry IDs will be contiguous starting from 0, so avoid
228
+ // creating the ID<->flat maps if possible. This code creates the map
229
+ // when it detects a non-contiguous case.
230
+ if (ID != entryIndex && entryFlatToID == null ) {
231
+ entryFlatToID = new TreeMap <>();
232
+ for (int i = 0 ; i < entryIndex ; i ++)
233
+ entryFlatToID .put (i , i );
234
+ entryIDToFlat = new TreeMap <>(entryFlatToID );
235
+ }
236
+ if (entryFlatToID != null ) {
237
+ entryFlatToID .put (entryIndex , ID );
238
+ entryIDToFlat .put (ID , entryIndex );
239
+ }
229
240
entryIndex ++;
230
241
getEntryNames ().put (ID ,Name );
231
242
getEntryChare ().put (ID , Chares [ChareID ]);
@@ -348,8 +359,13 @@ public String getEntryNameByID(int ID) {
348
359
}
349
360
350
361
public String getEntryNameByIndex (int index ) {
351
- if (entryFlatToID .containsKey (index )){
352
- return getEntryNames ().get (entryFlatToID .get (index ));
362
+ // Check if the ID is valid. If entryFlatToID exists, then check if
363
+ // there's a valid mapping for index there, otherwise the index is the
364
+ // ID, so check in the entry names map directly.
365
+ final boolean isValid = (entryFlatToID != null && entryFlatToID .containsKey (index )) ||
366
+ (entryFlatToID == null && getEntryNames ().containsKey (index ));
367
+ if (isValid ) {
368
+ return getEntryNames ().get (getEntryID (index ));
353
369
} else {
354
370
return "Unknown" ;
355
371
}
@@ -372,15 +388,15 @@ private String getEntryChareNameByID(int ID) {
372
388
}
373
389
374
390
public String getEntryChareNameByIndex (int index ) {
375
- return getEntryChare ().get (entryFlatToID . get (index )).name ;
391
+ return getEntryChare ().get (getEntryID (index )).name ;
376
392
}
377
393
378
394
public int getEntryChareDimensionsByID (int ID ) {
379
395
return getEntryChare ().get (ID ).dimensions ;
380
396
}
381
397
382
398
public int getEntryChareDimensionsByIndex (int index ) {
383
- return getEntryChare ().get (entryFlatToID . get (index )).dimensions ;
399
+ return getEntryChare ().get (getEntryID (index )).dimensions ;
384
400
}
385
401
386
402
public String getEntryFullNameByID (int ID ) {
@@ -391,12 +407,16 @@ public String getEntryFullNameByIndex(int index) {
391
407
return getEntryChareNameByIndex (index ) + "::" + getEntryNameByIndex (index );
392
408
}
393
409
394
- public Integer getEntryIndex (int ID ) {
395
- if (ID <0 )
396
- return ID ;
397
- return entryIDToFlat .get (ID );
398
- }
399
-
410
+ public int getEntryIndex (int ID ) {
411
+ if (ID <0 )
412
+ return ID ;
413
+ return (entryIDToFlat == null ) ? ID : entryIDToFlat .get (ID );
414
+ }
415
+
416
+ private int getEntryID (int index ) {
417
+ return (entryFlatToID == null ) ? index : entryFlatToID .get (index );
418
+ }
419
+
400
420
// *** user event accessors ***
401
421
public int getNumUserDefinedEvents () {
402
422
return userEvents .size ();
0 commit comments