All Downloads are FREE. Search and download functionalities are using the official Maven repository.

soot.toolkits.scalar.ObjectIntMapper Maven / Gradle / Ivy

There is a newer version: 4.6.0
Show newest version
package soot.toolkits.scalar;

/*-
 * #%L
 * Soot - a J*va Optimization Framework
 * %%
 * Copyright (C) 2002 Florian Loitsch
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

/**
 * Gives an injection of Objects to ints. Different instances may map different ints to the same object.
 */
public class ObjectIntMapper {

  private final Vector intToObjects;
  private final Map objectToInts;
  private int counter;

  public ObjectIntMapper() {
    this.intToObjects = new Vector();
    this.objectToInts = new HashMap();
    this.counter = 0;
  }

  public ObjectIntMapper(FlowUniverse flowUniverse) {
    this(flowUniverse.iterator(), flowUniverse.size());
  }

  public ObjectIntMapper(Collection collection) {
    this(collection.iterator(), collection.size());
  }

  private ObjectIntMapper(Iterator it, int initSize) {
    this.intToObjects = new Vector(initSize);
    this.objectToInts = new HashMap(initSize);
    this.counter = 0;
    while (it.hasNext()) {
      add(it.next());
    }
  }

  /**
   * adds o into the map. no test are made, if it is already in the map.
   */
  public int add(E o) {
    objectToInts.put(o, counter);
    intToObjects.add(o);
    return counter++;
  }

  /**
   * returns the mapping of o. if there has been a call to objectToInt with the same o
   * before, the same value will be returned.
   *
   * @param o
   * @return o's mapping
   */
  public int getInt(E o) {
    Integer i = objectToInts.get(o);
    return (i != null) ? i : add(o);
  }

  /**
   * returns the object associated to i.
   *
   * @param i
   * @return i's object
   */
  public E getObject(int i) {
    return intToObjects.get(i);
  }

  /**
   * returns true, if o has already been mapped.
   *
   * @param o
   * @return true if o has already a number.
   */
  public boolean contains(Object o) {
    return objectToInts.containsKey(o);
  }

  /**
   * returns the number of mapped objects.
   */
  public int size() {
    return counter;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy