-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrope.py
More file actions
1510 lines (1308 loc) · 43.6 KB
/
rope.py
File metadata and controls
1510 lines (1308 loc) · 43.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import py
import sys
import math
from rpython.rlib.rarithmetic import intmask, ovfcheck
from rpython.rlib.rarithmetic import r_uint, LONG_BIT
LOG2 = math.log(2)
NBITS = int(math.log(sys.maxint) / LOG2) + 2
# XXX should optimize the numbers
NEW_NODE_WHEN_LENGTH = 32
CONVERT_WHEN_SMALLER = 8
MAX_DEPTH = 32 # maybe should be smaller
CONCATENATE_WHEN_MULTIPLYING = 128
HIGHEST_BIT_SET = intmask(1L << (NBITS - 1))
def find_fib_index(l):
if l == 0:
return -1
a, b = 1, 2
i = 0
while 1:
if a <= l < b:
return i
a, b = b, a + b
i += 1
mul_by_1000003_pow_table = [intmask(pow(1000003, 2**_i, 2**LONG_BIT))
for _i in range(LONG_BIT)]
def masked_mul_by_1000003_pow(a, b):
"""Computes intmask(a * (1000003**b))."""
index = 0
b = r_uint(b)
while b:
if b & 1:
a = intmask(a * mul_by_1000003_pow_table[index])
b >>= 1
index += 1
return a
class GlobalRopeInfo(object):
"""contains data that is "global" to the whole string, e.g. requires
an iteration over the whole string"""
def __init__(self):
self.charbitmask = 0
self.hash = 0
self.is_bytestring = False
self.is_ascii = False
def combine(self, other, rightlength):
result = GlobalRopeInfo()
result.charbitmask = self.charbitmask | other.charbitmask
h1 = self.hash
h2 = other.hash
x = intmask(h2 + masked_mul_by_1000003_pow(h1, rightlength))
x |= HIGHEST_BIT_SET
result.hash = x
result.is_ascii = self.is_ascii and other.is_ascii
result.is_bytestring = self.is_bytestring and other.is_bytestring
return result
class StringNode(object):
_additional_info = None
def additional_info(self):
addinfo = self._additional_info
if addinfo is None:
return self.compute_additional_info()
return addinfo
def compute_additional_info(self):
raise NotImplementedError("base class")
def length(self):
raise NotImplementedError("base class")
def is_ascii(self):
raise NotImplementedError("base class")
def is_bytestring(self):
raise NotImplementedError("base class")
def depth(self):
return 0
def hash_part(self):
raise NotImplementedError("base class")
def charbitmask(self):
raise NotImplementedError("base class")
def check_balanced(self):
return True
def getchar(self, index):
raise NotImplementedError("abstract base class")
def getunichar(self, index):
raise NotImplementedError("abstract base class")
def getint(self, index):
raise NotImplementedError("abstract base class")
def getrope(self, index):
raise NotImplementedError("abstract base class")
def getslice(self, start, stop):
raise NotImplementedError("abstract base class")
def can_contain_int(self, value):
return True #conservative default
def view(self):
view([self])
def rebalance(self):
return self
def flatten_string(self):
raise NotImplementedError("abstract base class")
def flatten_unicode(self):
raise NotImplementedError("abstract base class")
def _concat(self, other):
raise NotImplementedError("abstract base class")
def __add__(self, other):
return concatenate(self, other)
def _cleanup_(self):
self.additional_info()
class LiteralNode(StringNode):
def find_int(self, what, start, stop):
raise NotImplementedError("abstract base class")
class LiteralStringNode(LiteralNode):
def __init__(self, s):
assert isinstance(s, str)
self.s = s
def length(self):
return len(self.s)
def is_ascii(self):
return self.additional_info().is_ascii
def is_bytestring(self):
return True
def flatten_string(self):
return self.s
def flatten_unicode(self):
return self.s.decode('latin-1')
def hash_part(self):
return self.additional_info().hash
def compute_additional_info(self):
additional_info = GlobalRopeInfo()
is_ascii = True
charbitmask = 0
partial_hash = 0
for c in self.s:
ordc = ord(c)
partial_hash = (1000003*partial_hash) + ord(c)
if ordc >= 128:
is_ascii = False
charbitmask |= intmask(1 << (ordc & 0x1F))
partial_hash = intmask(partial_hash)
partial_hash |= HIGHEST_BIT_SET
additional_info.hash = partial_hash
additional_info.is_ascii = is_ascii
additional_info.charbitmask = charbitmask
additional_info.is_bytestring = True
self._additional_info = additional_info
return additional_info
def charbitmask(self):
return self.additional_info().charbitmask
def getchar(self, index):
return self.s[index]
def getunichar(self, index):
return unichr(ord(self.s[index]))
def getint(self, index):
return ord(self.s[index])
def getrope(self, index):
return LiteralStringNode.PREBUILT[ord(self.s[index])]
def can_contain_int(self, value):
if value > 255:
return False
if self.is_ascii() and value > 127:
return False
return (1 << (value & 0x1f)) & self.charbitmask()
def getslice(self, start, stop):
assert 0 <= start <= stop
return LiteralStringNode(self.s[start:stop])
def find_int(self, what, start, stop):
if not self.can_contain_int(what):
return -1
return self.s.find(chr(what), start, stop)
def _concat(self, other):
if (isinstance(other, LiteralStringNode) and
len(other.s) + len(self.s) < NEW_NODE_WHEN_LENGTH):
return LiteralStringNode(self.s + other.s)
elif (isinstance(other, LiteralUnicodeNode) and
len(other.u) + len(self.s) < NEW_NODE_WHEN_LENGTH and
len(self.s) < CONVERT_WHEN_SMALLER):
return LiteralUnicodeNode(self.s.decode("latin-1") + other.u)
return BinaryConcatNode(self, other)
def dot(self, seen, toplevel=False):
if self in seen:
return
seen[self] = True
addinfo = str(self.s).replace('"', "'") or "_"
if len(addinfo) > 10:
addinfo = addinfo[:3] + "..." + addinfo[-3:]
yield ('"%s" [shape=box,label="length: %s\\n%s"];' % (
id(self), len(self.s),
repr(addinfo).replace('"', '').replace("\\", "\\\\")))
LiteralStringNode.EMPTY = LiteralStringNode("")
LiteralStringNode.PREBUILT = [LiteralStringNode(chr(i)) for i in range(256)]
del i
class LiteralUnicodeNode(LiteralNode):
def __init__(self, u):
assert isinstance(u, unicode)
self.u = u
def length(self):
return len(self.u)
def flatten_unicode(self):
return self.u
def is_ascii(self):
return False # usually not
def is_bytestring(self):
return False
def hash_part(self):
return self.additional_info().hash
def compute_additional_info(self):
additional_info = GlobalRopeInfo()
charbitmask = 0
partial_hash = 0
for c in self.u:
ordc = ord(c)
charbitmask |= intmask(1 << (ordc & 0x1F))
partial_hash = (1000003*partial_hash) + ordc
partial_hash = intmask(partial_hash)
partial_hash |= HIGHEST_BIT_SET
additional_info.charbitmask = intmask(charbitmask)
additional_info.hash = partial_hash
self._additional_info = additional_info
return additional_info
def charbitmask(self):
return self.additional_info().charbitmask
def getunichar(self, index):
return self.u[index]
def getint(self, index):
return ord(self.u[index])
def getrope(self, index):
ch = ord(self.u[index])
if ch < 256:
return LiteralStringNode.PREBUILT[ch]
if len(self.u) == 1:
return self
return LiteralUnicodeNode(unichr(ch))
def can_contain_int(self, value):
return (1 << (value & 0x1f)) & self.charbitmask()
def getslice(self, start, stop):
assert 0 <= start <= stop
return LiteralUnicodeNode(self.u[start:stop])
def find_int(self, what, start, stop):
if not self.can_contain_int(what):
return -1
return self.u.find(unichr(what), start, stop)
def _concat(self, other):
if (isinstance(other, LiteralUnicodeNode) and
len(other.u) + len(self.u) < NEW_NODE_WHEN_LENGTH):
return LiteralUnicodeNode(self.u + other.u)
elif (isinstance(other, LiteralStringNode) and
len(other.s) + len(self.u) < NEW_NODE_WHEN_LENGTH and
len(other.s) < CONVERT_WHEN_SMALLER):
return LiteralUnicodeNode(self.u + other.s.decode("latin-1"))
return BinaryConcatNode(self, other)
def dot(self, seen, toplevel=False):
if self in seen:
return
seen[self] = True
addinfo = repr(self.u).replace('"', "'") or "_"
if len(addinfo) > 10:
addinfo = addinfo[:3] + "..." + addinfo[-3:]
yield ('"%s" [shape=box,label="length: %s\\n%s"];' % (
id(self), len(self.u),
repr(addinfo).replace('"', '').replace("\\", "\\\\")))
def make_binary_get(getter):
def get(self, index):
while isinstance(self, BinaryConcatNode):
llen = self.left.length()
if index >= llen:
self = self.right
index -= llen
else:
self = self.left
return getattr(self, getter)(index)
return get
class BinaryConcatNode(StringNode):
def __init__(self, left, right, balanced=False):
self.left = left
self.right = right
try:
self.len = ovfcheck(left.length() + right.length())
except OverflowError:
raise
self._depth = 0
# XXX the balance should become part of the depth
self.balanced = balanced
if balanced:
self.balance_known = True
else:
self.balance_known = False
def is_ascii(self):
return self.additional_info().is_ascii
def is_bytestring(self):
return self.additional_info().is_bytestring
def check_balanced(self):
if self.balance_known:
return self.balanced
# balance calculation
# XXX improve?
if not self.left.check_balanced() or not self.right.check_balanced():
balanced = False
else:
balanced = (find_fib_index(self.len // (NEW_NODE_WHEN_LENGTH / 2)) >=
self._depth)
self.balanced = balanced
self.balance_known = True
return balanced
def length(self):
return self.len
def depth(self):
depth = self._depth
if not depth:
depth = self._depth = max(self.left.depth(),
self.right.depth()) + 1
return depth
getchar = make_binary_get("getchar")
getunichar = make_binary_get("getunichar")
getint = make_binary_get("getint")
getrope = make_binary_get("getrope")
def can_contain_int(self, value):
if self.is_bytestring() and value > 255:
return False
if self.is_ascii() and value > 127:
return False
return (1 << (value & 0x1f)) & self.charbitmask()
def getslice(self, start, stop):
if start == 0:
if stop == self.length():
return self
return getslice_left(self, stop)
if stop == self.length():
return getslice_right(self, start)
return concatenate(
getslice_right(self.left, start),
getslice_left(self.right, stop - self.left.length()))
def flatten_string(self):
f = fringe(self)
return "".join([node.flatten_string() for node in f])
def flatten_unicode(self):
f = fringe(self)
return u"".join([node.flatten_unicode() for node in f])
def hash_part(self):
return self.additional_info().hash
def compute_additional_info(self):
leftaddinfo = self.left.additional_info()
rightaddinfo = self.right.additional_info()
additional_info = leftaddinfo.combine(rightaddinfo,
self.right.length())
self._additional_info = additional_info
return additional_info
def charbitmask(self):
return self.additional_info().charbitmask
def rebalance(self):
if self.balanced:
return self
return rebalance([self], self.len)
def _concat(self, other):
if isinstance(other, LiteralNode):
r = self.right
if isinstance(r, LiteralNode):
return BinaryConcatNode(self.left,
r._concat(other))
return BinaryConcatNode(self, other)
def dot(self, seen, toplevel=False):
if self in seen:
return
seen[self] = True
if toplevel:
addition = ", fillcolor=red"
elif self.check_balanced():
addition = ", fillcolor=yellow"
else:
addition = ""
yield '"%s" [shape=octagon,label="+\\ndepth=%s, length=%s"%s];' % (
id(self), self.depth(), self.len, addition)
for child in [self.left, self.right]:
yield '"%s" -> "%s";' % (id(self), id(child))
for line in child.dot(seen):
yield line
def concatenate(node1, node2):
if node1.length() == 0:
return node2
if node2.length() == 0:
return node1
result = node1._concat(node2)
if rebalance and result.depth() > MAX_DEPTH: #XXX better check
return result.rebalance()
return result
def getslice(node, start, stop, step, slicelength=-1):
if slicelength == -1:
# XXX for testing only
slicelength = len(xrange(start, stop, step))
start, stop, node = find_straddling(node, start, stop)
if step != 1:
iter = SeekableItemIterator(node)
iter.seekforward(start)
if node.is_bytestring():
result = [iter.nextchar()]
for i in range(slicelength - 1):
iter.seekforward(step - 1)
result.append(iter.nextchar())
return rope_from_charlist(result)
else:
result = [iter.nextunichar()]
for i in range(slicelength - 1):
iter.seekforward(step - 1)
result.append(iter.nextunichar())
return rope_from_unicharlist(result)
return node.getslice(start, stop)
def getslice_one(node, start, stop):
start, stop, node = find_straddling(node, start, stop)
return node.getslice(start, stop)
def find_straddling(node, start, stop):
while 1:
if isinstance(node, BinaryConcatNode):
llen = node.left.length()
if start >= llen:
node = node.right
start = start - llen
stop = stop - llen
continue
if stop <= llen:
node = node.left
continue
return start, stop, node
def getslice_right(node, start):
while 1:
if start == 0:
return node
if isinstance(node, BinaryConcatNode):
llen = node.left.length()
if start >= llen:
node = node.right
start = start - llen
continue
else:
return concatenate(getslice_right(node.left, start),
node.right)
return node.getslice(start, node.length())
def getslice_left(node, stop):
while 1:
if stop == node.length():
return node
if isinstance(node, BinaryConcatNode):
llen = node.left.length()
if stop <= llen:
node = node.left
continue
else:
return concatenate(node.left,
getslice_left(node.right, stop - llen))
return node.getslice(0, stop)
def multiply(node, times):
if times <= 0:
return LiteralStringNode.EMPTY
if times == 1:
return node
twopower = node
number = 1
result = None
while number <= times:
if number & times:
if result is None:
result = twopower
elif result.length() < CONCATENATE_WHEN_MULTIPLYING:
result = concatenate(result, twopower)
else:
result = BinaryConcatNode(result, twopower)
try:
number = ovfcheck(number * 2)
except OverflowError:
break
if twopower.length() < CONCATENATE_WHEN_MULTIPLYING:
twopower = concatenate(twopower, twopower)
else:
twopower = BinaryConcatNode(twopower, twopower)
return result
def join(node, l):
if node.length() == 0:
return rebalance(l)
nodelist = [None] * (2 * len(l) - 1)
length = 0
for i in range(len(l)):
nodelist[2 * i] = l[i]
length += l[i].length()
for i in range(len(l) - 1):
nodelist[2 * i + 1] = node
length += (len(l) - 1) * node.length()
return rebalance(nodelist, length)
def rebalance(nodelist, sizehint=-1):
if sizehint < 0:
sizehint = 0
for node in nodelist:
sizehint += node.length()
if sizehint == 0:
return LiteralStringNode.EMPTY
nodelist.reverse()
# this code is based on the Fibonacci identity:
# sum(fib(i) for i in range(n+1)) == fib(n+2)
l = [None] * (find_fib_index(sizehint) + 2)
stack = nodelist
empty_up_to = len(l)
a = b = sys.maxint
first_node = None
while stack:
curr = stack.pop()
while isinstance(curr, BinaryConcatNode) and not curr.check_balanced():
stack.append(curr.right)
curr = curr.left
currlen = curr.length()
if currlen == 0:
continue
if currlen < a:
# we can put 'curr' to its preferred location, which is in
# the known empty part at the beginning of 'l'
a, b = 1, 2
empty_up_to = 0
while not (currlen < b):
empty_up_to += 1
a, b = b, a+b
else:
# sweep all elements up to the preferred location for 'curr'
while not (currlen < b and l[empty_up_to] is None):
if l[empty_up_to] is not None:
curr = l[empty_up_to]._concat(curr)
l[empty_up_to] = None
currlen = curr.length()
else:
empty_up_to += 1
a, b = b, a+b
if empty_up_to == len(l):
return curr
l[empty_up_to] = curr
first_node = curr
# sweep all elements
curr = first_node
for index in range(empty_up_to + 1, len(l)):
if l[index] is not None:
curr = BinaryConcatNode(l[index], curr)
assert curr is not None
return curr
# __________________________________________________________________________
# construction from normal strings
def rope_from_charlist(charlist):
nodelist = []
size = 0
for i in range(0, len(charlist), NEW_NODE_WHEN_LENGTH):
chars = charlist[i: min(len(charlist), i + NEW_NODE_WHEN_LENGTH)]
nodelist.append(LiteralStringNode("".join(chars)))
size += len(chars)
return rebalance(nodelist, size)
def rope_from_unicharlist(charlist):
nodelist = []
length = len(charlist)
if not length:
return LiteralStringNode.EMPTY
i = 0
while i < length:
unichunk = []
while i < length:
c = ord(charlist[i])
if c < 256:
break
unichunk.append(unichr(c))
i += 1
if unichunk:
nodelist.append(LiteralUnicodeNode(u"".join(unichunk)))
strchunk = []
while i < length:
c = ord(charlist[i])
if c >= 256:
break
strchunk.append(chr(c))
i += 1
if strchunk:
nodelist.append(LiteralStringNode("".join(strchunk)))
return rebalance(nodelist, length)
def rope_from_unicode(uni):
nodelist = []
length = len(uni)
if not length:
return LiteralStringNode.EMPTY
i = 0
while i < length:
start = i
while i < length:
c = ord(uni[i])
if c < 256:
break
i += 1
if i != start:
nodelist.append(LiteralUnicodeNode(uni[start:i]))
start = i
while i < length:
c = ord(uni[i])
if c >= 256:
break
i += 1
if i != start:
nodelist.append(LiteralStringNode(uni[start:i].encode("latin-1")))
return rebalance(nodelist, length)
def rope_from_unichar(unichar):
intval = ord(unichar)
if intval > 256:
return LiteralUnicodeNode(unichar)
return LiteralStringNode.PREBUILT[intval]
# __________________________________________________________________________
# searching
def find_int(node, what, start=0, stop=-1):
offset = 0
length = node.length()
if stop == -1:
stop = length
if start != 0 or stop != length:
newstart, newstop, node = find_straddling(node, start, stop)
offset = start - newstart
start = newstart
stop = newstop
assert 0 <= start <= stop
if isinstance(node, LiteralNode):
pos = node.find_int(what, start, stop)
if pos == -1:
return pos
return pos + offset
if not node.can_contain_int(what):
return -1
# invariant: stack should only contain nodes that can contain the int what
stack = [node]
i = 0
while stack:
curr = stack.pop()
while isinstance(curr, BinaryConcatNode):
if curr.left.can_contain_int(what):
if curr.right.can_contain_int(what):
stack.append(curr.right)
curr = curr.left
else:
i += curr.left.length()
# if left cannot contain what, then right must contain it
curr = curr.right
nodelength = curr.length()
fringenode = curr
if i + nodelength <= start:
i += nodelength
continue
searchstart = max(0, start - i)
searchstop = min(stop - i, nodelength)
if searchstop <= 0:
return -1
assert isinstance(fringenode, LiteralNode)
pos = fringenode.find_int(what, searchstart, searchstop)
if pos != -1:
return pos + i + offset
i += nodelength
return -1
def find(node, subnode, start=0, stop=-1):
len1 = node.length()
len2 = subnode.length()
if stop > len1 or stop == -1:
stop = len1
if stop - start < 0:
return -1
if len2 == 1:
return find_int(node, subnode.getint(0), start, stop)
if len2 == 0:
return start
if len2 > stop - start:
return -1
restart = construct_restart_positions_node(subnode)
return _find_node(node, subnode, start, stop, restart)
def _find_node(node, subnode, start, stop, restart):
len2 = subnode.length()
m = start
iter = SeekableItemIterator(node)
iter.seekforward(start)
c = iter.nextint()
i = 0
subiter = SeekableItemIterator(subnode)
d = subiter.nextint()
while m + i < stop:
if c == d:
i += 1
if i == len2:
return m
d = subiter.nextint()
if m + i < stop:
c = iter.nextint()
else:
# mismatch, go back to the last possible starting pos
if i == 0:
m += 1
if m + i < stop:
c = iter.nextint()
else:
e = restart[i - 1]
new_m = m + i - e
assert new_m <= m + i
seek = m + i - new_m
if seek:
iter.seekback(m + i - new_m)
c = iter.nextint()
m = new_m
subiter.seekback(i - e + 1)
d = subiter.nextint()
i = e
return -1
def construct_restart_positions_node(node):
length = node.length()
restart = [0] * length
restart[0] = 0
i = 1
j = 0
iter1 = ItemIterator(node)
iter1.nextint()
c1 = iter1.nextint()
iter2 = SeekableItemIterator(node)
c2 = iter2.nextint()
while 1:
if c1 == c2:
j += 1
if j < length:
c2 = iter2.nextint()
restart[i] = j
i += 1
if i < length:
c1 = iter1.nextint()
else:
break
elif j>0:
new_j = restart[j-1]
assert new_j < j
iter2.seekback(j - new_j + 1)
c2 = iter2.nextint()
j = new_j
else:
restart[i] = 0
i += 1
if i < length:
c1 = iter1.nextint()
else:
break
j = 0
iter2 = SeekableItemIterator(node)
c2 = iter2.nextint()
return restart
def view(objs):
from dotviewer import graphclient
content = ["digraph G{"]
seen = {}
for i, obj in enumerate(objs):
if obj is None:
content.append(str(i) + ";")
else:
content.extend(obj.dot(seen, toplevel=True))
content.append("}")
p = py.test.ensuretemp("automaton").join("temp.dot")
p.write("\n".join(content))
graphclient.display_dot_file(str(p))
# __________________________________________________________________________
# iteration
class FringeIterator(object):
def __init__(self, node):
self.stack = [node]
def next(self):
while self.stack:
curr = self.stack.pop()
while 1:
if isinstance(curr, BinaryConcatNode):
self.stack.append(curr.right)
curr = curr.left
else:
return curr
raise StopIteration
def _seekforward(self, length):
"""seek forward up to n characters, returning the number remaining chars.
experimental api"""
curr = None
while self.stack:
curr = self.stack.pop()
if length < curr.length():
break
length -= curr.length()
else:
raise StopIteration
while isinstance(curr, BinaryConcatNode):
left_length = curr.left.length()
if length < left_length:
self.stack.append(curr.right)
curr = curr.left
else:
length -= left_length
curr = curr.right
self.stack.append(curr)
return length
def fringe(node):
result = []
iter = FringeIterator(node)
while 1:
try:
result.append(iter.next())
except StopIteration:
return result
class ReverseFringeIterator(object):
def __init__(self, node):
self.stack = [node]
def next(self):
while self.stack:
curr = self.stack.pop()
while 1:
if isinstance(curr, BinaryConcatNode):
self.stack.append(curr.left)
curr = curr.right
else:
return curr
raise StopIteration
class ItemIterator(object):
def __init__(self, node, start=0):
self.iter = FringeIterator(node)
self.node = None
self.nodelength = 0
self.index = 0
if start:
self._advance_to(start)
def _advance_to(self, index):
self.index = self.iter._seekforward(index)
self.node = self.iter.next()
self.nodelength = self.node.length()
def getnode(self):
node = self.node
if node is None:
while 1:
node = self.node = self.iter.next()
nodelength = self.nodelength = node.length()
if nodelength != 0:
self.index = 0
return node
return node
def advance_index(self):
index = self.index
if index == self.nodelength - 1:
self.node = None
else:
self.index = index + 1
def nextchar(self):
node = self.getnode()
result = node.getchar(self.index)
self.advance_index()
return result
def nextunichar(self):
node = self.getnode()
result = node.getunichar(self.index)
self.advance_index()
return result
def nextrope(self):
node = self.getnode()
result = node.getrope(self.index)
self.advance_index()
return result
def nextint(self):
node = self.getnode()
result = node.getint(self.index)
self.advance_index()
return result
class ReverseItemIterator(object):
def __init__(self, node):
self.iter = ReverseFringeIterator(node)
self.node = None