    001package org.cpsolver.ifs.assignment;
    002
    003import java.util.ArrayList;
    004import java.util.Arrays;
    005import java.util.Collection;
    006import java.util.List;
    007
    008import org.cpsolver.ifs.assignment.context.AssignmentContextHolder;
    009import org.cpsolver.ifs.model.Value;
    010import org.cpsolver.ifs.model.Variable;
    011
    012
    013/**
    014 * An assignment using array to store values of all the variables of the model.
    015 * {@link Variable#getIndex()} is used to index the array.
    016 *
    017 * @see Assignment
    018 *
    019 * @version IFS 1.3 (Iterative Forward Search)<br>
    020 *          Copyright (C) 2014 Tomáš Müller<br>
    021 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    022 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    023 * <br>
    024 *          This library is free software; you can redistribute it and/or modify
    025 *          it under the terms of the GNU Lesser General Public License as
    026 *          published by the Free Software Foundation; either version 3 of the
    027 *          License, or (at your option) any later version. <br>
    028 * <br>
    029 *          This library is distributed in the hope that it will be useful, but
    030 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    031 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    032 *          Lesser General Public License for more details. <br>
    033 * <br>
    034 *          You should have received a copy of the GNU Lesser General Public
    035 *          License along with this library; if not see <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>.
    036 * @param <V> Variable
    037 * @param <T> Value
    038 **/
    039public class AssignmentArray<V extends Variable<V, T>, T extends Value<V, T>> extends AssignmentAbstract<V, T> {
    040    protected Object[] iAssignments = new Object[1000];
    041    protected Long[] iIteration = new Long[1000];
    042
    043    /** Creates an empty assignment
    044     * @param contexts assignment context holder
    045     **/
    046    public AssignmentArray(AssignmentContextHolder<V, T> contexts) {
    047        super(contexts);
    048    }
    049
    050    @Override
    051    public long getIteration(V variable) {
    052        try {
    053            Long it = iIteration[variable.getIndex()];
    054            return (it == null ? 0 : it);
    055        } catch (ArrayIndexOutOfBoundsException e) {
    056            return 0;
    057        }
    058    }
    059
    060    @Override
    061    @SuppressWarnings("unchecked")
    062    protected T getValueInternal(V variable) {
    063        try {
    064            return (T) iAssignments[variable.getIndex()];
    065        } catch (ArrayIndexOutOfBoundsException e) {
    066            return null;
    067        }
    068    }
    069
    070    @Override
    071    protected void setValueInternal(long iteration, V variable, T value) {
    072        if (iAssignments.length <= variable.getIndex()) {
    073            iAssignments = Arrays.copyOf(iAssignments, variable.getIndex() + 1000);
    074            iIteration = Arrays.copyOf(iIteration, variable.getIndex() + 1000);
    075        }
    076        if (value == null) {
    077            iAssignments[variable.getIndex()] = null;
    078            iIteration[variable.getIndex()] = null;
    079        } else {
    080            iAssignments[variable.getIndex()] = value;
    081            if (iteration > 0)
    082                iIteration[variable.getIndex()] = iteration;
    083        }
    084    }
    085
    086    @Override
    087    @SuppressWarnings("unchecked")
    088    public Collection<V> assignedVariables() {
    089        List<V> variables = new ArrayList<V>(iAssignments.length);
    090        for (Object value: iAssignments)
    091            if (value != null) variables.add(((T)value).variable());
    092        return variables;
    093    }
    094
    095    @Override
    096    @SuppressWarnings("unchecked")
    097    public Collection<T> assignedValues() {
    098        List<T> values = new ArrayList<T>(iAssignments.length);
    099        for (Object value: iAssignments)
    100            if (value != null) values.add((T)value);
    101        return values;
    102    }
    103
    104    @Override
    105    public int nrAssignedVariables() {
    106        int ret = 0;
    107        for (Object value: iAssignments)
    108            if (value != null) ret ++;
    109        return ret;
    110    }
    111}