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.ints.IntArrayList; |
22 | |
import it.unimi.dsi.fastutil.ints.IntIterator; |
23 | |
|
24 | |
import java.io.DataInput; |
25 | |
import java.io.DataOutput; |
26 | |
import java.io.IOException; |
27 | |
import java.util.Iterator; |
28 | |
|
29 | |
import org.apache.giraph.utils.EdgeIterables; |
30 | |
import org.apache.giraph.utils.Trimmable; |
31 | |
import org.apache.hadoop.io.IntWritable; |
32 | |
import org.apache.hadoop.io.NullWritable; |
33 | |
|
34 | |
import com.google.common.collect.UnmodifiableIterator; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class IntNullArrayEdges |
44 | |
implements ReuseObjectsOutEdges<IntWritable, NullWritable>, Trimmable { |
45 | |
|
46 | |
private IntArrayList neighbors; |
47 | |
|
48 | |
@Override |
49 | |
public void initialize(Iterable<Edge<IntWritable, NullWritable>> edges) { |
50 | 0 | EdgeIterables.initialize(this, edges); |
51 | 0 | } |
52 | |
|
53 | |
@Override |
54 | |
public void initialize(int capacity) { |
55 | 0 | neighbors = new IntArrayList(capacity); |
56 | 0 | } |
57 | |
|
58 | |
@Override |
59 | |
public void initialize() { |
60 | 0 | neighbors = new IntArrayList(); |
61 | 0 | } |
62 | |
|
63 | |
@Override |
64 | |
public int size() { |
65 | 0 | return neighbors.size(); |
66 | |
} |
67 | |
|
68 | |
@Override |
69 | |
public void add(Edge<IntWritable, NullWritable> edge) { |
70 | 0 | neighbors.add(edge.getTargetVertexId().get()); |
71 | 0 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
private void removeAt(int i) { |
79 | |
|
80 | |
|
81 | |
|
82 | 0 | if (i == neighbors.size() - 1) { |
83 | 0 | neighbors.popInt(); |
84 | |
} else { |
85 | 0 | neighbors.set(i, neighbors.popInt()); |
86 | |
} |
87 | 0 | } |
88 | |
|
89 | |
@Override |
90 | |
public void remove(IntWritable targetVertexId) { |
91 | |
|
92 | |
|
93 | 0 | for (int i = neighbors.size() - 1; i >= 0; --i) { |
94 | 0 | if (neighbors.getInt(i) == targetVertexId.get()) { |
95 | 0 | removeAt(i); |
96 | |
} |
97 | |
} |
98 | 0 | } |
99 | |
|
100 | |
@Override |
101 | |
public Iterator<Edge<IntWritable, NullWritable>> iterator() { |
102 | |
|
103 | 0 | return new UnmodifiableIterator<Edge<IntWritable, NullWritable>>() { |
104 | |
|
105 | 0 | private final IntIterator neighborsIt = neighbors.iterator(); |
106 | |
|
107 | 0 | private final Edge<IntWritable, NullWritable> representativeEdge = |
108 | 0 | EdgeFactory.create(new IntWritable(), NullWritable.get()); |
109 | |
|
110 | |
@Override |
111 | |
public boolean hasNext() { |
112 | 0 | return neighborsIt.hasNext(); |
113 | |
} |
114 | |
|
115 | |
@Override |
116 | |
public Edge<IntWritable, NullWritable> next() { |
117 | 0 | representativeEdge.getTargetVertexId().set(neighborsIt.nextInt()); |
118 | 0 | return representativeEdge; |
119 | |
} |
120 | |
}; |
121 | |
} |
122 | |
|
123 | |
@Override |
124 | |
public void write(DataOutput out) throws IOException { |
125 | 0 | out.writeInt(neighbors.size()); |
126 | 0 | IntIterator iterator = neighbors.iterator(); |
127 | 0 | while (iterator.hasNext()) { |
128 | 0 | out.writeInt(iterator.nextInt()); |
129 | |
} |
130 | 0 | } |
131 | |
|
132 | |
@Override |
133 | |
public void readFields(DataInput in) throws IOException { |
134 | 0 | int numEdges = in.readInt(); |
135 | 0 | initialize(numEdges); |
136 | 0 | for (int i = 0; i < numEdges; ++i) { |
137 | 0 | neighbors.add(in.readInt()); |
138 | |
} |
139 | 0 | } |
140 | |
|
141 | |
@Override |
142 | |
public void trim() { |
143 | 0 | neighbors.trim(); |
144 | 0 | } |
145 | |
} |