pont-us / PuffinPlot

A program to plot and analyse palaeomagnetic data
GNU General Public License v3.0
3 stars 0 forks source link

Make Suite.samplesById into a HashMap #373

Open pont-us opened 3 years ago

pont-us commented 3 years ago

I don't see any reason for it to be a LinkedHashMap: the ordering is never used, as far as I can see; we have ordering anyway in the main samples list; and now that samples can be added, deleted, and sorted, insertion order is meaningless. But it might be worth considering replacing them both with a SortedMap.

Update: I can't find any data structures which combine a hash table with an indexed array. (SortedMap and NavigableMap are iterable but not indexable, and I do NOT want O(n) indexing!) So I think I'm stuck with two data structures – three, if I count indicesBySample.

One potential solution: wrap samples, indicesBySample, and samplesById in a new class which ensures that they're all updated together. Probably the new class should implement List and work as a drop-in replacement for the current "samples" field. List methods call through to a private ArrayList of samples, as well as keeping the other indices up to date. I can extend the List interface to a SamplesList which also specifies the required getIndexBySample etc. methods.