Skip to content

Commit

Permalink
Change internal table to a set. 100x faster load, faster searches. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed May 21, 2020
1 parent 74ba2b6 commit c9574e3
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/particle/particle/particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
SupportsInt,
Union,
TextIO,
Set,
)

from hepunits.constants import c_light
Expand Down Expand Up @@ -229,7 +230,7 @@ def __repr__(self):
)

# Loaded table of entries
_table = None # type: Optional[List[Particle]]
_table = None # type: Optional[Set[Particle]]

# Names of loaded tables
_table_names = None # type: Optional[List[str]]
Expand Down Expand Up @@ -266,7 +267,7 @@ def table_loaded(cls):

@classmethod
def all(cls):
# type: () -> List[Particle]
# type: () -> Set[Particle]
"""
Access, hence get hold of, the internal particle data CSV table,
loading it from the default location if no table has yet been loaded.
Expand All @@ -275,7 +276,7 @@ def all(cls):
if not cls.table_loaded():
cls.load_table()

return cls._table if cls._table is not None else []
return cls._table if cls._table is not None else set()

@classmethod
def dump_table(
Expand Down Expand Up @@ -380,7 +381,7 @@ def dump_table(
pass

# Start with the full table
tbl_all = cls.all()
tbl_all = sorted(cls.all())

# Apply a filter, if specified
if filter_fn is not None:
Expand Down Expand Up @@ -429,7 +430,7 @@ def load_table(cls, filename=None, append=False, _name=None):
if append and not cls.table_loaded():
cls.load_table(append=False) # default load
elif not append:
cls._table = []
cls._table = set()
cls._table_names = []

# Tell MyPy that this is true
Expand Down Expand Up @@ -466,7 +467,7 @@ def load_table(cls, filename=None, append=False, _name=None):
if append and value in cls._table:
cls._table.remove(value) # type: ignore

cls._table.append(
cls._table.add(
cls(
pdgid=value,
mass=float(v["Mass"]),
Expand Down

0 comments on commit c9574e3

Please sign in to comment.