1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.edge; |
20 | |
|
21 | |
import it.unimi.dsi.fastutil.longs.LongIterator; |
22 | |
import it.unimi.dsi.fastutil.longs.LongOpenHashSet; |
23 | |
|
24 | |
import org.apache.giraph.utils.EdgeIterables; |
25 | |
import org.apache.giraph.utils.Trimmable; |
26 | |
import org.apache.hadoop.io.LongWritable; |
27 | |
import org.apache.hadoop.io.NullWritable; |
28 | |
|
29 | |
import java.io.DataInput; |
30 | |
import java.io.DataOutput; |
31 | |
import java.io.IOException; |
32 | |
import java.util.Iterator; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class LongNullHashSetEdges |
43 | |
implements ReuseObjectsOutEdges<LongWritable, NullWritable>, |
44 | |
MutableOutEdges<LongWritable, NullWritable>, |
45 | |
StrictRandomAccessOutEdges<LongWritable, NullWritable>, |
46 | |
Trimmable { |
47 | |
|
48 | |
private LongOpenHashSet neighbors; |
49 | |
|
50 | |
@Override |
51 | |
public void initialize(Iterable<Edge<LongWritable, NullWritable>> edges) { |
52 | 0 | EdgeIterables.initialize(this, edges); |
53 | 0 | } |
54 | |
|
55 | |
@Override |
56 | |
public void initialize(int capacity) { |
57 | 0 | neighbors = new LongOpenHashSet(capacity); |
58 | 0 | } |
59 | |
|
60 | |
@Override |
61 | |
public void initialize() { |
62 | 0 | neighbors = new LongOpenHashSet(); |
63 | 0 | } |
64 | |
|
65 | |
@Override |
66 | |
public void add(Edge<LongWritable, NullWritable> edge) { |
67 | 0 | neighbors.add(edge.getTargetVertexId().get()); |
68 | 0 | } |
69 | |
|
70 | |
@Override |
71 | |
public void remove(LongWritable targetVertexId) { |
72 | 0 | neighbors.remove(targetVertexId.get()); |
73 | 0 | } |
74 | |
|
75 | |
@Override |
76 | |
public int size() { |
77 | 0 | return neighbors.size(); |
78 | |
} |
79 | |
|
80 | |
@Override |
81 | |
public Iterator<Edge<LongWritable, NullWritable>> iterator() { |
82 | |
|
83 | |
|
84 | |
|
85 | 0 | return (Iterator) mutableIterator(); |
86 | |
} |
87 | |
|
88 | |
@Override |
89 | |
public Iterator<MutableEdge<LongWritable, NullWritable>> mutableIterator() { |
90 | 0 | return new Iterator<MutableEdge<LongWritable, NullWritable>>() { |
91 | |
|
92 | 0 | private LongIterator neighborsIt = neighbors.iterator(); |
93 | |
|
94 | 0 | private ReusableEdge<LongWritable, NullWritable> representativeEdge = |
95 | 0 | EdgeFactory.createReusable(new LongWritable()); |
96 | |
|
97 | |
public boolean hasNext() { |
98 | 0 | return neighborsIt.hasNext(); |
99 | |
} |
100 | |
|
101 | |
@Override |
102 | |
public MutableEdge<LongWritable, NullWritable> next() { |
103 | 0 | representativeEdge.getTargetVertexId().set(neighborsIt.nextLong()); |
104 | 0 | return representativeEdge; |
105 | |
} |
106 | |
|
107 | |
@Override |
108 | |
public void remove() { |
109 | 0 | neighborsIt.remove(); |
110 | 0 | } |
111 | |
}; |
112 | |
} |
113 | |
|
114 | |
@Override |
115 | |
public void write(DataOutput out) throws IOException { |
116 | 0 | out.writeInt(neighbors.size()); |
117 | 0 | LongIterator neighborsIt = neighbors.iterator(); |
118 | 0 | while (neighborsIt.hasNext()) { |
119 | 0 | out.writeLong(neighborsIt.nextLong()); |
120 | |
} |
121 | 0 | } |
122 | |
|
123 | |
@Override |
124 | |
public void readFields(DataInput in) throws IOException { |
125 | 0 | int numEdges = in.readInt(); |
126 | 0 | initialize(numEdges); |
127 | 0 | for (int i = 0; i < numEdges; ++i) { |
128 | 0 | neighbors.add(in.readLong()); |
129 | |
} |
130 | 0 | } |
131 | |
|
132 | |
@Override |
133 | |
public NullWritable getEdgeValue(LongWritable targetVertexId) { |
134 | 0 | if (neighbors.contains(targetVertexId.get())) { |
135 | 0 | return NullWritable.get(); |
136 | |
} else { |
137 | 0 | return null; |
138 | |
} |
139 | |
} |
140 | |
|
141 | |
@Override |
142 | |
public void setEdgeValue(LongWritable targetVertexId, |
143 | |
NullWritable edgeValue) { |
144 | |
|
145 | |
|
146 | |
|
147 | 0 | } |
148 | |
|
149 | |
@Override |
150 | |
public void trim() { |
151 | 0 | neighbors.trim(); |
152 | 0 | } |
153 | |
} |