Naloga 2 WIP

This commit is contained in:
Gašper Dobrovoljc
2025-03-31 11:59:36 +02:00
parent 469fa70a87
commit ed25569920
12 changed files with 226 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
import java.util.Comparator;
class AlphabeticalComparator implements Comparator<String> {
@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
}

22
naloga2/src/Main.java Normal file
View File

@@ -0,0 +1,22 @@
public class Main {
public static void main(String[] args) {
AlphabeticalComparator comparator = new AlphabeticalComparator();
TabelaTabel tabelaTabel = new TabelaTabel(comparator);
tabelaTabel.izpisi();
tabelaTabel.vstavi("1");
tabelaTabel.vstavi("12");
tabelaTabel.vstavi("123");
tabelaTabel.izpisi();
tabelaTabel.vstavi("321");
tabelaTabel.vstavi("6383");
tabelaTabel.vstavi("43");
tabelaTabel.vstavi("023");
tabelaTabel.izpisi();
}
}

View File

@@ -0,0 +1,109 @@
import java.util.Comparator;
class Item {
String value;
int count;
Item(String value) {
this.value = value;
this.count = 1;
}
}
public class TabelaTabel {
Comparator<String> comparator;
Item[] array = new Item[32];
int size = 0;
TabelaTabel(Comparator<String> comparator) {
this.comparator = comparator;
}
private boolean isFull() {
return size >= array.length;
}
private void resize() {
Item[] tmp = new Item[array.length * 2];
System.arraycopy(array, 0, tmp, 0, array.length);
array = tmp;
}
private int getSubarrayCount() {
if (size == 0) {
return 0;
}
return (int) (Math.log(size) / Math.log(2)) + 1;
}
private boolean isSubarrayFull(int i) {
return ((size >> i) & 1) != 0;
}
void vstavi(String element) {
Item[] tmp = {new Item(element)};
boolean success = false;
int k = getSubarrayCount();
for (int i = 0; i < k; i++) {
if (isSubarrayFull(i)) {
// Append subarray to tmp
int subArrLen = 1 << i;
Item[] newTmp = new Item[tmp.length + subArrLen];
System.arraycopy(tmp, 0, newTmp, 0, tmp.length);
System.arraycopy(array, subArrLen - 1, newTmp, tmp.length, subArrLen);
tmp = newTmp;
} else {
// Set subarray to tmp
System.arraycopy(tmp, 0, array, i, tmp.length);
success = true;
break;
}
}
if (!success) {
// Crate new subarray with size k and add tmp to it
if (isFull()) {
resize();
}
System.arraycopy(tmp, 0, array, k, tmp.length);
}
size++;
}
void najdi(String element) {
}
void izbrisi(String element) {
}
void izpisi() {
if (size == 0) {
System.out.println("prazen");
return;
}
for (int i = 0; i < getSubarrayCount(); i++) {
if (!isSubarrayFull(i)) {
System.out.println("...");
continue;
}
int startIndex = i == 0 ? 0 : 1 << (i - 1);
int subArrLen = 1 << i;
for (int j = startIndex; j < startIndex + subArrLen; j++) {
if (j > startIndex) {
System.out.print(", ");
}
if (array[j] == null) {
System.out.print("x");
} else {
System.out.printf("%s/%d", array[j].value, array[j].count);
}
}
System.out.println();
}
}
}