Skip to content

Commit

Permalink
find_all: return a dictionary isntead of a list
Browse files Browse the repository at this point in the history
New implementation causes the find_all results to depend on order of
dictionary elements. A dictionary is therefore a more appropriate return type.
  • Loading branch information
ptrus committed Apr 10, 2020
1 parent f065e05 commit 934e235
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
![ci](https://github.com/ptrus/suffix-trees/workflows/ci/badge.svg)
[![codecov](https://codecov.io/gh/ptrus/suffix-trees/branch/master/graph/badge.svg)](https://codecov.io/gh/ptrus/suffix-trees)

Python implementation of Suffix Trees and Generalized Suffix Trees. Also provided methods with typcal applications of STrees and GSTrees.
Python implementation of Suffix Trees and Generalized Suffix Trees. Also provided methods with typcal applications of STrees and GSTrees.

### Installation

Expand All @@ -19,7 +19,7 @@ from suffix_trees import STree
# Suffix-Tree example.
st = STree.STree("abcdefghab")
print(st.find("abc")) # 0
print(st.find_all("ab")) # [0, 8]
print(st.find_all("ab")) # {0, 8}

# Generalized Suffix-Tree example.
a = ["xxxabcxxx", "adsaabc", "ytysabcrew", "qqqabcqw", "aaabc"]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name='suffix-trees',
packages=['suffix_trees'],
version='0.2.6',
version='0.3.0',
description='Suffix trees, generalized suffix trees and string processing methods',
author='Peter Us',
author_email='[email protected]',
Expand Down
15 changes: 9 additions & 6 deletions suffix_trees/STree.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,14 @@ def find_all(self, y):
if i == len(edge) and y != '':
pass
else:
return []
return {}

node = node._get_transition_link(y[0])
if not node:
return []
return {}

leaves = node._get_leaves()
return [n.idx for n in leaves]
return {n.idx for n in leaves}

def _edgeLabel(self, node, parent):
"""Helper method, returns the edge label between a node and it's parent"""
Expand Down Expand Up @@ -276,7 +276,7 @@ def _get_suffix_link(self):
def _get_transition_link(self, suffix):
return False if suffix not in self.transition_links else self.transition_links[suffix]

def _add_transition_link(self, snode, suffix=''):
def _add_transition_link(self, snode, suffix):
self.transition_links[suffix] = snode

def _has_transition(self, suffix):
Expand All @@ -291,7 +291,10 @@ def _traverse(self, f):
f(self)

def _get_leaves(self):
# Python <3.6 dicts don't perserve insertion order (and even after, we
# shouldn't rely on dicts perserving the order) therefore these can be
# out-of-order, so we return a set of leaves.
if self.is_leaf():
return [self]
return {self}
else:
return [x for n in self.transition_links.values() for x in n._get_leaves()]
return {x for n in self.transition_links.values() for x in n._get_leaves()}
2 changes: 1 addition & 1 deletion suffix_trees/test/test_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ def test_find_all_substring_true(self, data):
@given(data())
def test_find_all_substring_false(self, data):
(string, substr) = data.draw(string_and_not_substring())
assert STree.STree(string).find_all(substr) == []
assert STree.STree(string).find_all(substr) == {}
2 changes: 1 addition & 1 deletion suffix_trees/test/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def test_missing():
def test_find():
st = STree.STree("abcdefghab")
assert st.find("abc") == 0
assert st.find_all("ab") == [0, 8]
assert st.find_all("ab") == {0, 8}

0 comments on commit 934e235

Please sign in to comment.