Naloga 2 - Insertion
This commit is contained in:
parent
ed25569920
commit
40d98a3011
@ -4,19 +4,23 @@ public class Main {
|
||||
|
||||
TabelaTabel tabelaTabel = new TabelaTabel(comparator);
|
||||
|
||||
tabelaTabel.izpisi();
|
||||
|
||||
tabelaTabel.vstavi("1");
|
||||
tabelaTabel.vstavi("12");
|
||||
tabelaTabel.vstavi("123");
|
||||
tabelaTabel.vstavi("a");
|
||||
tabelaTabel.vstavi("bb");
|
||||
tabelaTabel.vstavi("bbb");
|
||||
tabelaTabel.vstavi("abcd");
|
||||
tabelaTabel.vstavi("ccc");
|
||||
tabelaTabel.vstavi("dddd");
|
||||
tabelaTabel.vstavi("dddd");
|
||||
|
||||
tabelaTabel.izpisi();
|
||||
|
||||
tabelaTabel.vstavi("321");
|
||||
tabelaTabel.vstavi("6383");
|
||||
tabelaTabel.vstavi("43");
|
||||
tabelaTabel.vstavi("023");
|
||||
|
||||
tabelaTabel.izpisi();
|
||||
// tabelaTabel.najdi("abcd");
|
||||
//
|
||||
// tabelaTabel.izpisi();
|
||||
//
|
||||
// tabelaTabel.vstavi("kdfj");
|
||||
// tabelaTabel.vstavi("asf");
|
||||
//
|
||||
// tabelaTabel.izpisi();
|
||||
}
|
||||
}
|
||||
|
@ -8,25 +8,24 @@ class Item {
|
||||
this.value = value;
|
||||
this.count = 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value + "/" + count;
|
||||
}
|
||||
}
|
||||
|
||||
public class TabelaTabel {
|
||||
Comparator<String> comparator;
|
||||
Item[] array = new Item[32];
|
||||
int size = 0;
|
||||
private final Comparator<String> comparator;
|
||||
private Item[][] array = new Item[0][];
|
||||
private int size;
|
||||
|
||||
TabelaTabel(Comparator<String> comparator) {
|
||||
public 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 boolean isSubarrayFull(int index) {
|
||||
return ((size >> index) & 1) != 0;
|
||||
}
|
||||
|
||||
private int getSubarrayCount() {
|
||||
@ -36,71 +35,97 @@ public class TabelaTabel {
|
||||
return (int) (Math.log(size) / Math.log(2)) + 1;
|
||||
}
|
||||
|
||||
private boolean isSubarrayFull(int i) {
|
||||
return ((size >> i) & 1) != 0;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void vstavi(String element) {
|
||||
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) {
|
||||
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);
|
||||
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 {
|
||||
// Merge subarray with tmp
|
||||
tmp = mergeArrays(tmp, array[subArrI]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
// Crate new subarray with size k and add tmp to it
|
||||
if (isFull()) {
|
||||
resize();
|
||||
resizeIfFull();
|
||||
System.arraycopy(tmp, 0, array[subArrCount], 0, tmp.length);
|
||||
for (int i = 0; i < subArrCount; i++) {
|
||||
array[i] = null;
|
||||
}
|
||||
System.arraycopy(tmp, 0, array, k, tmp.length);
|
||||
}
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
void najdi(String element) {
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
void izbrisi(String element) {
|
||||
|
||||
}
|
||||
|
||||
void izpisi() {
|
||||
public void izpisi() {
|
||||
if (size == 0) {
|
||||
System.out.println("prazen");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < getSubarrayCount(); i++) {
|
||||
if (!isSubarrayFull(i)) {
|
||||
int subArrCount = getSubarrayCount();
|
||||
for (int i = 0; i < subArrCount; i++) {
|
||||
if (array[i] == null) {
|
||||
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) {
|
||||
for (int j = 0; j < array[i].length; j++) {
|
||||
if (j > 0) {
|
||||
System.out.print(", ");
|
||||
}
|
||||
if (array[j] == null) {
|
||||
System.out.print("x");
|
||||
if (array[i][j] == null) {
|
||||
System.out.println("x");
|
||||
} else {
|
||||
System.out.printf("%s/%d", array[j].value, array[j].count);
|
||||
System.out.printf("%s/%d", array[i][j].value, array[i][j].count);
|
||||
}
|
||||
}
|
||||
System.out.println();
|
||||
|
114
naloga2/src/TabelaTabelOld.java
Normal file
114
naloga2/src/TabelaTabelOld.java
Normal file
@ -0,0 +1,114 @@
|
||||
import java.util.Comparator;
|
||||
|
||||
public class TabelaTabelOld {
|
||||
Comparator<String> comparator;
|
||||
Item[] array = new Item[32];
|
||||
int size = 0;
|
||||
|
||||
TabelaTabelOld(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;
|
||||
int subArrStartIndex = subArrLen - 1;
|
||||
Item[] newTmp = new Item[tmp.length + subArrLen];
|
||||
// System.arraycopy(tmp, 0, newTmp, 0, tmp.length);
|
||||
// System.arraycopy(array, subArrLen - 1, newTmp, tmp.length, subArrLen);
|
||||
|
||||
// int j = 0, l = 0, r = 0;
|
||||
// while (l < tmp.length && r < subArrLen) {
|
||||
// newTmp[j++] = comparator.compare(tmp[l], array[subArrStartIndex + r]) < 0 ? tmp[l] : array[subArrStartIndex + r];
|
||||
// }
|
||||
|
||||
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++;
|
||||
}
|
||||
|
||||
int findIndex(String element) {
|
||||
int[][] arr = new int[10][];
|
||||
arr[0] = new int[10];
|
||||
arr[1] = new int[5];
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user