001package org.cpsolver.ifs.assignment.context;
002
003import org.cpsolver.ifs.assignment.Assignment;
004import org.cpsolver.ifs.assignment.DefaultParallelAssignment;
005import org.cpsolver.ifs.model.Value;
006import org.cpsolver.ifs.model.Variable;
007
008
009/**
010 * A simple assignment context holder implementation used by the {@link DefaultParallelAssignment} class.
011 * {@link CanHoldContext} are used when possible, storing contexts in arrays, on the
012 * {@link DefaultParallelAssignment#getIndex()} position.
013 * 
014 * @see AssignmentContext
015 * @see AssignmentContextReference
016 * @see AssignmentContextHolder
017 * 
018 * @author  Tomáš Müller
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 DefaultParallelAssignmentContextHolder<V extends Variable<V, T>, T extends Value<V, T>> extends AssignmentContextHolderMap<V, T> {
040    protected int iIndex = -1;
041
042    public DefaultParallelAssignmentContextHolder(int threadIndex) {
043        iIndex = threadIndex;
044    }
045    
046    @Override
047    @SuppressWarnings("unchecked")
048    public <U extends AssignmentContext> U getAssignmentContext(Assignment<V, T> assignment, AssignmentContextReference<V, T, U> reference) {
049        if (iIndex >= 0 && iIndex < CanHoldContext.sMaxSize && reference.getParent() instanceof CanHoldContext) {
050            AssignmentContext[] contexts = ((CanHoldContext)reference.getParent()).getContext();
051            U context = (U)contexts[iIndex];
052            
053            if (context == null) {
054                context = reference.getParent().createAssignmentContext(assignment);
055                contexts[iIndex] = context;
056            }
057            
058            return context;
059        } else {
060            return super.getAssignmentContext(assignment, reference);
061        }
062    }
063    
064    @Override
065    public <C extends AssignmentContext> void clearContext(AssignmentContextReference<V, T, C> reference) {
066        if (iIndex >= 0 && iIndex < CanHoldContext.sMaxSize && reference.getParent() instanceof CanHoldContext) {
067            AssignmentContext[] contexts = ((CanHoldContext)reference.getParent()).getContext();
068            contexts[iIndex] = null;
069        } else {
070            super.clearContext(reference);
071        }
072    }
073}