|
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,24 @@ 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. 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 the expression `Image[]`, |
| 212 | + # but with a `2` instead of `1` in the 5th position, |
| 213 | + # and adding two extra fields: the length in the 3rd position, |
| 214 | + # and a hash in the 6th place. |
| 215 | + return ( |
| 216 | + GRAPH_EXPRESSION_SORT_KEY, |
| 217 | + SymbolGraph, |
| 218 | + len(self.vertices), |
| 219 | + tuple(), |
| 220 | + 2, |
| 221 | + hash(self), |
| 222 | + ) |
229 | 223 |
|
230 | 224 |
|
231 | 225 | class _FullGraphRewrite(Exception): |
@@ -397,31 +391,24 @@ def is_mixed_graph(self): |
397 | 391 | def is_multigraph(self): |
398 | 392 | return isinstance(self.G, (nx.MultiDiGraph, nx.MultiGraph)) |
399 | 393 |
|
400 | | - def get_sort_key(self, pattern_sort=False) -> tuple: |
| 394 | + @property |
| 395 | + def element_order(self) -> tuple: |
401 | 396 | """ |
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. |
| 397 | + Return a value which is used in ordering elements |
| 398 | + of an expression. The tuple is ultimately compared lexicographically. |
407 | 399 | """ |
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 | | - ] |
| 400 | + # Return the precedence the expression `Image[]`, |
| 401 | + # but with a `2` instead of `1` in the 5th position, |
| 402 | + # and adding two extra fields: the length in the 3rd position, |
| 403 | + # and a hash in the 6th place. |
| 404 | + return ( |
| 405 | + GRAPH_EXPRESSION_SORT_KEY, |
| 406 | + SymbolGraph, |
| 407 | + len(self.vertices), |
| 408 | + tuple(), |
| 409 | + 2, |
| 410 | + hash(self), |
| 411 | + ) |
425 | 412 |
|
426 | 413 | def sort_vertices(self, vertices): |
427 | 414 | return sorted(vertices) |
|
0 commit comments