|
| 1 | +import collections |
| 2 | +import types |
| 3 | +import sys |
| 4 | +import math |
| 5 | +import numbers |
| 6 | + |
| 7 | +from python_toolbox import misc_tools |
| 8 | +from python_toolbox import binary_search |
| 9 | +from python_toolbox import dict_tools |
| 10 | +from python_toolbox import nifty_collections |
| 11 | +from python_toolbox import caching |
| 12 | + |
| 13 | +from layout_rabbit import shy_math_tools |
| 14 | +from layout_rabbit import shy_sequence_tools |
| 15 | +from layout_rabbit import shy_cute_iter_tools |
| 16 | +from layout_rabbit import shy_nifty_collections |
| 17 | + |
| 18 | +from . import misc |
| 19 | +from layout_rabbit import shy_misc_tools |
| 20 | + |
| 21 | +infinity = float('inf') |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +class ChainSpace(shy_sequence_tools.CuteSequenceMixin, collections.Sequence): |
| 26 | + ''' |
| 27 | + A space of sequences chained together. |
| 28 | + |
| 29 | + This is similar to `itertools.chain`, except that items can be fetched by |
| 30 | + index number rather than just iteration. |
| 31 | + ''' |
| 32 | + def __init__(self, sequences): |
| 33 | + |
| 34 | + self.sequences = nifty_collections.LazyTuple( |
| 35 | + (shy_sequence_tools.ensure_iterable_is_immutable_sequence( |
| 36 | + sequence, default_type=nifty_collections.LazyTuple) |
| 37 | + for sequence in sequences) |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | + @caching.CachedProperty |
| 42 | + @nifty_collections.LazyTuple.factory() |
| 43 | + def accumulated_lengths(self): |
| 44 | + ''' |
| 45 | + A sequence of the accumulated length as every sequence is added. |
| 46 | + |
| 47 | + For example, if this chain space has sequences with lengths of 10, 100 |
| 48 | + and 1000, this would be `[0, 10, 110, 1110]`. |
| 49 | + ''' |
| 50 | + total = 0 |
| 51 | + yield 0 |
| 52 | + for sequence in self.sequences: |
| 53 | + total += shy_sequence_tools.get_length(sequence) |
| 54 | + yield total |
| 55 | + |
| 56 | + |
| 57 | + length = caching.CachedProperty(lambda self: self.accumulated_lengths[-1]) |
| 58 | + |
| 59 | + |
| 60 | + def __repr__(self): |
| 61 | + return '<%s: %s>' % ( |
| 62 | + type(self).__name__, |
| 63 | + '+'.join(str(len(sequence)) for sequence in sequences), |
| 64 | + ) |
| 65 | + |
| 66 | + def __getitem__(self, i): |
| 67 | + if isinstance(i, slice): |
| 68 | + raise NotImplementedError |
| 69 | + assert isinstance(i, int) |
| 70 | + sequence_index = binary_search.binary_search_by_index( |
| 71 | + self.accumulated_lengths, lambda x: x, |
| 72 | + i, rounding=binary_search.LOW_IF_BOTH |
| 73 | + ) |
| 74 | + if sequence_index is None: |
| 75 | + raise IndexError |
| 76 | + sequence_start = self.accumulated_lengths[sequence_index] |
| 77 | + return self.sequences[sequence_index][i - sequence_start] |
| 78 | + |
| 79 | + |
| 80 | + def __iter__(self): |
| 81 | + for sequence in self.sequences: |
| 82 | + # yield from sequence Commenting for fucking Pypy |
| 83 | + for i in sequence: yield i |
| 84 | + |
| 85 | + _reduced = property(lambda self: (type(self), self.sequences)) |
| 86 | + |
| 87 | + __eq__ = lambda self, other: (isinstance(other, ChainSpace) and |
| 88 | + self._reduced == other._reduced) |
| 89 | + |
| 90 | + def __contains__(self, item): |
| 91 | + return any(item in sequence for sequence in self.sequences |
| 92 | + if (not isinstance(sequence, str) or isinstance(item, str))) |
| 93 | + |
| 94 | + def index(self, item): |
| 95 | + for sequence, accumulated_length in zip(self.sequences, |
| 96 | + self.accumulated_lengths): |
| 97 | + try: |
| 98 | + return sequence.index(item) + accumulated_length |
| 99 | + except IndexError: |
| 100 | + pass |
| 101 | + else: |
| 102 | + raise IndexError |
| 103 | + |
| 104 | + def __bool__(self): |
| 105 | + try: |
| 106 | + next(iter(self)) |
| 107 | + except StopIteration: |
| 108 | + return False |
| 109 | + else: |
| 110 | + return True |
| 111 | + |
| 112 | + |
| 113 | + |
0 commit comments