Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Set;
- /**
- * An implementation of {@link IntRelation} using a {@link Map} of {@link Sets}.
- *
- * @author s133269
- */
- public class IntRelationMapOfSets extends IntRelation {
- /**
- * Representation of the relation.
- */
- protected final Map<Integer, Set<Integer>> relation;
- /*
- * Representation invariants
- *
- * NotNull: relation != null
- * SetsNotNull: (\forall i; relation.containsKey(i); relation.get(i) != null),
- * where relation.containsKey(i) == 0 <= i < relation.size()
- * SetsInExtent: (\forall i; relation.containsKey(i);
- * relation.get(i) subset of [0 .. relation.size()))
- *
- * Abstraction function
- *
- * AF(this) = set of (a, b) such that relation.get(a).contains(b)
- */
- public IntRelationMapOfSets(final int n) throws IllegalArgumentException {
- super(n);
- if (n < 0) {
- throw new IllegalArgumentException(
- "IntRelationMapOfSets.constructor.pre violated:"
- + " n == " + n + " < 0");
- } else {
- relation = new HashMap<Integer, Set<Integer>>();
- for (int i = 0; i < n; i++) {
- relation.put(i, new HashSet<Integer>());
- }
- }
- }
- @Override
- public boolean isRepOk() {
- // NotNull
- if (relation == null) {
- throw new IllegalStateException("relation == null");
- }
- for (int i = 0; i < extent(); i++) {
- final Set<Integer> set = relation.get(i);
- // SetsNotNull
- if (set == null) {
- throw new IllegalStateException(
- "relation.get(" + i + ") == null");
- }
- // SetsInExtent
- for (int j = 0; j < set.size(); j++) {
- if (!(0 <= j && j < extent())) {
- throw new IllegalStateException(
- "relation.get(" + i + ") contains " + j);
- }
- }
- }
- return true;
- }
- @Override
- public int extent() {
- return relation.size();
- }
- @Override
- public boolean areRelated(int a, int b) throws IllegalArgumentException {
- if (!isValidPair(a, b)) {
- throw new IllegalArgumentException(
- "IntRelationMapOfSets.areRelated.pre violated:"
- + a + " or " + b + " or both are not"
- + " within the range [0... " + relation.size() + ")");
- }
- if (relation.containsKey(a) == false) {
- return false;
- }
- else if (relation.get(a) == null){
- return false;
- }
- return relation.get(a).contains(b);
- }
- @Override
- public void add(int a, int b) throws IllegalArgumentException {
- if (!isValidPair(a, b)) {
- throw new IllegalArgumentException(
- "IntRelationMapOfSets.add.pre violated:"
- + a + " or " + b + " or both are not"
- + " within the range [0... " + relation.size() + ")");
- } else {
- relation.get(a).add(b);
- }
- }
- @Override
- public void remove(int a, int b) throws IllegalArgumentException {
- if (!isValidPair(a, b)) {
- throw new IllegalArgumentException(
- "IntRelationMapOfSets.add.pre violated:"
- + a + " or " + b + " or both are not"
- + " within the range [0... " + relation.size() + ")");
- } else {
- relation.get(a).remove(b);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement