Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 34 additions & 47 deletions pymathics/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from mathics.core.element import BaseElement
from mathics.core.evaluation import Evaluation
from mathics.core.expression import Expression
from mathics.core.keycomparable import IMAGE_EXPRESSION_SORT_KEY
from mathics.core.pattern import pattern_objects
from mathics.core.symbols import Symbol, SymbolList, SymbolTrue
from mathics.core.systemsymbols import (
Expand All @@ -42,6 +43,7 @@
SymbolUndirectedEdge,
)

GRAPH_EXPRESSION_SORT_KEY = IMAGE_EXPRESSION_SORT_KEY + 1

WL_MARKER_TO_NETWORKX = {
"Circle": "o",
Expand Down Expand Up @@ -200,32 +202,24 @@ def _evaluate_atom(self, graph, options, compute):
def __str__(self):
return "-Graph-"

def get_sort_key(self, pattern_sort=False) -> tuple:
@property
def element_order(self) -> tuple:
"""
Returns a particular encoded list (which should be a tuple) that is used
in ``Sort[]`` comparisons and in the ordering that occurs
in an M-Expression which has the ``Orderless`` property.

See the docstring for element.get_sort_key() for more detail.
Return a value which is used in ordering elements
of an expression. The tuple is ultimately compared lexicographically.
"""

if pattern_sort:
return super(_NetworkXBuiltin, self).get_sort_key(True)
else:
# Return a sort_key tuple.
# but with a `2` instead of `1` in the 5th position,
# and adding two extra fields: the length in the 5th position,
# and a hash in the 6th place.
return [
1,
3,
self.class_head_name,
tuple(),
2,
len(self.vertices),
hash(self),
]
return hash(self)
# Return the precedence the expression `Image[]`,
# but with a `2` instead of `1` in the 5th position,
# and adding two extra fields: the length in the 3rd position,
# and a hash in the 6th place.
return (
GRAPH_EXPRESSION_SORT_KEY,
SymbolGraph,
len(self.vertices),
tuple(),
2,
hash(self),
)


class _FullGraphRewrite(Exception):
Expand Down Expand Up @@ -397,31 +391,24 @@ def is_mixed_graph(self):
def is_multigraph(self):
return isinstance(self.G, (nx.MultiDiGraph, nx.MultiGraph))

def get_sort_key(self, pattern_sort=False) -> tuple:
@property
def element_order(self) -> tuple:
"""
Returns a particular encoded list (which should be a tuple) that is used
in ``Sort[]`` comparisons and in the ordering that occurs
in an M-Expression which has the ``Orderless`` property.

See the docstring for element.get_sort_key() for more detail.
Return a value which is used in ordering elements
of an expression. The tuple is ultimately compared lexicographically.
"""

if pattern_sort:
return super(Graph, self).get_sort_key(True)
else:
# Return a sort_key tuple.
# but with a `2` instead of `1` in the 5th position,
# and adding two extra fields: the length in the 5th position,
# and a hash in the 6th place.
return [
1,
3,
self.class_head_name,
tuple(),
2,
len(self.vertices),
hash(self),
]
# Return the precedence the expression `Image[]`,
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mmatera: I adjusted this comment based on https://github.com/Mathics3/mathics-core/blob/master/mathics/builtin/image/base.py#L120-L131, but as I look at this, the comment is a little opaque.

Why do we want to change "1" to 2"?

# but with a `2` instead of `1` in the 5th position,
# and adding two extra fields: the length in the 3rd position,
# and a hash in the 6th place.
return (
GRAPH_EXPRESSION_SORT_KEY,
SymbolGraph,
len(self.vertices),
tuple(),
2,
hash(self),
)

def sort_vertices(self, vertices):
return sorted(vertices)
Expand Down
Loading