summaryrefslogtreecommitdiffstats
path: root/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/Minterm.java
blob: 1aeb0a2be725b35849cb09f10de612c08f84fb66 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*******************************************************************************
 * Copyright (c) 2009 Elliott Baron
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Elliott Baron - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.extension;

import java.util.HashMap;
import java.util.Map;

public class Minterm<E> {
	private Map<E, Value> values;

	public enum Value {
		TRUE,
		FALSE,
		DONTCARE
	}

	public Minterm(Map<E, Value> values) {
		this.values = values;
	}

	public void setValue(E var, Value val) {
		values.put(var, val);
	}

	public Value getValue(E var) {
		return values.get(var);
	}

	public int length() {
		return values.size();
	}

	public Minterm<E> copy() {
		return new Minterm<E>(new HashMap<E, Value>(values));
	}

	public Minterm<E> combineTerms(Minterm<E> other) {
		Minterm<E> ret = null;
		E diff = null;
		for (E var : values.keySet()) {
			if (!values.get(var).equals(other.getValue(var))) {
				if (diff != null) {
					// More than one difference
					diff = null;
					break;
				}
				diff = var;
			}
		}
		
		// One difference, combine
		if (diff != null) {
			ret = copy();
			// Set the differing value to don't care
			ret.setValue(diff, Value.DONTCARE);
		}
		
		return ret;
	}
	
	public boolean implies(Minterm<E> other) {
		boolean result = true;
		for (E var : values.keySet()) {
			Value value = values.get(var);
			if (value != Value.DONTCARE && value != other.getValue(var)) {
				result = false;
			}
		}
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Minterm<?>) {
			Minterm<?> other = (Minterm<?>) obj;
			return values.equals(other.values);
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return values.hashCode();
	}
	
	@Override
	public String toString() {
		StringBuffer buf = new StringBuffer();
		for (E k : values.keySet()) {
			buf.append(k);
			buf.append(" ");
		}
		buf.append("\n");
		for (Value v : values.values()) {
			switch (v) {
			case TRUE:
				buf.append("1");
				break;
			case FALSE:
				buf.append("0");
				break;
			default:
				buf.append("-");
			}
			buf.append(" ");
		}
		return buf.toString();	
	}
}