163 lines
4.6 KiB
Java
163 lines
4.6 KiB
Java
import java.util.Comparator;
|
|
|
|
class Item {
|
|
String value;
|
|
int count;
|
|
|
|
Item(String value) {
|
|
this.value = value;
|
|
this.count = 1;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return value + "/" + count;
|
|
}
|
|
}
|
|
|
|
public class TabelaTabel {
|
|
private final Comparator<String> comparator;
|
|
private Item[][] array = new Item[0][];
|
|
private int size = 0, count = 0;
|
|
|
|
public TabelaTabel(Comparator<String> comparator) {
|
|
this.comparator = comparator;
|
|
}
|
|
|
|
private int getSubarrayCount() {
|
|
if (size == 0) {
|
|
return 0;
|
|
}
|
|
return (int) (Math.log(size) / Math.log(2)) + 1;
|
|
}
|
|
|
|
private void resizeIfFull() {
|
|
if (getSubarrayCount() == array.length) {
|
|
Item[][] tmp = new Item[array.length + 1][];
|
|
System.arraycopy(array, 0, tmp, 0, array.length);
|
|
tmp[array.length] = new Item[(int) Math.pow(2, array.length)];
|
|
array = tmp;
|
|
}
|
|
}
|
|
|
|
private Item[] mergeArrays(Item[] arr1, Item[] arr2) {
|
|
Item[] res = new Item[arr1.length + arr2.length];
|
|
int i = 0, j = 0, k = 0;
|
|
while (i < arr1.length && j < arr2.length) {
|
|
res[k++] = comparator.compare(arr1[i].value, arr2[j].value) < 0 ? arr1[i++] : arr2[j++];
|
|
}
|
|
while (i < arr1.length) {
|
|
res[k++] = arr1[i++];
|
|
}
|
|
while (j < arr2.length) {
|
|
res[k++] = arr2[j++];
|
|
}
|
|
return res;
|
|
}
|
|
|
|
public void vstavi(String element) {
|
|
for (int i = 0; i < getSubarrayCount(); i++) {
|
|
if (array[i] == null) {
|
|
continue;
|
|
}
|
|
for (int j = 0; j < array[i].length; j++) {
|
|
if (comparator.compare(array[i][j].value, element) == 0) {
|
|
array[i][j].count++;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
Item[] tmp = {new Item(element)};
|
|
|
|
boolean success = false;
|
|
int subArrCount = getSubarrayCount();
|
|
for (int subArrI = 0; subArrI < subArrCount; subArrI++) {
|
|
if (array[subArrI] == null) {
|
|
array[subArrI] = new Item[(int) Math.pow(2, subArrI)];
|
|
System.arraycopy(tmp, 0, array[subArrI], 0, tmp.length);
|
|
for (int i = 0; i < subArrI; i++) {
|
|
array[i] = null;
|
|
}
|
|
success = true;
|
|
break;
|
|
} else {
|
|
tmp = mergeArrays(tmp, array[subArrI]);
|
|
}
|
|
}
|
|
|
|
if (!success) {
|
|
resizeIfFull();
|
|
System.arraycopy(tmp, 0, array[subArrCount], 0, tmp.length);
|
|
for (int i = 0; i < subArrCount; i++) {
|
|
array[i] = null;
|
|
}
|
|
}
|
|
|
|
size++;
|
|
count++;
|
|
}
|
|
|
|
public void najdi(String element) {
|
|
for (int i = 0; i < getSubarrayCount(); i++) {
|
|
if (array[i] == null) {
|
|
continue;
|
|
}
|
|
for (int j = 0; j < array[i].length; j++) {
|
|
if (comparator.compare(array[i][j].value, element) == 0) {
|
|
System.out.println("true");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
System.out.println("false");
|
|
}
|
|
|
|
public void izbrisi(String element) {
|
|
for (int i = 0; i < getSubarrayCount(); i++) {
|
|
if (array[i] == null) {
|
|
continue;
|
|
}
|
|
for (int j = 0; j < array[i].length; j++) {
|
|
if (array[i][j] != null && comparator.compare(array[i][j].value, element) == 0) {
|
|
System.out.println("true");
|
|
array[i][j].count--;
|
|
if (array[i][j].count == 0) {
|
|
array[i][j] = null;
|
|
count--;
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
System.out.println("false");
|
|
}
|
|
|
|
public void izpisi() {
|
|
if (count == 0) {
|
|
System.out.println("prazen");
|
|
return;
|
|
}
|
|
|
|
int subArrCount = getSubarrayCount();
|
|
for (int i = 0; i < subArrCount; i++) {
|
|
if (array[i] == null) {
|
|
System.out.println("...");
|
|
continue;
|
|
}
|
|
|
|
for (int j = 0; j < array[i].length; j++) {
|
|
if (j > 0) {
|
|
System.out.print(", ");
|
|
}
|
|
if (array[i][j] == null) {
|
|
System.out.print("x");
|
|
} else {
|
|
System.out.print(array[i][j]);
|
|
}
|
|
}
|
|
System.out.println();
|
|
}
|
|
}
|
|
}
|