/** * (C) Copyright IBM Corp. 2010, 2015 * * Licensed 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. *  */ package com.ibm.bi.dml.lops.compile; import java.util.Comparator; import com.ibm.bi.dml.lops.Lop; /** * * Comparator class used in sorting the LopDAG in topological order. Refer to * doTopologicalSort_strict_order() in dml/lops/compile/Dag.java * * Topological sort guarantees the following: * * 1) All lops with level i appear before any lop with level greater than i * (source nodes are at level 0) * * 2) Within a given level, nodes are ordered by their ID i.e., by the other in * which they are created * * compare() method is designed to respect the above two requirements. * * @param <N> */ public class LopComparator<N extends Lop> implements Comparator<N> { @Override public int compare(N o1, N o2) { if (o1.getLevel() < o2.getLevel()) return -1; // o1 is less than o2 else if (o1.getLevel() > o2.getLevel()) return 1; // o1 is greater than o2 else { if (o1.getID() < o2.getID()) return -1; // o1 is less than o2 else if (o1.getID() > o2.getID()) return 1; // o1 is greater than o2 else throw new RuntimeException("Unexpected error: ID's of two lops are same."); } } }