View Javadoc

1   /*******************************************************************************
2   * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2008 Daniel Le Berre
3   *
4   * All rights reserved. This program and the accompanying materials
5   * are made available under the terms of the Eclipse Public License v1.0
6   * which accompanies this distribution, and is available at
7   * http://www.eclipse.org/legal/epl-v10.html
8   *
9   * Alternatively, the contents of this file may be used under the terms of
10  * either the GNU Lesser General Public License Version 2.1 or later (the
11  * "LGPL"), in which case the provisions of the LGPL are applicable instead
12  * of those above. If you wish to allow use of your version of this file only
13  * under the terms of the LGPL, and not to allow others to use your version of
14  * this file under the terms of the EPL, indicate your decision by deleting
15  * the provisions above and replace them with the notice and other provisions
16  * required by the LGPL. If you do not delete the provisions above, a recipient
17  * may use your version of this file under the terms of the EPL or the LGPL.
18  * 
19  * Based on the original MiniSat specification from:
20  * 
21  * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22  * Sixth International Conference on Theory and Applications of Satisfiability
23  * Testing, LNCS 2919, pp 502-518, 2003.
24  *
25  * See www.minisat.se for the original solver in C++.
26  * 
27  *******************************************************************************/
28  package org.sat4j.minisat.constraints.cnf;
29  
30  import java.util.HashSet;
31  import java.util.Set;
32  
33  import org.sat4j.core.VecInt;
34  import org.sat4j.minisat.core.IMarkableLits;
35  import org.sat4j.specs.IVecInt;
36  
37  public class MarkableLits extends Lits implements IMarkableLits {
38  
39      /**
40       * 
41       */
42      private static final long serialVersionUID = 1L;
43  
44      private int[] marks;
45  
46      private static final int DEFAULTMARK = 1;
47  
48      @Override
49      public void init(int nvar) {
50          super.init(nvar);
51          marks = new int[(nvar << 1) + 2];
52      }
53  
54      public void setMark(int p, int mark) {
55          assert marks != null;
56          assert p > 1;
57          assert p < marks.length;
58          marks[p] = mark;
59      }
60  
61      public void setMark(int p) {
62          setMark(p, DEFAULTMARK);
63      }
64  
65      public int getMark(int p) {
66          return marks[p];
67      }
68  
69      public boolean isMarked(int p) {
70          return marks[p] != MARKLESS;
71      }
72  
73      public void resetMark(int p) {
74          marks[p] = MARKLESS;
75      }
76  
77      public void resetAllMarks() {
78          for (int i = 2; i < marks.length; i++)
79              resetMark(i);
80      }
81  
82      public IVecInt getMarkedLiterals() {
83          IVecInt marked = new VecInt();
84          for (int i = 2; i < marks.length; i++) {
85              if (isMarked(i))
86                  marked.push(i);
87          }
88          return marked;
89      }
90  
91      public IVecInt getMarkedLiterals(int mark) {
92          IVecInt marked = new VecInt();
93          for (int i = 2; i < marks.length; i++) {
94              if (getMark(i) == mark)
95                  marked.push(i);
96          }
97          return marked;
98      }
99  
100     public IVecInt getMarkedVariables() {
101         IVecInt marked = new VecInt();
102         for (int i = 2; i < marks.length; i += 2) {
103             if (isMarked(i) || isMarked(i + 1))
104                 marked.push(i >> 1);
105         }
106         return marked;
107     }
108 
109     public IVecInt getMarkedVariables(int mark) {
110         IVecInt marked = new VecInt();
111         for (int i = 2; i < marks.length; i += 2) {
112             if (getMark(i) == mark || getMark(i + 1) == mark)
113                 marked.push(i >> 1);
114         }
115         return marked;
116     }
117 
118     public Set<Integer> getMarks() {
119         Set<Integer> markers = new HashSet<Integer>();
120         for (int m : marks)
121             if (m != MARKLESS)
122                 markers.add(m);
123         return markers;
124     }
125 }