Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
structs.py
Go to the documentation of this file.
1
import
itertools
2
3
from
.compat
import
collections_abc
4
5
6
class
DirectedGraph
(object):
7
"""A graph structure with directed edges."""
8
9
def
__init__
(self):
10
self.
_vertices
= set()
11
self.
_forwards
= {}
# <key> -> Set[<key>]
12
self.
_backwards
= {}
# <key> -> Set[<key>]
13
14
def
__iter__
(self):
15
return
iter(self.
_vertices
)
16
17
def
__len__
(self):
18
return
len
(self.
_vertices
)
19
20
def
__contains__
(self, key):
21
return
key
in
self.
_vertices
22
23
def
copy(self):
24
"""Return a shallow copy of this graph."""
25
other =
DirectedGraph
()
26
other._vertices
= set(self.
_vertices
)
27
other._forwards
= {k: set(v)
for
k, v
in
self.
_forwards
.items()}
28
other._backwards
= {k: set(v)
for
k, v
in
self.
_backwards
.items()}
29
return
other
30
31
def
add
(self, key):
32
"""Add a new vertex to the graph."""
33
if
key
in
self.
_vertices
:
34
raise
ValueError(
"vertex exists"
)
35
self.
_vertices
.
add
(key)
36
self.
_forwards
[key] = set()
37
self.
_backwards
[key] = set()
38
39
def
remove
(self, key):
40
"""Remove a vertex from the graph, disconnecting all edges from/to it."""
41
self.
_vertices
.
remove
(key)
42
for
f
in
self.
_forwards
.pop(key):
43
self.
_backwards
[f].
remove
(key)
44
for
t
in
self.
_backwards
.pop(key):
45
self.
_forwards
[t].
remove
(key)
46
47
def
connected
(self, f, t):
48
return
f
in
self.
_backwards
[t]
and
t
in
self.
_forwards
[f]
49
50
def
connect
(self, f, t):
51
"""Connect two existing vertices.
52
53
Nothing happens if the vertices are already connected.
54
"""
55
if
t
not
in
self.
_vertices
:
56
raise
KeyError
(t)
57
self.
_forwards
[f].
add
(t)
58
self.
_backwards
[t].
add
(f)
59
60
def
iter_edges
(self):
61
for
f, children
in
self.
_forwards
.items():
62
for
t
in
children:
63
yield
f, t
64
65
def
iter_children
(self, key):
66
return
iter(self.
_forwards
[key])
67
68
def
iter_parents
(self, key):
69
return
iter(self.
_backwards
[key])
70
71
72
class
IteratorMapping
(
collections_abc.Mapping
):
73
def
__init__
(self, mapping, accessor, appends=None):
74
self.
_mapping
= mapping
75
self.
_accessor
= accessor
76
self.
_appends
= appends
or
{}
77
78
def
__repr__
(self):
79
return
"IteratorMapping({!r}, {!r}, {!r})"
.format(
80
self.
_mapping
,
81
self.
_accessor
,
82
self.
_appends
,
83
)
84
85
def
__bool__
(self):
86
return
bool(self.
_mapping
or
self.
_appends
)
87
88
__nonzero__ = __bool__
# XXX: Python 2.
89
90
def
__contains__
(self, key):
91
return
key
in
self.
_mapping
or
key
in
self.
_appends
92
93
def
__getitem__
(self, k):
94
try
:
95
v = self.
_mapping
[k]
96
except
KeyError:
97
return
iter(self.
_appends
[k])
98
return
itertools.chain
(self.
_accessor
(v), self.
_appends
.get(k, ()))
99
100
def
__iter__
(self):
101
more = (k
for
k
in
self.
_appends
if
k
not
in
self.
_mapping
)
102
return
itertools.chain
(self.
_mapping
, more)
103
104
def
__len__
(self):
105
more =
sum
(1
for
k
in
self.
_appends
if
k
not
in
self.
_mapping
)
106
return
len
(self.
_mapping
) + more
107
108
109
class
_FactoryIterableView
(object):
110
"""Wrap an iterator factory returned by `find_matches()`.
111
112
Calling `iter()` on this class would invoke the underlying iterator
113
factory, making it a "collection with ordering" that can be iterated
114
through multiple times, but lacks random access methods presented in
115
built-in Python sequence types.
116
"""
117
118
def
__init__
(self, factory):
119
self.
_factory
= factory
120
self.
_iterable
=
None
121
122
def
__repr__
(self):
123
return
"{}({})"
.format(type(self).__name__, list(self))
124
125
def
__bool__
(self):
126
try
:
127
next(iter(self))
128
except
StopIteration:
129
return
False
130
return
True
131
132
__nonzero__ = __bool__
# XXX: Python 2.
133
134
def
__iter__
(self):
135
iterable = (
136
self.
_factory
()
if
self.
_iterable
is
None
else
self.
_iterable
137
)
138
self.
_iterable
, current =
itertools.tee
(iterable)
139
return
current
140
141
142
class
_SequenceIterableView
(object):
143
"""Wrap an iterable returned by find_matches().
144
145
This is essentially just a proxy to the underlying sequence that provides
146
the same interface as `_FactoryIterableView`.
147
"""
148
149
def
__init__
(self, sequence):
150
self.
_sequence
= sequence
151
152
def
__repr__
(self):
153
return
"{}({})"
.format(type(self).__name__, self.
_sequence
)
154
155
def
__bool__
(self):
156
return
bool(self.
_sequence
)
157
158
__nonzero__ = __bool__
# XXX: Python 2.
159
160
def
__iter__
(self):
161
return
iter(self.
_sequence
)
162
163
164
def
build_iter_view(matches):
165
"""Build an iterable view from the value returned by `find_matches()`."""
166
if
callable(matches):
167
return
_FactoryIterableView
(matches)
168
if
not
isinstance
(matches,
collections_abc.Sequence
):
169
matches = list(matches)
170
return
_SequenceIterableView
(matches)
pip._vendor.resolvelib.structs.DirectedGraph
Definition
structs.py:6
pip._vendor.resolvelib.structs.DirectedGraph.connect
connect(self, f, t)
Definition
structs.py:50
pip._vendor.resolvelib.structs.DirectedGraph.__iter__
__iter__(self)
Definition
structs.py:14
pip._vendor.resolvelib.structs.DirectedGraph.iter_edges
iter_edges(self)
Definition
structs.py:60
pip._vendor.resolvelib.structs.DirectedGraph.remove
remove(self, key)
Definition
structs.py:39
pip._vendor.resolvelib.structs.DirectedGraph.__len__
__len__(self)
Definition
structs.py:17
pip._vendor.resolvelib.structs.DirectedGraph._vertices
_vertices
Definition
structs.py:10
pip._vendor.resolvelib.structs.DirectedGraph._backwards
_backwards
Definition
structs.py:12
pip._vendor.resolvelib.structs.DirectedGraph.connected
connected(self, f, t)
Definition
structs.py:47
pip._vendor.resolvelib.structs.DirectedGraph.__contains__
__contains__(self, key)
Definition
structs.py:20
pip._vendor.resolvelib.structs.DirectedGraph.iter_parents
iter_parents(self, key)
Definition
structs.py:68
pip._vendor.resolvelib.structs.DirectedGraph.__init__
__init__(self)
Definition
structs.py:9
pip._vendor.resolvelib.structs.DirectedGraph.add
add(self, key)
Definition
structs.py:31
pip._vendor.resolvelib.structs.DirectedGraph.iter_children
iter_children(self, key)
Definition
structs.py:65
pip._vendor.resolvelib.structs.DirectedGraph._forwards
_forwards
Definition
structs.py:11
pip._vendor.resolvelib.structs.IteratorMapping
Definition
structs.py:72
pip._vendor.resolvelib.structs.IteratorMapping.__iter__
__iter__(self)
Definition
structs.py:100
pip._vendor.resolvelib.structs.IteratorMapping._appends
_appends
Definition
structs.py:76
pip._vendor.resolvelib.structs.IteratorMapping.__repr__
__repr__(self)
Definition
structs.py:78
pip._vendor.resolvelib.structs.IteratorMapping._mapping
_mapping
Definition
structs.py:74
pip._vendor.resolvelib.structs.IteratorMapping.__len__
__len__(self)
Definition
structs.py:104
pip._vendor.resolvelib.structs.IteratorMapping.__contains__
__contains__(self, key)
Definition
structs.py:90
pip._vendor.resolvelib.structs.IteratorMapping.__bool__
__bool__(self)
Definition
structs.py:85
pip._vendor.resolvelib.structs.IteratorMapping.__getitem__
__getitem__(self, k)
Definition
structs.py:93
pip._vendor.resolvelib.structs.IteratorMapping.__init__
__init__(self, mapping, accessor, appends=None)
Definition
structs.py:73
pip._vendor.resolvelib.structs.IteratorMapping._accessor
_accessor
Definition
structs.py:75
pip._vendor.resolvelib.structs._FactoryIterableView
Definition
structs.py:109
pip._vendor.resolvelib.structs._FactoryIterableView.__iter__
__iter__(self)
Definition
structs.py:134
pip._vendor.resolvelib.structs._FactoryIterableView.__repr__
__repr__(self)
Definition
structs.py:122
pip._vendor.resolvelib.structs._FactoryIterableView._iterable
_iterable
Definition
structs.py:120
pip._vendor.resolvelib.structs._FactoryIterableView.__bool__
__bool__(self)
Definition
structs.py:125
pip._vendor.resolvelib.structs._FactoryIterableView._factory
_factory
Definition
structs.py:119
pip._vendor.resolvelib.structs._FactoryIterableView.__init__
__init__(self, factory)
Definition
structs.py:118
pip._vendor.resolvelib.structs._SequenceIterableView
Definition
structs.py:142
pip._vendor.resolvelib.structs._SequenceIterableView._sequence
_sequence
Definition
structs.py:150
pip._vendor.resolvelib.structs._SequenceIterableView.__iter__
__iter__(self)
Definition
structs.py:160
pip._vendor.resolvelib.structs._SequenceIterableView.__init__
__init__(self, sequence)
Definition
structs.py:149
pip._vendor.resolvelib.structs._SequenceIterableView.__repr__
__repr__(self)
Definition
structs.py:152
pip._vendor.resolvelib.structs._SequenceIterableView.__bool__
__bool__(self)
Definition
structs.py:155
i
for i
Definition
prime_search.m:10
venv
lib
python3.12
site-packages
pip
_vendor
resolvelib
structs.py
Generated by
1.9.8