uebung09
This commit is contained in:
80
uebung09/src/collection/BigSet.java
Normal file
80
uebung09/src/collection/BigSet.java
Normal file
@@ -0,0 +1,80 @@
|
||||
package collection;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
class BigSet<E> implements Set<E> {
|
||||
|
||||
private E first;
|
||||
private Set<E> rest;
|
||||
|
||||
public BigSet(E first, Set<E> rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return new Iterator<E>() {
|
||||
|
||||
private int cnt;
|
||||
private Iterator<E> restIt;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (cnt == 0) return true;
|
||||
if (restIt == null) restIt = rest.iterator();
|
||||
return restIt.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (cnt++ == 0) return first;
|
||||
if (restIt == null) restIt = rest.iterator();
|
||||
return restIt.next();
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return rest.size() + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object el) {
|
||||
return first.equals(el) || rest.contains(el);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<E> union(Set<E> other) {
|
||||
return rest.union(other).add(first);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<E> add(E element) {
|
||||
if (contains(element)) return this;
|
||||
return new BigSet<E>(element, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<E> intersection(Set<E> other) {
|
||||
final Set<E> set = rest.intersection(other);
|
||||
if (!other.contains(first))
|
||||
return set;
|
||||
else if (set.isEmpty())
|
||||
return SetFactory.create(first);
|
||||
return new BigSet<>(first, set);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean subsetOf(Set<?> other) {
|
||||
return other.contains(first) && rest.subsetOf(other);
|
||||
}
|
||||
}
|
||||
61
uebung09/src/collection/EmptySet.java
Normal file
61
uebung09/src/collection/EmptySet.java
Normal file
@@ -0,0 +1,61 @@
|
||||
package collection;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
class EmptySet<E> implements Set<E> {
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return new Iterator<E>() {
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object el) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set union(Set other) {
|
||||
return other;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set add(Object element) {
|
||||
return SetFactory.create(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set intersection(Set other) {
|
||||
return new EmptySet();
|
||||
//return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean subsetOf(Set other) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,8 +11,7 @@ public class SetFactory {
|
||||
* @return the empty set.
|
||||
*/
|
||||
static <T> Set<T> create() {
|
||||
// TODO implement
|
||||
return null;
|
||||
return new EmptySet<T>();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -23,8 +22,7 @@ public class SetFactory {
|
||||
* @return the singleton set containing the specified element.
|
||||
*/
|
||||
static <T> Set<T> create(T element) {
|
||||
// TODO implement
|
||||
return null;
|
||||
return new SingeltonSet<T>(element);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,7 +33,14 @@ public class SetFactory {
|
||||
* @return a set containing the specified elements.
|
||||
*/
|
||||
static <T> Set<T> create(T... elems) {
|
||||
// TODO implement
|
||||
return null;
|
||||
if (elems.length == 0) return create();
|
||||
if (elems.length == 1) return create(elems[0]);
|
||||
Set<T> result = create();
|
||||
|
||||
for (T t : elems) {
|
||||
result = result.add(t);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
77
uebung09/src/collection/SingeltonSet.java
Normal file
77
uebung09/src/collection/SingeltonSet.java
Normal file
@@ -0,0 +1,77 @@
|
||||
package collection;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
class SingeltonSet<E> implements Set<E> {
|
||||
|
||||
private final E elem;
|
||||
|
||||
public SingeltonSet(E elem) {
|
||||
this.elem = elem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator iterator() {
|
||||
return new Iterator<E>() {
|
||||
|
||||
boolean returned = true;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return returned;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if(!hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
returned = false;
|
||||
return elem;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object el) {
|
||||
return elem.equals(el);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set union(Set other) {
|
||||
return other.add(elem);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set add(Object element) {
|
||||
if (elem.equals(element)) {
|
||||
return this;
|
||||
}
|
||||
|
||||
return new BigSet(element, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set intersection(Set other) {
|
||||
return other.contains(elem) ? this : SetFactory.create();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean subsetOf(Set other) {
|
||||
return other.contains(elem);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user