2 * Copyright (c) 2003, the JUNG Project and the Regents of the University
6 * This software is open-source under the BSD license; see either
8 * http://jung.sourceforge.net/license.txt for a description.
10 package edu.uci.ics.jung.algorithms.util;
12 import java.util.Collection;
14 import org.apache.commons.collections15.BidiMap;
15 import org.apache.commons.collections15.bidimap.DualHashBidiMap;
18 * A class providing static methods useful for improving the
19 * performance of graph algorithms.
24 public class Indexer {
27 * Returns a <code>BidiMap</code> mapping each element of the collection to its
28 * index as encountered while iterating over the collection. The purpose
29 * of the index operation is to supply an O(1) replacement operation for the
30 * O(n) <code>indexOf(element)</code> method of a <code>List</code>
33 * @return a bidirectional map from collection elements to 0-based indices
35 public static <T> BidiMap<T,Integer> create(Collection<T> collection) {
36 return create(collection, 0);
39 * Returns a <code>BidiMap</code> mapping each element of the collection to its
40 * index as encountered while iterating over the collection. The purpose
41 * of the index operation is to supply an O(1) replacement operation for the
42 * O(n) <code>indexOf(element)</code> method of a <code>List</code>
45 * @param start start index
46 * @return a bidirectional map from collection elements to start-based indices
48 public static <T> BidiMap<T,Integer> create(Collection<T> collection, int start) {
49 BidiMap<T,Integer> map = new DualHashBidiMap<T,Integer>();
51 for(T t : collection) {