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