Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions src/main/java/BitSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.lang.String;
import java.util.Objects;

public class BitSet<T> {

private int size;
private ArrayList<T> arr = new ArrayList<T>();

public BitSet(int size) {
setSize(size);
}

public BitSet(int size, T a, T b, T c) {
setSize(size);
arr.add(a);
arr.add(b);
arr.add(c);
}

public BitSet(int size, T a) {
setSize(size);
arr.add(a);
}

public BitSet(int size, T a, T b, T c, T d, T e) {
setSize(size);
arr.add(a);
arr.add(b);
arr.add(c);
arr.add(d);
arr.add(e);
}

public void setSize(int size) {
this.size = size;
}

public int getSize() {
return size;
}

public void addElement(T el) {
if (!arr.contains(el) && arr.size() < size) {
arr.add(el);
} else if (arr.size() >= size) {
throw new IndexOutOfBoundsException();
}
}

public void addElement(T[] el) {
for (int i = 0; i < el.length; i++) {
if (!arr.contains(el[i]) && arr.size() < size) {
arr.add(el[i]);
} else if (arr.size() >= size) {
throw new IndexOutOfBoundsException();
}
}
}

public void delete(T el) {
arr.remove(el);
setSize(this.size - 1);
}

public void delete(T[] el) {
for (int i = 0; i < el.length; i++) {
arr.remove(el[i]);
}
setSize(this.size - el.length);
}

@Override
public String toString() {
StringBuilder str = new StringBuilder();
for (int i = 0; i < arr.size(); i++) {
str.append(arr.get(i) + " ");
}
return str.toString().trim();
}

public T get(int index) {
return arr.get(index);
}

public boolean contain(T el) {
return arr.contains(el);
}

public BitSet<T> intersection(BitSet<T> other) {
BitSet<T> intersections = new BitSet<T>(this.arr.size() + other.arr.size());

for (int i = 0; i < size; i++) {
if (other.arr.contains(this.get(i))) {
intersections.arr.add(this.get(i));
}
}
intersections.setSize(intersections.arr.size());
return intersections;
}

public BitSet<T> association(BitSet<T> other) {
BitSet<T> associations = new BitSet<T>(this.arr.size() + other.arr.size());
associations.arr = this.arr;
for (int i = 0; i < other.arr.size(); i++) {
associations.addElement(other.get(i));
}
associations.setSize(associations.arr.size());
return associations;
}

public BitSet<T> addition(BitSet<T> other) {
BitSet<T> additions = new BitSet<T>(this.arr.size() + other.arr.size());
for (int i = 0; i < size; i++) {
if (!other.arr.contains(this.get(i))) {
additions.arr.add(this.get(i));
}
}
additions.setSize(additions.arr.size());
return additions;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BitSet<?> bitSet = (BitSet<?>) o;
return size == bitSet.size && Objects.equals(arr, bitSet.arr);
}

@Override
public int hashCode() {
return Objects.hash(size, arr);
}
}
120 changes: 120 additions & 0 deletions src/test/java/BitSetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class BitSetTest {
@Test
public void testAddElement() {
BitSet<Integer> n = new BitSet<Integer>(3);
BitSet<Integer> expected = new BitSet<>(3, 10, 28, 101);
n.addElement(10);
n.addElement(28);
n.addElement(101);
assertEquals(expected, n);
}

@Test
public void testAddArrayElement() {
BitSet<Integer> n = new BitSet<Integer>(5);
BitSet<Integer> expected = new BitSet<>(5, 10, 28, 101,23,96);
Integer[] mas = new Integer[]{10, 23, 96};
n.addElement(10);
n.addElement(28);
n.addElement(101);
n.addElement(mas);
assertEquals(expected, n);
}

@Test
public void testDelete() {
BitSet<Integer> n = new BitSet<Integer>(4);
BitSet<Integer> expected = new BitSet<>(3, 10, 28, 56);
n.addElement(10);
n.addElement(28);
n.addElement(101);
n.addElement(56);
n.delete(101);
assertEquals(expected, n);
}

@Test
public void testDeleteArrayElement() {
BitSet<Integer> n = new BitSet<Integer>(6);
BitSet<Integer> expected = new BitSet<>(3, 28, 101,99);
Integer[] mas = new Integer[]{10, 23, 96};
n.addElement(10);
n.addElement(28);
n.addElement(101);
n.addElement(99);
n.addElement(mas);
n.delete(mas);
assertEquals(expected, n);
}

@Test
public void testGet() {
BitSet<Integer> n = new BitSet<Integer>(3);
n.addElement(10);
n.addElement(28);
n.addElement(101);
Integer expected = 28;
assertEquals(expected, n.get(1));
}

@Test
public void testContain() {
BitSet<Integer> n = new BitSet<Integer>(3);
n.addElement(10);
n.addElement(28);
n.addElement(101);
boolean expected = true;
boolean expected2 = false;
assertEquals(expected, n.contain(101));
assertEquals(expected2, n.contain(1010));
}

@Test
public void testIntersection() {
BitSet<Integer> otherN = new BitSet<Integer>(2);
BitSet<Integer> expected = new BitSet<Integer>(1, 10);
otherN.addElement(10);
otherN.addElement(36);

BitSet<Integer> n1 = new BitSet<Integer>(3);
n1.addElement(10);
n1.addElement(45);
n1.addElement(96);

assertEquals(expected, otherN.intersection(n1));
}

@Test
public void testAssociation() {
BitSet<String> otherN = new BitSet<String>(2);
BitSet<String> expected = new BitSet<>(3, "10","36","45");
otherN.addElement("10");
otherN.addElement("36");

BitSet<String> n1 = new BitSet<String>(2);
n1.addElement("10");
n1.addElement("45");

assertEquals(expected, otherN.association(n1));
}

@Test
public void testAddition() {
BitSet<String> otherN = new BitSet<String>(2);
BitSet<String> expected = new BitSet<>(1, "36");
otherN.addElement("10");
otherN.addElement("36");

BitSet<String> n1 = new BitSet<String>(3);
n1.addElement("10");
n1.addElement("45");
n1.addElement("96");

assertEquals(expected, otherN.addition(n1));
}

}