Skip to content

Commit

Permalink
Add conflicting method to Matches object
Browse files Browse the repository at this point in the history
  • Loading branch information
Toilal committed Oct 24, 2015
1 parent edea7e3 commit d86f648
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ It has the following additional methods and properties on it.
Retrieves a list of *hole* ``Match`` objects for given range. A hole match is created for each range where no match
is available.

- ``conflicting(match, predicate=None, index=None)``

Retrieves a list of ``Match`` objects that conflicts with given match.

- ``chain_before(self, position, seps, start=0, predicate=None, index=None)``:

Retrieves a list of chained matches, before position, matching predicate and separated by characters from seps only.
Expand Down
25 changes: 24 additions & 1 deletion rebulk/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def chain_before(self, position, seps, start=0, predicate=None, index=None):

for i in reversed(range(start, position)):
index_matches = self.at_index(i)
filtered_matches = [index_match for index_match in index_matches if predicate and predicate(index_match)]
filtered_matches = [index_match for index_match in index_matches if not predicate or predicate(index_match)]
if filtered_matches:
for chain_match in filtered_matches:
if chain_match not in chain:
Expand Down Expand Up @@ -315,6 +315,29 @@ def holes(self, start=0, end=None, formatter=None, ignore=None, predicate=None,
ret[-1].end = min(rindex, end)
return filter_index(ret, predicate, index)

def conflicting(self, match, predicate=None, index=None):
"""
Retrieves a list of ``Match`` objects that conflicts with given match.
:param match:
:type match:
:param predicate:
:type predicate:
:param index:
:type index:
:return:
:rtype:
"""
ret = []

for i in range(*match.span):
ret.extend(self.starting(i))
if i != match.span[0]:
ret.extend(self.ending(i))

ret.remove(match)

return filter_index(ret, predicate, index)

def at_match(self, match, predicate=None, index=None):
"""
Retrieves a list of matches from given match.
Expand Down
8 changes: 1 addition & 7 deletions rebulk/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,8 @@ def conflict_prefer_longer(matches):
"""
to_remove_matches = IdentitySet()
for match in filter(lambda match: not match.private, matches):
conflicting_matches = IdentitySet()
conflicting_matches = matches.conflicting(match)

for i in range(*match.span):
conflicting_matches.update(matches.starting(i))
if i != match.span[0]:
conflicting_matches.update(matches.ending(i))

conflicting_matches.remove(match)
if conflicting_matches:
# keep the match only if it's the longest
for conflicting_match in filter(lambda match: not match.private, conflicting_matches):
Expand Down

0 comments on commit d86f648

Please sign in to comment.