package org.apache.cassandra.utils.IntervalTree;
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */


import java.util.LinkedList;
import java.util.List;

public class IntervalTree<T>
{
    private final IntervalNode head;

    public IntervalTree()
    {
        head = null;
    }

    public IntervalTree(List<Interval> intervals)
    {
        head = new IntervalNode(intervals);
    }

    public Comparable max()
    {
        return head.v_max;
    }

    public Comparable min()
    {
        return head.v_min;
    }

    public List<T> search(Interval<T> searchInterval)
    {
        List<T> retlist = new LinkedList<T>();
        searchInternal(head, searchInterval, retlist);
        return retlist;
    }

    protected void searchInternal(IntervalNode node, Interval<T> searchInterval, List<T> retList)
    {
        if (null == head)
            return;
        if (null == node || node.v_pt == null)
            return;
        //if searchInterval.contains(node.v_pt)
        //then add every interval contained in this node to the result set then search left and right for further
        //overlapping intervals
        if (searchInterval.contains(node.v_pt))
        {
            for (Interval<T> interval : node.intersects_left)
            {
                retList.add(interval.Data);
            }

            searchInternal(node.left, searchInterval, retList);
            searchInternal(node.right, searchInterval, retList);
            return;
        }

        //if v.pt < searchInterval.left
        //add intervals in v with v[i].right >= searchInterval.left
        //L contains no overlaps
        //R May
        if (node.v_pt.compareTo(searchInterval.min) < 0)
        {
            for (Interval<T> interval : node.intersects_right)
            {
                if (interval.max.compareTo(searchInterval.min) >= 0)
                {
                    retList.add(interval.Data);
                }
                else break;
            }
            searchInternal(node.right, searchInterval, retList);
            return;
        }

        //if v.pt > searchInterval.right
        //add intervals in v with [i].left <= searchInterval.right
        //R contains no overlaps
        //L May
        if (node.v_pt.compareTo(searchInterval.max) > 0)
        {
            for (Interval<T> interval : node.intersects_left)
            {
                if (interval.min.compareTo(searchInterval.max) <= 0)
                {
                    retList.add(interval.Data);
                }
                else break;
            }
            searchInternal(node.left, searchInterval, retList);
            return;
        }
    }
}

@@ -858,16 +858,44 @@
 *%0A */%0A%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -1393,26 +1393,25 @@
 %3E re
-tlist
+sults
  = new 
-Linked
+Array
 List
@@ -1464,21 +1464,21 @@
 rval, re
-tlist
+sults
 );%0A     
@@ -1493,13 +1493,13 @@
 n re
-tlist
+sults
 ;%0A  
@@ -1594,21 +1594,21 @@
 st%3CT%3E re
-tList
+sults
 )%0A    %7B%0A
@@ -2058,37 +2058,37 @@
               re
-tList
+sults
 .add(interval.Da
@@ -2155,37 +2155,37 @@
 archInterval, re
-tList
+sults
 );%0A            s
@@ -2220,37 +2220,37 @@
 archInterval, re
-tList
+sults
 );%0A            r
@@ -2669,37 +2669,37 @@
               re
-tList
+sults
 .add(interval.Da
@@ -2812,37 +2812,37 @@
 archInterval, re
-tList
+sults
 );%0A            r
@@ -3268,21 +3268,21 @@
       re
-tList
+sults
 .add(int
@@ -3414,13 +3414,13 @@
 , re
-tList
+sults
 );%0A 

rename retList -> results and switch from Linked to ArrayList