1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 }