Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package crossnum;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Represents a list of variable values that must sum to some required value.
- *
- * @author dub_nerd
- *
- */
- public class Tuple {
- /**
- * List of variables.
- */
- private List<Variable> variables = new ArrayList<Variable>();
- /**
- * The required sum.
- */
- private int requiredSum;
- /**
- * Lock variables so that they are "owned" by this tuple. Anything not
- * already locked becomes locked.
- *
- * @return the number of locks obtained.
- */
- int lock() {
- int locks = 0;
- for (Variable v : variables()) {
- if (!v.isLocked()) {
- v.lock(this);
- locks++;
- }
- }
- return locks;
- }
- /**
- * Unlock variables "owned" by this tuple.
- *
- * @return the number of locks released.
- */
- int unlock() {
- int locks = 0;
- for (Variable v : variables()) {
- if (v.getLock() == this) {
- v.lock(null);
- locks++;
- }
- }
- return locks;
- }
- /**
- * Return the number of variables in tuple which are not yet locked
- * @return number unlocked
- */
- int getNumUnlocked() {
- return (int)variables.stream().filter(v -> !v.isLocked()).count();
- }
- /**
- * Get the value to which all the remaining unlocked unknowns must sum.
- * @return remainder sum
- */
- int getRemainderSum() {
- // Get the total of all our unknowns that are already locked
- int lockedTotal = variables().stream().filter(v -> v.isLocked())
- .mapToInt(v -> v.getValue()).sum();
- // Subtract from total required sum to get remainder sum
- int remainderSum = getRequiredSum() - lockedTotal;
- return remainderSum;
- }
- // Other accessor methods
- public List<Variable> variables() {
- return variables;
- }
- public int getRequiredSum() {
- return requiredSum;
- }
- public void setRequiredSum(int requiredSum) {
- this.requiredSum = requiredSum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement