Repository navigation
Expand file tree
/
Copy pathWithNodeReferences.scala
More file actions
164 lines (140 loc) · 6.51 KB
/
Copy pathWithNodeReferences.scala
File metadata and controls
164 lines (140 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package scalax.collection.io.jsoniter.nonlabeled
import scala.collection.immutable.ArraySeq
import scala.reflect.ClassTag
import com.github.plokhotnyuk.jsoniter_scala.macros.{CodecMakerConfig, JsonCodecMaker}
import scalax.collection.generic.{AnyDiEdge, AnyDiHyperEdge, AnyHyperEdge, AnyUnDiEdge}
import scalax.collection.{OneOrMore, Several}
/** ADT for JSON codecs of non-labeled edges where edge ends are represented by node IDs.
*/
sealed trait WithNodeReferences[Id <: AnyVal | String]:
def edgeT: String
sealed trait AnyHyperEdgeWithNodeReferences[Id <: AnyVal | String] extends WithNodeReferences[Id]
sealed trait AnyDiHyperEdgeWithNodeReferences[Id <: AnyVal | String] extends AnyHyperEdgeWithNodeReferences[Id]
sealed trait AnyEdgeWithNodeReferences[Id <: AnyVal | String] extends AnyHyperEdgeWithNodeReferences[Id]:
def id1: Id
def id2: Id
object AnyHyperEdgeWithNodeReferences:
inline def compactClassNames: CodecMakerConfig =
CodecMakerConfig.withAdtLeafClassNameMapper { className =>
JsonCodecMaker.simpleClassName(className) match
case "HyperEdgeWithNodeReferences" => "HyperR"
case "DiHyperEdgeWithNodeReferences" => "DiHyperR"
case "UnDiEdgeWithNodeReferences" => "UnDiR"
case "DiEdgeWithNodeReferences" => "DiR"
case x => throw new IllegalArgumentException(s"Unexpected className $x")
}
object AnyDiHyperEdgeWithNodeReferences:
inline def compactClassNames: CodecMakerConfig =
CodecMakerConfig.withAdtLeafClassNameMapper { className =>
JsonCodecMaker.simpleClassName(className) match
case "DiHyperEdgeWithNodeReferences" => "DiHyperR"
case "DiEdgeWithNodeReferences" => "DiR"
case x => throw new IllegalArgumentException(s"Unexpected className $x")
}
object AnyEdgeWithNodeReferences:
def unapply[Id <: AnyVal | String](e: AnyEdgeWithNodeReferences[Id]): (String, Id, Id) =
(e.edgeT, e.id1, e.id2)
inline def compactClassNames: CodecMakerConfig =
CodecMakerConfig.withAdtLeafClassNameMapper { className =>
JsonCodecMaker.simpleClassName(className) match
case "UnDiEdgeWithNodeReferences" => "UnDiR"
case "DiEdgeWithNodeReferences" => "DiR"
case x => throw new IllegalArgumentException(s"Unexpected className $x")
}
case class DiEdgeWithNodeReferences[Id <: AnyVal | String](edgeT: String, sourceId: Id, targetId: Id)
extends AnyEdgeWithNodeReferences[Id]
with AnyDiHyperEdgeWithNodeReferences[Id]:
def id1: Id = sourceId
def id2: Id = targetId
object DiEdgeWithNodeReferences:
def apply[N, Id <: AnyVal | String](edge: AnyDiEdge[N], id: N => Id): DiEdgeWithNodeReferences[Id] =
apply(edge, id, edge.getClass.getSimpleName)
def apply[N, Id <: AnyVal | String](edge: AnyDiEdge[N], id: N => Id, edgeT: String): DiEdgeWithNodeReferences[Id] =
new DiEdgeWithNodeReferences[Id](
edgeT,
id(edge.source),
id(edge.target)
)
import scalax.collection.edges.DiEdge
def diEdgeFactory[N]: PartialFunction[(String, N, N), DiEdge[N]] =
case ("DiEdge", source, target) => DiEdge(source, target)
case class UnDiEdgeWithNodeReferences[Id <: AnyVal | String](edgeT: String, id1: Id, id2: Id)
extends AnyEdgeWithNodeReferences[Id]
object UnDiEdgeWithNodeReferences:
def apply[N, Id <: AnyVal | String](edge: AnyUnDiEdge[N], id: N => Id): UnDiEdgeWithNodeReferences[Id] =
apply(edge, id, edge.getClass.getSimpleName)
def apply[N, Id <: AnyVal | String](
edge: AnyUnDiEdge[N],
id: N => Id,
edgeT: String
): UnDiEdgeWithNodeReferences[Id] =
new UnDiEdgeWithNodeReferences[Id](
edgeT,
id(edge.node1),
id(edge.node2)
)
import scalax.collection.edges.UnDiEdge
def unDiEdgeFactory[N]: PartialFunction[(String, N, N), UnDiEdge[N]] = { case ("UnDiEdge", source, target) =>
UnDiEdge(source, target)
}
case class DiHyperEdgeWithNodeReferences[Id <: AnyVal | String](
edgeT: String,
sourceIds: Iterable[Id],
targetIds: Iterable[Id]
) extends AnyDiHyperEdgeWithNodeReferences[Id]
object DiHyperEdgeWithNodeReferences:
def apply[N, Id <: AnyVal | String: ClassTag](
edge: AnyDiHyperEdge[N],
id: N => Id
): AnyDiHyperEdgeWithNodeReferences[Id] =
apply(edge, id, edge.getClass.getSimpleName)
def apply[N, Id <: AnyVal | String: ClassTag](
edge: AnyDiHyperEdge[N],
id: N => Id,
edgeT: String
): AnyDiHyperEdgeWithNodeReferences[Id] =
edge match
case e: AnyDiEdge[N] => DiEdgeWithNodeReferences(e, id, edgeT)
case h: AnyDiHyperEdge[N] =>
def toIterator(ends: OneOrMore[N]) = ArraySeq.from(ends.iterator map id)
new DiHyperEdgeWithNodeReferences[Id](
edgeT,
toIterator(edge.sources),
toIterator(edge.targets)
)
import scalax.collection.hyperedges.{ordered, DiHyperEdge}
def diHyperEdgeFactory[N]: PartialFunction[(String, OneOrMore[N], OneOrMore[N]), DiHyperEdge[N]] = {
case ("DiHyperEdge", sources, targets) =>
DiHyperEdge(sources, targets)
}
def orderedDiHyperEdgeFactory[N]: PartialFunction[(String, OneOrMore[N], OneOrMore[N]), ordered.DiHyperEdge[N]] = {
case ("ordered.DiHyperEdge", sources, targets) =>
ordered.DiHyperEdge(sources, targets)
}
case class HyperEdgeWithNodeReferences[Id <: AnyVal | String](edgeT: String, endIds: Iterable[Id])
extends AnyHyperEdgeWithNodeReferences[Id]
object HyperEdgeWithNodeReferences:
def apply[N, Id <: AnyVal | String: ClassTag](
edge: AnyHyperEdge[N],
id: N => Id
): AnyHyperEdgeWithNodeReferences[Id] =
apply(edge, id, edge.getClass.getSimpleName)
def apply[N, Id <: AnyVal | String: ClassTag](
edge: AnyHyperEdge[N],
id: N => Id,
edgeT: String
): AnyHyperEdgeWithNodeReferences[Id] =
edge match
case e: AnyDiEdge[N] => DiEdgeWithNodeReferences(e, id, edgeT)
case e: AnyUnDiEdge[N] => UnDiEdgeWithNodeReferences(e, id, edgeT)
case h: AnyDiHyperEdge[N] => DiHyperEdgeWithNodeReferences(h, id, edgeT)
case h: AnyHyperEdge[N] =>
new HyperEdgeWithNodeReferences[Id](edgeT, ArraySeq.from(edge.ends.iterator map id))
import scalax.collection.hyperedges.{ordered, HyperEdge}
def hyperEdgeFactory[N]: PartialFunction[(String, Several[N]), HyperEdge[N]] = { case ("HyperEdge", ends) =>
HyperEdge(ends)
}
def orderedHyperEdgeFactory[N]: PartialFunction[(String, Several[N]), ordered.HyperEdge[N]] = {
case ("ordered.HyperEdge", ends) =>
ordered.HyperEdge(ends)
}