From 77c0185f49f6a22e84943496c3dd3b0559865cca Mon Sep 17 00:00:00 2001 From: firefry Date: Mon, 29 Dec 2014 21:43:51 +0200 Subject: [PATCH] Remove legacy code to start it all over again! --- README.md | 39 --- .../com/burakovv/algorithms/AvlTreeMap.java | 242 -------------- .../com/burakovv/algorithms/BinarySearch.java | 27 -- .../burakovv/algorithms/BinarySearchTree.java | 165 ---------- .../algorithms/BinarySearchTreeMap.java | 165 ---------- .../java/com/burakovv/algorithms/Graph.java | 79 ----- .../com/burakovv/algorithms/HeapSort.java | 35 -- .../burakovv/algorithms/InsertionSort.java | 19 -- .../burakovv/algorithms/OrderStatistic.java | 30 -- .../algorithms/OrderStatisticTree.java | 298 ------------------ .../com/burakovv/algorithms/QuickSort.java | 40 --- .../com/burakovv/algorithms/RadixSort.java | 42 --- .../burakovv/algorithms/RedBlackTreeSet.java | 288 ----------------- .../burakovv/algorithms/SegmentTreeRMQ.java | 89 ------ .../burakovv/algorithms/SparseTableRMQ.java | 86 ----- .../com/burakovv/data/ComparableData.java | 13 - .../java/com/burakovv/data/CustomMap.java | 11 - .../java/com/burakovv/data/CustomSet.java | 9 - .../com/burakovv/data/CustomSetFromMap.java | 29 -- .../burakovv/data/DynamicOrderStatistic.java | 11 - .../java/com/burakovv/data/IntOption.java | 50 --- .../java/com/burakovv/data/NumberData.java | 10 - .../java/com/burakovv/data/OrderedData.java | 44 --- .../data/impl/IntArrayDataWrapper.java | 60 ---- .../algorithms/AbstractSearchTest.java | 106 ------- .../burakovv/algorithms/AvlTreeSetTest.java | 51 --- .../burakovv/algorithms/BinarySearchTest.java | 68 ---- .../algorithms/BinarySearchTreeSetTest.java | 15 - .../algorithms/BinarySearchTreeTest.java | 265 ---------------- .../burakovv/algorithms/CustomSetTest.java | 169 ---------- .../algorithms/DynamicOrderStatisticTest.java | 44 --- .../com/burakovv/algorithms/GraphTest.java | 61 ---- .../com/burakovv/algorithms/HeapSortTest.java | 10 - .../algorithms/InsertionSortTest.java | 15 - .../algorithms/OrderStatisticTest.java | 39 --- .../burakovv/algorithms/QuickSortTest.java | 10 - .../burakovv/algorithms/RadixSortTest.java | 24 -- .../algorithms/RedBlackTreeSetTest.java | 85 ----- .../algorithms/SegmentTreeRMQTest.java | 121 ------- .../algorithms/SparseTableRMQTest.java | 104 ------ 40 files changed, 3068 deletions(-) delete mode 100644 README.md delete mode 100644 src/main/java/com/burakovv/algorithms/AvlTreeMap.java delete mode 100644 src/main/java/com/burakovv/algorithms/BinarySearch.java delete mode 100644 src/main/java/com/burakovv/algorithms/BinarySearchTree.java delete mode 100644 src/main/java/com/burakovv/algorithms/BinarySearchTreeMap.java delete mode 100644 src/main/java/com/burakovv/algorithms/Graph.java delete mode 100644 src/main/java/com/burakovv/algorithms/HeapSort.java delete mode 100644 src/main/java/com/burakovv/algorithms/InsertionSort.java delete mode 100644 src/main/java/com/burakovv/algorithms/OrderStatistic.java delete mode 100644 src/main/java/com/burakovv/algorithms/OrderStatisticTree.java delete mode 100644 src/main/java/com/burakovv/algorithms/QuickSort.java delete mode 100644 src/main/java/com/burakovv/algorithms/RadixSort.java delete mode 100644 src/main/java/com/burakovv/algorithms/RedBlackTreeSet.java delete mode 100644 src/main/java/com/burakovv/algorithms/SegmentTreeRMQ.java delete mode 100644 src/main/java/com/burakovv/algorithms/SparseTableRMQ.java delete mode 100644 src/main/java/com/burakovv/data/ComparableData.java delete mode 100644 src/main/java/com/burakovv/data/CustomMap.java delete mode 100644 src/main/java/com/burakovv/data/CustomSet.java delete mode 100644 src/main/java/com/burakovv/data/CustomSetFromMap.java delete mode 100644 src/main/java/com/burakovv/data/DynamicOrderStatistic.java delete mode 100644 src/main/java/com/burakovv/data/IntOption.java delete mode 100644 src/main/java/com/burakovv/data/NumberData.java delete mode 100644 src/main/java/com/burakovv/data/OrderedData.java delete mode 100644 src/main/java/com/burakovv/data/impl/IntArrayDataWrapper.java delete mode 100644 src/test/java/com/burakovv/algorithms/AbstractSearchTest.java delete mode 100644 src/test/java/com/burakovv/algorithms/AvlTreeSetTest.java delete mode 100644 src/test/java/com/burakovv/algorithms/BinarySearchTest.java delete mode 100644 src/test/java/com/burakovv/algorithms/BinarySearchTreeSetTest.java delete mode 100644 src/test/java/com/burakovv/algorithms/BinarySearchTreeTest.java delete mode 100644 src/test/java/com/burakovv/algorithms/CustomSetTest.java delete mode 100644 src/test/java/com/burakovv/algorithms/DynamicOrderStatisticTest.java delete mode 100644 src/test/java/com/burakovv/algorithms/GraphTest.java delete mode 100644 src/test/java/com/burakovv/algorithms/HeapSortTest.java delete mode 100644 src/test/java/com/burakovv/algorithms/InsertionSortTest.java delete mode 100644 src/test/java/com/burakovv/algorithms/OrderStatisticTest.java delete mode 100644 src/test/java/com/burakovv/algorithms/QuickSortTest.java delete mode 100644 src/test/java/com/burakovv/algorithms/RadixSortTest.java delete mode 100644 src/test/java/com/burakovv/algorithms/RedBlackTreeSetTest.java delete mode 100644 src/test/java/com/burakovv/algorithms/SegmentTreeRMQTest.java delete mode 100644 src/test/java/com/burakovv/algorithms/SparseTableRMQTest.java diff --git a/README.md b/README.md deleted file mode 100644 index 38550a5..0000000 --- a/README.md +++ /dev/null @@ -1,39 +0,0 @@ -algorithms-java -=============== - -Algorithms, writen in Java. - -TODO list: - -1. ~~Binary search~~ -2. ~~Order statistic~~ -3. ~~Insertion sort~~ -4. ~~Heap sort~~ -5. ~~Quick sort~~ -6. ~~Radix sort~~ -7. Hash table -8. ~~Binary search tree~~ -9. ~~Red-black tree~~ -10. ~~Order-statistic tree~~ -11. Segment tree -12. K-D tree -13. ~~AVL-tree~~ -14. ~~Range Minimum Query (Sparse Table)~~ -15. ~~Range Minimum Query (Segment Tree)~~ -16. Huffman coding -17. B-tree -18. Bimonial tree -19. Disjoint-set data structure -20. ~~BFS~~ -21. ~~DFS~~ -22. Strongly connected components -23. Minimum spanning-tree problem (Kruskal and Prim) -24. Bellman-Ford -25. Dijkstra -26. Floyd-Warshall -27. Maximum flow problem -28. Strassen algorithm -29. Fast Fourier Transform (Cooley–Tukey) -30. Knuth–Morris–Pratt -31. Aho-Corasick -32. Convex hull diff --git a/src/main/java/com/burakovv/algorithms/AvlTreeMap.java b/src/main/java/com/burakovv/algorithms/AvlTreeMap.java deleted file mode 100644 index 87b16fd..0000000 --- a/src/main/java/com/burakovv/algorithms/AvlTreeMap.java +++ /dev/null @@ -1,242 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.CustomMap; - -import java.util.Comparator; - -public class AvlTreeMap implements CustomMap { - private final Comparator comparator; - Node root; - - public AvlTreeMap() { - this.comparator = null; - } - - public AvlTreeMap(Comparator comparator) { - this.comparator = comparator; - } - - @Override - public V put(K key, V value) { - Node y = null; - Node x = root; - int lastComparisonResult = -1; - while (x != null) { - y = x; - lastComparisonResult = compare(key, x.key); - if (lastComparisonResult == 0) { - V result = x.value; - x.value = value; - return result; - } - x = lastComparisonResult < 0 ? x.left : x.right; - } - Node newNode = new Node(key, value); - if (y == null) { - root = newNode; - } else if (lastComparisonResult < 0) { - makeLeftChild(y, newNode); - } else { - makeRightChild(y, newNode); - } - balance(newNode); - return null; - } - - protected void balance(Node node) { - while (node != null) { - updateHeight(node); - int nodeFactor = factor(node); - if (nodeFactor == 2) { - if (factor(node.left) == -1) { - leftRotate(node.left); - } - rightRotate(node); - node = node.parent.parent; - } else if (nodeFactor == -2) { - if (factor(node.right) == 1) { - rightRotate(node.right); - } - leftRotate(node); - node = node.parent.parent; - } else { - node = node.parent; - } - } - } - - private int factor(Node node) { - return heightOf(node.left) - heightOf(node.right); - } - - private static int heightOf(Node node) { - return node == null ? 0 : node.height; - } - - protected int compare(K a, K b) { - if (comparator != null) { - return comparator.compare(a, b); - } - return ((Comparable) a).compareTo(b); - } - - @Override - public V remove(Object key) { - try { - return removeK((K) key); - } catch (ClassCastException e) { - return null; - } - } - - private V removeK(K key) { - Node z = search(key); - if (z == null) { - return null; - } - V result = z.value; - Node y = (z.left != null && z.right != null) ? successor(z) : z; - //y can't have both children by definition (successor can not have left child in this situation) - Node yChild = (y.left != null) ? y.left : y.right; - if (yChild != null) { - yChild.parent = y.parent; - } - if (y.parent == null) { - root = yChild; - } else if (y.parent.left == y) { - y.parent.left = yChild; - } else { - y.parent.right = yChild; - } - if (y != z) { - z.key = y.key; - z.value = y.value; - } - balance(y.parent); - return result; - } - - @Override - public V get(Object key) { - try { - Node node = search((K) key); - return node == null ? null : node.value; - } catch (ClassCastException e) { - return null; - } - } - - private Node search(K key) { - Node node = root; - while (node != null) { - int compareResult = compare(key, node.key); - if (compareResult == 0) { - return node; - } else if (compareResult < 0) { - node = node.left; - } else { - node = node.right; - } - } - return null; - } - - private Node successor(Node node) { - if (node.right != null) { - return min(node.right); - } - Node parent = node.parent; - while (parent != null && parent.right == node) { - node = parent; - parent = parent.parent; - } - return parent; - } - - private Node min(Node node) { - while (node.left != null) { - node = node.left; - } - return node; - } - - private static void makeLeftChild(Node parent, Node leftChild) { - if (parent.left != null) { - throw new IllegalArgumentException("Parent already has left child"); - } - if (leftChild.parent != null) { - throw new IllegalArgumentException("Child already has a parent"); - } - parent.left = leftChild; - leftChild.parent = parent; - } - - private static void makeRightChild(Node parent, Node rightChild) { - if (parent.right != null) { - throw new IllegalArgumentException("Parent already has right child"); - } - if (rightChild.parent != null) { - throw new IllegalArgumentException("Child already has a parent"); - } - parent.right = rightChild; - rightChild.parent = parent; - } - - private void leftRotate(Node x) { - Node y = x.right; - x.right = y.left; - if (y.left != null) { - y.left.parent = x; - } - y.parent = x.parent; - if (x.parent == null) { - root = y; - } else if (x.parent.left == x) { - x.parent.left = y; - } else { - x.parent.right = y; - } - x.parent = y; - y.left = x; - updateHeight(x); - updateHeight(y); - } - - private void rightRotate(Node y) { - Node x = y.left; - y.left = x.right; - if (y.left != null) { - y.left.parent = y; - } - x.right = y; - x.parent = y.parent; - if (y.parent == null) { - root = x; - } else if (y.parent.left == y) { - y.parent.left = x; - } else { - y.parent.right = x; - } - y.parent = x; - updateHeight(y); - updateHeight(x); - } - - private void updateHeight(Node node) { - node.height = 1 + Math.max(heightOf(node.left), heightOf(node.right)); - } - - static final class Node { - Node left; - Node right; - Node parent; - T key; - V value; - int height; - - public Node(T key, V value) { - this.key = key; - this.value = value; - } - } -} diff --git a/src/main/java/com/burakovv/algorithms/BinarySearch.java b/src/main/java/com/burakovv/algorithms/BinarySearch.java deleted file mode 100644 index 7ab1336..0000000 --- a/src/main/java/com/burakovv/algorithms/BinarySearch.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.ComparableData; - -public class BinarySearch { - - /** - * Searching key should be the first buffer element - */ - public static int search(ComparableData data) { - int left = data.getOffset(); - int right = left + data.getSize() - 1; - while (left <= right) { - int middle = (left + right) / 2; - int compareResult = data.compare(data.getBufferOffset(), middle); - if (compareResult == 0) { - return middle; - } else if (compareResult < 0) { - right = middle - 1; - } else { - left = middle + 1; - } - } - return -left - 1; - } - -} diff --git a/src/main/java/com/burakovv/algorithms/BinarySearchTree.java b/src/main/java/com/burakovv/algorithms/BinarySearchTree.java deleted file mode 100644 index c5c542a..0000000 --- a/src/main/java/com/burakovv/algorithms/BinarySearchTree.java +++ /dev/null @@ -1,165 +0,0 @@ -package com.burakovv.algorithms; - -import java.util.Arrays; - -public class BinarySearchTree { - public static final int NIL = -1; - - private final int[] parentArray; - private final int[] leftArray; - private final int[] rightArray; - private final int[] values; - - private int next = 0; - private int root = NIL; - - public static BinarySearchTree create(int[] values, int[][] childs, int root) { - BinarySearchTree tree = new BinarySearchTree(values.length); - tree.root = root; - tree.next = values.length; - Arrays.fill(tree.parentArray, NIL); - for (int i = 0; i < values.length; i++) { - tree.values[i] = values[i]; - int left = childs[i][0]; - int right = childs[i][1]; - tree.leftArray[i] = left; - if (left != NIL) { - tree.parentArray[left] = i; - } - tree.rightArray[i] = right; - if (right != NIL) { - tree.parentArray[right] = i; - } - } - return tree; - } - - public BinarySearchTree(int size) { - parentArray = new int[size]; - leftArray = new int[size]; - rightArray = new int[size]; - values = new int[size]; - } - - public void inorderTreeWalk(int root, Visitor visitor) { - if (root == NIL) { - return; - } - inorderTreeWalk(leftArray[root], visitor); - visitor.visit(this, root); - inorderTreeWalk(rightArray[root], visitor); - } - - public int min(int root) { - int node; - while ((node = leftArray[root]) != NIL) { - root = node; - } - return root; - } - - public int max(int root) { - int node; - while ((node = rightArray[root]) != NIL) { - root = node; - } - return root; - } - - public int search(int node, int value) { - if (node == NIL) { - return node; - } - int compareResult = Integer.compare(value, values[node]); - if (compareResult == 0) { - return node; - } else if (compareResult < 0) { - return search(leftArray[node], value); - } else { - return search(rightArray[node], value); - } - } - - public int successor(int node) { - int right = rightArray[node]; - if (right != NIL) { - return min(right); - } - int parent = parentArray[node]; - while (parent != NIL && rightArray[parent] == node) { - node = parent; - parent = parentArray[parent]; - } - return parent; - } - - public int predecessor(int node) { - int left = leftArray[node]; - if (left != NIL) { - return max(left); - } - int parent = parentArray[node]; - while (parent != NIL && leftArray[parent] == node) { - node = parent; - parent = parentArray[parent]; - } - return parent; - } - - public int insert(int value) { - int node = next++; - parentArray[node] = leftArray[node] = rightArray[node] = NIL; - values[node] = value; - int x = root; - int p = NIL; - while (x != NIL) { - p = x; - x = (value < values[x]) ? leftArray[x] : rightArray[x]; - } - parentArray[node] = p; - if (p == NIL) { - root = node; - } else if (value < values[p]) { - leftArray[p] = node; - parentArray[node] = p; - } else { - rightArray[p] = node; - parentArray[node] = p; - } - return node; - } - - public int remove(int node) { - int y = leftArray[node] != NIL && rightArray[node] != NIL ? successor(node) : node; - int x = leftArray[y]; - if (x == NIL) { - x = rightArray[y]; - } - if (x != NIL) { - parentArray[x] = parentArray[y]; - } - if (parentArray[y] == NIL) { - root = x; - } else if (y == leftArray[parentArray[y]]) { - leftArray[parentArray[y]] = x; - } else { - rightArray[parentArray[y]] = x; - } - if (y != node) { - values[node] = values[y]; - } - return y; - } - - public int getRoot() { - return root; - } - - public int getValue(int node) { - return values[node]; - } - - public static interface Visitor { - void visit(BinarySearchTree tree, int node); - } -} diff --git a/src/main/java/com/burakovv/algorithms/BinarySearchTreeMap.java b/src/main/java/com/burakovv/algorithms/BinarySearchTreeMap.java deleted file mode 100644 index ad00854..0000000 --- a/src/main/java/com/burakovv/algorithms/BinarySearchTreeMap.java +++ /dev/null @@ -1,165 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.CustomMap; - -import java.util.Comparator; - -public class BinarySearchTreeMap implements CustomMap { - private final Comparator comparator; - private Node root; - - public BinarySearchTreeMap() { - this.comparator = null; - } - - public BinarySearchTreeMap(Comparator comparator) { - this.comparator = comparator; - } - - @Override - public V put(K key, V value) { - Node y = null; - Node x = root; - int lastComparisonResult = -1; - while (x != null) { - y = x; - lastComparisonResult = compare(key, x.key); - if (lastComparisonResult == 0) { - V result = x.value; - x.value = value; - return result; - } - x = lastComparisonResult < 0 ? x.left : x.right; - } - Node newNode = new Node(key, value); - if (y == null) { - root = newNode; - } else if (lastComparisonResult < 0) { - makeLeftChild(y, newNode); - } else { - makeRightChild(y, newNode); - } - return null; - } - - protected int compare(K a, K b) { - if (comparator != null) { - return comparator.compare(a, b); - } - return ((Comparable) a).compareTo(b); - } - - @Override - public V remove(Object key) { - try { - return removeK((K) key); - } catch (ClassCastException e) { - return null; - } - } - - private V removeK(K key) { - Node z = search(key); - if (z == null) { - return null; - } - V result = z.value; - Node y = (z.left != null && z.right != null) ? successor(z) : z; - //y can't have both children by definition (successor can not have left child in this situation) - Node yChild = (y.left != null) ? y.left : y.right; - if (yChild != null) { - yChild.parent = y.parent; - } - if (y.parent == null) { - root = yChild; - } else if (y.parent.left == y) { - y.parent.left = yChild; - } else { - y.parent.right = yChild; - } - if (y != z) { - z.key = y.key; - z.value = y.value; - } - return result; - } - - @Override - public V get(Object key) { - try { - Node node = search((K) key); - return node == null ? null : node.value; - } catch (ClassCastException e) { - return null; - } - } - - private Node search(K key) { - Node node = root; - while (node != null) { - int compareResult = compare(key, node.key); - if (compareResult == 0) { - return node; - } else if (compareResult < 0) { - node = node.left; - } else { - node = node.right; - } - } - return null; - } - - private Node successor(Node node) { - if (node.right != null) { - return min(node.right); - } - Node parent = node.parent; - while (parent != null && parent.right == node) { - node = parent; - parent = parent.parent; - } - return parent; - } - - private Node min(Node node) { - while (node.left != null) { - node = node.left; - } - return node; - } - - private static void makeLeftChild(Node parent, Node leftChild) { - if (parent.left != null) { - throw new IllegalArgumentException("Parent already has left child"); - } - if (leftChild.parent != null) { - throw new IllegalArgumentException("Child already has a parent"); - } - parent.left = leftChild; - leftChild.parent = parent; - } - - private static void makeRightChild(Node parent, Node rightChild) { - if (parent.right != null) { - throw new IllegalArgumentException("Parent already has right child"); - } - if (rightChild.parent != null) { - throw new IllegalArgumentException("Child already has a parent"); - } - parent.right = rightChild; - rightChild.parent = parent; - } - - static final class Node { - Node left; - Node right; - Node parent; - T key; - V value; - - public Node(T key, V value) { - this.key = key; - this.value = value; - } - } -} diff --git a/src/main/java/com/burakovv/algorithms/Graph.java b/src/main/java/com/burakovv/algorithms/Graph.java deleted file mode 100644 index 4b47ceb..0000000 --- a/src/main/java/com/burakovv/algorithms/Graph.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.burakovv.algorithms; - -import java.util.Arrays; - -public class Graph { - public static final int NIL = Integer.MIN_VALUE; - - private final int[] a; - private int size; - - private final int[] queue; - private final boolean[] marks; - - public Graph(int size) { - this.size = size; - this.queue = new int[size]; - this.marks = new boolean[size]; - this.a = new int[size * size]; - Arrays.fill(a, NIL); - } - - public void connectDirectly(int i, int j, int edgeValue) { - a[indexOf(i, j)] = edgeValue; - } - - public void connect(int i, int j, int edgeValue) { - connectDirectly(i, j, edgeValue); - connectDirectly(j, i, edgeValue); - } - - public void breadthFirstSearch(int node, Visitor visitor) { - Arrays.fill(marks, false); - marks[node] = true; - queue[0] = node; - int n = 1; - int i = 0; - while (i < n) { - int visitNode = queue[i++]; - if (visitor.visit(visitNode)) { - return; - } - for (int j = 0; j < size; j++) { - if (!marks[j] && a[indexOf(visitNode, j)] != NIL) { - marks[j] = true; - queue[n++] = j; - } - } - } - } - - public void depthFirstSearch(int node, Visitor visitor) { - Arrays.fill(marks, false); - marks[node] = true; - depthFirstSearchStep(node, visitor); - } - - private boolean depthFirstSearchStep(int node, Visitor visitor) { - if (visitor.visit(node)) { - return true; - } - for (int j = 0; j < size; j++) { - if (!marks[j] && a[indexOf(node, j)] != NIL) { - marks[j] = true; - if (depthFirstSearchStep(j, visitor)) { - return true; - } - } - } - return false; - } - - private int indexOf(int i, int j) { - return i * size + j; - } - - public static interface Visitor { - boolean visit(int node); - } -} diff --git a/src/main/java/com/burakovv/algorithms/HeapSort.java b/src/main/java/com/burakovv/algorithms/HeapSort.java deleted file mode 100644 index b3ad194..0000000 --- a/src/main/java/com/burakovv/algorithms/HeapSort.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.ComparableData; - -public class HeapSort { - - public static void sort(ComparableData data) { - int size = data.getSize(); - for (int i = size / 2; i >= 0; i--) { - heapify(data, i, size); - } - for (int i = size - 1; i > 0; i--) { - data.swap(0, i); - heapify(data, 0, i); - } - } - - private static void heapify(ComparableData data, int i, int size) { - int dataOffset = data.getOffset(); - int left = 2 * i; - int right = left + 1; - int max = i; - if (left < size && data.compare(dataOffset + max, dataOffset + left) < 0) { - max = left; - } - if (right < size && data.compare(dataOffset + max, dataOffset + right) < 0) { - max = right; - } - if (max != i) { - data.swap(dataOffset + i, dataOffset + max); - heapify(data, max, size); - } - } - -} diff --git a/src/main/java/com/burakovv/algorithms/InsertionSort.java b/src/main/java/com/burakovv/algorithms/InsertionSort.java deleted file mode 100644 index 5eb92cc..0000000 --- a/src/main/java/com/burakovv/algorithms/InsertionSort.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.ComparableData; - -public class InsertionSort { - - public static void sort(ComparableData data) { - int offset = data.getOffset(); - int size = data.getSize(); - for (int i = offset + 1, limit = offset + size; i < limit; i++) { - int j = i - 1; - while (j >= offset && data.compare(j, j + 1) > 0) { - data.swap(j, j + 1); - j--; - } - } - - } -} diff --git a/src/main/java/com/burakovv/algorithms/OrderStatistic.java b/src/main/java/com/burakovv/algorithms/OrderStatistic.java deleted file mode 100644 index 870911a..0000000 --- a/src/main/java/com/burakovv/algorithms/OrderStatistic.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.ComparableData; - -public class OrderStatistic { - - /** - * This method can modify input data. - * @return the index of element that shall be on {@code k} position after sorting the data - * (but without actually sorting the data) - */ - public static int select(ComparableData data, int k) { - return select(data.getOffset() + k, data, data.getOffset(), data.getOffset() + data.getSize() - 1); - } - - private static int select(int k, ComparableData data, int left, int right) { - if (left == right) { - return k; - } - int p = QuickSort.randomPartition(data, left, right); - if (p == k) { - return p; - } else if (p < k) { - return select(k, data, p + 1, right); - } else { - return select(k, data, left, p - 1); - } - } - -} diff --git a/src/main/java/com/burakovv/algorithms/OrderStatisticTree.java b/src/main/java/com/burakovv/algorithms/OrderStatisticTree.java deleted file mode 100644 index 52bc400..0000000 --- a/src/main/java/com/burakovv/algorithms/OrderStatisticTree.java +++ /dev/null @@ -1,298 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.DynamicOrderStatistic; - -import java.util.Comparator; - -public class OrderStatisticTree implements DynamicOrderStatistic { - - private static final Comparator DEFAULT_COMPARATOR = new Comparator() { - @Override - public int compare(Object o1, Object o2) { - return ((Comparable) o1).compareTo(o2); - } - }; - - final Comparator comparator; - final Node NIL = createNil(); - Node root = NIL; - - public OrderStatisticTree() { - this.comparator = DEFAULT_COMPARATOR; - } - - public OrderStatisticTree(Comparator comparator) { - this.comparator = comparator; - } - - private Node createNil() { - Node node = new Node(null); - node.size = 0; - node.color = Color.BLACK; - return node; - } - - public void add(E key) { - Node y = NIL; - Node x = root; - int lastComparisonResult = -1; - while (x != NIL) { - x.size++; - y = x; - lastComparisonResult = comparator.compare(key, x.key); - x = lastComparisonResult < 0 ? x.left : x.right; - } - Node newNode = new Node(key); - if (y == NIL) { - root = newNode; - } else if (lastComparisonResult < 0) { - y.left = newNode; - newNode.parent = y; - } else { - y.right = newNode; - newNode.parent = y; - } - newNode.color = Color.RED; - addFixup(newNode); - } - - private void addFixup(Node z) { - while (z.parent.color == Color.RED) { - if (z.parent == z.parent.parent.left) { - if (z.parent.parent.right.color == Color.RED) { - z.parent.color = Color.BLACK; - z = z.parent.parent; - z.right.color = Color.BLACK; - z.color = Color.RED; - } else { - if (z == z.parent.right) { - z = z.parent; - leftRotate(z); - } - z.parent.color = Color.BLACK; - z.parent.parent.color = Color.RED; - rightRotate(z.parent.parent); - } - } else { - if (z.parent.parent.left.color == Color.RED) { - z.parent.color = Color.BLACK; - z = z.parent.parent; - z.left.color = Color.BLACK; - z.color = Color.RED; - } else { - if (z == z.parent.left) { - z = z.parent; - rightRotate(z); - } - z.parent.color = Color.BLACK; - z.parent.parent.color = Color.RED; - leftRotate(z.parent.parent); - } - } - } - root.color = Color.BLACK; - } - - public boolean remove(E key) { - Node z = search(key); - if (z == NIL) { - return false; - } - Node y = (z.left != NIL && z.right != NIL) ? successor(z) : z; - //y can't have both children by definition (successor can not have left child in this situation) - Node yChild = (y.left != NIL) ? y.left : y.right; - yChild.parent = y.parent; //Even if yChild is NIL - if (y.parent == NIL) { - root = yChild; - } else if (y.parent.left == y) { - y.parent.left = yChild; - } else { - y.parent.right = yChild; - } - if (y != z) { - z.key = y.key; - } - Node sizeFixup = y.parent; - while (sizeFixup != NIL) { - updateSize(sizeFixup); - sizeFixup = sizeFixup.parent; - } - if (y.color == Color.BLACK) { - removeFixup(yChild); - } - return true; - } - - private void removeFixup(Node x) { - while (x != root && x.color == Color.BLACK) { - if (x == x.parent.left) { - Node w = x.parent.right; - if (w.color == Color.RED) { - w.color = Color.BLACK; - x.parent.color = Color.RED; - leftRotate(x.parent); - w = x.parent.right; - } - if (w.left.color == Color.BLACK && w.right.color == Color.BLACK) { - w.color = Color.RED; - x = x.parent; - } else { - if (w.right.color == Color.BLACK) { - w.left.color = Color.BLACK; - w.color = Color.RED; - rightRotate(w); - w = x.parent.right; - } - //w is black, w.right is red - w.color = x.parent.color; - x.parent.color = w.right.color = Color.BLACK; - leftRotate(x.parent); - x = root; - } - } else { - Node w = x.parent.left; - if (w.color == Color.RED) { - w.color = Color.BLACK; - x.parent.color = Color.RED; - rightRotate(x.parent); - w = x.parent.left; - } - if (w.right.color == Color.BLACK && w.left.color == Color.BLACK) { - w.color = Color.RED; - x = x.parent; - } else { - if (w.left.color == Color.BLACK) { - w.right.color = Color.BLACK; - w.color = Color.RED; - leftRotate(w); - w = x.parent.left; - } - //w is black, w.left is red - w.color = x.parent.color; - x.parent.color = w.left.color = Color.BLACK; - rightRotate(x.parent); - x = root; - } - } - } - x.color = Color.BLACK; - } - - private Node search(E key) { - Node node = root; - while (node != NIL) { - int compareResult = compare(key, node); - if (compareResult == 0) { - return node; - } else if (compareResult < 0) { - node = node.left; - } else { - node = node.right; - } - } - return NIL; - } - - private int compare(E key, Node node) { - return comparator.compare(key, node.key); - } - - private Node successor(Node node) { - if (node.right != NIL) { - return min(node.right); - } - Node parent = node.parent; - while (parent != NIL && parent.right == node) { - node = parent; - parent = parent.parent; - } - return parent; - } - - private Node min(Node node) { - while (node.left != NIL) { - node = node.left; - } - return node; - } - - private void leftRotate(Node x) { - Node y = x.right; - x.right = y.left; - if (y.left != NIL) { - y.left.parent = x; - } - y.parent = x.parent; - if (x.parent == NIL) { - root = y; - } else if (x.parent.left == x) { - x.parent.left = y; - } else { - x.parent.right = y; - } - x.parent = y; - y.left = x; - updateSize(x); - updateSize(y); - } - - private void rightRotate(Node y) { - Node x = y.left; - y.left = x.right; - if (y.left != NIL) { - y.left.parent = y; - } - x.right = y; - x.parent = y.parent; - if (y.parent == NIL) { - root = x; - } else if (y.parent.left == y) { - y.parent.left = x; - } else { - y.parent.right = x; - } - y.parent = x; - updateSize(y); - updateSize(x); - } - - private void updateSize(Node node) { - node.size = node.left.size + node.right.size + 1; - } - - @Override - public E select(int k) { - return select(root, k + 1).key; - } - - private Node select(Node root, int k) { - int r = root.left.size + 1; - if (r == k) { - return root; - } else if (r > k) { - return select(root.left, k); - } else { - return select(root.right, k - r); - } - } - - final class Node { - T key; - Node left = NIL; - Node right = NIL; - Node parent = NIL; - Color color; - - /** The size of sub-tree with this node as the root */ - int size = 1; - - public Node(T key) { - this.key = key; - } - } - - static enum Color { - BLACK, - RED, - } -} diff --git a/src/main/java/com/burakovv/algorithms/QuickSort.java b/src/main/java/com/burakovv/algorithms/QuickSort.java deleted file mode 100644 index 0a51e2b..0000000 --- a/src/main/java/com/burakovv/algorithms/QuickSort.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.ComparableData; - -import java.util.Random; - -public class QuickSort { - private static final Random random = new Random(31415926); - - public static void sort(ComparableData data) { - sort(data, data.getOffset(), data.getOffset() + data.getSize() - 1); - } - - private static void sort(ComparableData data, int left, int right) { - if (left >= right) { - return; - } - int k = randomPartition(data, left, right); - sort(data, left, k - 1); - sort(data, k + 1, right); - } - - public static int randomPartition(ComparableData data, int left, int right) { - int k = left + random.nextInt(right - left); - data.swap(k, right); - return partition(data, left, right); - } - - public static int partition(ComparableData data, int left, int right) { - int i = left; - for (int j = left; j <= right; j++) { - if (data.compare(j, right) <= 0) { - data.swap(i, j); - i++; - } - } - return i - 1; - } - -} diff --git a/src/main/java/com/burakovv/algorithms/RadixSort.java b/src/main/java/com/burakovv/algorithms/RadixSort.java deleted file mode 100644 index 9eff23d..0000000 --- a/src/main/java/com/burakovv/algorithms/RadixSort.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.NumberData; - -import java.util.Arrays; - -public class RadixSort { - - /** - *
{@code data.getBufferSize() >= data.getSize()}
- * {@code data} is sorted - */ - public static void sort(NumberData data) { - int[] count = new int[10]; - long m = 1; - boolean nonzeroValuesFound; - do { - nonzeroValuesFound = false; - Arrays.fill(count, 0); - for (int i = 0, limit = data.getSize(); i < limit; i++) { - long number = data.getNumber(data.getOffset() + i) / m; - if (number > 0) { - nonzeroValuesFound = true; - } - count[((int) (number % 10))]++; - } - if (nonzeroValuesFound) { - for (int i = 0, limit = data.getSize(); i < limit; i++) { - data.swap(data.getOffset() + i, data.getBufferOffset() + i); - } - for (int i = 1; i < count.length; i++) { - count[i] += count[i - 1]; - } - for (int i = data.getSize() - 1; i >= 0; i--) { - data.swap(data.getBufferOffset() + i, data.getOffset() + (count[((int) ((data.getNumber(data.getBufferOffset() + i) / m) % 10))]--) - 1); - } - m *= 10; - } - } while (nonzeroValuesFound); - } - -} diff --git a/src/main/java/com/burakovv/algorithms/RedBlackTreeSet.java b/src/main/java/com/burakovv/algorithms/RedBlackTreeSet.java deleted file mode 100644 index d2c198e..0000000 --- a/src/main/java/com/burakovv/algorithms/RedBlackTreeSet.java +++ /dev/null @@ -1,288 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.CustomSet; - -import java.util.Comparator; - -public class RedBlackTreeSet implements CustomSet { - static Node NIL = createNil(); - - private static Node createNil() { - Node node = new Node(null); - node.color = Color.BLACK; - return node; - } - - private final Comparator comparator; - Node root = NIL; - - public RedBlackTreeSet(Comparator comparator) { - this.comparator = comparator; - } - - @Override - public boolean add(E value) { - Node y = NIL; - Node x = root; - int lastComparisonResult = -1; - while (x != NIL) { - y = x; - lastComparisonResult = comparator.compare(value, x.value); - if (lastComparisonResult == 0) { - return false; - } - x = lastComparisonResult < 0 ? x.left : x.right; - } - Node newNode = new Node(value); - if (y == NIL) { - root = newNode; - } else if (lastComparisonResult < 0) { - makeLeftChild(y, newNode); - } else { - makeRightChild(y, newNode); - } - newNode.color = Color.RED; - addFixup(newNode); - return true; - } - - private void addFixup(Node z) { - while (z.parent.color == Color.RED) { - if (z.parent == z.parent.parent.left) { - if (z.parent.parent.right.color == Color.RED) { - z.parent.color = Color.BLACK; - z = z.parent.parent; - z.right.color = Color.BLACK; - z.color = Color.RED; - } else { - if (z == z.parent.right) { - z = z.parent; - leftRotate(z); - } - z.parent.color = Color.BLACK; - z.parent.parent.color = Color.RED; - rightRotate(z.parent.parent); - } - } else { - if (z.parent.parent.left.color == Color.RED) { - z.parent.color = Color.BLACK; - z = z.parent.parent; - z.left.color = Color.BLACK; - z.color = Color.RED; - } else { - if (z == z.parent.left) { - z = z.parent; - rightRotate(z); - } - z.parent.color = Color.BLACK; - z.parent.parent.color = Color.RED; - leftRotate(z.parent.parent); - } - } - } - root.color = Color.BLACK; - } - - @Override - public boolean remove(Object value) { - try { - return removeE((E) value); - } catch (ClassCastException e) { - return false; - } - } - - private boolean removeE(E value) { - Node z = search(value); - if (z == NIL) { - return false; - } - Node y = (z.left != NIL && z.right != NIL) ? successor(z) : z; - //y can't have both children by definition (successor can not have left child in this situation) - Node yChild = (y.left != NIL) ? y.left : y.right; - yChild.parent = y.parent; //Even if yChild is NIL - if (y.parent == NIL) { - root = yChild; - } else if (y.parent.left == y) { - y.parent.left = yChild; - } else { - y.parent.right = yChild; - } - if (y != z) { - z.value = y.value; - } - if (y.color == Color.BLACK) { - removeFixup(yChild); - } - return true; - } - - private void removeFixup(Node x) { - while (x != root && x.color == Color.BLACK) { - if (x == x.parent.left) { - Node w = x.parent.right; - if (w.color == Color.RED) { - w.color = Color.BLACK; - x.parent.color = Color.RED; - leftRotate(x.parent); - w = x.parent.right; - } - if (w.left.color == Color.BLACK && w.right.color == Color.BLACK) { - w.color = Color.RED; - x = x.parent; - } else { - if (w.right.color == Color.BLACK) { - w.left.color = Color.BLACK; - w.color = Color.RED; - rightRotate(w); - w = x.parent.right; - } - //w is black, w.right is red - w.color = x.parent.color; - x.parent.color = w.right.color = Color.BLACK; - leftRotate(x.parent); - x = root; - } - } else { - Node w = x.parent.left; - if (w.color == Color.RED) { - w.color = Color.BLACK; - x.parent.color = Color.RED; - rightRotate(x.parent); - w = x.parent.left; - } - if (w.right.color == Color.BLACK && w.left.color == Color.BLACK) { - w.color = Color.RED; - x = x.parent; - } else { - if (w.left.color == Color.BLACK) { - w.right.color = Color.BLACK; - w.color = Color.RED; - leftRotate(w); - w = x.parent.left; - } - //w is black, w.left is red - w.color = x.parent.color; - x.parent.color = w.left.color = Color.BLACK; - rightRotate(x.parent); - x = root; - } - } - } - x.color = Color.BLACK; - } - - @Override - public boolean contains(E value) { - return search(value) != NIL; - } - - private Node search(E value) { - Node node = root; - while (node != NIL) { - int compareResult = comparator.compare(value, node.value); - if (compareResult == 0) { - return node; - } else if (compareResult < 0) { - node = node.left; - } else { - node = node.right; - } - } - return NIL; - } - - private Node successor(Node node) { - if (node.right != NIL) { - return min(node.right); - } - Node parent = node.parent; - while (parent != NIL && parent.right == node) { - node = parent; - parent = parent.parent; - } - return parent; - } - - private Node min(Node node) { - while (node.left != NIL) { - node = node.left; - } - return node; - } - - private static void makeLeftChild(Node parent, Node leftChild) { - if (parent.left != NIL) { - throw new IllegalArgumentException("Parent already has left child"); - } - if (leftChild.parent != NIL) { - throw new IllegalArgumentException("Child already has a parent"); - } - parent.left = leftChild; - leftChild.parent = parent; - } - - private static void makeRightChild(Node parent, Node rightChild) { - if (parent.right != NIL) { - throw new IllegalArgumentException("Parent already has right child"); - } - if (rightChild.parent != NIL) { - throw new IllegalArgumentException("Child already has a parent"); - } - parent.right = rightChild; - rightChild.parent = parent; - } - - private void leftRotate(Node x) { - Node y = x.right; - x.right = y.left; - if (y.left != NIL) { - y.left.parent = x; - } - y.parent = x.parent; - if (x.parent == NIL) { - root = y; - } else if (x.parent.left == x) { - x.parent.left = y; - } else { - x.parent.right = y; - } - x.parent = y; - y.left = x; - } - - private void rightRotate(Node y) { - Node x = y.left; - y.left = x.right; - if (y.left != NIL) { - y.left.parent = y; - } - x.right = y; - x.parent = y.parent; - if (y.parent == NIL) { - root = x; - } else if (y.parent.left == y) { - y.parent.left = x; - } else { - y.parent.right = x; - } - y.parent = x; - } - - static final class Node { - T value; - Node left = NIL; - Node right = NIL; - Node parent = NIL; - Color color; - - public Node(T value) { - this.value = value; - } - } - - static enum Color { - BLACK, - RED, - } -} diff --git a/src/main/java/com/burakovv/algorithms/SegmentTreeRMQ.java b/src/main/java/com/burakovv/algorithms/SegmentTreeRMQ.java deleted file mode 100644 index c4f5015..0000000 --- a/src/main/java/com/burakovv/algorithms/SegmentTreeRMQ.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.burakovv.algorithms; - -public class SegmentTreeRMQ { - private final Composer composer; - private final E[] a; - private final int offset; - - public SegmentTreeRMQ(Composer composer, int capacity) { - this.composer = composer; - capacity = validCapacity(capacity); - a = (E[]) new Object[capacity * 2 - 1]; - offset = capacity - 1; - } - - public void set(int index, E value) { - int i = offset + index; - a[i] = value; - while (i > 0) { - int p = parent(i); - E newValue = compose(a[left(p)], a[right(p)]); - if (a[p] == newValue) { - break; - } - a[p] = newValue; - i = p; - } - } - - protected int parent(int i) { - return (i - 1) / 2; - } - - protected int left(int p) { - return 2 * p + 1; - } - - protected int right(int p) { - return 2 * p + 2; - } - - public E composeRange(int left, int right) { - return composeRange(left, right, 0, 0, offset); - } - - private E composeRange(int left, int right, int node, int nodeLeft, int nodeRight) { - if (left <= nodeLeft && right >= nodeRight) { - return a[node]; - } - int middle = (nodeLeft + nodeRight) / 2; - - E leftRange = null; - boolean hasLeftRange = false; - - if (middle >= left) { - hasLeftRange = true; - leftRange = composeRange(left, right, left(node), nodeLeft, middle); - } - - E rightRange; - - if (middle < right) { - rightRange = composeRange(left, right, right(node), middle + 1, nodeRight); - if (hasLeftRange) { - return compose(leftRange, rightRange); - } else { - return rightRange; - } - } else { - return leftRange; - } - } - - private E compose(E a, E b) { - return (a == null || b == null) ? null : composer.compose(a, b); - } - - private static int validCapacity(int capacity) { - int d = 1; - while (d < capacity) { - d = d * 2; - } - return d; - } - - public static interface Composer { - E compose(E a, E b); - } - -} diff --git a/src/main/java/com/burakovv/algorithms/SparseTableRMQ.java b/src/main/java/com/burakovv/algorithms/SparseTableRMQ.java deleted file mode 100644 index b6daad1..0000000 --- a/src/main/java/com/burakovv/algorithms/SparseTableRMQ.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.burakovv.algorithms; - -import java.util.ArrayList; -import java.util.List; - -/** - * Allows to perform function on range of predefined sequence of elements by the time of O(1) (and O(n * log(n)) preprocessing time) - */ -public class SparseTableRMQ { - private final Function function; - private final E[][] a; - - public static Builder newBuilder(Function function) { - return new Builder(function); - } - - private SparseTableRMQ(Function function, List list) { - this.function = function; - int m = getArraysCount(list.size()); - a = (E[][]) new Object[m][]; - a[0] = (E[]) list.toArray(); - int i = 1; - int d = 1; - int n = list.size() - d; - while (n > 0) { - a[i] = (E[]) new Object[n]; - for (int j = 0; j < n; j++) { - a[i][j] = function.combine(a[i - 1][j], a[i - 1][j + d]); - } - d *= 2; - n -= d; - i++; - } - } - - private int getArraysCount(int n) { - int d = 1; - int total = 1; - while (n > 0) { - n = n - d; - d *= 2; - total++; - } - return total; - } - - public E composeRange(int left, int right) { - int diff = right - left + 1; - int i = 0; - int d = 1; - while (d * 2 <= diff) { - d *= 2; - i++; - } - return function.combine(a[i][left], a[i][right - d + 1]); - } - - public static class Builder { - private final ArrayList list = new ArrayList(); - private Function function; - - private Builder(Function function) { - this.function = function; - } - - public Builder add(E value) { - list.add(value); - return this; - } - - public Builder add(E... values) { - for (int i = 0; i < values.length; i++) { - list.add(values[i]); - } - return this; - } - - public SparseTableRMQ build() { - return new SparseTableRMQ(function, list); - } - } - - public interface Function { - E combine(E a, E b); - } -} diff --git a/src/main/java/com/burakovv/data/ComparableData.java b/src/main/java/com/burakovv/data/ComparableData.java deleted file mode 100644 index f03d07b..0000000 --- a/src/main/java/com/burakovv/data/ComparableData.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.burakovv.data; - -/** - * Introduces {@code compare()} method for OrderedData elements - */ -public interface ComparableData extends OrderedData { - - /** - * @return result of comparing element in cell {@code i} with element in cell {@code j} - */ - int compare(int i, int j); - -} diff --git a/src/main/java/com/burakovv/data/CustomMap.java b/src/main/java/com/burakovv/data/CustomMap.java deleted file mode 100644 index 9bac448..0000000 --- a/src/main/java/com/burakovv/data/CustomMap.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.burakovv.data; - -public interface CustomMap { - - V get(Object key); - - V put(K key, V value); - - V remove(Object key); - -} diff --git a/src/main/java/com/burakovv/data/CustomSet.java b/src/main/java/com/burakovv/data/CustomSet.java deleted file mode 100644 index 8cd33e4..0000000 --- a/src/main/java/com/burakovv/data/CustomSet.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.burakovv.data; - -public interface CustomSet { - boolean add(E value); - - boolean remove(Object value); - - boolean contains(E value); -} diff --git a/src/main/java/com/burakovv/data/CustomSetFromMap.java b/src/main/java/com/burakovv/data/CustomSetFromMap.java deleted file mode 100644 index 0dd092d..0000000 --- a/src/main/java/com/burakovv/data/CustomSetFromMap.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.burakovv.data; - -import com.burakovv.data.CustomMap; -import com.burakovv.data.CustomSet; - -public class CustomSetFromMap implements CustomSet { - private static final Object PRESENT = new Object(); - protected final CustomMap map; - - public CustomSetFromMap(CustomMap map) { - this.map = map; - } - - public boolean add(E value) { - return map.put(value, PRESENT) != PRESENT; - } - - public boolean remove(Object value) { - return map.remove(value) == PRESENT; - } - - public boolean contains(E value) { - return map.get(value) == PRESENT; - } - - public CustomMap getMap() { - return map; - } -} diff --git a/src/main/java/com/burakovv/data/DynamicOrderStatistic.java b/src/main/java/com/burakovv/data/DynamicOrderStatistic.java deleted file mode 100644 index 9692e34..0000000 --- a/src/main/java/com/burakovv/data/DynamicOrderStatistic.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.burakovv.data; - -public interface DynamicOrderStatistic { - - void add(E element); - - boolean remove(E element); - - E select(int k); - -} diff --git a/src/main/java/com/burakovv/data/IntOption.java b/src/main/java/com/burakovv/data/IntOption.java deleted file mode 100644 index ef907cf..0000000 --- a/src/main/java/com/burakovv/data/IntOption.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.burakovv.data; - -public abstract class IntOption { - - public static IntOption of(int value) { - return new Valued(value); - } - - public static IntOption empty() { - return Null.INSTANCE; - } - - private IntOption() {} - - public abstract boolean hasValue(); - public abstract int getValue(); - - private static final class Valued extends IntOption { - private final int value; - - private Valued(int value) { - this.value = value; - } - - @Override - public boolean hasValue() { - return false; - } - - @Override - public int getValue() { - return value; - } - } - - private static final class Null extends IntOption { - private static final Null INSTANCE = new Null(); - - @Override - public boolean hasValue() { - return false; - } - - @Override - public int getValue() { - throw new UnsupportedOperationException(); - } - } - -} diff --git a/src/main/java/com/burakovv/data/NumberData.java b/src/main/java/com/burakovv/data/NumberData.java deleted file mode 100644 index 1967b79..0000000 --- a/src/main/java/com/burakovv/data/NumberData.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.burakovv.data; - -public interface NumberData extends ComparableData { - - /** - * @return the number associated with given index - */ - long getNumber(int index); - -} diff --git a/src/main/java/com/burakovv/data/OrderedData.java b/src/main/java/com/burakovv/data/OrderedData.java deleted file mode 100644 index 96552b0..0000000 --- a/src/main/java/com/burakovv/data/OrderedData.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.burakovv.data; - -/** - * Provides access to abstract ordered data. Designed to hide actual data type. Such representation allows to manipulate with - * collection of elements without having actual references to any of them. It makes possible to create implementations with Objects - * as well as with primitive types omitting generalization and useless autoboxing overhead. There are a lot of algorithms where storing - * reference to element is necessary. There is a special area called "buffer" for this purpose. - * - *
- * +---------------------------+---------+
- * |            Data           | Buffer  |
- * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- * | | | | | | | | | | | | | | | | | | | |
- * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- * 
- */ -public interface OrderedData { - - /** - * @return the index of the first element - */ - int getOffset(); - - /** - * @return the number of data elements - */ - int getSize(); - - /** - * @return the index of first buffer cell - */ - int getBufferOffset(); - - /** - * @return the size of the buffer - */ - int getBufferSize(); - - /** - * Simultaneously moves element from cell {@code i} to cell {@code j} and element from cell {@code j} to cell {@code i} - */ - void swap(int i, int j); - -} diff --git a/src/main/java/com/burakovv/data/impl/IntArrayDataWrapper.java b/src/main/java/com/burakovv/data/impl/IntArrayDataWrapper.java deleted file mode 100644 index d1d075c..0000000 --- a/src/main/java/com/burakovv/data/impl/IntArrayDataWrapper.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.burakovv.data.impl; - -import com.burakovv.data.NumberData; - -public class IntArrayDataWrapper implements NumberData { - private final int[] data; - private final int offset; - private final int size; - private final int bufferOffset; - private final int bufferSize; - - public IntArrayDataWrapper(int[] data, int offset, int size, int bufferOffset, int bufferSize) { - this.data = data; - this.offset = offset; - this.size = size; - this.bufferOffset = bufferOffset; - this.bufferSize = bufferSize; - } - - @Override - public int compare(int i, int j) { - return Integer.compare(data[i], data[j]); - } - - @Override - public int getOffset() { - return offset; - } - - @Override - public int getSize() { - return size; - } - - @Override - public int getBufferOffset() { - return bufferOffset; - } - - @Override - public int getBufferSize() { - return bufferSize; - } - - @Override - public void swap(int i, int j) { - int tmp = data[i]; - data[i] = data[j]; - data[j] = tmp; - } - - public void set(int index, int value) { - data[index] = value; - } - - @Override - public long getNumber(int index) { - return data[index]; - } -} diff --git a/src/test/java/com/burakovv/algorithms/AbstractSearchTest.java b/src/test/java/com/burakovv/algorithms/AbstractSearchTest.java deleted file mode 100644 index 2988a4e..0000000 --- a/src/test/java/com/burakovv/algorithms/AbstractSearchTest.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.ComparableData; -import com.burakovv.data.impl.IntArrayDataWrapper; -import org.junit.After; -import org.junit.Assert; -import org.junit.Test; - -import java.util.Arrays; -import java.util.Random; - -import static org.junit.Assert.assertTrue; - -public abstract class AbstractSearchTest { - - protected static volatile long maxTime = 0; - - @After - public void tearDown() { - System.out.println("Max sorting time: " + maxTime); - - } - - @Test - public void testEmptyArray() { - assertSortSucceeded(new int[0]); - } - - @Test - public void testSmallArray() { - assertSortSucceeded(new int[] {1, 1, 2, 3, 5, 7, 12, 19}); - assertSortSucceeded(new int[] {2, 19, 3, 6, 1, 4, 7, 12}); - } - - @Test - public void testBoundaryValues() { - assertSortSucceeded(new int[] { 0, Integer.MIN_VALUE, Integer.MAX_VALUE }); - assertSortSucceeded(new int[] { 0, Integer.MAX_VALUE, Integer.MIN_VALUE }); - assertSortSucceeded(new int[] { Integer.MIN_VALUE, 0, Integer.MAX_VALUE }); - assertSortSucceeded(new int[] { Integer.MAX_VALUE, 0, Integer.MIN_VALUE }); - assertSortSucceeded(new int[] { Integer.MIN_VALUE, Integer.MAX_VALUE, 0 }); - assertSortSucceeded(new int[] { Integer.MAX_VALUE, Integer.MIN_VALUE, 0 }); - } - - @Test - public void testLargeArray() { - int[] array = new int[isFast() ? 1000000 : 10000]; - for (int i = 0; i < 5; i++) { - fillWithRandomData(array, i); - assertSortSucceeded(array); - } - } - - protected boolean isFast() { - return true; - } - - private void fillWithRandomData(int[] array, int source) { - Random random = new Random(source * 17); - for (int i = 0; i < array.length; i++) { - array[i] = random.nextInt(); - } - array[random.nextInt(array.length)] = Integer.MIN_VALUE; - array[random.nextInt(array.length)] = Integer.MAX_VALUE; - array[random.nextInt(array.length)] = 0; - } - - private void assertSortSucceeded(int[] array) { - preprocessArray(array); - int requiredBufferSize = getRequiredBufferSize(array.length); - int[] copy = Arrays.copyOf(array, array.length + requiredBufferSize); - IntArrayDataWrapper data = new IntArrayDataWrapper(copy, 0, array.length, array.length, requiredBufferSize); - long startedAt = System.currentTimeMillis(); - sort(data); - long endAt = System.currentTimeMillis(); - updateMaxTime(endAt - startedAt); - assertSorted(copy, array.length); - assertEquals(array, copy); - } - - protected void preprocessArray(int[] array) {} - - private void updateMaxTime(long delay) { - maxTime = Math.max(maxTime, delay); - } - - protected int getRequiredBufferSize(int length) { - return 0; - } - - private void assertEquals(int[] a, int[] b) { - Arrays.sort(a); - for (int i = 0; i < a.length; i++) { - Assert.assertEquals(a[i], b[i]); - } - } - - private void assertSorted(int[] array, int length) { - for (int i = 1; i < length; i++) { - assertTrue(array[i] >= array[i - 1]); - } - } - - protected abstract void sort(ComparableData comparableData); - -} diff --git a/src/test/java/com/burakovv/algorithms/AvlTreeSetTest.java b/src/test/java/com/burakovv/algorithms/AvlTreeSetTest.java deleted file mode 100644 index bd244cf..0000000 --- a/src/test/java/com/burakovv/algorithms/AvlTreeSetTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.CustomSet; -import com.burakovv.data.CustomSetFromMap; -import org.junit.Assert; - -import java.util.Comparator; - -public class AvlTreeSetTest extends CustomSetTest { - private static final boolean IS_SPEED_TEST = false; - - @Override - protected CustomSet createCustomSet(Comparator comparator) { - return new CustomSetFromMap(new AvlTreeMap(comparator)); - } - - @Override - protected void afterIteration(CustomSet customSet) { - super.afterIteration(customSet); - if (!IS_SPEED_TEST) { - checkBalanced(((AvlTreeMap) ((CustomSetFromMap) customSet).getMap()).root, 0); - } - } - - public static int checkBalanced(AvlTreeMap.Node node, int current) { - if (node == null) { - return current; - } - Assert.assertTrue(Math.abs((node.left == null ? 0 : node.left.height) - (node.right == null ? 0 : node.right.height)) < 2); - int max = Math.max( - checkBalanced(node.left, current + 1), - checkBalanced(node.right, current + 1)); - Assert.assertEquals(node.height, max - current); - return max; - } - - @Override - protected int getRandomTestIterations() { - return IS_SPEED_TEST ? 5000000 : 50000; - } - - @Override - protected int getIncDecTestIterations() { - return IS_SPEED_TEST ? 5000000 : 50000; - } - - @Override - protected int getRandomRange() { - return IS_SPEED_TEST ? 100000 : 1000; - } -} diff --git a/src/test/java/com/burakovv/algorithms/BinarySearchTest.java b/src/test/java/com/burakovv/algorithms/BinarySearchTest.java deleted file mode 100644 index 112eb70..0000000 --- a/src/test/java/com/burakovv/algorithms/BinarySearchTest.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.impl.IntArrayDataWrapper; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class BinarySearchTest { - - @Test - public void testSearch() throws Exception { - final int[] data = new int[]{1, 3, 5, 7}; - assertEquals(-1, search(0, data)); - assertEquals(0, search(1, data)); - assertEquals(-2, search(2, data)); - assertEquals(1, search(3, data)); - assertEquals(-3, search(4, data)); - assertEquals(2, search(5, data)); - assertEquals(-4, search(6, data)); - assertEquals(3, search(7, data)); - assertEquals(-5, search(8, data)); - } - - private int search(int key, int[] data) { - return search(key, data, 0, data.length); - } - - private int search(int key, int[] data, int offset, int size) { - IntArrayDataWrapper intArrayDataWrapper = new IntArrayDataWrapper(Arrays.copyOf(data, data.length + 1), offset, size, size, 1); - intArrayDataWrapper.set(intArrayDataWrapper.getBufferOffset(), key); - return BinarySearch.search(intArrayDataWrapper); - } - - @Test - public void testCustomOffset() { - final int[] array = {0, 3, 4, 6, 1, 2, 4, 8}; - assertEquals(1, search(3, array, 0, 4)); - assertEquals(1, search(3, array, 0, 3)); - assertEquals(1, search(3, array, 1, 2)); - assertEquals(-2, search(2, array, 0, 4)); - assertEquals(5, search(2, array, 4, 4)); - assertEquals(-4, search(5, array, 0, 4)); - assertEquals(-8, search(5, array, 4, 4)); - assertEquals(-1, search(0, array, 0, 0)); - assertEquals(-6, search(0, array, 5, 0)); - } - - @Test - public void testBoundaryValues() { - { - final int[] array = {Integer.MIN_VALUE, Integer.MAX_VALUE}; - assertEquals(0, search(Integer.MIN_VALUE, array)); - assertEquals(-2, search(0, array)); - assertEquals(1, search(Integer.MAX_VALUE, array)); - } - - { - final int[] array = {-2147483648, -1073741823, 1073741823, 2147483647}; - assertEquals(0, search(-2147483648, array)); - assertEquals(1, search(-1073741823, array)); - assertEquals(2, search(1073741823, array)); - assertEquals(3, search(2147483647, array)); - } - } - -} diff --git a/src/test/java/com/burakovv/algorithms/BinarySearchTreeSetTest.java b/src/test/java/com/burakovv/algorithms/BinarySearchTreeSetTest.java deleted file mode 100644 index 6f8ea8a..0000000 --- a/src/test/java/com/burakovv/algorithms/BinarySearchTreeSetTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.CustomSet; -import com.burakovv.data.CustomSetFromMap; - -import java.util.Comparator; - -public class BinarySearchTreeSetTest extends CustomSetTest { - - @Override - protected CustomSet createCustomSet(Comparator comparator) { - return new CustomSetFromMap(new BinarySearchTreeMap(comparator)); - } - -} diff --git a/src/test/java/com/burakovv/algorithms/BinarySearchTreeTest.java b/src/test/java/com/burakovv/algorithms/BinarySearchTreeTest.java deleted file mode 100644 index 7b98cc1..0000000 --- a/src/test/java/com/burakovv/algorithms/BinarySearchTreeTest.java +++ /dev/null @@ -1,265 +0,0 @@ -package com.burakovv.algorithms; - -import org.junit.Test; - -import java.lang.reflect.Array; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Random; - -import static com.burakovv.algorithms.BinarySearchTree.*; -import static org.junit.Assert.*; - -public class BinarySearchTreeTest { - - @Test - public void testTree0() { - BinarySearchTree tree = create( - new int[] { 3 }, - new int[][] { - { NIL, NIL }, - }, 0 - ); - tree.inorderTreeWalk(tree.getRoot(), new TestVisitor(0)); - assertEquals(0, tree.search(tree.getRoot(), 3)); - assertEquals(0, tree.min(0)); - assertEquals(0, tree.max(0)); - assertEquals(NIL, tree.successor(0)); - assertEquals(NIL, tree.predecessor(0)); - assertEquals(0, tree.remove(0)); - } - - @Test - public void testTree1() { - BinarySearchTree tree = create( - new int[] { 2, 1 }, - new int[][] { - { NIL, NIL }, - { NIL, 0 }, - }, 1 - ); - tree.inorderTreeWalk(tree.getRoot(), new TestVisitor(1, 0)); - assertEquals(1, tree.search(tree.getRoot(), 1)); - assertEquals(0, tree.search(tree.getRoot(), 2)); - assertEquals(1, tree.min(tree.getRoot())); - assertEquals(0, tree.max(tree.getRoot())); - assertEquals(NIL, tree.successor(0)); - assertEquals(1, tree.predecessor(0)); - assertEquals(0, tree.successor(1)); - assertEquals(NIL, tree.predecessor(1)); - } - - @Test - public void testTree2() { - BinarySearchTree tree = create( - new int[] { 3, 1, 4 }, - new int[][] { - { 1, NIL }, - { NIL, NIL }, - { 0, NIL }, - }, 2 - ); - tree.inorderTreeWalk(tree.getRoot(), new TestVisitor(1, 0, 2)); - assertEquals(0, tree.search(tree.getRoot(), 3)); - assertEquals(1, tree.search(tree.getRoot(), 1)); - assertEquals(2, tree.search(tree.getRoot(), 4)); - assertEquals(1, tree.min(tree.getRoot())); - assertEquals(2, tree.max(tree.getRoot())); - assertEquals(1, tree.predecessor(0)); - assertEquals(2, tree.successor(0)); - assertEquals(NIL, tree.predecessor(1)); - assertEquals(0, tree.successor(1)); - assertEquals(0, tree.predecessor(2)); - assertEquals(NIL, tree.successor(2)); - } - - @Test - public void testTree3() { - BinarySearchTree tree = create( - new int[] { 3, 1, 4 }, - new int[][] { - { NIL, NIL }, - { NIL, 0 }, - { 1, NIL }, - }, 2 - ); - tree.inorderTreeWalk(tree.getRoot(), new TestVisitor(1, 0, 2)); - assertEquals(0, tree.search(tree.getRoot(), 3)); - assertEquals(1, tree.search(tree.getRoot(), 1)); - assertEquals(2, tree.search(tree.getRoot(), 4)); - assertEquals(1, tree.min(tree.getRoot())); - assertEquals(2, tree.max(tree.getRoot())); - assertEquals(1, tree.predecessor(0)); - assertEquals(2, tree.successor(0)); - assertEquals(NIL, tree.predecessor(1)); - assertEquals(0, tree.successor(1)); - assertEquals(0, tree.predecessor(2)); - assertEquals(NIL, tree.successor(2)); - } - - @Test - public void testTree4() { - BinarySearchTree tree = create( - new int[] { 3, 1, 4 }, - new int[][] { - { 1, 2 }, - { NIL, NIL }, - { NIL, NIL }, - }, 0 - ); - tree.inorderTreeWalk(tree.getRoot(), new TestVisitor(1, 0, 2)); - assertEquals(0, tree.search(tree.getRoot(), 3)); - assertEquals(1, tree.search(tree.getRoot(), 1)); - assertEquals(2, tree.search(tree.getRoot(), 4)); - assertEquals(1, tree.min(tree.getRoot())); - assertEquals(2, tree.max(tree.getRoot())); - assertEquals(1, tree.predecessor(0)); - assertEquals(2, tree.successor(0)); - assertEquals(NIL, tree.predecessor(1)); - assertEquals(0, tree.successor(1)); - assertEquals(0, tree.predecessor(2)); - assertEquals(NIL, tree.successor(2)); - } - - @Test - public void testTree5() { - BinarySearchTree tree = create( - new int[] { 3, 1, 4 }, - new int[][] { - { NIL, NIL }, - { NIL, 2 }, - { 0, NIL }, - }, 1 - ); - tree.inorderTreeWalk(tree.getRoot(), new TestVisitor(1, 0, 2)); - assertEquals(0, tree.search(tree.getRoot(), 3)); - assertEquals(1, tree.search(tree.getRoot(), 1)); - assertEquals(2, tree.search(tree.getRoot(), 4)); - assertEquals(1, tree.min(tree.getRoot())); - assertEquals(2, tree.max(tree.getRoot())); - assertEquals(1, tree.predecessor(0)); - assertEquals(2, tree.successor(0)); - assertEquals(NIL, tree.predecessor(1)); - assertEquals(0, tree.successor(1)); - assertEquals(0, tree.predecessor(2)); - assertEquals(NIL, tree.successor(2)); - } - - @Test - public void testTree6() { - BinarySearchTree tree = create( - new int[] { 3, 1, 4 }, - new int[][] { - { NIL, 2 }, - { NIL, 0 }, - { NIL, NIL }, - }, 1 - ); - tree.inorderTreeWalk(tree.getRoot(), new TestVisitor(1, 0, 2)); - assertEquals(0, tree.search(tree.getRoot(), 3)); - assertEquals(1, tree.search(tree.getRoot(), 1)); - assertEquals(2, tree.search(tree.getRoot(), 4)); - assertEquals(1, tree.min(tree.getRoot())); - assertEquals(2, tree.max(tree.getRoot())); - assertEquals(1, tree.predecessor(0)); - assertEquals(2, tree.successor(0)); - assertEquals(NIL, tree.predecessor(1)); - assertEquals(0, tree.successor(1)); - assertEquals(0, tree.predecessor(2)); - assertEquals(NIL, tree.successor(2)); - } - - @Test - public void testRemove() { - BinarySearchTree tree = create( - new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, - new int[][] { - { NIL, 1 }, - { NIL, 3 }, - { NIL, NIL }, - { 2, NIL }, - { 0, 9 }, - { NIL, 7 }, - { NIL, NIL }, - { 6, 8 }, - { NIL, NIL }, - { 5, NIL }, - }, 4 - ); - tree.inorderTreeWalk(tree.getRoot(), new RemoveTestVisitor(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); - tree.remove(tree.search(tree.getRoot(), 1+4)); - tree.inorderTreeWalk(tree.getRoot(), new RemoveTestVisitor(0, 1, 2, 3, 5, 6, 7, 8, 9)); - tree.remove(tree.search(tree.getRoot(), 1+2)); - tree.inorderTreeWalk(tree.getRoot(), new RemoveTestVisitor(0, 1, 3, 5, 6, 7, 8, 9)); - tree.remove(tree.search(tree.getRoot(), 1+9)); - tree.inorderTreeWalk(tree.getRoot(), new RemoveTestVisitor(0, 1, 3, 5, 6, 7, 8)); - tree.remove(tree.search(tree.getRoot(), 1+0)); - tree.inorderTreeWalk(tree.getRoot(), new RemoveTestVisitor(1, 3, 5, 6, 7, 8)); - tree.remove(tree.search(tree.getRoot(), 1+7)); - tree.inorderTreeWalk(tree.getRoot(), new RemoveTestVisitor(1, 3, 5, 6, 8)); - tree.remove(tree.search(tree.getRoot(), 1+3)); - tree.inorderTreeWalk(tree.getRoot(), new RemoveTestVisitor(1, 5, 6, 8)); - tree.remove(tree.search(tree.getRoot(), 1+1)); - tree.inorderTreeWalk(tree.getRoot(), new RemoveTestVisitor(5, 6, 8)); - tree.remove(tree.search(tree.getRoot(), 1+8)); - tree.inorderTreeWalk(tree.getRoot(), new RemoveTestVisitor(5, 6)); - tree.remove(tree.search(tree.getRoot(), 1+6)); - tree.inorderTreeWalk(tree.getRoot(), new RemoveTestVisitor(5)); - } - - @Test - public void testInsert() { - BinarySearchTree tree = new BinarySearchTree(10); - tree.inorderTreeWalk(tree.getRoot(), new TestVisitor()); - Random random = new Random(42); - ArrayList list = new ArrayList(10); - ArrayList control = new ArrayList(10); - for (int i = 1; i <= 10; i++) { - list.add(i); - } - Collections.shuffle(list, random); - for (int i = 0; i < 10; i++) { - int val = list.get(i); - tree.insert(val); - control.add(val); - Collections.sort(control); - int[] array = new int[control.size()]; - for (int j = 0; j < control.size(); j++) { - array[j] = control.get(j); - } - tree.inorderTreeWalk(tree.getRoot(), new TestVisitor(array) { - @Override - public void visit(BinarySearchTree tree, int node) { - super.visit(tree, tree.getValue(node)); - } - }); - } - } - - private class TestVisitor implements Visitor { - private final int[] vertexes; - private int next; - - public TestVisitor(int... vertexes) { - this.vertexes = vertexes; - this.next = 0; - } - - @Override - public void visit(BinarySearchTree tree, int node) { - assertEquals(vertexes[next], node); - next++; - } - } - - private class RemoveTestVisitor extends TestVisitor { - public RemoveTestVisitor(int... vertexes) { - super(vertexes); - } - - @Override - public void visit(BinarySearchTree tree, int node) { - super.visit(tree, tree.getValue(node) - 1); - } - } -} diff --git a/src/test/java/com/burakovv/algorithms/CustomSetTest.java b/src/test/java/com/burakovv/algorithms/CustomSetTest.java deleted file mode 100644 index 2ab981a..0000000 --- a/src/test/java/com/burakovv/algorithms/CustomSetTest.java +++ /dev/null @@ -1,169 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.CustomSet; -import org.junit.Test; - -import java.util.Comparator; -import java.util.Random; -import java.util.TreeSet; - -import static org.junit.Assert.assertEquals; - -public abstract class CustomSetTest { - - protected int getRandomTestIterations() { - return 10000000; - } - - protected int getIncDecTestIterations() { - return 40000; - } - - protected int getRandomRange() { - return 100000; - } - - @Test - public void testRandom() { - doTest(new Provider() { - private final Random random = new Random(31415926); - - @Override - public int nextValue() { - return random.nextInt(getRandomRange()); - } - - @Override - public int nextAction() { - return random.nextInt(3); - } - - @Override - public int getIterations() { - return getRandomTestIterations(); - } - }); - } - - @Test - public void testInc() { - doTest(new Provider() { - private int next = 0; - private final Random random = new Random(31415926); - - @Override - public int nextValue() { - int result = next; - next = (next + 1) % 100000; - return result; - } - - @Override - public int nextAction() { - return random.nextInt(3); - } - - @Override - public int getIterations() { - return getIncDecTestIterations(); - } - }); - } - - @Test - public void testDec() { - doTest(new Provider() { - private int next = 99999; - private final Random random = new Random(31415926); - - @Override - public int nextValue() { - int result = next; - next = next == 0 ? 99999 : next - 1; - return result; - } - - @Override - public int nextAction() { - return random.nextInt(3); - } - - @Override - public int getIterations() { - return getIncDecTestIterations(); - } - }); - } - - private void doTest(Provider provider) { - CustomSet customSet = createCustomSet(Comparator.naturalOrder()); - TreeSet systemSet = new TreeSet(); - - long totalSystemTime = 0; - long totalCustomTime = 0; - - for (int i = 0; i < provider.getIterations(); i++) { - int action = provider.nextAction(); - int value = provider.nextValue(); - boolean expected = false; - boolean actual = false; - long startAt; - long entAt; - switch (action) { - case 0: - startAt = System.currentTimeMillis(); - expected = systemSet.add(value); - entAt = System.currentTimeMillis(); - totalSystemTime += entAt - startAt; - - startAt = System.currentTimeMillis(); - actual = customSet.add(value); - entAt = System.currentTimeMillis(); - totalCustomTime += entAt - startAt; - - break; - case 1: - startAt = System.currentTimeMillis(); - expected = systemSet.contains(value); - entAt = System.currentTimeMillis(); - totalSystemTime += entAt - startAt; - - startAt = System.currentTimeMillis(); - actual = customSet.contains(value); - entAt = System.currentTimeMillis(); - totalCustomTime += entAt - startAt; - - break; - case 2: - startAt = System.currentTimeMillis(); - expected = systemSet.remove(value); - entAt = System.currentTimeMillis(); - totalSystemTime += entAt - startAt; - - startAt = System.currentTimeMillis(); - actual = customSet.remove(value); - entAt = System.currentTimeMillis(); - totalCustomTime += entAt - startAt; - break; - } - assertEquals(expected, actual); - - afterIteration(customSet); - } - - System.out.println("Total system time: " + totalSystemTime); - System.out.println("Total custom time: " + totalCustomTime); - } - - protected void afterIteration(CustomSet customSet) { - //Designed for overriding - } - - protected abstract CustomSet createCustomSet(Comparator comparator); - - private static interface Provider { - int nextValue(); - int nextAction(); - int getIterations(); - } -} diff --git a/src/test/java/com/burakovv/algorithms/DynamicOrderStatisticTest.java b/src/test/java/com/burakovv/algorithms/DynamicOrderStatisticTest.java deleted file mode 100644 index e4424f2..0000000 --- a/src/test/java/com/burakovv/algorithms/DynamicOrderStatisticTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.DynamicOrderStatistic; -import org.junit.Assert; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; -import java.util.Random; - -public class DynamicOrderStatisticTest { - - @Test - public void testRandom() { - Comparator integerComparator = Comparator.naturalOrder(); - DynamicOrderStatistic statistic = createDynamicOrderStatistic(integerComparator); - List sample = new ArrayList(); - Random random = new Random(21303242); - for (int i = 0; i < 1000000; i++) { - Integer element = random.nextInt(1000); - switch (random.nextInt(5)) { - case 0: - statistic.add(element); - sample.add(element); - sample.sort(integerComparator); - break; - default: - statistic.remove(element); - sample.remove(element); - break; - } - if (!sample.isEmpty()) { - Integer k = random.nextInt(sample.size()); - Assert.assertEquals(statistic.select(k), sample.get(k)); - } - } - } - - private DynamicOrderStatistic createDynamicOrderStatistic(Comparator integerComparator) { - return new OrderStatisticTree(integerComparator); - } - -} diff --git a/src/test/java/com/burakovv/algorithms/GraphTest.java b/src/test/java/com/burakovv/algorithms/GraphTest.java deleted file mode 100644 index 76325c9..0000000 --- a/src/test/java/com/burakovv/algorithms/GraphTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.burakovv.algorithms; - -import org.junit.Assert; -import org.junit.Test; - -public class GraphTest extends Assert { - - @Test - public void testSearch() { - Graph graph = new Graph(5); - graph.connectDirectly(0, 1, 1); - graph.connectDirectly(0, 2, 1); - graph.connectDirectly(1, 2, 1); - graph.connectDirectly(1, 3, 1); - graph.connectDirectly(3, 4, 1); - graph.connectDirectly(4, 2, 1); - - TestVisitor visitor = new TestVisitor(new int[]{0}, new int[]{1, 2}, new int[]{3}, new int[]{4}); - graph.breadthFirstSearch(0, visitor); - visitor.assertFinished(); - - visitor = new TestVisitor(new int[]{0, 1, 2, 3, 4}); - graph.depthFirstSearch(0, visitor); - visitor.assertFinished(); - } - - private class TestVisitor implements Graph.Visitor { - private final int[][] steps; - private final int[] sizes; - private int index; - - public TestVisitor(int[]... steps) { - this.steps = steps; - this.sizes = new int[steps.length]; - for (int i = 0; i < sizes.length; i++) { - sizes[i] = steps[i].length; - } - index = 0; - } - - @Override - public boolean visit(int node) { - assertNotEquals(Graph.NIL, node); - int i = 0; - while (i < steps[index].length && steps[index][i] != node) { - i++; - } - assertTrue(i < steps[index].length); - steps[index][i] = Graph.NIL; - sizes[index]--; - while (index < sizes.length && sizes[index] == 0) { - index++; - } - return false; - } - - public void assertFinished() { - assertFalse(index < sizes.length); - } - } -} diff --git a/src/test/java/com/burakovv/algorithms/HeapSortTest.java b/src/test/java/com/burakovv/algorithms/HeapSortTest.java deleted file mode 100644 index e31d104..0000000 --- a/src/test/java/com/burakovv/algorithms/HeapSortTest.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.ComparableData; - -public class HeapSortTest extends AbstractSearchTest { - @Override - protected void sort(ComparableData comparableData) { - HeapSort.sort(comparableData); - } -} diff --git a/src/test/java/com/burakovv/algorithms/InsertionSortTest.java b/src/test/java/com/burakovv/algorithms/InsertionSortTest.java deleted file mode 100644 index 617265a..0000000 --- a/src/test/java/com/burakovv/algorithms/InsertionSortTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.ComparableData; - -public class InsertionSortTest extends AbstractSearchTest { - @Override - protected void sort(ComparableData comparableData) { - InsertionSort.sort(comparableData); - } - - @Override - protected boolean isFast() { - return false; - } -} diff --git a/src/test/java/com/burakovv/algorithms/OrderStatisticTest.java b/src/test/java/com/burakovv/algorithms/OrderStatisticTest.java deleted file mode 100644 index ed1fc79..0000000 --- a/src/test/java/com/burakovv/algorithms/OrderStatisticTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.ComparableData; -import com.burakovv.data.impl.IntArrayDataWrapper; -import org.junit.Assert; -import org.junit.Test; - -import java.util.Random; - -public class OrderStatisticTest extends Assert { - - @Test - public void testSmallSequences() { - Random random = new Random(31415927); - int[] a = new int[8]; - int[] b = new int[a.length]; - for (int size = 1; size <= a.length; size++) { - for (int i = 0; i < size; i++) { - a[i] = random.nextInt(); - b[i] = a[i]; - } - QuickSort.sort(new IntArrayDataWrapper(b, 0, size, size, 0)); - ComparableData data = new IntArrayDataWrapper(a, 0, size, size, 0); - for (int test = 0; test < 1000; test++) { - for (int i = 0; i < size - 1; i++) { - for (int j = i + 1; j < size; j++) { - if (random.nextBoolean()) { - data.swap(i, j); - } - } - } - int k = random.nextInt(size); - int i = OrderStatistic.select(data, k); - assertEquals(a[i], b[k]); - } - } - } - -} diff --git a/src/test/java/com/burakovv/algorithms/QuickSortTest.java b/src/test/java/com/burakovv/algorithms/QuickSortTest.java deleted file mode 100644 index 8724a01..0000000 --- a/src/test/java/com/burakovv/algorithms/QuickSortTest.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.ComparableData; - -public class QuickSortTest extends AbstractSearchTest { - @Override - protected void sort(ComparableData comparableData) { - QuickSort.sort(comparableData); - } -} diff --git a/src/test/java/com/burakovv/algorithms/RadixSortTest.java b/src/test/java/com/burakovv/algorithms/RadixSortTest.java deleted file mode 100644 index 56c636c..0000000 --- a/src/test/java/com/burakovv/algorithms/RadixSortTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.ComparableData; -import com.burakovv.data.NumberData; - -public class RadixSortTest extends AbstractSearchTest { - - @Override - protected void sort(ComparableData data) { - RadixSort.sort((NumberData) data); - } - - @Override - protected int getRequiredBufferSize(int length) { - return length; - } - - @Override - protected void preprocessArray(int[] array) { - for (int i = 0; i < array.length; i++) { - array[i] = (array[i] == Integer.MIN_VALUE) ? Integer.MAX_VALUE : Math.abs(array[i]); - } - } -} diff --git a/src/test/java/com/burakovv/algorithms/RedBlackTreeSetTest.java b/src/test/java/com/burakovv/algorithms/RedBlackTreeSetTest.java deleted file mode 100644 index 791e222..0000000 --- a/src/test/java/com/burakovv/algorithms/RedBlackTreeSetTest.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.burakovv.algorithms; - -import com.burakovv.data.CustomSet; -import org.junit.Assert; - -import java.util.Comparator; - -import static com.burakovv.algorithms.RedBlackTreeSet.NIL; - -public class RedBlackTreeSetTest extends CustomSetTest { - private static final boolean IS_SPEED_TEST = false; - - @Override - protected CustomSet createCustomSet(Comparator comparator) { - return new RedBlackTreeSet(comparator); - } - - @Override - protected void afterIteration(CustomSet customSet) { - super.afterIteration(customSet); - if (!IS_SPEED_TEST) { - RedBlackTreeSet set = (RedBlackTreeSet) customSet; - assertCorresct(set); - Assert.assertTrue(isBalanced(set)); - } - } - - @Override - protected int getRandomTestIterations() { - return IS_SPEED_TEST ? 5000000 : 50000; - } - - @Override - protected int getIncDecTestIterations() { - return IS_SPEED_TEST ? 5000000 : 50000; - } - - @Override - protected int getRandomRange() { - return IS_SPEED_TEST ? 100000 : 1000; - } - - public static void assertCorresct(RedBlackTreeSet set) { - if (set.root.color == RedBlackTreeSet.Color.RED) { - throw new RuntimeException("The root is red"); - } - findBlacks(set.root, 0); - } - - private static int findBlacks(RedBlackTreeSet.Node node, int current) { - if (node.color == RedBlackTreeSet.Color.BLACK) { - current++; - } - if (node == NIL) { - return current; - } - if (node.color == RedBlackTreeSet.Color.RED && (node.left.color == RedBlackTreeSet.Color.RED || node.right.color == RedBlackTreeSet.Color.RED)) { - throw new RuntimeException("Red has red child"); - } - int leftBlacks = findBlacks(node.left, current); - int rightBlacks = findBlacks(node.right, current); - if (leftBlacks != rightBlacks) { - throw new RuntimeException("Tree is not balanced"); - } - return leftBlacks; - } - - public static boolean isBalanced(RedBlackTreeSet set) { - int minWay = minWayLength(set.root, 0); - int maxWay = maxWayLength(set.root, 0); - return 2 * minWay >= maxWay; - } - - private static int minWayLength(RedBlackTreeSet.Node node, int current) { - return node == NIL ? current : Math.min( - minWayLength(node.left, current + 1), - minWayLength(node.right, current + 1)); - } - - private static int maxWayLength(RedBlackTreeSet.Node node, int current) { - return node == NIL ? current : Math.max( - maxWayLength(node.left, current + 1), - maxWayLength(node.right, current + 1)); - } -} diff --git a/src/test/java/com/burakovv/algorithms/SegmentTreeRMQTest.java b/src/test/java/com/burakovv/algorithms/SegmentTreeRMQTest.java deleted file mode 100644 index e0c43fc..0000000 --- a/src/test/java/com/burakovv/algorithms/SegmentTreeRMQTest.java +++ /dev/null @@ -1,121 +0,0 @@ -package com.burakovv.algorithms; - -import junit.framework.TestCase; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.List; -import java.util.Random; - -public class SegmentTreeRMQTest extends TestCase { - - private static final TestFunction GCD_FUNCTION = new TestFunction() { - @Override - public int func(int a, int b) { - return (b == 0) ? a : func(b, a % b); - } - }; - - private static final TestFunction MIN_FUNCTION = new TestFunction() { - @Override - public int func(int a, int b) { - return Math.min(a, b); - } - }; - - private static final TestFunction MAX_FUNCTION = new TestFunction() { - @Override - public int func(int a, int b) { - return Math.max(a, b); - } - }; - - @Test - public void test() { - SegmentTreeRMQ segmentTree = new SegmentTreeRMQ(MAX_FUNCTION.createRMQFunction(), 8); - segmentTree.set(0, 1); - segmentTree.set(1, 3); - segmentTree.set(2, 2); - segmentTree.set(3, 6); - segmentTree.set(4, 2); - segmentTree.set(5, 1); - segmentTree.set(6, 5); - segmentTree.set(7, 3); - assertEquals(1, segmentTree.composeRange(0, 0).intValue()); - assertEquals(3, segmentTree.composeRange(0, 1).intValue()); - assertEquals(2, segmentTree.composeRange(2, 2).intValue()); - assertEquals(6, segmentTree.composeRange(3, 5).intValue()); - assertEquals(3, segmentTree.composeRange(1, 2).intValue()); - assertEquals(5, segmentTree.composeRange(4, 7).intValue()); - assertEquals(5, segmentTree.composeRange(4, 6).intValue()); - assertEquals(2, segmentTree.composeRange(4, 5).intValue()); - assertEquals(3, segmentTree.composeRange(7, 7).intValue()); - } - - @Test - public void testCorrectness() { - testLargeData(10000, 10000, true, GCD_FUNCTION); - testLargeData(10000, 10000, true, MAX_FUNCTION); - testLargeData(10000, 10000, true, MIN_FUNCTION); - } - - @Test(timeout = 5000) - public void testSpeed() { - testLargeData(100000, 1000000, false, GCD_FUNCTION); - testLargeData(100000, 1000000, false, MAX_FUNCTION); - testLargeData(100000, 1000000, false, MIN_FUNCTION); - } - - public void testLargeData(int size, int iterations, boolean checkCorrectness, TestFunction function) { - Random random = new Random(315926); - SegmentTreeRMQ table = new SegmentTreeRMQ(function.createRMQFunction(), size); - ArrayList list = new ArrayList(size); - for (int i = 0; i < size; i++) { - int value = random.nextInt(Integer.MAX_VALUE); - table.set(i, value); - list.add(value); - } - for (int i = 0; i < iterations; i++) { - - int index = random.nextInt(size); - int value = random.nextInt(Integer.MAX_VALUE); - table.set(index, value); - list.set(index, value); - - int left = random.nextInt(size); - int right = left + random.nextInt(size - left); - Integer select = table.composeRange(left, right); - if (checkCorrectness) { - assertEquals(function.composeRange(list, left, right), select); - } else { - assertNotNull(select); - } - } - } - - private static abstract class TestFunction { - public SegmentTreeRMQ.Composer createRMQFunction() { - return new SegmentTreeRMQ.Composer() { - @Override - public Integer compose(Integer a, Integer b) { - return func(a, b); - } - }; - } - - public Integer composeRange(List list, int left, int right) { - if (left == right) { - return list.get(left); - } - int middle = (left + right) / 2; - return funcOrNull(composeRange(list, left, middle), composeRange(list, middle + 1, right)); - } - - private Integer funcOrNull(Integer a, Integer b) { - return (a == null || b == null) ? null : func(a, b); - } - - abstract int func(int select, int select1); - } - -} \ No newline at end of file diff --git a/src/test/java/com/burakovv/algorithms/SparseTableRMQTest.java b/src/test/java/com/burakovv/algorithms/SparseTableRMQTest.java deleted file mode 100644 index b0acdf7..0000000 --- a/src/test/java/com/burakovv/algorithms/SparseTableRMQTest.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.burakovv.algorithms; - -import junit.framework.TestCase; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.List; -import java.util.Random; - -public class SparseTableRMQTest extends TestCase { - - private static final TestFunction GCD_FUNCTION = new TestFunction() { - @Override - public int func(int a, int b) { - return (b == 0) ? a : func(b, a % b); - } - }; - - private static final TestFunction MIN_FUNCTION = new TestFunction() { - @Override - public int func(int a, int b) { - return Math.min(a, b); - } - }; - - private static final TestFunction MAX_FUNCTION = new TestFunction() { - @Override - public int func(int a, int b) { - return Math.max(a, b); - } - }; - - @Test - public void test() { - SparseTableRMQ table = SparseTableRMQ.newBuilder(MAX_FUNCTION.createRMQFunction()).add(1, 3, 2, 6, 2, 1, 5, 3).build(); - assertEquals(1, table.composeRange(0, 0).intValue()); - assertEquals(3, table.composeRange(0, 1).intValue()); - assertEquals(2, table.composeRange(2, 2).intValue()); - assertEquals(6, table.composeRange(3, 5).intValue()); - assertEquals(3, table.composeRange(1, 2).intValue()); - assertEquals(5, table.composeRange(4, 7).intValue()); - assertEquals(5, table.composeRange(4, 6).intValue()); - assertEquals(2, table.composeRange(4, 5).intValue()); - assertEquals(3, table.composeRange(7, 7).intValue()); - } - - @Test - public void testCorrectness() { - testLargeData(10000, 10000, true, GCD_FUNCTION); - testLargeData(10000, 10000, true, MAX_FUNCTION); - testLargeData(10000, 10000, true, MIN_FUNCTION); - } - - @Test(timeout = 5000) - public void testSpeed() { - testLargeData(100000, 1000000, false, GCD_FUNCTION); - testLargeData(100000, 1000000, false, MAX_FUNCTION); - testLargeData(100000, 1000000, false, MIN_FUNCTION); - } - - public void testLargeData(int size, int iterations, boolean checkCorrectness, TestFunction function) { - SparseTableRMQ.Builder builder = SparseTableRMQ.newBuilder(function.createRMQFunction()); - Random random = new Random(315926); - ArrayList list = new ArrayList(size); - for (int i = 0; i < size; i++) { - int value = random.nextInt(Integer.MAX_VALUE); - builder.add(value); - list.add(value); - } - SparseTableRMQ table = builder.build(); - for (int i = 0; i < iterations; i++) { - int left = random.nextInt(size); - int right = left + random.nextInt(size - left); - Integer select = table.composeRange(left, right); - if (checkCorrectness) { - assertEquals(function.composeRange(list, left, right), select); - } else { - assertNotNull(select); - } - } - } - - private static abstract class TestFunction { - public SparseTableRMQ.Function createRMQFunction() { - return new SparseTableRMQ.Function() { - @Override - public Integer combine(Integer a, Integer b) { - return func(a, b); - } - }; - } - - public Integer composeRange(List list, int left, int right) { - if (left == right) { - return list.get(left); - } - int middle = (left + right) / 2; - return func(composeRange(list, left, middle), composeRange(list, middle + 1, right)); - } - - abstract int func(int select, int select1); - } - -} \ No newline at end of file