| 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 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 s = self.__find_partition(partitions, s.node_number()) | 236 s = self.__find_partition(partitions, s.node_number()) |
| 237 if first: | 237 if first: |
| 238 first = False | 238 first = False |
| 239 mapped_partition = s | 239 mapped_partition = s |
| 240 assert mapped_partition == s | 240 assert mapped_partition == s |
| 241 | 241 |
| 242 @staticmethod | 242 @staticmethod |
| 243 def __partition_count(partitions): | 243 def __partition_count(partitions): |
| 244 return len(reduce(lambda acc, p: acc | p.set(), partitions, set())) | 244 return len(reduce(lambda acc, p: acc | p.set(), partitions, set())) |
| 245 | 245 |
| 246 def __merge_partitions(self, id_map, partitions): |
| 247 mapping = {} |
| 248 name_map = {} |
| 249 for partition in partitions: |
| 250 name_map[partition] = str(partition) |
| 251 alphabet = self.__generate_alphabet() |
| 252 for partition in partitions: |
| 253 state_id = iter(partition).next() |
| 254 state = id_map[state_id] |
| 255 node = { |
| 256 'transitions' : {}, |
| 257 'terminal' : state in self.__dfa.terminal_set(), |
| 258 'action' : state.action(), |
| 259 } |
| 260 mapping[str(partition)] = node |
| 261 for key in alphabet: |
| 262 transition_state = state.key_matches(key) |
| 263 if not transition_state: |
| 264 continue |
| 265 transition_id = transition_state.node_number() |
| 266 transition_partition = self.__find_partition(partitions, transition_id) |
| 267 assert transition_partition |
| 268 node['transitions'][key] = name_map[transition_partition] |
| 269 start_id = self.__dfa.start_state().node_number() |
| 270 start_name = name_map[self.__find_partition(partitions, start_id)] |
| 271 return (start_name, mapping) |
| 272 |
| 246 def minimize(self): | 273 def minimize(self): |
| 247 (id_map, partitions) = self.__partition() | 274 (id_map, partitions) = self.__partition() |
| 248 node_count = self.__dfa.node_count() | 275 node_count = self.__dfa.node_count() |
| 249 assert self.__partition_count(partitions) == node_count | 276 assert self.__partition_count(partitions) == node_count |
| 250 if len(partitions) == 1: | 277 if len(partitions) == 1: |
| 251 return self.__dfa | 278 return self.__dfa |
| 252 working_set = set(partitions) | 279 working_set = set(partitions) |
| 253 alphabet = self.__generate_alphabet() | 280 alphabet = self.__generate_alphabet() |
| 254 all_state_ids = set(id_map.keys()) | 281 all_state_ids = set(id_map.keys()) |
| 255 while working_set: | 282 while working_set: |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 working_set.add(intersection) | 314 working_set.add(intersection) |
| 288 working_set.add(difference) | 315 working_set.add(difference) |
| 289 elif len(intersection) <= len(difference): | 316 elif len(intersection) <= len(difference): |
| 290 working_set.add(intersection) | 317 working_set.add(intersection) |
| 291 else: | 318 else: |
| 292 working_set.add(difference) | 319 working_set.add(difference) |
| 293 partitions = new_partitions | 320 partitions = new_partitions |
| 294 self.__verify_partitions(id_map, partitions) | 321 self.__verify_partitions(id_map, partitions) |
| 295 if len(partitions) == len(id_map): | 322 if len(partitions) == len(id_map): |
| 296 return self.__dfa | 323 return self.__dfa |
| 297 # merge partitions | 324 (start_name, mapping) = self.__merge_partitions(id_map, partitions) |
| 325 return Dfa(start_name, mapping) |
| OLD | NEW |