| OLD | NEW |
| 1 # Copyright 2013 the V8 project authors. All rights reserved. | 1 # Copyright 2013 the V8 project authors. All rights reserved. |
| 2 # Redistribution and use in source and binary forms, with or without | 2 # Redistribution and use in source and binary forms, with or without |
| 3 # modification, are permitted provided that the following conditions are | 3 # modification, are permitted provided that the following conditions are |
| 4 # met: | 4 # met: |
| 5 # | 5 # |
| 6 # * Redistributions of source code must retain the above copyright | 6 # * Redistributions of source code must retain the above copyright |
| 7 # notice, this list of conditions and the following disclaimer. | 7 # notice, this list of conditions and the following disclaimer. |
| 8 # * Redistributions in binary form must reproduce the above | 8 # * Redistributions in binary form must reproduce the above |
| 9 # copyright notice, this list of conditions and the following | 9 # copyright notice, this list of conditions and the following |
| 10 # disclaimer in the documentation and/or other materials provided | 10 # disclaimer in the documentation and/or other materials provided |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 return isinstance(other, TransitionKey) and self.__term == other.__term | 217 return isinstance(other, TransitionKey) and self.__term == other.__term |
| 218 | 218 |
| 219 def range_iter(self, encoding): | 219 def range_iter(self, encoding): |
| 220 for c in self.__flatten(): | 220 for c in self.__flatten(): |
| 221 if c.name() == 'NUMERIC_RANGE_KEY': | 221 if c.name() == 'NUMERIC_RANGE_KEY': |
| 222 yield ('PRIMARY_RANGE', (c.args()[0], c.args()[1])) | 222 yield ('PRIMARY_RANGE', (c.args()[0], c.args()[1])) |
| 223 elif c.name() == 'NAMED_RANGE_KEY': | 223 elif c.name() == 'NAMED_RANGE_KEY': |
| 224 yield ('CLASS', c.args()[0]) | 224 yield ('CLASS', c.args()[0]) |
| 225 elif c.name() == 'TERM_KEY': | 225 elif c.name() == 'TERM_KEY': |
| 226 yield ('UNIQUE', c.args()[0]) | 226 yield ('UNIQUE', c.args()[0]) |
| 227 elif c.name() == 'OMEGA_KEY': |
| 228 yield ('OMEGA', ()) |
| 227 else: | 229 else: |
| 228 assert False, 'unimplemented %s' % c | 230 assert False, 'unimplemented %s' % c |
| 229 | 231 |
| 230 @staticmethod | 232 @staticmethod |
| 231 def __component_str(encoding, component): | 233 def __component_str(encoding, component): |
| 232 if component.name() == 'TERM_KEY': | 234 if component.name() == 'TERM_KEY': |
| 233 return component.args()[0] | 235 return component.args()[0] |
| 234 elif component.name() == 'NAMED_RANGE_KEY': | 236 elif component.name() == 'NAMED_RANGE_KEY': |
| 235 return component.args()[0] | 237 return component.args()[0] |
| 236 elif component.name() == 'EPSILON_KEY': | 238 elif component.name() == 'EPSILON_KEY': |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 if c0 > 0: | 299 if c0 > 0: |
| 298 (a, b, sign) = (b, a, 1) # normalize ordering so that a0 < b0 | 300 (a, b, sign) = (b, a, 1) # normalize ordering so that a0 < b0 |
| 299 assert a[0] < b[0] | 301 assert a[0] < b[0] |
| 300 if b[1] <= a[1]: # subset | 302 if b[1] <= a[1]: # subset |
| 301 return 3 * sign | 303 return 3 * sign |
| 302 if a[1] < b[0]: # non overlap | 304 if a[1] < b[0]: # non overlap |
| 303 return 2 * sign | 305 return 2 * sign |
| 304 return 5 * sign # partial overlap | 306 return 5 * sign # partial overlap |
| 305 | 307 |
| 306 @staticmethod | 308 @staticmethod |
| 309 def __is_composable(term): |
| 310 return term.name() != 'EPSILON_KEY' and term.name() != 'OMEGA_KEY' |
| 311 |
| 312 @staticmethod |
| 307 def __construct(encoding, components): | 313 def __construct(encoding, components): |
| 308 if isinstance(components, Term): | 314 if isinstance(components, Term): |
| 309 components = [components] | 315 components = [components] |
| 310 is_unique = False | 316 is_unique = False |
| 311 acc = [] | 317 acc = [] |
| 312 last = Term.empty_term() | 318 last = Term.empty_term() |
| 313 for current in TransitionKey.__flatten_components(components): | 319 for current in TransitionKey.__flatten_components(components): |
| 314 name = current.name() | 320 name = current.name() |
| 315 if last: | 321 # verify arguments |
| 316 assert name != 'EPSILON_KEY' and name != 'OMEGA_KEY', 'cannot merge' | |
| 317 c = TransitionKey.__component_compare(last, current) | |
| 318 assert c == -1 or c == -2, 'bad order %s %s' % (str(last), str(current)) | |
| 319 if name == 'EPSILON_KEY' or name == 'OMEGA_KEY' or name == 'TERM_KEY': | 322 if name == 'EPSILON_KEY' or name == 'OMEGA_KEY' or name == 'TERM_KEY': |
| 320 pass | 323 pass |
| 321 elif name == 'NUMERIC_RANGE_KEY': | 324 elif name == 'NUMERIC_RANGE_KEY': |
| 322 assert encoding.is_primary_range(current.args()) | 325 assert encoding.is_primary_range(current.args()) |
| 323 if last.name() == 'NUMERIC_RANGE_KEY': | 326 if last.name() == 'NUMERIC_RANGE_KEY': |
| 324 assert last.args()[1] + 1 < current.args()[0], 'ranges must be merged' | 327 assert last.args()[1] + 1 < current.args()[0], 'ranges must be merged' |
| 325 elif name == 'NAMED_RANGE_KEY': | 328 elif name == 'NAMED_RANGE_KEY': |
| 326 assert encoding.named_range(current.args()[0]) | 329 assert encoding.named_range(current.args()[0]) |
| 327 else: | 330 else: |
| 328 raise Exception('illegal component %s' % str(current)) | 331 raise Exception('illegal component %s' % str(current)) |
| 332 # verify ordering, composability |
| 333 if last: |
| 334 assert TransitionKey.__is_composable(current), 'cannot compose' |
| 335 if len(acc) == 1: |
| 336 assert TransitionKey.__is_composable(last), 'cannot compose' |
| 337 c = TransitionKey.__component_compare(last, current) |
| 338 assert c == -1 or c == -2, 'bad order %s %s' % (str(last), str(current)) |
| 329 acc.append(current) | 339 acc.append(current) |
| 330 last = current | 340 last = current |
| 331 assert acc, "must have components" | 341 assert acc, "must have components" |
| 332 return acc[0] if len(acc) == 1 else Term('COMPOSITE_KEY', *acc) | 342 return acc[0] if len(acc) == 1 else Term('COMPOSITE_KEY', *acc) |
| 333 | 343 |
| 334 def __init__(self, encoding, components): | 344 def __init__(self, encoding, components): |
| 335 self.__term = TransitionKey.__construct(encoding, components) | 345 self.__term = TransitionKey.__construct(encoding, components) |
| 336 self.__cached_hash = None | 346 self.__cached_hash = None |
| 337 | 347 |
| 338 def to_string(self, encoding): | 348 def to_string(self, encoding): |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 @staticmethod | 398 @staticmethod |
| 389 def __disjoint_components_from_keys(encoding, keys, merge_ranges = False): | 399 def __disjoint_components_from_keys(encoding, keys, merge_ranges = False): |
| 390 return TransitionKey.__disjoint_components( | 400 return TransitionKey.__disjoint_components( |
| 391 encoding, TransitionKey.__flatten_keys(keys), merge_ranges) | 401 encoding, TransitionKey.__flatten_keys(keys), merge_ranges) |
| 392 | 402 |
| 393 @staticmethod | 403 @staticmethod |
| 394 def __disjoint_components(encoding, components, merge_ranges): | 404 def __disjoint_components(encoding, components, merge_ranges): |
| 395 range_map = {} | 405 range_map = {} |
| 396 other_keys = set([]) | 406 other_keys = set([]) |
| 397 for x in components: | 407 for x in components: |
| 398 assert x.name() != 'EPSILON_KEY' and x.name() != 'OMEGA_KEY' | |
| 399 if x.name() != 'NUMERIC_RANGE_KEY': | 408 if x.name() != 'NUMERIC_RANGE_KEY': |
| 400 other_keys.add(x) | 409 other_keys.add(x) |
| 401 continue | 410 continue |
| 402 (start, end) = x.args() | 411 (start, end) = x.args() |
| 403 if not start in range_map: | 412 if not start in range_map: |
| 404 range_map[start] = [] | 413 range_map[start] = [] |
| 405 range_map[start].append(end) | 414 range_map[start].append(end) |
| 406 ranges = TransitionKey.__disjoint_keys(encoding, range_map) | 415 ranges = TransitionKey.__disjoint_keys(encoding, range_map) |
| 407 if merge_ranges: | 416 if merge_ranges: |
| 408 ranges = TransitionKey.__merge_ranges(encoding, ranges) | 417 ranges = TransitionKey.__merge_ranges(encoding, ranges) |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 yield key(last[1] + 1, r[0] - 1) | 474 yield key(last[1] + 1, r[0] - 1) |
| 466 last = r | 475 last = r |
| 467 upper_bound = encoding.primary_range()[1] | 476 upper_bound = encoding.primary_range()[1] |
| 468 if last == None: | 477 if last == None: |
| 469 r = encoding.primary_range() | 478 r = encoding.primary_range() |
| 470 yield key(r[0], r[1]) | 479 yield key(r[0], r[1]) |
| 471 elif last[1] < upper_bound: | 480 elif last[1] < upper_bound: |
| 472 yield key(last[1] + 1, upper_bound) | 481 yield key(last[1] + 1, upper_bound) |
| 473 for c in sorted(classes, TransitionKey.__component_compare): | 482 for c in sorted(classes, TransitionKey.__component_compare): |
| 474 yield c | 483 yield c |
| OLD | NEW |