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