-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
48 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
lphy/src/main/java/lphy/core/vectorization/VectorMatch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package lphy.core.vectorization; | ||
|
||
import lphy.core.graphicalmodel.components.*; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.List; | ||
|
||
/** | ||
* Vectorization for arrays | ||
*/ | ||
public class VectorMatch { | ||
|
||
public static int vectorMatch(List<Argument> arguments, Object[] initargs) { | ||
int vectorMatches = 0; | ||
for (int i = 0; i < arguments.size(); i++) { | ||
Argument argument = arguments.get(i); | ||
Value argValue = (Value) initargs[i]; | ||
|
||
if (argValue == null) { | ||
if (!argument.optional) return 0; | ||
} else { | ||
if (argument.type.isAssignableFrom(argValue.value().getClass())) { | ||
// direct type match | ||
} else if (argValue.value().getClass().isArray() && | ||
argument.type.isAssignableFrom(argValue.value().getClass().getComponentType())) { | ||
// vector match | ||
vectorMatches += 1; | ||
} | ||
} | ||
} | ||
return vectorMatches; | ||
} | ||
|
||
public static Generator vectorGenerator(Constructor constructor, List<Argument> arguments, Object[] vectorArgs) throws IllegalAccessException, InvocationTargetException, InstantiationException { | ||
|
||
if (GenerativeDistribution.class.isAssignableFrom(constructor.getDeclaringClass())) { | ||
return new VectorizedDistribution(constructor, arguments, vectorArgs); | ||
} else if (DeterministicFunction.class.isAssignableFrom(constructor.getDeclaringClass())) { | ||
return new VectorizedFunction(constructor, arguments, vectorArgs); | ||
} else | ||
throw new IllegalArgumentException("Unexpected Generator class! Expecting a GenerativeDistribution or a DeterministicFunction"); | ||
|
||
} | ||
} |