Skip to content

Commit

Permalink
Merge pull request #36 from microsoft/bi-index
Browse files Browse the repository at this point in the history
BiIndex
  • Loading branch information
tavianator committed Jul 16, 2021
2 parents f7653af + c4aaf72 commit f22eb44
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
12 changes: 6 additions & 6 deletions python/bistring/_alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import bisect
from typing import Any, Callable, Iterable, Iterator, List, Optional, Sequence, Tuple, TypeVar, Union, cast, overload

from ._typing import AnyBounds, Bounds, Index, Range
from ._typing import AnyBounds, BiIndex, Bounds, Index, Range


T = TypeVar('T')
Expand Down Expand Up @@ -77,7 +77,7 @@ class Alignment:
_original: List[int]
_modified: List[int]

def __init__(self, values: Iterable[Bounds]):
def __init__(self, values: Iterable[BiIndex]):
"""
:param values:
The sequence of aligned indices. Each element should be a tuple ``(x, y)``, where `x` is the original
Expand Down Expand Up @@ -273,7 +273,7 @@ def _infer_matrix(cls, original: Sequence[T], modified: Sequence[U], cost_fn: Co
return result

@classmethod
def _infer_recursive(cls, original: Sequence[T], modified: Sequence[U], cost_fn: CostFn[T, U]) -> List[Bounds]:
def _infer_recursive(cls, original: Sequence[T], modified: Sequence[U], cost_fn: CostFn[T, U]) -> List[BiIndex]:
"""
Hirschberg's algorithm for computing optimal alignments in linear space.
Expand Down Expand Up @@ -341,19 +341,19 @@ def infer(cls, original: Sequence[T], modified: Sequence[U], cost_fn: Optional[C
result = cls._infer_recursive(original, modified, real_cost_fn)
return Alignment(result)

def __iter__(self) -> Iterator[Tuple[int, int]]:
def __iter__(self) -> Iterator[BiIndex]:
return zip(self._original, self._modified)

def __len__(self) -> int:
return len(self._original)

@overload
def __getitem__(self, index: int) -> Bounds: ...
def __getitem__(self, index: int) -> BiIndex: ...

@overload
def __getitem__(self, index: slice) -> Alignment: ...

def __getitem__(self, index: Index) -> Union[Bounds, Alignment]:
def __getitem__(self, index: Index) -> Union[BiIndex, Alignment]:
"""
Indexing an alignment returns the nth pair of aligned positions:
Expand Down
4 changes: 2 additions & 2 deletions python/bistring/_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ._alignment import Alignment
from ._bistr import bistr, String
from ._regex import compile_regex, expand_template
from ._typing import Bounds, Regex, Replacement
from ._typing import BiIndex, Regex, Replacement


class BistrBuilder:
Expand Down Expand Up @@ -57,7 +57,7 @@ class BistrBuilder:

_original: bistr
_modified: List[str]
_alignment: List[Bounds]
_alignment: List[BiIndex]
_opos: int
_mpos: int

Expand Down
2 changes: 2 additions & 0 deletions python/bistring/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from typing import Callable, Match, Pattern, Tuple, Union


BiIndex = Tuple[int, int]

Bounds = Tuple[int, int]

AnyBounds = Union[int, range, slice, Bounds]
Expand Down
21 changes: 21 additions & 0 deletions python/tests/test_alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ def test_empty():
assert alignment.modified_bounds(0, 0) == (0, 0)


def test_indexing():
data = [
(0, 1),
(1, 2),
(2, 4),
(3, 8),
(4, 16),
]
length = len(data)
alignment = Alignment(data)

assert len(alignment) == length

for i in range(length):
assert alignment[i] == data[i]
assert alignment[i - length] == data[i - length]

for j in range(i + 1, length + 1):
assert list(alignment[i:j]) == data[i:j]


def test_identity():
alignment = Alignment.identity(1, 16)

Expand Down

0 comments on commit f22eb44

Please sign in to comment.