This project has retired. For details please refer to its Attic page.
ArrayListEdges xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.giraph.edge;
20  
21  import com.google.common.collect.Lists;
22  import org.apache.giraph.utils.Trimmable;
23  import org.apache.giraph.utils.WritableUtils;
24  import org.apache.hadoop.io.Writable;
25  import org.apache.hadoop.io.WritableComparable;
26  
27  import java.io.DataInput;
28  import java.io.DataOutput;
29  import java.io.IOException;
30  import java.util.ArrayList;
31  import java.util.Iterator;
32  
33  /**
34   * {@link OutEdges} implementation backed by an {@link ArrayList}.
35   * Parallel edges are allowed.
36   *
37   * @param <I> Vertex id
38   * @param <E> Edge value
39   */
40  public class ArrayListEdges<I extends WritableComparable, E extends Writable>
41      extends ConfigurableOutEdges<I, E>
42      implements MutableOutEdges<I, E>, Trimmable {
43    /** List of edges. */
44    private ArrayList<Edge<I, E>> edgeList;
45  
46    @Override
47    public void initialize(Iterable<Edge<I, E>> edges) {
48      // If the iterable is actually an instance of ArrayList,
49      // we simply copy the reference.
50      // Otherwise we have to add every edge.
51      if (edges instanceof ArrayList) {
52        edgeList = (ArrayList<Edge<I, E>>) edges;
53      } else {
54        edgeList = Lists.newArrayList(edges);
55      }
56    }
57  
58    @Override
59    public void initialize(int capacity) {
60      edgeList = Lists.newArrayListWithCapacity(capacity);
61    }
62  
63    @Override
64    public void initialize() {
65      edgeList = Lists.newArrayList();
66    }
67  
68    @Override
69    public void add(Edge<I, E> edge) {
70      edgeList.add(edge);
71    }
72  
73    @Override
74    public void remove(I targetVertexId) {
75      for (Iterator<Edge<I, E>> edges = edgeList.iterator(); edges.hasNext();) {
76        Edge<I, E> edge = edges.next();
77        if (edge.getTargetVertexId().equals(targetVertexId)) {
78          edges.remove();
79        }
80      }
81    }
82  
83    @Override
84    public int size() {
85      return edgeList.size();
86    }
87  
88    @Override
89    public final Iterator<Edge<I, E>> iterator() {
90      return edgeList.iterator();
91    }
92  
93    @Override
94    @SuppressWarnings("unchecked")
95    public Iterator<MutableEdge<I, E>> mutableIterator() {
96      // The downcast is fine because all concrete Edge implementations are
97      // mutable, but we only expose the mutation functionality when appropriate.
98      return (Iterator) iterator();
99    }
100 
101   @Override
102   public void write(DataOutput out) throws IOException {
103     out.writeInt(edgeList.size());
104     for (Edge<I, E> edge : edgeList) {
105       edge.getTargetVertexId().write(out);
106       edge.getValue().write(out);
107     }
108   }
109 
110   @Override
111   public void readFields(DataInput in) throws IOException {
112     int numEdges = in.readInt();
113     initialize(numEdges);
114     for (int i = 0; i < numEdges; ++i) {
115       Edge<I, E> edge = getConf().createEdge();
116       WritableUtils.readEdge(in, edge);
117       edgeList.add(edge);
118     }
119   }
120 
121   @Override
122   public void trim() {
123     edgeList.trimToSize();
124   }
125 }