Skip to content

Commit

Permalink
Iterate over objects in TDirectory in linear time. (#638) (#639)
Browse files Browse the repository at this point in the history
* Iterate over objects in TDirectory in linear time.

* Remove the debug_counter.

(cherry picked from commit 809cf8e)
  • Loading branch information
jpivarski authored Jul 2, 2022
1 parent 7d38054 commit 5e6036c
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/uproot/reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1444,12 +1444,19 @@ def __init__(self, path, cursor, context, file, parent):
)

self._keys = []
self._keys_lookup = {}
for _ in range(num_keys):
key = ReadOnlyKey(
keys_chunk, keys_cursor, {}, file, self, read_strings=True
)
name = key.fName
if name not in self._keys_lookup:
self._keys_lookup[name] = []
self._keys_lookup[name].append(len(self._keys))
self._keys.append(key)

self._len = None

self.hook_after_keys(
chunk=chunk,
cursor=cursor,
Expand Down Expand Up @@ -1901,11 +1908,13 @@ def _ipython_key_completions_(self):
return self.iterkeys() # noqa: B301 (not a dict)

def __len__(self):
return len(self._keys) + sum(
len(x.get())
for x in self._keys
if x.fClassName in ("TDirectory", "TDirectoryFile")
)
if self._len is None:
self._len = len(self._keys) + sum(
len(x.get())
for x in self._keys
if x.fClassName in ("TDirectory", "TDirectoryFile")
)
return self._len

def __contains__(self, where):
try:
Expand Down Expand Up @@ -2031,14 +2040,14 @@ def key(self, where):
item, cycle = where, None

last = None
for key in self._keys:
if key.fName == item:
if cycle == key.fCycle:
return key
elif cycle is None and last is None:
last = key
elif cycle is None and last.fCycle < key.fCycle:
last = key
for index in self._keys_lookup.get(item, []):
key = self._keys[index]
if cycle == key.fCycle:
return key
elif cycle is None and last is None:
last = key
elif cycle is None and last.fCycle < key.fCycle:
last = key

if last is not None:
return last
Expand Down

0 comments on commit 5e6036c

Please sign in to comment.