Skip to content

Commit

Permalink
Commented on the safety of the reverse mapping index in Names.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Nov 13, 2023
1 parent ffd2093 commit 0ba0912
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/biocutils/Names.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@ def __init__(self, iterable: Optional[Iterable] = None, coerce: bool = True):
self._reverse = None

# Enable fast indexing by name, but only on demand. This reverse mapping
# field is strictly internal.
# field is strictly internal and should be completely transparent to the
# user; so, calls to map() can be considered as 'non-mutating', as it
# shouldn't manifest in any visible changes to the Names object. I guess
# that things become a little hairy in multi-threaded contexts where I
# should probably protect the final assignment to _reverse. But then
# again, Python is single-threaded anyway, so maybe it doesn't matter.
def _populate_reverse_index(self):
if self._reverse is None:
self._reverse = {}
revmap = {}
for i, n in enumerate(self):
if n not in self._reverse:
self._reverse[n] = i
if n not in revmap:
revmap[n] = i
self._reverse = revmap

def _wipe_reverse_index(self):
self._reverse = None
Expand Down

0 comments on commit 0ba0912

Please sign in to comment.