001/*
002 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.lir.alloc.lsra;
024
025/**
026 * Represents a range of integers from a start (inclusive) to an end (exclusive.
027 */
028public final class Range {
029
030    public static final Range EndMarker = new Range(Integer.MAX_VALUE, Integer.MAX_VALUE, null);
031
032    /**
033     * The start of the range, inclusive.
034     */
035    public int from;
036
037    /**
038     * The end of the range, exclusive.
039     */
040    public int to;
041
042    /**
043     * A link to allow the range to be put into a singly linked list.
044     */
045    public Range next;
046
047    boolean intersects(Range r) {
048        return intersectsAt(r) != -1;
049    }
050
051    /**
052     * Creates a new range.
053     *
054     * @param from the start of the range, inclusive
055     * @param to the end of the range, exclusive
056     * @param next link to the next range in a linked list
057     */
058    Range(int from, int to, Range next) {
059        this.from = from;
060        this.to = to;
061        this.next = next;
062    }
063
064    int intersectsAt(Range other) {
065        Range r1 = this;
066        Range r2 = other;
067
068        assert r2 != null : "null ranges not allowed";
069        assert r1 != EndMarker && r2 != EndMarker : "empty ranges not allowed";
070
071        do {
072            if (r1.from < r2.from) {
073                if (r1.to <= r2.from) {
074                    r1 = r1.next;
075                    if (r1 == EndMarker) {
076                        return -1;
077                    }
078                } else {
079                    return r2.from;
080                }
081            } else {
082                if (r2.from < r1.from) {
083                    if (r2.to <= r1.from) {
084                        r2 = r2.next;
085                        if (r2 == EndMarker) {
086                            return -1;
087                        }
088                    } else {
089                        return r1.from;
090                    }
091                } else { // r1.from() == r2.from()
092                    if (r1.from == r1.to) {
093                        r1 = r1.next;
094                        if (r1 == EndMarker) {
095                            return -1;
096                        }
097                    } else {
098                        if (r2.from == r2.to) {
099                            r2 = r2.next;
100                            if (r2 == EndMarker) {
101                                return -1;
102                            }
103                        } else {
104                            return r1.from;
105                        }
106                    }
107                }
108            }
109        } while (true);
110    }
111
112    @Override
113    public String toString() {
114        return "[" + from + ", " + to + "]";
115    }
116}