Class LazyWeight

java.lang.Object
it.univr.di.labeledvalue.lazy.LazyWeight
Direct Known Subclasses:
LazyCombiner, LazyNumber, LazyPiece

public abstract class LazyWeight
extends Object
Represents the leftist peace of a piece-wise function (PWF) set present in an edge. This entity should be the top of an hierarchy of 4 different types of PWF. Best practice of object programming would require to write down such hierarchy and to implement all methods exploiting method overriding.
Although, there is a big issue: the two main methods (sum and max) have different type of behavior according to the two different type of operands (i.e., sum works in one way if both operands are of A type, in another if both are of B type, but completely different if one is A and the other is B. LazyWeight has 4 different subtypes).
In other words, while hierarchy like Figure<-Circle Figure<-Rectangular with method 'area' are perfect example of hierarchy to implement, this class is different and, I think, requires a more efficient implementation that avoid overload for resolving all possible combinations of types.
For this reason, the class is organized as an hierarchy but only for the member. The two methods sum and max are implemented as static member on the top class, where all mixes of operands are managed exploiting a tag that allows a fast subtype identification. This class and its subclasses MUST BE considered IMMUTABLE.
Author:
posenato
  • Method Details

    • formatLazy

      public static final String formatLazy​(double n)
      Parameters:
      n - a double
      Returns:
      the value of n as a String using Constants.INFINITY_SYMBOL for the infinitive and NaN for not valid integer
    • max

      public static LazyWeight max​(LazyWeight op1, LazyWeight op2, Label finalLabel)
       P? --op1,alpha--> T <--op2,beta------------------- Y
                                         <--max(op1,op2), finalLabel---
         
       finalLabel := alpha * beta
       Condition for applying: op1<0
       
      Parameters:
      op1 -
      op2 -
      finalLabel -
      Returns:
      the max between op1 and op2
    • sum

      public static LazyWeight sum​(LazyWeight op1, LazyWeight op2, Label sumLabel)
      X --op1,alpha--> Y -op2,beta--> T -------op1+op2,sumLabel---> sumLabel := alpha * beta Condition for applying: op1+op2<0 and (op1<0 or sumLabel in P*)
      Parameters:
      op1 -
      op2 -
      sumLabel - must be determined in extended (with unknowns) way.
      Returns:
      the sum of op1 and op2
    • sumWithOverflowCheck

      public static final double sumWithOverflowCheck​(double a, double b) throws ArithmeticException
      Determines the sum of a and b. If any of them is already INFINITY, returns INFINITY.
      Parameters:
      a - an integer
      b - an integer
      Returns:
      the controlled sum
      Throws:
      ArithmeticException - if the sum of two inputs is greater/lesser than the maximum/minimum integer representable by a int
    • equals

      public boolean equals​(Object o)
      Overrides:
      equals in class Object
    • getType

      public LazyWeight.SubType getType()
    • getValue

      public abstract double getValue()
    • getX

      public abstract org.apache.commons.math3.fraction.Fraction getX()
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • setX

      public abstract void setX​(org.apache.commons.math3.fraction.Fraction newX)
    • toString

      public String toString()
      Overrides:
      toString in class Object