Commit b92661a5 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Use a dictionary instead of a linear lookup by hash.

Most of the time was spent in the todo_by_hash function which is
not efficient enough for large todo files.
parent 1c0a04eb
...@@ -44,6 +44,7 @@ class TodoList(object): ...@@ -44,6 +44,7 @@ class TodoList(object):
The string will be parsed. The string will be parsed.
""" """
self._todos = [] self._todos = []
self._tododict = {} # hash(todo) to todo lookup
self._depgraph = Graph.DirectedGraph() self._depgraph = Graph.DirectedGraph()
self.add_list(p_todostrings) self.add_list(p_todostrings)
...@@ -81,18 +82,6 @@ class TodoList(object): ...@@ -81,18 +82,6 @@ class TodoList(object):
return result return result
def todo_by_hash(self, p_hash):
"""
Given the hash value of a todo, return the corresponding instance.
"""
result = None
for todo in self._todos:
if hash(todo) == p_hash:
result = todo
break
return result
def todo_by_dep_id(self, p_dep_id): def todo_by_dep_id(self, p_dep_id):
""" """
Returns the todo that has the id tag set to the value p_dep_id. Returns the todo that has the id tag set to the value p_dep_id.
...@@ -160,6 +149,7 @@ class TodoList(object): ...@@ -160,6 +149,7 @@ class TodoList(object):
def add_todos(self, p_todos): def add_todos(self, p_todos):
for todo in p_todos: for todo in p_todos:
self._todos.append(todo) self._todos.append(todo)
self._tododict[hash(todo)] = todo
self._maintain_dep_graph(todo) self._maintain_dep_graph(todo)
self._update_parent_cache() self._update_parent_cache()
...@@ -280,7 +270,7 @@ class TodoList(object): ...@@ -280,7 +270,7 @@ class TodoList(object):
given todo. given todo.
""" """
parents = self._depgraph.incoming_neighbors(hash(p_todo), not p_only_direct) parents = self._depgraph.incoming_neighbors(hash(p_todo), not p_only_direct)
return [self.todo_by_hash(parent) for parent in parents] return [self._tododict[parent] for parent in parents]
def children(self, p_todo, p_only_direct=False): def children(self, p_todo, p_only_direct=False):
""" """
...@@ -289,7 +279,7 @@ class TodoList(object): ...@@ -289,7 +279,7 @@ class TodoList(object):
""" """
children = \ children = \
self._depgraph.outgoing_neighbors(hash(p_todo), not p_only_direct) self._depgraph.outgoing_neighbors(hash(p_todo), not p_only_direct)
return [self.todo_by_hash(child) for child in children] return [self._tododict[child] for child in children]
def clean_dependencies(self): def clean_dependencies(self):
""" """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment