1 /*
2  * ExclusiveList.java
3  * This file is part of JaCoP.
4  * <p>
5  * JaCoP is a Java Constraint Programming solver.
6  * <p>
7  * Copyright (C) 2000-2008 Krzysztof Kuchcinski and Radoslaw Szymanek
8  * <p>
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  * <p>
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  * <p>
19  * Notwithstanding any other provision of this License, the copyright
20  * owners of this work supplement the terms of this License with terms
21  * prohibiting misrepresentation of the origin of this work and requiring
22  * that modified versions of this work be marked in reasonable ways as
23  * different from the original version. This supplement of the license
24  * terms is in accordance with Section 7 of GNU Affero General Public
25  * License version 3.
26  * <p>
27  * You should have received a copy of the GNU Affero General Public License
28  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 package org.jacop.constraints;
32 
33 import org.jacop.core.IntVar;
34 
35 import java.util.ArrayList;
36 import java.util.List;
37 
38 /**
39  * Defines a list of exclusive items.
40  *
41  * @author Krzysztof Kuchcinski and Radoslaw Szymanek
42  * @version 4.8
43  */
44 
45 class ExclusiveList extends ArrayList<ExclusiveItem> {
46 
47     private static final long serialVersionUID = 8683452581100000004L;
48 
ExclusiveList()49     ExclusiveList() {
50         super();
51     }
52 
condition(int n, int m)53     IntVar condition(int n, int m) {
54         IntVar c = null;
55         int i = 0;
56         while (c == null && i < size()) {
57             ExclusiveItem v = get(i);
58             if (((n + 1) == v.i1 && (m + 1) == v.i2) || ((m + 1) == v.i1 && (n + 1) == v.i2))
59                 c = v.cond;
60             i++;
61         }
62         return c;
63     }
64 
fdvs(int index)65     List<? extends IntVar> fdvs(int index) {
66         List<IntVar> list = new ArrayList<IntVar>();
67         for (int i = 0; i < size(); i++) {
68             ExclusiveItem v = get(i);
69             if (index == v.i1 && !v.cond.singleton())
70                 list.add(v.cond);
71             else if (index == v.i2 && !v.cond.singleton())
72                 list.add(v.cond);
73         }
74         return list;
75     }
76 
listFor(int index)77     ExclusiveList listFor(int index) {
78         ExclusiveList list = new ExclusiveList();
79         for (int i = 0; i < size(); i++) {
80             ExclusiveItem v = get(i);
81             if (index == v.i1)
82                 list.add(v);
83             else if (index == v.i2)
84                 list.add(new ExclusiveItem(v.i2, v.i1, v.cond));
85         }
86         return list;
87     }
88 
onList(int index)89     boolean onList(int index) {
90         boolean found = false;
91         int i = 0;
92         while (!found && i < size()) {
93             ExclusiveItem v = get(i);
94             found = index == v.i1 || index == v.i2;
95             i++;
96         }
97         return found;
98     }
99 
toString()100     @Override public String toString() {
101 
102         StringBuffer result = new StringBuffer("[");
103 
104         for (int i = 0; i < this.size(); i++) {
105 
106             result.append(this.get(i).toString());
107 
108             if (i + 1 < this.size())
109                 result.append(", ");
110         }
111 
112         result.append("]");
113 
114         return result.toString();
115     }
116 
117 }
118