Skip to content

Commit 0e7fa00

Browse files
committed
Add copy constructors for graph and neighborlist
1 parent 8fd7982 commit 0e7fa00

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/main/java/info/debatty/java/graphs/Graph.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ public class Graph<T> implements Serializable {
8888
private int window_size = 0;
8989
private int current_sequence = 0;
9090

91+
/**
92+
* Copy constructor.
93+
*
94+
* @param origin
95+
*/
96+
public Graph(Graph<T> origin) {
97+
this.k = origin.k;
98+
this.window_size = origin.window_size;
99+
this.current_sequence = origin.current_sequence;
100+
this.similarity = origin.similarity;
101+
this.map = new HashMap<Node<T>, NeighborList>(origin.size());
102+
103+
for (Node node : origin.getNodes()) {
104+
this.map.put(node, new NeighborList(origin.get(node)));
105+
}
106+
}
107+
91108
/**
92109
* Initialize an empty graph, and set k (number of edges per node).
93110
* Default k is 10.

src/main/java/info/debatty/java/graphs/NeighborList.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ public static ArrayList<Edge> Convert2Edges(HashMap<Node, NeighborList> graph) {
2828
return edges;
2929
}
3030

31+
/**
32+
* Copy constructor.
33+
*
34+
* @param origin
35+
*/
36+
public NeighborList(final NeighborList origin) {
37+
super(origin.size());
38+
for (Neighbor neighbor : origin) {
39+
this.add(neighbor);
40+
}
41+
42+
}
43+
3144
/**
3245
* Create a new neighborlist of given size.
3346
* @param size size of the neighborlist

0 commit comments

Comments
 (0)