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 tabelaTabel = new TabelaTabel(comparator);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        tabelaTabel.izpisi();
 | 
					        tabelaTabel.vstavi("a");
 | 
				
			||||||
 | 
					        tabelaTabel.vstavi("bb");
 | 
				
			||||||
        tabelaTabel.vstavi("1");
 | 
					        tabelaTabel.vstavi("bbb");
 | 
				
			||||||
        tabelaTabel.vstavi("12");
 | 
					        tabelaTabel.vstavi("abcd");
 | 
				
			||||||
        tabelaTabel.vstavi("123");
 | 
					        tabelaTabel.vstavi("ccc");
 | 
				
			||||||
 | 
					        tabelaTabel.vstavi("dddd");
 | 
				
			||||||
 | 
					        tabelaTabel.vstavi("dddd");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        tabelaTabel.izpisi();
 | 
					        tabelaTabel.izpisi();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        tabelaTabel.vstavi("321");
 | 
					//        tabelaTabel.najdi("abcd");
 | 
				
			||||||
        tabelaTabel.vstavi("6383");
 | 
					//
 | 
				
			||||||
        tabelaTabel.vstavi("43");
 | 
					//        tabelaTabel.izpisi();
 | 
				
			||||||
        tabelaTabel.vstavi("023");
 | 
					//
 | 
				
			||||||
 | 
					//        tabelaTabel.vstavi("kdfj");
 | 
				
			||||||
        tabelaTabel.izpisi();
 | 
					//        tabelaTabel.vstavi("asf");
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//        tabelaTabel.izpisi();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -8,25 +8,24 @@ class Item {
 | 
				
			|||||||
        this.value = value;
 | 
					        this.value = value;
 | 
				
			||||||
        this.count = 1;
 | 
					        this.count = 1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public String toString() {
 | 
				
			||||||
 | 
					        return value + "/" + count;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class TabelaTabel {
 | 
					public class TabelaTabel {
 | 
				
			||||||
    Comparator<String> comparator;
 | 
					    private final Comparator<String> comparator;
 | 
				
			||||||
    Item[] array = new Item[32];
 | 
					    private Item[][] array = new Item[0][];
 | 
				
			||||||
    int size = 0;
 | 
					    private int size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    TabelaTabel(Comparator<String> comparator) {
 | 
					    public TabelaTabel(Comparator<String> comparator) {
 | 
				
			||||||
        this.comparator = comparator;
 | 
					        this.comparator = comparator;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private boolean isFull() {
 | 
					    private boolean isSubarrayFull(int index) {
 | 
				
			||||||
        return size >= array.length;
 | 
					        return ((size >> index) & 1) != 0;
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private void resize() {
 | 
					 | 
				
			||||||
        Item[] tmp = new Item[array.length * 2];
 | 
					 | 
				
			||||||
        System.arraycopy(array, 0, tmp, 0, array.length);
 | 
					 | 
				
			||||||
        array = tmp;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private int getSubarrayCount() {
 | 
					    private int getSubarrayCount() {
 | 
				
			||||||
@ -36,71 +35,97 @@ public class TabelaTabel {
 | 
				
			|||||||
        return (int) (Math.log(size) / Math.log(2)) + 1;
 | 
					        return (int) (Math.log(size) / Math.log(2)) + 1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private boolean isSubarrayFull(int i) {
 | 
					    private void resizeIfFull() {
 | 
				
			||||||
        return ((size >> i) & 1) != 0;
 | 
					        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)};
 | 
					        Item[] tmp = {new Item(element)};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        boolean success = false;
 | 
					        boolean success = false;
 | 
				
			||||||
        int k = getSubarrayCount();
 | 
					        int subArrCount = getSubarrayCount();
 | 
				
			||||||
        for (int i = 0; i < k; i++) {
 | 
					        for (int subArrI = 0; subArrI < subArrCount; subArrI++) {
 | 
				
			||||||
            if (isSubarrayFull(i)) {
 | 
					            if (array[subArrI] == null) {
 | 
				
			||||||
                // Append subarray to tmp
 | 
					                array[subArrI] = new Item[(int) Math.pow(2, subArrI)];
 | 
				
			||||||
                int subArrLen = 1 << i;
 | 
					                System.arraycopy(tmp, 0, array[subArrI], 0, tmp.length);
 | 
				
			||||||
                Item[] newTmp = new Item[tmp.length + subArrLen];
 | 
					                for (int i = 0; i < subArrI; i++) {
 | 
				
			||||||
                System.arraycopy(tmp, 0, newTmp, 0, tmp.length);
 | 
					                    array[i] = null;
 | 
				
			||||||
                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;
 | 
					                success = true;
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                // Merge subarray with tmp
 | 
				
			||||||
 | 
					                tmp = mergeArrays(tmp, array[subArrI]);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!success) {
 | 
					        if (!success) {
 | 
				
			||||||
            // Crate new subarray with size k and add tmp to it
 | 
					            resizeIfFull();
 | 
				
			||||||
            if (isFull()) {
 | 
					            System.arraycopy(tmp, 0, array[subArrCount], 0, tmp.length);
 | 
				
			||||||
                resize();
 | 
					            for (int i = 0; i < subArrCount; i++) {
 | 
				
			||||||
 | 
					                array[i] = null;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            System.arraycopy(tmp, 0, array, k, tmp.length);
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        size++;
 | 
					        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) {
 | 
					    public void izpisi() {
 | 
				
			||||||
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    void izpisi() {
 | 
					 | 
				
			||||||
        if (size == 0) {
 | 
					        if (size == 0) {
 | 
				
			||||||
            System.out.println("prazen");
 | 
					            System.out.println("prazen");
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for (int i = 0; i < getSubarrayCount(); i++) {
 | 
					        int subArrCount = getSubarrayCount();
 | 
				
			||||||
            if (!isSubarrayFull(i)) {
 | 
					        for (int i = 0; i < subArrCount; i++) {
 | 
				
			||||||
 | 
					            if (array[i] == null) {
 | 
				
			||||||
                System.out.println("...");
 | 
					                System.out.println("...");
 | 
				
			||||||
                continue;
 | 
					                continue;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            int startIndex = i == 0 ? 0 : 1 << (i - 1);
 | 
					            for (int j = 0; j < array[i].length; j++) {
 | 
				
			||||||
            int subArrLen = 1 << i;
 | 
					                if (j > 0) {
 | 
				
			||||||
            for (int j = startIndex; j < startIndex + subArrLen; j++) {
 | 
					 | 
				
			||||||
                if (j > startIndex) {
 | 
					 | 
				
			||||||
                    System.out.print(", ");
 | 
					                    System.out.print(", ");
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                if (array[j] == null) {
 | 
					                if (array[i][j] == null) {
 | 
				
			||||||
                    System.out.print("x");
 | 
					                    System.out.println("x");
 | 
				
			||||||
                } else {
 | 
					                } 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();
 | 
					            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