1 /*******************************************************************************
2 * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
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 * Contributors:
28 * CRIL - initial API and implementation
29 *******************************************************************************/
30 package org.sat4j.core;
31
32 /**
33 * Utility methods to avoid using bit manipulation inside code. One should use
34 * Java 1.5 import static feature to use it without class qualification inside
35 * the code.
36 *
37 * In the DIMACS format, the literals are represented by signed integers, 0
38 * denoting the end of the clause. In the solver, the literals are represented
39 * by positive integers, in order to use them as index in arrays for instance.
40 *
41 * <pre>
42 * int p : a literal (p>1)
43 * p ˆ 1 : the negation of the literal
44 * p >> 1 : the DIMACS number representing the variable.
45 * int v : a DIMACS variable (v>0)
46 * v << 1 : a positive literal for that variable in the solver.
47 * v << 1 ˆ 1 : a negative literal for that variable.
48 * </pre>
49 *
50 * @author leberre
51 *
52 */
53 public final class LiteralsUtils {
54
55 private LiteralsUtils() {
56 // no instance supposed to be created.
57 }
58
59 /**
60 * Returns the variable associated to the literal
61 *
62 * @param p
63 * a literal in internal representation
64 * @return the Dimacs variable associated to that literal.
65 */
66 public static int var(int p) {
67 assert p > 1;
68 return p >> 1;
69 }
70
71 /**
72 * Returns the opposite literal.
73 *
74 * @param p
75 * a literal in internal representation
76 * @return the opposite literal in internal representation
77 */
78 public static int neg(int p) {
79 return p ^ 1;
80 }
81
82 /**
83 * Returns the positive literal associated with a variable.
84 *
85 * @param var
86 * a variable in Dimacs format
87 * @return the positive literal associated with this variable in internal
88 * representation
89 */
90 public static int posLit(int var) {
91 return var << 1;
92 }
93
94 /**
95 * Returns the negative literal associated with a variable.
96 *
97 * @param var
98 * a variable in Dimacs format
99 * @return the negative literal associated with this variable in internal
100 * representation
101 */
102 public static int negLit(int var) {
103 return var << 1 ^ 1;
104 }
105
106 /**
107 * decode the internal representation of a literal in internal
108 * representation into Dimacs format.
109 *
110 * @param p
111 * the literal in internal representation
112 * @return the literal in dimacs representation
113 */
114 public static int toDimacs(int p) {
115 return ((p & 1) == 0 ? 1 : -1) * (p >> 1);
116 }
117
118 /**
119 * encode the classical Dimacs representation (negated integers for negated
120 * literals) into the internal format.
121 *
122 * @param x
123 * the literal in Dimacs format
124 * @return the literal in internal format.
125 * @since 2.2
126 */
127 public static int toInternal(int x) {
128 return x < 0 ? -x << 1 ^ 1 : x << 1;
129 }
130 }