Package it.univr.di.labeledvalue.lazy
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.
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
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
LazyWeight.SubType
-
Method Summary
Modifier and Type Method Description boolean
equals(Object o)
static String
formatLazy(double n)
LazyWeight.SubType
getType()
abstract double
getValue()
abstract org.apache.commons.math3.fraction.Fraction
getX()
int
hashCode()
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<0abstract void
setX(org.apache.commons.math3.fraction.Fraction newX)
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*)
static double
sumWithOverflowCheck(double a, double b)
Determines the sum ofa
andb
.String
toString()
-
Method Details
-
formatLazy
- Parameters:
n
- a double- Returns:
- the value of
n
as a String usingConstants.INFINITY_SYMBOL
for the infinitive andNaN
for not valid integer
-
max
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
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
Determines the sum ofa
andb
. If any of them is already INFINITY, returns INFINITY.- Parameters:
a
- an integerb
- an integer- Returns:
- the controlled sum
- Throws:
ArithmeticException
- if the sum of two inputs is greater/lesser than the maximum/minimum integer representable by aint
-
equals
-
getType
-
getValue
public abstract double getValue() -
getX
public abstract org.apache.commons.math3.fraction.Fraction getX() -
hashCode
public int hashCode() -
setX
public abstract void setX(org.apache.commons.math3.fraction.Fraction newX) -
toString
-