001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
005 *  You may not use this file except in compliance with the License. You
006 *  may obtain a copy of the License at
007 *
008 *      http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.jikesrvm.compilers.opt.regalloc;
014
015import java.util.Comparator;
016
017/**
018 * Implements a set of Basic Intervals, sorted by end number.
019 * <p>
020 * This version uses container-mapping as a function in the comparator.
021 */
022class IncreasingEndMappedIntervalSet extends IntervalSet {
023  /** Support for Set serialization */
024  static final long serialVersionUID = -3121737650157210290L;
025
026  /**
027   * Imposes an ascending ordering based on the end points of basic intervals.
028   * For mapped basic intervals, the register numbers are also compared.
029   * <p>
030   * Note that this ordering would be inconsistent with equals if both objects
031   * of type {@link BasicInterval} and {@link MappedBasicInterval} were contained
032   * in the set. A comparison of a MappedBasicInterval with a BasicInterval may
033   * consider both to be the same because their begin and end are the same but
034   * the equals methods would not consider the objects as equal.
035   */
036  private static class EndComparator implements Comparator<BasicInterval> {
037    @Override
038    public int compare(BasicInterval b1, BasicInterval b2) {
039      int result = b1.getEnd() - b2.getEnd();
040      if (result == 0) {
041        result = b1.getBegin() - b2.getBegin();
042      }
043      if (result == 0) {
044        if (b1 instanceof MappedBasicInterval) {
045          if (b2 instanceof MappedBasicInterval) {
046            MappedBasicInterval mb1 = (MappedBasicInterval) b1;
047            MappedBasicInterval mb2 = (MappedBasicInterval) b2;
048            return mb1.container.getRegister().number - mb2.container.getRegister().number;
049          }
050        }
051      }
052      return result;
053    }
054  }
055
056  static final IncreasingEndMappedIntervalSet.EndComparator c = new EndComparator();
057
058  IncreasingEndMappedIntervalSet() {
059    super(c);
060  }
061}