    001package org.cpsolver.ifs.assignment.context;
    002
    003import java.util.HashMap;
    004import java.util.Map;
    005
    006import org.cpsolver.ifs.assignment.Assignment;
    007import org.cpsolver.ifs.model.Value;
    008import org.cpsolver.ifs.model.Variable;
    009
    010
    011/**
    012 * A simple assignment context holder implementation using {@link HashMap} to store all the assignment contexts.
    013 * {@link AssignmentContextReference#getIndex()} are used to index the map.
    014 *
    015 * @see AssignmentContext
    016 * @see AssignmentContextReference
    017 *
    018 * @version IFS 1.3 (Iterative Forward Search)<br>
    019 *          Copyright (C) 2014 Tomáš Müller<br>
    020 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    021 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    022 * <br>
    023 *          This library is free software; you can redistribute it and/or modify
    024 *          it under the terms of the GNU Lesser General Public License as
    025 *          published by the Free Software Foundation; either version 3 of the
    026 *          License, or (at your option) any later version. <br>
    027 * <br>
    028 *          This library is distributed in the hope that it will be useful, but
    029 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    030 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    031 *          Lesser General Public License for more details. <br>
    032 * <br>
    033 *          You should have received a copy of the GNU Lesser General Public
    034 *          License along with this library; if not see <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>.
    035 * @param <V> Variable
    036 * @param <T> Value
    037 **/
    038public class AssignmentContextHolderMap<V extends Variable<V, T>, T extends Value<V, T>> implements AssignmentContextHolder<V, T> {
    039    protected Map<Integer,AssignmentContext> iContexts = new HashMap<Integer, AssignmentContext>();
    040
    041    public AssignmentContextHolderMap() {
    042    }
    043
    044    @Override
    045    @SuppressWarnings("unchecked")
    046    public <U extends AssignmentContext> U getAssignmentContext(Assignment<V, T> assignment, AssignmentContextReference<V, T, U> reference) {
    047        U context = (U) iContexts.get(reference.getIndex());
    048        if (context != null) return context;
    049
    050        context = reference.getParent().createAssignmentContext(assignment);
    051        iContexts.put(reference.getIndex(), context);
    052        return context;
    053    }
    054
    055    @Override
    056    public <C extends AssignmentContext> void clearContext(AssignmentContextReference<V, T, C> reference) {
    057        iContexts.remove(reference.getIndex());
    058    }
    059
    060}