Naloga 2
This commit is contained in:
		
							parent
							
								
									40d98a3011
								
							
						
					
					
						commit
						0a7fd2a162
					
				@ -4,23 +4,23 @@ public class Main {
 | 
			
		||||
 | 
			
		||||
        TabelaTabel tabelaTabel = new TabelaTabel(comparator);
 | 
			
		||||
 | 
			
		||||
        tabelaTabel.vstavi("a");
 | 
			
		||||
        tabelaTabel.vstavi("bb");
 | 
			
		||||
        tabelaTabel.vstavi("bbb");
 | 
			
		||||
        tabelaTabel.vstavi("abc");
 | 
			
		||||
        tabelaTabel.vstavi("abcd");
 | 
			
		||||
        tabelaTabel.vstavi("ccc");
 | 
			
		||||
        tabelaTabel.vstavi("dddd");
 | 
			
		||||
        tabelaTabel.vstavi("dddd");
 | 
			
		||||
        tabelaTabel.vstavi("dca");
 | 
			
		||||
 | 
			
		||||
        tabelaTabel.izpisi();
 | 
			
		||||
 | 
			
		||||
//        tabelaTabel.najdi("abcd");
 | 
			
		||||
//
 | 
			
		||||
//        tabelaTabel.izpisi();
 | 
			
		||||
//
 | 
			
		||||
//        tabelaTabel.vstavi("kdfj");
 | 
			
		||||
//        tabelaTabel.vstavi("asf");
 | 
			
		||||
//
 | 
			
		||||
//        tabelaTabel.izpisi();
 | 
			
		||||
        tabelaTabel.izbrisi("dca");
 | 
			
		||||
 | 
			
		||||
        tabelaTabel.izpisi();
 | 
			
		||||
 | 
			
		||||
        tabelaTabel.izbrisi("abcd");
 | 
			
		||||
 | 
			
		||||
        tabelaTabel.izpisi();
 | 
			
		||||
 | 
			
		||||
        tabelaTabel.izbrisi("abc");
 | 
			
		||||
        tabelaTabel.izbrisi("dca");
 | 
			
		||||
 | 
			
		||||
        tabelaTabel.izpisi();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -18,7 +18,7 @@ class Item {
 | 
			
		||||
public class TabelaTabel {
 | 
			
		||||
    private final Comparator<String> comparator;
 | 
			
		||||
    private Item[][] array = new Item[0][];
 | 
			
		||||
    private int size;
 | 
			
		||||
    private int size = 0, count = 0;
 | 
			
		||||
 | 
			
		||||
    public TabelaTabel(Comparator<String> comparator) {
 | 
			
		||||
        this.comparator = comparator;
 | 
			
		||||
@ -60,6 +60,18 @@ public class TabelaTabel {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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;
 | 
			
		||||
@ -88,6 +100,7 @@ public class TabelaTabel {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        size++;
 | 
			
		||||
        count++;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void najdi(String element) {
 | 
			
		||||
@ -105,8 +118,28 @@ public class TabelaTabel {
 | 
			
		||||
        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 (size == 0) {
 | 
			
		||||
        if (count == 0) {
 | 
			
		||||
            System.out.println("prazen");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
@ -123,7 +156,7 @@ public class TabelaTabel {
 | 
			
		||||
                    System.out.print(", ");
 | 
			
		||||
                }
 | 
			
		||||
                if (array[i][j] == null) {
 | 
			
		||||
                    System.out.println("x");
 | 
			
		||||
                    System.out.print("x");
 | 
			
		||||
                } else {
 | 
			
		||||
                    System.out.printf("%s/%d", array[i][j].value, array[i][j].count);
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
@ -1,114 +0,0 @@
 | 
			
		||||
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