Merge "Bug 2347: DOMConcurrentDataCommitCoordinator uses wrong phase name"
[controller.git] / third-party / net.sf.jung2 / src / main / java / edu / uci / ics / jung / algorithms / util / Indexer.java
1 /*
2 * Copyright (c) 2003, the JUNG Project and the Regents of the University 
3 * of California
4 * All rights reserved.
5 *
6 * This software is open-source under the BSD license; see either
7 * "license.txt" or
8 * http://jung.sourceforge.net/license.txt for a description.
9 */
10 package edu.uci.ics.jung.algorithms.util;
11
12 import java.util.Collection;
13
14 import org.apache.commons.collections15.BidiMap;
15 import org.apache.commons.collections15.bidimap.DualHashBidiMap;
16
17 /**
18  * A class providing static methods useful for improving the
19  * performance of graph algorithms.
20  * 
21  * @author Tom Nelson
22  *
23  */
24 public class Indexer {
25         
26         /**
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>
31          * @param <T>
32          * @param collection
33          * @return a bidirectional map from collection elements to 0-based indices
34          */
35         public static <T> BidiMap<T,Integer> create(Collection<T> collection) {
36             return create(collection, 0);
37         }
38         /**
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>
43          * @param <T>
44          * @param collection
45          * @param start start index
46          * @return a bidirectional map from collection elements to start-based indices
47          */
48         public static <T> BidiMap<T,Integer> create(Collection<T> collection, int start) {
49                 BidiMap<T,Integer> map = new DualHashBidiMap<T,Integer>();
50                 int i=start;
51                 for(T t : collection) {
52                         map.put(t,i++);
53                 }
54                 return map;
55         }
56 }