/*
* Copyright (c) 2003, the JUNG Project and the Regents of the University
* of California
* All rights reserved.
*
* This software is open-source under the BSD license; see either
* "license.txt" or
* http://jung.sourceforge.net/license.txt for a description.
*/
package edu.uci.ics.jung.algorithms.util;
import java.util.Collection;
import org.apache.commons.collections15.BidiMap;
import org.apache.commons.collections15.bidimap.DualHashBidiMap;
/**
* A class providing static methods useful for improving the
* performance of graph algorithms.
*
* @author Tom Nelson
*
*/
public class Indexer {
/**
* Returns a BidiMap
mapping each element of the collection to its
* index as encountered while iterating over the collection. The purpose
* of the index operation is to supply an O(1) replacement operation for the
* O(n) indexOf(element)
method of a List
* @param
* @param collection
* @return a bidirectional map from collection elements to 0-based indices
*/
public static BidiMap create(Collection collection) {
return create(collection, 0);
}
/**
* Returns a BidiMap
mapping each element of the collection to its
* index as encountered while iterating over the collection. The purpose
* of the index operation is to supply an O(1) replacement operation for the
* O(n) indexOf(element)
method of a List
* @param
* @param collection
* @param start start index
* @return a bidirectional map from collection elements to start-based indices
*/
public static BidiMap create(Collection collection, int start) {
BidiMap map = new DualHashBidiMap();
int i=start;
for(T t : collection) {
map.put(t,i++);
}
return map;
}
}