1.1 --- a/iixr/files.py Tue Jan 25 01:55:43 2011 +0100
1.2 +++ b/iixr/files.py Fri Jan 28 01:36:25 2011 +0100
1.3 @@ -199,15 +199,31 @@
1.4 v_in = self.read_number()
1.5 value.append(v + v_in)
1.6 else:
1.7 - expect_delta = 1
1.8 - for v in last:
1.9 + i = 0
1.10 + n = len(last)
1.11 + value = list(last)
1.12 +
1.13 + # Traverse a copy of the last value.
1.14 +
1.15 + while i < n:
1.16 v_in = self.read_number()
1.17 - if expect_delta:
1.18 - value.append(v + v_in)
1.19 - if v_in != 0:
1.20 - expect_delta = 0
1.21 - else:
1.22 - value.append(v_in - 1)
1.23 +
1.24 + # While zeros are read, retain the last value elements.
1.25 + # Otherwise, add the delta...
1.26 +
1.27 + if v_in != 0:
1.28 + value[i] += v_in
1.29 + i += 1
1.30 +
1.31 + # Then set absolute values for the remaining elements.
1.32 +
1.33 + while i < n:
1.34 + value[i] = self.read_number() - 1
1.35 + i += 1
1.36 + break
1.37 +
1.38 + i += 1
1.39 +
1.40 return tuple(value)
1.41 else:
1.42 return last + self.read_number()
2.1 --- a/iixr/phrases.py Tue Jan 25 01:55:43 2011 +0100
2.2 +++ b/iixr/phrases.py Fri Jan 28 01:36:25 2011 +0100
2.3 @@ -4,7 +4,7 @@
2.4 Phrase iterators providing navigation over common positions for a number of
2.5 different terms.
2.6
2.7 -Copyright (C) 2009, 2010 Paul Boddie <paul@boddie.org.uk>
2.8 +Copyright (C) 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk>
2.9
2.10 This program is free software; you can redistribute it and/or modify it under
2.11 the terms of the GNU General Public License as published by the Free Software
2.12 @@ -140,45 +140,75 @@
2.13 if self.iters:
2.14 while 1:
2.15 current, first_token, next = self.iters[0]
2.16 - values = [current]
2.17 - last = current
2.18 - last_token = first_token
2.19 +
2.20 + # Only examine the sequence if the first iterator is in the right
2.21 + # position.
2.22 +
2.23 + if first_token == 0:
2.24 + values = [current]
2.25 + last = current
2.26 + last_token = first_token
2.27 +
2.28 + # Find a sequence of positions providing a phrase.
2.29 +
2.30 + expected_token = 1
2.31
2.32 - # Find a sequence of positions providing a phrase.
2.33 + for current, current_token, _next in self.iters[1:]:
2.34 +
2.35 + # If an iterator appears out of sequence, request a new
2.36 + # result from it. Likewise, if an iterator produces a
2.37 + # position equal to the last, another result is
2.38 + # required.
2.39 +
2.40 + if current_token != expected_token or \
2.41 + self.sub_positions(current, last) == 0:
2.42 +
2.43 + del self.iters[expected_token]
2.44 + self._add_next(_next, current_token)
2.45 + break
2.46
2.47 - for current, current_token, _next in self.iters[1:]:
2.48 - if not self.is_phrase_position(last, last_token, current, current_token):
2.49 - break
2.50 - values.append(current)
2.51 - last = current
2.52 - last_token = current_token
2.53 + # If the current position is more than one place after
2.54 + # the last, reset the sequence.
2.55 +
2.56 + if self.sub_positions(current, last) > 1:
2.57 + del self.iters[0]
2.58 + self._add_next(next, first_token)
2.59 + break
2.60 +
2.61 + values.append(current)
2.62 + last = current
2.63 + last_token = current_token
2.64 +
2.65 + expected_token += 1
2.66 +
2.67 + else:
2.68 + del self.iters[0]
2.69 +
2.70 + # Handle future end of iteration.
2.71 +
2.72 + try:
2.73 + self._add_next(next, first_token)
2.74 + except StopIteration:
2.75 + self.iters = []
2.76 +
2.77 + return values
2.78 +
2.79 + # Try to get a first iterator with the right position.
2.80 +
2.81 else:
2.82 del self.iters[0]
2.83 -
2.84 - # Handle future end of iteration.
2.85 -
2.86 - try:
2.87 - self._add_next(next, first_token)
2.88 - except StopIteration:
2.89 - self.iters = []
2.90 -
2.91 - return values
2.92 -
2.93 - del self.iters[0]
2.94 - self._add_next(next, first_token)
2.95 + self._add_next(next, first_token)
2.96 else:
2.97 raise StopIteration
2.98
2.99 - def is_phrase_position(self, last, last_token, current, current_token):
2.100 - if current_token <= last_token:
2.101 - return 0
2.102 + def sub_positions(self, a, b):
2.103
2.104 # NOTE: For position sequences, assume that the first value is the token
2.105 # NOTE: index/position.
2.106
2.107 - if isinstance(last, (list, tuple)):
2.108 - return current[0] - last[0] == 1
2.109 + if isinstance(a, (list, tuple)):
2.110 + return a[0] - b[0]
2.111 else:
2.112 - return current - last == 1
2.113 + return a - b
2.114
2.115 # vim: tabstop=4 expandtab shiftwidth=4