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