|
21 | 21 | from mathics.core.element import BaseElement |
22 | 22 | from mathics.core.evaluation import Evaluation |
23 | 23 | from mathics.core.expression import Expression |
| 24 | +from mathics.core.keycomparable import IMAGE_EXPRESSION_SORT_KEY |
24 | 25 | from mathics.core.pattern import pattern_objects |
25 | 26 | from mathics.core.symbols import Symbol, SymbolList, SymbolTrue |
26 | 27 | from mathics.core.systemsymbols import ( |
|
42 | 43 | SymbolUndirectedEdge, |
43 | 44 | ) |
44 | 45 |
|
| 46 | +GRAPH_EXPRESSION_SORT_KEY = IMAGE_EXPRESSION_SORT_KEY + 1 |
45 | 47 |
|
46 | 48 | WL_MARKER_TO_NETWORKX = { |
47 | 49 | "Circle": "o", |
@@ -200,32 +202,28 @@ def _evaluate_atom(self, graph, options, compute): |
200 | 202 | def __str__(self): |
201 | 203 | return "-Graph-" |
202 | 204 |
|
203 | | - def get_sort_key(self, pattern_sort=False) -> tuple: |
| 205 | + @property |
| 206 | + def element_order(self) -> tuple: |
204 | 207 | """ |
205 | | - Returns a particular encoded list (which should be a tuple) that is used |
206 | | - in ``Sort[]`` comparisons and in the ordering that occurs |
207 | | - in an M-Expression which has the ``Orderless`` property. |
208 | | -
|
209 | | - See the docstring for element.get_sort_key() for more detail. |
| 208 | + Return a value which is used in ordering elements |
| 209 | + of an expression and Sort[]. The tuple is ultimately compared lexicographically. |
210 | 210 | """ |
211 | | - |
212 | | - if pattern_sort: |
213 | | - return super(_NetworkXBuiltin, self).get_sort_key(True) |
214 | | - else: |
215 | | - # Return a sort_key tuple. |
216 | | - # but with a `2` instead of `1` in the 5th position, |
217 | | - # and adding two extra fields: the length in the 5th position, |
218 | | - # and a hash in the 6th place. |
219 | | - return [ |
220 | | - 1, |
221 | | - 3, |
222 | | - self.class_head_name, |
223 | | - tuple(), |
224 | | - 2, |
225 | | - len(self.vertices), |
226 | | - hash(self), |
227 | | - ] |
228 | | - return hash(self) |
| 211 | + # Return the precedence similar to the key for an `Image[]` |
| 212 | + # object, but with a `2` instead of `1` in the 5th position, |
| 213 | + # |
| 214 | + # Graphs with fewer vertices in a graph should appear before |
| 215 | + # nodes with more vertices. This property is captured by the |
| 216 | + # 3rd position, of the order tuple. A hash of the object is put |
| 217 | + # last for two graphs are structurally the same but different, so that |
| 218 | + # the same graph object will appear consecutively in a Sort[]. |
| 219 | + return ( |
| 220 | + GRAPH_EXPRESSION_SORT_KEY, |
| 221 | + SymbolGraph, |
| 222 | + len(self.vertices), |
| 223 | + tuple(), |
| 224 | + 2, |
| 225 | + hash(self), |
| 226 | + ) |
229 | 227 |
|
230 | 228 |
|
231 | 229 | class _FullGraphRewrite(Exception): |
@@ -397,31 +395,28 @@ def is_mixed_graph(self): |
397 | 395 | def is_multigraph(self): |
398 | 396 | return isinstance(self.G, (nx.MultiDiGraph, nx.MultiGraph)) |
399 | 397 |
|
400 | | - def get_sort_key(self, pattern_sort=False) -> tuple: |
| 398 | + @property |
| 399 | + def element_order(self) -> tuple: |
401 | 400 | """ |
402 | | - Returns a particular encoded list (which should be a tuple) that is used |
403 | | - in ``Sort[]`` comparisons and in the ordering that occurs |
404 | | - in an M-Expression which has the ``Orderless`` property. |
405 | | -
|
406 | | - See the docstring for element.get_sort_key() for more detail. |
| 401 | + Return a value which is used in ordering elements |
| 402 | + of an expression and Sort[]. The tuple is ultimately compared lexicographically. |
407 | 403 | """ |
408 | | - |
409 | | - if pattern_sort: |
410 | | - return super(Graph, self).get_sort_key(True) |
411 | | - else: |
412 | | - # Return a sort_key tuple. |
413 | | - # but with a `2` instead of `1` in the 5th position, |
414 | | - # and adding two extra fields: the length in the 5th position, |
415 | | - # and a hash in the 6th place. |
416 | | - return [ |
417 | | - 1, |
418 | | - 3, |
419 | | - self.class_head_name, |
420 | | - tuple(), |
421 | | - 2, |
422 | | - len(self.vertices), |
423 | | - hash(self), |
424 | | - ] |
| 404 | + # Return the precedence similar to the key for an `Image[]` |
| 405 | + # object, but with a `2` instead of `1` in the 5th position, |
| 406 | + # |
| 407 | + # Graphs with fewer vertices in a graph should appear before |
| 408 | + # nodes with more vertices. This property is captured by the |
| 409 | + # 3rd position, of the order tuple. A hash of the object is put |
| 410 | + # last for two graphs are structurally the same but different, so that |
| 411 | + # the same graph object will appear consecutively in a Sort[]. |
| 412 | + return ( |
| 413 | + GRAPH_EXPRESSION_SORT_KEY, |
| 414 | + SymbolGraph, |
| 415 | + len(self.vertices), |
| 416 | + tuple(), |
| 417 | + 2, |
| 418 | + hash(self), |
| 419 | + ) |
425 | 420 |
|
426 | 421 | def sort_vertices(self, vertices): |
427 | 422 | return sorted(vertices) |
|
0 commit comments