From 15c530558ed9c25ecf5d896a228e38a63ba5b944 Mon Sep 17 00:00:00 2001 From: Pavel Vesely <vesely@iuuk.mff.cuni.cz> Date: Fri, 29 Sep 2023 22:23:45 +0200 Subject: [PATCH] cleanup --- 01-tree_successor/cpp/Makefile | 12 - 01-tree_successor/cpp/test_main.cpp | 43 -- 01-tree_successor/cpp/tree_successor.h | 73 ---- 01-tree_successor/cpp/tree_successor_test.cpp | 54 --- 01-tree_successor/python/tree_successor.py | 46 --- .../python/tree_successor_test.py | 34 -- 01-tree_successor/task.md | 13 - 02-splay_operation/cpp/Makefile | 12 - 02-splay_operation/cpp/splay_operation.h | 158 ------- .../cpp/splay_operation_test.cpp | 192 --------- 02-splay_operation/cpp/splay_tests.txt | 62 --- 02-splay_operation/cpp/test_main.cpp | 43 -- 02-splay_operation/python/splay_operation.py | 118 ------ .../python/splay_operation_test.py | 145 ------- 02-splay_operation/python/splay_tests.txt | 62 --- 02-splay_operation/task.md | 9 - 03-splay_experiment/cpp/Makefile | 22 - 03-splay_experiment/cpp/random.h | 61 --- 03-splay_experiment/cpp/splay_experiment.cpp | 208 ---------- 03-splay_experiment/python/Makefile | 15 - .../python/splay_experiment.py | 127 ------ 03-splay_experiment/task.md | 87 ---- 04-ab_tree/cpp/Makefile | 12 - 04-ab_tree/cpp/ab_tree.h | 135 ------ 04-ab_tree/cpp/ab_tree_test.cpp | 150 ------- 04-ab_tree/cpp/test_main.cpp | 43 -- 04-ab_tree/python/ab_tree.py | 64 --- 04-ab_tree/python/ab_tree_test.py | 111 ----- 04-ab_tree/task.md | 15 - 05-ab_experiment/cpp/Makefile | 22 - 05-ab_experiment/cpp/ab_experiment.cpp | 389 ------------------ 05-ab_experiment/cpp/random.h | 59 --- 05-ab_experiment/python/Makefile | 15 - 05-ab_experiment/python/ab_experiment.py | 259 ------------ 05-ab_experiment/task.md | 84 ---- 06-matrix_transpose/cpp/Makefile | 12 - 06-matrix_transpose/cpp/matrix_tests.h | 213 ---------- 06-matrix_transpose/cpp/matrix_transpose.h | 24 -- .../cpp/matrix_transpose_test.cpp | 49 --- 06-matrix_transpose/cpp/test_main.cpp | 43 -- 06-matrix_transpose/python/matrix_tests.py | 165 -------- .../python/matrix_transpose.py | 24 -- .../python/matrix_transpose_test.py | 43 -- 06-matrix_transpose/task.md | 6 - 07-matrix_experiment/cpp/Makefile | 26 -- .../cpp/matrix_experiment_real.cpp | 90 ---- .../cpp/matrix_experiment_sim.cpp | 80 ---- 07-matrix_experiment/cpp/matrix_tests.h | 213 ---------- 07-matrix_experiment/python/Makefile | 17 - .../python/matrix_experiment_sim.py | 42 -- 07-matrix_experiment/python/matrix_tests.py | 165 -------- 07-matrix_experiment/task.md | 90 ---- 08-cuckoo_hash/cpp/Makefile | 13 - 08-cuckoo_hash/cpp/cuckoo_hash.h | 103 ----- 08-cuckoo_hash/cpp/cuckoo_hash_test.cpp | 35 -- 08-cuckoo_hash/cpp/random.h | 59 --- 08-cuckoo_hash/cpp/test_main.cpp | 43 -- 08-cuckoo_hash/python/cuckoo_hash.py | 56 --- 08-cuckoo_hash/python/cuckoo_hash_test.py | 41 -- 08-cuckoo_hash/task.md | 14 - 09-hash_experiment/cpp/Makefile | 20 - 09-hash_experiment/cpp/hash_experiment.cpp | 314 -------------- 09-hash_experiment/cpp/random.h | 61 --- 09-hash_experiment/python/Makefile | 14 - 09-hash_experiment/python/hash_experiment.py | 217 ---------- 09-hash_experiment/task.md | 76 ---- 10-find_duplicates/cpp/Makefile | 13 - 10-find_duplicates/cpp/find_duplicates.h | 19 - .../cpp/find_duplicates_test.cpp | 212 ---------- 10-find_duplicates/cpp/test_main.cpp | 43 -- 10-find_duplicates/python/find_duplicates.py | 18 - .../python/find_duplicates_test.py | 144 ------- 10-find_duplicates/task.md | 27 -- 11-kgrams/cpp/Makefile | 13 - 11-kgrams/cpp/kgrams.h | 59 --- 11-kgrams/cpp/kgrams_test.cpp | 79 ---- 11-kgrams/cpp/test_main.cpp | 43 -- 11-kgrams/python/kgrams.py | 43 -- 11-kgrams/python/kgrams_test.py | 60 --- 11-kgrams/task.md | 10 - 12-range_tree/cpp/Makefile | 13 - 12-range_tree/cpp/range_tree.h | 177 -------- 12-range_tree/cpp/range_tree_test.cpp | 81 ---- 12-range_tree/cpp/test_main.cpp | 43 -- 12-range_tree/python/range_tree.py | 129 ------ 12-range_tree/python/range_tree_test.py | 68 --- 12-range_tree/task.md | 24 -- 87 files changed, 6640 deletions(-) delete mode 100644 01-tree_successor/cpp/Makefile delete mode 100644 01-tree_successor/cpp/test_main.cpp delete mode 100644 01-tree_successor/cpp/tree_successor.h delete mode 100644 01-tree_successor/cpp/tree_successor_test.cpp delete mode 100644 01-tree_successor/python/tree_successor.py delete mode 100644 01-tree_successor/python/tree_successor_test.py delete mode 100644 01-tree_successor/task.md delete mode 100644 02-splay_operation/cpp/Makefile delete mode 100644 02-splay_operation/cpp/splay_operation.h delete mode 100644 02-splay_operation/cpp/splay_operation_test.cpp delete mode 100644 02-splay_operation/cpp/splay_tests.txt delete mode 100644 02-splay_operation/cpp/test_main.cpp delete mode 100644 02-splay_operation/python/splay_operation.py delete mode 100644 02-splay_operation/python/splay_operation_test.py delete mode 100644 02-splay_operation/python/splay_tests.txt delete mode 100644 02-splay_operation/task.md delete mode 100644 03-splay_experiment/cpp/Makefile delete mode 100644 03-splay_experiment/cpp/random.h delete mode 100644 03-splay_experiment/cpp/splay_experiment.cpp delete mode 100644 03-splay_experiment/python/Makefile delete mode 100755 03-splay_experiment/python/splay_experiment.py delete mode 100644 03-splay_experiment/task.md delete mode 100644 04-ab_tree/cpp/Makefile delete mode 100644 04-ab_tree/cpp/ab_tree.h delete mode 100644 04-ab_tree/cpp/ab_tree_test.cpp delete mode 100644 04-ab_tree/cpp/test_main.cpp delete mode 100644 04-ab_tree/python/ab_tree.py delete mode 100644 04-ab_tree/python/ab_tree_test.py delete mode 100644 04-ab_tree/task.md delete mode 100644 05-ab_experiment/cpp/Makefile delete mode 100644 05-ab_experiment/cpp/ab_experiment.cpp delete mode 100644 05-ab_experiment/cpp/random.h delete mode 100644 05-ab_experiment/python/Makefile delete mode 100755 05-ab_experiment/python/ab_experiment.py delete mode 100644 05-ab_experiment/task.md delete mode 100644 06-matrix_transpose/cpp/Makefile delete mode 100644 06-matrix_transpose/cpp/matrix_tests.h delete mode 100644 06-matrix_transpose/cpp/matrix_transpose.h delete mode 100644 06-matrix_transpose/cpp/matrix_transpose_test.cpp delete mode 100644 06-matrix_transpose/cpp/test_main.cpp delete mode 100644 06-matrix_transpose/python/matrix_tests.py delete mode 100644 06-matrix_transpose/python/matrix_transpose.py delete mode 100644 06-matrix_transpose/python/matrix_transpose_test.py delete mode 100644 06-matrix_transpose/task.md delete mode 100644 07-matrix_experiment/cpp/Makefile delete mode 100644 07-matrix_experiment/cpp/matrix_experiment_real.cpp delete mode 100644 07-matrix_experiment/cpp/matrix_experiment_sim.cpp delete mode 100644 07-matrix_experiment/cpp/matrix_tests.h delete mode 100644 07-matrix_experiment/python/Makefile delete mode 100755 07-matrix_experiment/python/matrix_experiment_sim.py delete mode 100644 07-matrix_experiment/python/matrix_tests.py delete mode 100644 07-matrix_experiment/task.md delete mode 100644 08-cuckoo_hash/cpp/Makefile delete mode 100644 08-cuckoo_hash/cpp/cuckoo_hash.h delete mode 100644 08-cuckoo_hash/cpp/cuckoo_hash_test.cpp delete mode 100644 08-cuckoo_hash/cpp/random.h delete mode 100644 08-cuckoo_hash/cpp/test_main.cpp delete mode 100644 08-cuckoo_hash/python/cuckoo_hash.py delete mode 100755 08-cuckoo_hash/python/cuckoo_hash_test.py delete mode 100644 08-cuckoo_hash/task.md delete mode 100644 09-hash_experiment/cpp/Makefile delete mode 100644 09-hash_experiment/cpp/hash_experiment.cpp delete mode 100644 09-hash_experiment/cpp/random.h delete mode 100644 09-hash_experiment/python/Makefile delete mode 100644 09-hash_experiment/python/hash_experiment.py delete mode 100644 09-hash_experiment/task.md delete mode 100644 10-find_duplicates/cpp/Makefile delete mode 100644 10-find_duplicates/cpp/find_duplicates.h delete mode 100644 10-find_duplicates/cpp/find_duplicates_test.cpp delete mode 100644 10-find_duplicates/cpp/test_main.cpp delete mode 100644 10-find_duplicates/python/find_duplicates.py delete mode 100644 10-find_duplicates/python/find_duplicates_test.py delete mode 100644 10-find_duplicates/task.md delete mode 100644 11-kgrams/cpp/Makefile delete mode 100644 11-kgrams/cpp/kgrams.h delete mode 100644 11-kgrams/cpp/kgrams_test.cpp delete mode 100644 11-kgrams/cpp/test_main.cpp delete mode 100644 11-kgrams/python/kgrams.py delete mode 100644 11-kgrams/python/kgrams_test.py delete mode 100644 11-kgrams/task.md delete mode 100644 12-range_tree/cpp/Makefile delete mode 100644 12-range_tree/cpp/range_tree.h delete mode 100644 12-range_tree/cpp/range_tree_test.cpp delete mode 100644 12-range_tree/cpp/test_main.cpp delete mode 100644 12-range_tree/python/range_tree.py delete mode 100644 12-range_tree/python/range_tree_test.py delete mode 100644 12-range_tree/task.md diff --git a/01-tree_successor/cpp/Makefile b/01-tree_successor/cpp/Makefile deleted file mode 100644 index 9feafbe..0000000 --- a/01-tree_successor/cpp/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -test: tree_successor_test - ./$< - -CXXFLAGS=-std=c++11 -O2 -Wall -Wextra -g -Wno-sign-compare - -tree_successor_test: tree_successor.h tree_successor_test.cpp test_main.cpp - $(CXX) $(CXXFLAGS) $^ -o $@ - -clean: - rm -f tree_successor_test - -.PHONY: clean test diff --git a/01-tree_successor/cpp/test_main.cpp b/01-tree_successor/cpp/test_main.cpp deleted file mode 100644 index 3f4aff0..0000000 --- a/01-tree_successor/cpp/test_main.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include <cstdlib> -#include <functional> -#include <iostream> -#include <string> -#include <utility> -#include <vector> - -using namespace std; - -extern vector<pair<string, function<void()>>> tests; - -void expect_failed(const string& message) { - cerr << "Test error: " << message << endl; - exit(1); -} - -int main(int argc, char* argv[]) { - vector<string> required_tests; - - if (argc > 1) { - required_tests.assign(argv + 1, argv + argc); - } else { - for (const auto& test : tests) - required_tests.push_back(test.first); - } - - for (const auto& required_test : required_tests) { - bool found = false; - for (const auto& test : tests) - if (required_test == test.first) { - cerr << "Running test " << required_test << endl; - test.second(); - found = true; - break; - } - if (!found) { - cerr << "Unknown test " << required_test << endl; - return 1; - } - } - - return 0; -} diff --git a/01-tree_successor/cpp/tree_successor.h b/01-tree_successor/cpp/tree_successor.h deleted file mode 100644 index d1306f6..0000000 --- a/01-tree_successor/cpp/tree_successor.h +++ /dev/null @@ -1,73 +0,0 @@ -// A node of the tree -class Node { - public: - int key; - Node* left; - Node* right; - Node* parent; - - // Constructor - Node(int key, Node* parent=nullptr) { - this->key = key; - this->parent = parent; - this->left = nullptr; - this->right = nullptr; - } -}; - -// Binary tree -class Tree { - public: - // Pointer to root of the tree; nullptr if the tree is empty. - Node* root = nullptr; - - // Insert a key into the tree. - // If the key is already present, nothing happens. - void insert(int key) { - if (!root) { - root = new Node(key); - return; - } - - Node* node = root; - while (node->key != key) { - if (key < node->key) { - if (!node->left) - node->left = new Node(key, node); - node = node->left; - } else { - if (!node->right) - node->right = new Node(key, node); - node = node->right; - } - } - } - - // Return successor of the given node. - // - // The successor of a node is the node with the next higher key. - // Return nullptr if there is no such node. - // If the argument is nullptr, return the node with the smallest key. - Node* successor(Node* node) { - // TODO: Implement - } - - // Destructor to free all allocated memory. - ~Tree() { - Node* node = root; - while (node) { - Node* next; - if (node->left) { - next = node->left; - node->left = nullptr; - } else if (node->right) { - next = node->right; - node->right = nullptr; - } else { - next = node->parent; - delete node; - } - node = next; - } - } -}; diff --git a/01-tree_successor/cpp/tree_successor_test.cpp b/01-tree_successor/cpp/tree_successor_test.cpp deleted file mode 100644 index 8b794af..0000000 --- a/01-tree_successor/cpp/tree_successor_test.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include <algorithm> -#include <functional> -#include <cstdint> -#include <string> -#include <utility> -#include <vector> - -#include "tree_successor.h" - -using namespace std; - -// If the condition is not true, report an error and halt. -#define EXPECT(condition, message) do { if (!(condition)) expect_failed(message); } while (0) -void expect_failed(const string& message); - -void test(const vector<int>& sequence) { - Tree tree; - for (const auto& element : sequence) - tree.insert(element); - - vector<int> sorted_sequence(sequence); - sort(sorted_sequence.begin(), sorted_sequence.end()); - - Node* node = tree.successor(nullptr); - for (const auto& element : sorted_sequence) { - EXPECT(node, "Expected successor " + to_string(element) + ", got nullptr"); - EXPECT(node->key == element, - "Expected successor " + to_string(element) + ", got " + to_string(node->key)); - node = tree.successor(node); - } - EXPECT(!node, "Expected no successor, got " + to_string(node->key)); -} - -vector<pair<string, function<void()>>> tests = { - {"right_path", [] - { vector<int> numbers; - for (int i = 0; i < 10000; i++) numbers.push_back((int)(7.13*i)); - test(numbers); - } - }, - {"left_path", [] - { vector<int> numbers; - for (int i = 9999; i >= 0; i--) numbers.push_back((int)(7.13*i)); - test(numbers); - } - }, - {"random_tree", [] - { vector<int> numbers = {997}; - for (int i = 2; i < 199999; i++) - numbers.push_back((numbers.back() * int64_t(997)) % 199999); - test(numbers); - } - }, -}; diff --git a/01-tree_successor/python/tree_successor.py b/01-tree_successor/python/tree_successor.py deleted file mode 100644 index d0377ab..0000000 --- a/01-tree_successor/python/tree_successor.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python3 - -class Node: - """Node in a binary tree `Tree`""" - - def __init__(self, key, left=None, right=None, parent=None): - self.key = key - self.left = left - self.right = right - self.parent = parent - -class Tree: - """A simple binary search tree""" - - def __init__(self, root=None): - self.root = root - - def insert(self, key): - """Insert key into the tree. - - If the key is already present, do nothing. - """ - if self.root is None: - self.root = Node(key) - return - - node = self.root - while node.key != key: - if key < node.key: - if node.left is None: - node.left = Node(key, parent=node) - node = node.left - else: - if node.right is None: - node.right = Node(key, parent=node) - node = node.right - - def successor(self, node=None): - """Return successor of the given node. - - The successor of a node is the node with the next greater key. - Return None if there is no such node. - If the argument is None, return the node with the smallest key. - """ - # TODO: Implement - raise NotImplementedError diff --git a/01-tree_successor/python/tree_successor_test.py b/01-tree_successor/python/tree_successor_test.py deleted file mode 100644 index 41d6914..0000000 --- a/01-tree_successor/python/tree_successor_test.py +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env python3 -import sys - -import tree_successor - -def test_sequence(sequence): - tree = tree_successor.Tree() - for i in sequence: tree.insert(i) - - node = tree.successor(None) - for element in sorted(sequence): - assert node is not None, "Expected successor {}, got None".format(element) - assert node.key == element, "Expected successor {}, got {}".format(element, node.key) - node = tree.successor(node) - assert node is None, "Expected no successor, got {}".format(node.key) - -def gen_increasing_seq(): - return [int(7.13*i) for i in range(10000)] - -tests = [ - ("right_path", lambda: test_sequence(gen_increasing_seq())), - ("left_path", lambda: test_sequence(list(reversed(gen_increasing_seq())))), - ("random_tree", lambda: test_sequence([pow(997, i, 199999) for i in range(1, 199999)])), -] - -if __name__ == "__main__": - for required_test in sys.argv[1:] or [name for name, _ in tests]: - for name, test in tests: - if name == required_test: - print("Running test {}".format(name), file=sys.stderr) - test() - break - else: - raise ValueError("Unknown test {}".format(name)) diff --git a/01-tree_successor/task.md b/01-tree_successor/task.md deleted file mode 100644 index eb2187e..0000000 --- a/01-tree_successor/task.md +++ /dev/null @@ -1,13 +0,0 @@ -Given an implementation of a simple binary search tree including parent -pointers, implement a `successor` method. The methods is given a node -and it should return another node of the tree with the next higher key -(i.e., the smallest of keys which are greater than the given one). - -- If there is no such node, it should return a null pointer. -- The given node can also be a null pointer, in which case the method should - return the smallest node in the tree. - -You can expect that `successor` method is never called on an empty tree. - -You should submit the file `tree_successor.*` (but not the -`tree_successor_test.*`). diff --git a/02-splay_operation/cpp/Makefile b/02-splay_operation/cpp/Makefile deleted file mode 100644 index 4ca8274..0000000 --- a/02-splay_operation/cpp/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -test: splay_operation_test - ./$< - -CXXFLAGS=-std=c++11 -O2 -Wall -Wextra -g -Wno-sign-compare - -splay_operation_test: splay_operation.h splay_operation_test.cpp test_main.cpp - $(CXX) $(CXXFLAGS) $^ -o $@ - -clean:: - rm -f splay_operation_test - -.PHONY: clean test diff --git a/02-splay_operation/cpp/splay_operation.h b/02-splay_operation/cpp/splay_operation.h deleted file mode 100644 index 249f3dd..0000000 --- a/02-splay_operation/cpp/splay_operation.h +++ /dev/null @@ -1,158 +0,0 @@ -// A node of the tree -class Node { - public: - int key; - Node* left; - Node* right; - Node* parent; - - // Constructor - Node(int key, Node* parent=nullptr, Node* left=nullptr, Node* right=nullptr) { - this->key = key; - this->parent = parent; - this->left = left; - this->right = right; - if (left) left->parent = this; - if (right) right->parent = this; - } -}; - -// Binary tree -class Tree { - public: - // Pointer to root of the tree; nullptr if the tree is empty. - Node* root; - - Tree(Node* root=nullptr) { - this->root = root; - } - - // Rotate the given `node` up. Perform a single rotation of the edge - // between the node and its parent, choosing left or right rotation - // appropriately. - virtual void rotate(Node* node) { - if (node->parent) { - if (node->parent->left == node) { - if (node->right) node->right->parent = node->parent; - node->parent->left = node->right; - node->right = node->parent; - } else { - if (node->left) node->left->parent = node->parent; - node->parent->right = node->left; - node->left = node->parent; - } - if (node->parent->parent) { - if (node->parent->parent->left == node->parent) - node->parent->parent->left = node; - else - node->parent->parent->right = node; - } else { - root = node; - } - - Node* original_parent = node->parent; - node->parent = node->parent->parent; - original_parent->parent = node; - } - } - - // Look up the given key in the tree, returning the - // the node with the requested key or nullptr. - Node* lookup(int key) { - // TODO: Utilize splay suitably. - Node* node = root; - while (node) { - if (node->key == key) { - return node; - } - if (key < node->key) - node = node->left; - else - node = node->right; - } - return nullptr; - } - - // Insert a key into the tree. - // If the key is already present, nothing happens. - void insert(int key) { - // TODO: Utilize splay suitably. - if (!root) { - root = new Node(key); - return; - } - - Node* node = root; - while (node->key != key) { - if (key < node->key) { - if (!node->left) - node->left = new Node(key, node); - node = node->left; - } else { - if (!node->right) - node->right = new Node(key, node); - node = node->right; - } - } - } - - // Delete given key from the tree. - // It the key is not present, nothing happens. - void remove(int key) { - // TODO: Utilize splay suitably. - Node* node = root; - while (node && node->key != key) { - if (key < node->key) - node = node->left; - else - node = node->right; - } - - if (node) { - if (node->left && node->right) { - Node* replacement = node->right; - while (replacement->left) - replacement = replacement->left; - node->key = replacement->key; - node = replacement; - } - - Node* replacement = node->left ? node->left : node->right; - if (node->parent) { - if (node->parent->left == node) node->parent->left = replacement; - else node->parent->right = replacement; - } else { - root = replacement; - } - if (replacement) - replacement->parent = node->parent; - delete node; - } - } - - // Splay the given node. - // If a single rotation needs to be performed, perform it as the last rotation - // (i.e., to move the splayed node to the root of the tree). - virtual void splay(Node* node) { - // TODO: Implement - } - - // Destructor to free all allocated memory. - ~Tree() { - Node* node = root; - while (node) { - Node* next; - if (node->left) { - next = node->left; - node->left = nullptr; - } else if (node->right) { - next = node->right; - node->right = nullptr; - } else { - next = node->parent; - delete node; - } - node = next; - } - } -}; diff --git a/02-splay_operation/cpp/splay_operation_test.cpp b/02-splay_operation/cpp/splay_operation_test.cpp deleted file mode 100644 index d1e6c99..0000000 --- a/02-splay_operation/cpp/splay_operation_test.cpp +++ /dev/null @@ -1,192 +0,0 @@ -#include <algorithm> -#include <cassert> -#include <fstream> -#include <functional> -#include <string> -#include <utility> -#include <vector> - -#include "splay_operation.h" - -using namespace std; - -// If the condition is not true, report an error and halt. -#define EXPECT(condition, message) do { if (!(condition)) expect_failed(message); } while (0) -void expect_failed(const string& message); - -// Flatten the tree: return a sorted list of all keys in the tree. -vector<int> flatten(const Tree& tree) { - constexpr int L = 0, R = 1, F = 2; - - Node* node = tree.root; - vector<int> flattened, stack = {L}; - while (!stack.empty()) { - if (stack.back() == L) { - stack.back() = R; - if (node->left) { - node = node->left; - stack.push_back(L); - } - } else if (stack.back() == R) { - flattened.push_back(node->key); - stack.back() = F; - if (node->right) { - node = node->right; - stack.push_back(L); - } - } else { - node = node->parent; - stack.pop_back(); - } - } - return flattened; -} - -// Test for splay operation with required helpers -class TestSplay { - public: - static Node* deserialize_node(const string& text, int& index) { - EXPECT(text[index++] == '(', "Internal error during example deserialization"); - if (text[index] == ')') { - index++; - return nullptr; - } else { - int comma = text.find(',', index); - int key = stoi(text.substr(index, comma - index)); - Node* left = deserialize_node(text, (index=comma + 1)); - Node* right = deserialize_node(text, ++index); - EXPECT(text[index++] == ')', "Internal error during example deserialization"); - return new Node(key, nullptr, left, right); - } - } - - static Node* deserialize_root(const string& text) { - int index = 0; - Node* root = deserialize_node(text, index); - assert(index == text.size()); - return root; - } - - static string compare(Node* system, Node* gold) { - if (!system && gold) { - return "expected node with key " + to_string(gold->key) + ", found None"; - } else if (system && !gold) { - return "expected None, found node with key " + to_string(system->key); - } else if (system && gold) { - if (system->key != gold->key) - return "expected node with key " + to_string(gold->key) + ", found " + to_string(system->key); - auto result = compare(system->left, gold->left); - if (!result.empty()) return result; - return compare(system->right, gold->right); - } - return string(); - } - - static void test() { - ifstream splay_tests_file("splay_tests.txt"); - EXPECT(splay_tests_file.is_open(), "Cannot open splay_tests.txt file with the tests"); - - string original, splayed; - int target; - while (splay_tests_file >> original >> target >> splayed) { - Tree original_tree(deserialize_root(original)); - Tree splayed_tree(deserialize_root(splayed)); - - Node* target_node = original_tree.root; - while (target_node && target_node->key != target) - if (target < target_node->key) - target_node = target_node->left; - else - target_node = target_node->right; - EXPECT(target_node, "Internal error during finding the target node in the tree to splay"); - - original_tree.splay(target_node); - auto error = compare(original_tree.root, splayed_tree.root); - EXPECT(error.empty(), "Error running splay on key " + to_string(target) + " of " + original + ": " + error); - } - } -}; - -void test_lookup() { - // Insert even numbers - Tree tree; - for (int i = 0; i < 5000000; i += 2) - tree.insert(i); - - // Find non-existing - for (int i = 1; i < 5000000; i += 2) - for (int j = 0; j < 10; j++) - EXPECT(!tree.lookup(i), "Non-existing element was found"); - - // Find existing - for (int i = 0; i < 5000000; i += 2) - for (int j = 0; j < 10; j++) - EXPECT(tree.lookup(i), "Existing element was not found"); -} - -void test_insert() { - // Test validity first - { - Tree tree; - vector<int> sequence = {997}; - for (int i = 2; i < 1999; i++) - sequence.push_back((sequence.back() * sequence.front()) % 1999); - for (const auto& i : sequence) - tree.insert(i); - - vector<int> flattened = flatten(tree); - sort(sequence.begin(), sequence.end()); - EXPECT(flattened == sequence, "Incorrect tree after a sequence of inserts"); - } - - // Test speed - { - Tree tree; - for (int i = 0; i < 5000000; i++) - for (int j = 0; j < 10; j++) - tree.insert(i); - } -} - -void test_remove() { - // Test validity first - { - Tree tree; - for (int i = 2; i < 1999 * 2; i++) - tree.insert(i); - - vector<int> sequence = {2 * 997}; - for (int i = 2; i < 1999; i++) - sequence.push_back(2 * ((sequence.back() * sequence.front() / 4) % 1999)); - for (const auto& i : sequence) - tree.remove(i + 1); - - vector<int> flattened = flatten(tree); - sort(sequence.begin(), sequence.end()); - EXPECT(flattened == sequence, "Correct tree after a sequence of removes"); - } - - // Test speed - { - Tree tree; - for (int i = 0; i < 5000000; i++) - tree.insert(i); - - // Non-existing elements - for (int i = 1; i < 5000000; i += 2) - for (int j = 0; j < 10; j++) - tree.remove(i); - - // Existing elements - for (int i = 2; i < 5000000; i += 2) - for (int j = 0; j < 10; j++) - tree.remove(i); - } -} - -vector<pair<string, function<void()>>> tests = { - { "splay", TestSplay::test }, - { "lookup", test_lookup }, - { "insert", test_insert }, - { "remove", test_remove }, -}; diff --git a/02-splay_operation/cpp/splay_tests.txt b/02-splay_operation/cpp/splay_tests.txt deleted file mode 100644 index 7f3259e..0000000 --- a/02-splay_operation/cpp/splay_tests.txt +++ /dev/null @@ -1,62 +0,0 @@ -(3,(1,(0,(),()),(2,(),())),(4,(),())) 4 (4,(3,(1,(0,(),()),(2,(),())),()),()) -(6,(5,(),()),(8,(7,(),()),(9,(),()))) 5 (5,(),(6,(),(8,(7,(),()),(9,(),())))) -(3,(1,(0,(),()),(2,(),())),(7,(5,(4,(),()),(6,(),())),(8,(),()))) 8 (8,(7,(3,(1,(0,(),()),(2,(),())),(5,(4,(),()),(6,(),()))),()),()) -(11,(9,(7,(6,(),()),(8,(),())),(10,(),())),(13,(12,(),()),(14,(),()))) 10 (10,(9,(7,(6,(),()),(8,(),())),()),(11,(),(13,(12,(),()),(14,(),())))) -(8,(6,(5,(),()),(7,(),())),(10,(9,(),()),(12,(11,(),()),(13,(),())))) 9 (9,(8,(6,(5,(),()),(7,(),())),()),(10,(),(12,(11,(),()),(13,(),())))) -(16,(12,(11,(),()),(14,(13,(),()),(15,(),()))),(18,(17,(),()),(19,(),()))) 11 (11,(),(12,(),(16,(14,(13,(),()),(15,(),())),(18,(17,(),()),(19,(),()))))) -(3,(1,(0,(),()),(2,(),())),(7,(5,(4,(),()),(6,(),())),(11,(9,(8,(),()),(10,(),())),(12,(),())))) 12 (12,(3,(1,(0,(),()),(2,(),())),(11,(7,(5,(4,(),()),(6,(),())),(9,(8,(),()),(10,(),()))),())),()) -(17,(11,(9,(8,(),()),(10,(),())),(15,(13,(12,(),()),(14,(),())),(16,(),()))),(19,(18,(),()),(20,(),()))) 16 (16,(15,(11,(9,(8,(),()),(10,(),())),(13,(12,(),()),(14,(),()))),()),(17,(),(19,(18,(),()),(20,(),())))) -(9,(7,(6,(),()),(8,(),())),(15,(13,(11,(10,(),()),(12,(),())),(14,(),())),(17,(16,(),()),(18,(),())))) 14 (14,(9,(7,(6,(),()),(8,(),())),(13,(11,(10,(),()),(12,(),())),())),(15,(),(17,(16,(),()),(18,(),())))) -(23,(19,(17,(15,(14,(),()),(16,(),())),(18,(),())),(21,(20,(),()),(22,(),()))),(25,(24,(),()),(26,(),()))) 18 (18,(17,(15,(14,(),()),(16,(),())),()),(23,(19,(),(21,(20,(),()),(22,(),()))),(25,(24,(),()),(26,(),())))) -(8,(6,(5,(),()),(7,(),())),(12,(10,(9,(),()),(11,(),())),(14,(13,(),()),(16,(15,(),()),(17,(),()))))) 13 (13,(8,(6,(5,(),()),(7,(),())),(12,(10,(9,(),()),(11,(),())),())),(14,(),(16,(15,(),()),(17,(),())))) -(22,(16,(14,(13,(),()),(15,(),())),(18,(17,(),()),(20,(19,(),()),(21,(),())))),(24,(23,(),()),(25,(),()))) 17 (17,(16,(14,(13,(),()),(15,(),())),()),(22,(18,(),(20,(19,(),()),(21,(),()))),(24,(23,(),()),(25,(),())))) -(14,(12,(11,(),()),(13,(),())),(20,(16,(15,(),()),(18,(17,(),()),(19,(),()))),(22,(21,(),()),(23,(),())))) 15 (15,(14,(12,(11,(),()),(13,(),())),()),(16,(),(20,(18,(17,(),()),(19,(),())),(22,(21,(),()),(23,(),()))))) -(28,(24,(20,(19,(),()),(22,(21,(),()),(23,(),()))),(26,(25,(),()),(27,(),()))),(30,(29,(),()),(31,(),()))) 19 (19,(),(28,(20,(),(24,(22,(21,(),()),(23,(),())),(26,(25,(),()),(27,(),())))),(30,(29,(),()),(31,(),())))) -(3,(1,(0,(),()),(2,(),())),(7,(5,(4,(),()),(6,(),())),(11,(9,(8,(),()),(10,(),())),(15,(13,(12,(),()),(14,(),())),(16,(),()))))) 16 (16,(7,(3,(1,(0,(),()),(2,(),())),(5,(4,(),()),(6,(),()))),(15,(11,(9,(8,(),()),(10,(),())),(13,(12,(),()),(14,(),()))),())),()) -(25,(15,(13,(12,(),()),(14,(),())),(19,(17,(16,(),()),(18,(),())),(23,(21,(20,(),()),(22,(),())),(24,(),())))),(27,(26,(),()),(28,(),()))) 24 (24,(15,(13,(12,(),()),(14,(),())),(23,(19,(17,(16,(),()),(18,(),())),(21,(20,(),()),(22,(),()))),())),(25,(),(27,(26,(),()),(28,(),())))) -(11,(9,(8,(),()),(10,(),())),(21,(15,(13,(12,(),()),(14,(),())),(19,(17,(16,(),()),(18,(),())),(20,(),()))),(23,(22,(),()),(24,(),())))) 20 (20,(11,(9,(8,(),()),(10,(),())),(19,(15,(13,(12,(),()),(14,(),())),(17,(16,(),()),(18,(),()))),())),(21,(),(23,(22,(),()),(24,(),())))) -(33,(29,(23,(21,(20,(),()),(22,(),())),(27,(25,(24,(),()),(26,(),())),(28,(),()))),(31,(30,(),()),(32,(),()))),(35,(34,(),()),(36,(),()))) 28 (28,(27,(23,(21,(20,(),()),(22,(),())),(25,(24,(),()),(26,(),()))),()),(29,(),(33,(31,(30,(),()),(32,(),())),(35,(34,(),()),(36,(),()))))) -(9,(7,(6,(),()),(8,(),())),(13,(11,(10,(),()),(12,(),())),(19,(17,(15,(14,(),()),(16,(),())),(18,(),())),(21,(20,(),()),(22,(),()))))) 18 (18,(13,(9,(7,(6,(),()),(8,(),())),(11,(10,(),()),(12,(),()))),(17,(15,(14,(),()),(16,(),())),())),(19,(),(21,(20,(),()),(22,(),())))) -(31,(21,(19,(18,(),()),(20,(),())),(27,(25,(23,(22,(),()),(24,(),())),(26,(),())),(29,(28,(),()),(30,(),())))),(33,(32,(),()),(34,(),()))) 26 (26,(21,(19,(18,(),()),(20,(),())),(25,(23,(22,(),()),(24,(),())),())),(31,(27,(),(29,(28,(),()),(30,(),()))),(33,(32,(),()),(34,(),())))) -(17,(15,(14,(),()),(16,(),())),(27,(23,(21,(19,(18,(),()),(20,(),())),(22,(),())),(25,(24,(),()),(26,(),()))),(29,(28,(),()),(30,(),())))) 22 (22,(17,(15,(14,(),()),(16,(),())),(21,(19,(18,(),()),(20,(),())),())),(27,(23,(),(25,(24,(),()),(26,(),()))),(29,(28,(),()),(30,(),())))) -(39,(35,(31,(29,(27,(26,(),()),(28,(),())),(30,(),())),(33,(32,(),()),(34,(),()))),(37,(36,(),()),(38,(),()))),(41,(40,(),()),(42,(),()))) 30 (30,(29,(27,(26,(),()),(28,(),())),()),(35,(31,(),(33,(32,(),()),(34,(),()))),(39,(37,(36,(),()),(38,(),())),(41,(40,(),()),(42,(),()))))) -(8,(6,(5,(),()),(7,(),())),(12,(10,(9,(),()),(11,(),())),(16,(14,(13,(),()),(15,(),())),(18,(17,(),()),(20,(19,(),()),(21,(),())))))) 17 (17,(12,(8,(6,(5,(),()),(7,(),())),(10,(9,(),()),(11,(),()))),(16,(14,(13,(),()),(15,(),())),())),(18,(),(20,(19,(),()),(21,(),())))) -(30,(20,(18,(17,(),()),(19,(),())),(24,(22,(21,(),()),(23,(),())),(26,(25,(),()),(28,(27,(),()),(29,(),()))))),(32,(31,(),()),(33,(),()))) 25 (25,(20,(18,(17,(),()),(19,(),())),(24,(22,(21,(),()),(23,(),())),())),(30,(26,(),(28,(27,(),()),(29,(),()))),(32,(31,(),()),(33,(),())))) -(16,(14,(13,(),()),(15,(),())),(26,(20,(18,(17,(),()),(19,(),())),(22,(21,(),()),(24,(23,(),()),(25,(),())))),(28,(27,(),()),(29,(),())))) 21 (21,(16,(14,(13,(),()),(15,(),())),(20,(18,(17,(),()),(19,(),())),())),(26,(22,(),(24,(23,(),()),(25,(),()))),(28,(27,(),()),(29,(),())))) -(38,(34,(28,(26,(25,(),()),(27,(),())),(30,(29,(),()),(32,(31,(),()),(33,(),())))),(36,(35,(),()),(37,(),()))),(40,(39,(),()),(41,(),()))) 29 (29,(28,(26,(25,(),()),(27,(),())),()),(34,(30,(),(32,(31,(),()),(33,(),()))),(38,(36,(35,(),()),(37,(),())),(40,(39,(),()),(41,(),()))))) -(14,(12,(11,(),()),(13,(),())),(18,(16,(15,(),()),(17,(),())),(24,(20,(19,(),()),(22,(21,(),()),(23,(),()))),(26,(25,(),()),(27,(),()))))) 19 (19,(18,(14,(12,(11,(),()),(13,(),())),(16,(15,(),()),(17,(),()))),()),(20,(),(24,(22,(21,(),()),(23,(),())),(26,(25,(),()),(27,(),()))))) -(36,(26,(24,(23,(),()),(25,(),())),(32,(28,(27,(),()),(30,(29,(),()),(31,(),()))),(34,(33,(),()),(35,(),())))),(38,(37,(),()),(39,(),()))) 27 (27,(26,(24,(23,(),()),(25,(),())),()),(36,(28,(),(32,(30,(29,(),()),(31,(),())),(34,(33,(),()),(35,(),())))),(38,(37,(),()),(39,(),())))) -(22,(20,(19,(),()),(21,(),())),(32,(28,(24,(23,(),()),(26,(25,(),()),(27,(),()))),(30,(29,(),()),(31,(),()))),(34,(33,(),()),(35,(),())))) 23 (23,(22,(20,(19,(),()),(21,(),())),()),(32,(24,(),(28,(26,(25,(),()),(27,(),())),(30,(29,(),()),(31,(),())))),(34,(33,(),()),(35,(),())))) -(44,(40,(36,(32,(31,(),()),(34,(33,(),()),(35,(),()))),(38,(37,(),()),(39,(),()))),(42,(41,(),()),(43,(),()))),(46,(45,(),()),(47,(),()))) 31 (31,(),(40,(32,(),(36,(34,(33,(),()),(35,(),())),(38,(37,(),()),(39,(),())))),(44,(42,(41,(),()),(43,(),())),(46,(45,(),()),(47,(),()))))) -(3,(1,(0,(),()),(2,(),())),(7,(5,(4,(),()),(6,(),())),(11,(9,(8,(),()),(10,(),())),(15,(13,(12,(),()),(14,(),())),(19,(17,(16,(),()),(18,(),())),(20,(),())))))) 20 (20,(3,(1,(0,(),()),(2,(),())),(11,(7,(5,(4,(),()),(6,(),())),(9,(8,(),()),(10,(),()))),(19,(15,(13,(12,(),()),(14,(),())),(17,(16,(),()),(18,(),()))),()))),()) -(37,(23,(21,(20,(),()),(22,(),())),(27,(25,(24,(),()),(26,(),())),(31,(29,(28,(),()),(30,(),())),(35,(33,(32,(),()),(34,(),())),(36,(),()))))),(39,(38,(),()),(40,(),()))) 36 (36,(27,(23,(21,(20,(),()),(22,(),())),(25,(24,(),()),(26,(),()))),(35,(31,(29,(28,(),()),(30,(),())),(33,(32,(),()),(34,(),()))),())),(37,(),(39,(38,(),()),(40,(),())))) -(15,(13,(12,(),()),(14,(),())),(29,(19,(17,(16,(),()),(18,(),())),(23,(21,(20,(),()),(22,(),())),(27,(25,(24,(),()),(26,(),())),(28,(),())))),(31,(30,(),()),(32,(),())))) 28 (28,(15,(13,(12,(),()),(14,(),())),(19,(17,(16,(),()),(18,(),())),(27,(23,(21,(20,(),()),(22,(),())),(25,(24,(),()),(26,(),()))),()))),(29,(),(31,(30,(),()),(32,(),())))) -(49,(45,(35,(33,(32,(),()),(34,(),())),(39,(37,(36,(),()),(38,(),())),(43,(41,(40,(),()),(42,(),())),(44,(),())))),(47,(46,(),()),(48,(),()))),(51,(50,(),()),(52,(),()))) 44 (44,(35,(33,(32,(),()),(34,(),())),(43,(39,(37,(36,(),()),(38,(),())),(41,(40,(),()),(42,(),()))),())),(49,(45,(),(47,(46,(),()),(48,(),()))),(51,(50,(),()),(52,(),())))) -(11,(9,(8,(),()),(10,(),())),(15,(13,(12,(),()),(14,(),())),(25,(19,(17,(16,(),()),(18,(),())),(23,(21,(20,(),()),(22,(),())),(24,(),()))),(27,(26,(),()),(28,(),()))))) 24 (24,(11,(9,(8,(),()),(10,(),())),(15,(13,(12,(),()),(14,(),())),(23,(19,(17,(16,(),()),(18,(),())),(21,(20,(),()),(22,(),()))),()))),(25,(),(27,(26,(),()),(28,(),())))) -(45,(31,(29,(28,(),()),(30,(),())),(41,(35,(33,(32,(),()),(34,(),())),(39,(37,(36,(),()),(38,(),())),(40,(),()))),(43,(42,(),()),(44,(),())))),(47,(46,(),()),(48,(),()))) 40 (40,(31,(29,(28,(),()),(30,(),())),(39,(35,(33,(32,(),()),(34,(),())),(37,(36,(),()),(38,(),()))),())),(45,(41,(),(43,(42,(),()),(44,(),()))),(47,(46,(),()),(48,(),())))) -(23,(21,(20,(),()),(22,(),())),(37,(33,(27,(25,(24,(),()),(26,(),())),(31,(29,(28,(),()),(30,(),())),(32,(),()))),(35,(34,(),()),(36,(),()))),(39,(38,(),()),(40,(),())))) 32 (32,(23,(21,(20,(),()),(22,(),())),(31,(27,(25,(24,(),()),(26,(),())),(29,(28,(),()),(30,(),()))),())),(33,(),(37,(35,(34,(),()),(36,(),())),(39,(38,(),()),(40,(),()))))) -(57,(53,(49,(43,(41,(40,(),()),(42,(),())),(47,(45,(44,(),()),(46,(),())),(48,(),()))),(51,(50,(),()),(52,(),()))),(55,(54,(),()),(56,(),()))),(59,(58,(),()),(60,(),()))) 48 (48,(47,(43,(41,(40,(),()),(42,(),())),(45,(44,(),()),(46,(),()))),()),(57,(49,(),(53,(51,(50,(),()),(52,(),())),(55,(54,(),()),(56,(),())))),(59,(58,(),()),(60,(),())))) -(9,(7,(6,(),()),(8,(),())),(13,(11,(10,(),()),(12,(),())),(17,(15,(14,(),()),(16,(),())),(23,(21,(19,(18,(),()),(20,(),())),(22,(),())),(25,(24,(),()),(26,(),())))))) 22 (22,(9,(7,(6,(),()),(8,(),())),(17,(13,(11,(10,(),()),(12,(),())),(15,(14,(),()),(16,(),()))),(21,(19,(18,(),()),(20,(),())),()))),(23,(),(25,(24,(),()),(26,(),())))) -(43,(29,(27,(26,(),()),(28,(),())),(33,(31,(30,(),()),(32,(),())),(39,(37,(35,(34,(),()),(36,(),())),(38,(),())),(41,(40,(),()),(42,(),()))))),(45,(44,(),()),(46,(),()))) 38 (38,(33,(29,(27,(26,(),()),(28,(),())),(31,(30,(),()),(32,(),()))),(37,(35,(34,(),()),(36,(),())),())),(43,(39,(),(41,(40,(),()),(42,(),()))),(45,(44,(),()),(46,(),())))) -(21,(19,(18,(),()),(20,(),())),(35,(25,(23,(22,(),()),(24,(),())),(31,(29,(27,(26,(),()),(28,(),())),(30,(),())),(33,(32,(),()),(34,(),())))),(37,(36,(),()),(38,(),())))) 30 (30,(21,(19,(18,(),()),(20,(),())),(25,(23,(22,(),()),(24,(),())),(29,(27,(26,(),()),(28,(),())),()))),(35,(31,(),(33,(32,(),()),(34,(),()))),(37,(36,(),()),(38,(),())))) -(55,(51,(41,(39,(38,(),()),(40,(),())),(47,(45,(43,(42,(),()),(44,(),())),(46,(),())),(49,(48,(),()),(50,(),())))),(53,(52,(),()),(54,(),()))),(57,(56,(),()),(58,(),()))) 46 (46,(41,(39,(38,(),()),(40,(),())),(45,(43,(42,(),()),(44,(),())),())),(55,(51,(47,(),(49,(48,(),()),(50,(),()))),(53,(52,(),()),(54,(),()))),(57,(56,(),()),(58,(),())))) -(17,(15,(14,(),()),(16,(),())),(21,(19,(18,(),()),(20,(),())),(31,(27,(25,(23,(22,(),()),(24,(),())),(26,(),())),(29,(28,(),()),(30,(),()))),(33,(32,(),()),(34,(),()))))) 26 (26,(17,(15,(14,(),()),(16,(),())),(21,(19,(18,(),()),(20,(),())),(25,(23,(22,(),()),(24,(),())),()))),(31,(27,(),(29,(28,(),()),(30,(),()))),(33,(32,(),()),(34,(),())))) -(51,(37,(35,(34,(),()),(36,(),())),(47,(43,(41,(39,(38,(),()),(40,(),())),(42,(),())),(45,(44,(),()),(46,(),()))),(49,(48,(),()),(50,(),())))),(53,(52,(),()),(54,(),()))) 42 (42,(37,(35,(34,(),()),(36,(),())),(41,(39,(38,(),()),(40,(),())),())),(51,(47,(43,(),(45,(44,(),()),(46,(),()))),(49,(48,(),()),(50,(),()))),(53,(52,(),()),(54,(),())))) -(29,(27,(26,(),()),(28,(),())),(43,(39,(35,(33,(31,(30,(),()),(32,(),())),(34,(),())),(37,(36,(),()),(38,(),()))),(41,(40,(),()),(42,(),()))),(45,(44,(),()),(46,(),())))) 34 (34,(29,(27,(26,(),()),(28,(),())),(33,(31,(30,(),()),(32,(),())),())),(39,(35,(),(37,(36,(),()),(38,(),()))),(43,(41,(40,(),()),(42,(),())),(45,(44,(),()),(46,(),()))))) -(63,(59,(55,(51,(49,(47,(46,(),()),(48,(),())),(50,(),())),(53,(52,(),()),(54,(),()))),(57,(56,(),()),(58,(),()))),(61,(60,(),()),(62,(),()))),(65,(64,(),()),(66,(),()))) 50 (50,(49,(47,(46,(),()),(48,(),())),()),(63,(55,(51,(),(53,(52,(),()),(54,(),()))),(59,(57,(56,(),()),(58,(),())),(61,(60,(),()),(62,(),())))),(65,(64,(),()),(66,(),())))) -(8,(6,(5,(),()),(7,(),())),(12,(10,(9,(),()),(11,(),())),(16,(14,(13,(),()),(15,(),())),(20,(18,(17,(),()),(19,(),())),(22,(21,(),()),(24,(23,(),()),(25,(),()))))))) 21 (21,(8,(6,(5,(),()),(7,(),())),(16,(12,(10,(9,(),()),(11,(),())),(14,(13,(),()),(15,(),()))),(20,(18,(17,(),()),(19,(),())),()))),(22,(),(24,(23,(),()),(25,(),())))) -(42,(28,(26,(25,(),()),(27,(),())),(32,(30,(29,(),()),(31,(),())),(36,(34,(33,(),()),(35,(),())),(38,(37,(),()),(40,(39,(),()),(41,(),())))))),(44,(43,(),()),(45,(),()))) 37 (37,(32,(28,(26,(25,(),()),(27,(),())),(30,(29,(),()),(31,(),()))),(36,(34,(33,(),()),(35,(),())),())),(42,(38,(),(40,(39,(),()),(41,(),()))),(44,(43,(),()),(45,(),())))) -(20,(18,(17,(),()),(19,(),())),(34,(24,(22,(21,(),()),(23,(),())),(28,(26,(25,(),()),(27,(),())),(30,(29,(),()),(32,(31,(),()),(33,(),()))))),(36,(35,(),()),(37,(),())))) 29 (29,(20,(18,(17,(),()),(19,(),())),(24,(22,(21,(),()),(23,(),())),(28,(26,(25,(),()),(27,(),())),()))),(34,(30,(),(32,(31,(),()),(33,(),()))),(36,(35,(),()),(37,(),())))) -(54,(50,(40,(38,(37,(),()),(39,(),())),(44,(42,(41,(),()),(43,(),())),(46,(45,(),()),(48,(47,(),()),(49,(),()))))),(52,(51,(),()),(53,(),()))),(56,(55,(),()),(57,(),()))) 45 (45,(40,(38,(37,(),()),(39,(),())),(44,(42,(41,(),()),(43,(),())),())),(54,(50,(46,(),(48,(47,(),()),(49,(),()))),(52,(51,(),()),(53,(),()))),(56,(55,(),()),(57,(),())))) -(16,(14,(13,(),()),(15,(),())),(20,(18,(17,(),()),(19,(),())),(30,(24,(22,(21,(),()),(23,(),())),(26,(25,(),()),(28,(27,(),()),(29,(),())))),(32,(31,(),()),(33,(),()))))) 25 (25,(16,(14,(13,(),()),(15,(),())),(20,(18,(17,(),()),(19,(),())),(24,(22,(21,(),()),(23,(),())),()))),(30,(26,(),(28,(27,(),()),(29,(),()))),(32,(31,(),()),(33,(),())))) -(50,(36,(34,(33,(),()),(35,(),())),(46,(40,(38,(37,(),()),(39,(),())),(42,(41,(),()),(44,(43,(),()),(45,(),())))),(48,(47,(),()),(49,(),())))),(52,(51,(),()),(53,(),()))) 41 (41,(36,(34,(33,(),()),(35,(),())),(40,(38,(37,(),()),(39,(),())),())),(50,(46,(42,(),(44,(43,(),()),(45,(),()))),(48,(47,(),()),(49,(),()))),(52,(51,(),()),(53,(),())))) -(28,(26,(25,(),()),(27,(),())),(42,(38,(32,(30,(29,(),()),(31,(),())),(34,(33,(),()),(36,(35,(),()),(37,(),())))),(40,(39,(),()),(41,(),()))),(44,(43,(),()),(45,(),())))) 33 (33,(28,(26,(25,(),()),(27,(),())),(32,(30,(29,(),()),(31,(),())),())),(38,(34,(),(36,(35,(),()),(37,(),()))),(42,(40,(39,(),()),(41,(),())),(44,(43,(),()),(45,(),()))))) -(62,(58,(54,(48,(46,(45,(),()),(47,(),())),(50,(49,(),()),(52,(51,(),()),(53,(),())))),(56,(55,(),()),(57,(),()))),(60,(59,(),()),(61,(),()))),(64,(63,(),()),(65,(),()))) 49 (49,(48,(46,(45,(),()),(47,(),())),()),(62,(54,(50,(),(52,(51,(),()),(53,(),()))),(58,(56,(55,(),()),(57,(),())),(60,(59,(),()),(61,(),())))),(64,(63,(),()),(65,(),())))) -(14,(12,(11,(),()),(13,(),())),(18,(16,(15,(),()),(17,(),())),(22,(20,(19,(),()),(21,(),())),(28,(24,(23,(),()),(26,(25,(),()),(27,(),()))),(30,(29,(),()),(31,(),())))))) 23 (23,(14,(12,(11,(),()),(13,(),())),(22,(18,(16,(15,(),()),(17,(),())),(20,(19,(),()),(21,(),()))),())),(24,(),(28,(26,(25,(),()),(27,(),())),(30,(29,(),()),(31,(),()))))) -(48,(34,(32,(31,(),()),(33,(),())),(38,(36,(35,(),()),(37,(),())),(44,(40,(39,(),()),(42,(41,(),()),(43,(),()))),(46,(45,(),()),(47,(),()))))),(50,(49,(),()),(51,(),()))) 39 (39,(38,(34,(32,(31,(),()),(33,(),())),(36,(35,(),()),(37,(),()))),()),(48,(40,(),(44,(42,(41,(),()),(43,(),())),(46,(45,(),()),(47,(),())))),(50,(49,(),()),(51,(),())))) -(26,(24,(23,(),()),(25,(),())),(40,(30,(28,(27,(),()),(29,(),())),(36,(32,(31,(),()),(34,(33,(),()),(35,(),()))),(38,(37,(),()),(39,(),())))),(42,(41,(),()),(43,(),())))) 31 (31,(26,(24,(23,(),()),(25,(),())),(30,(28,(27,(),()),(29,(),())),())),(40,(32,(),(36,(34,(33,(),()),(35,(),())),(38,(37,(),()),(39,(),())))),(42,(41,(),()),(43,(),())))) -(60,(56,(46,(44,(43,(),()),(45,(),())),(52,(48,(47,(),()),(50,(49,(),()),(51,(),()))),(54,(53,(),()),(55,(),())))),(58,(57,(),()),(59,(),()))),(62,(61,(),()),(63,(),()))) 47 (47,(46,(44,(43,(),()),(45,(),())),()),(60,(56,(48,(),(52,(50,(49,(),()),(51,(),())),(54,(53,(),()),(55,(),())))),(58,(57,(),()),(59,(),()))),(62,(61,(),()),(63,(),())))) -(22,(20,(19,(),()),(21,(),())),(26,(24,(23,(),()),(25,(),())),(36,(32,(28,(27,(),()),(30,(29,(),()),(31,(),()))),(34,(33,(),()),(35,(),()))),(38,(37,(),()),(39,(),()))))) 27 (27,(22,(20,(19,(),()),(21,(),())),(26,(24,(23,(),()),(25,(),())),())),(36,(28,(),(32,(30,(29,(),()),(31,(),())),(34,(33,(),()),(35,(),())))),(38,(37,(),()),(39,(),())))) -(56,(42,(40,(39,(),()),(41,(),())),(52,(48,(44,(43,(),()),(46,(45,(),()),(47,(),()))),(50,(49,(),()),(51,(),()))),(54,(53,(),()),(55,(),())))),(58,(57,(),()),(59,(),()))) 43 (43,(42,(40,(39,(),()),(41,(),())),()),(56,(52,(44,(),(48,(46,(45,(),()),(47,(),())),(50,(49,(),()),(51,(),())))),(54,(53,(),()),(55,(),()))),(58,(57,(),()),(59,(),())))) -(34,(32,(31,(),()),(33,(),())),(48,(44,(40,(36,(35,(),()),(38,(37,(),()),(39,(),()))),(42,(41,(),()),(43,(),()))),(46,(45,(),()),(47,(),()))),(50,(49,(),()),(51,(),())))) 35 (35,(34,(32,(31,(),()),(33,(),())),()),(44,(36,(),(40,(38,(37,(),()),(39,(),())),(42,(41,(),()),(43,(),())))),(48,(46,(45,(),()),(47,(),())),(50,(49,(),()),(51,(),()))))) -(68,(64,(60,(56,(52,(51,(),()),(54,(53,(),()),(55,(),()))),(58,(57,(),()),(59,(),()))),(62,(61,(),()),(63,(),()))),(66,(65,(),()),(67,(),()))),(70,(69,(),()),(71,(),()))) 51 (51,(),(68,(60,(52,(),(56,(54,(53,(),()),(55,(),())),(58,(57,(),()),(59,(),())))),(64,(62,(61,(),()),(63,(),())),(66,(65,(),()),(67,(),())))),(70,(69,(),()),(71,(),())))) diff --git a/02-splay_operation/cpp/test_main.cpp b/02-splay_operation/cpp/test_main.cpp deleted file mode 100644 index 3f4aff0..0000000 --- a/02-splay_operation/cpp/test_main.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include <cstdlib> -#include <functional> -#include <iostream> -#include <string> -#include <utility> -#include <vector> - -using namespace std; - -extern vector<pair<string, function<void()>>> tests; - -void expect_failed(const string& message) { - cerr << "Test error: " << message << endl; - exit(1); -} - -int main(int argc, char* argv[]) { - vector<string> required_tests; - - if (argc > 1) { - required_tests.assign(argv + 1, argv + argc); - } else { - for (const auto& test : tests) - required_tests.push_back(test.first); - } - - for (const auto& required_test : required_tests) { - bool found = false; - for (const auto& test : tests) - if (required_test == test.first) { - cerr << "Running test " << required_test << endl; - test.second(); - found = true; - break; - } - if (!found) { - cerr << "Unknown test " << required_test << endl; - return 1; - } - } - - return 0; -} diff --git a/02-splay_operation/python/splay_operation.py b/02-splay_operation/python/splay_operation.py deleted file mode 100644 index 4e40e04..0000000 --- a/02-splay_operation/python/splay_operation.py +++ /dev/null @@ -1,118 +0,0 @@ -#!/usr/bin/env python3 - -class Node: - """Node in a binary tree `Tree`""" - - def __init__(self, key, left=None, right=None, parent=None): - self.key = key - self.parent = parent - self.left = left - self.right = right - if left is not None: left.parent = self - if right is not None: right.parent = self - -class Tree: - """A simple binary search tree""" - - def __init__(self, root=None): - self.root = root - - def rotate(self, node): - """ Rotate the given `node` up. - - Performs a single rotation of the edge between the given node - and its parent, choosing left or right rotation appropriately. - """ - if node.parent is not None: - if node.parent.left == node: - if node.right is not None: node.right.parent = node.parent - node.parent.left = node.right - node.right = node.parent - else: - if node.left is not None: node.left.parent = node.parent - node.parent.right = node.left - node.left = node.parent - if node.parent.parent is not None: - if node.parent.parent.left == node.parent: - node.parent.parent.left = node - else: - node.parent.parent.right = node - else: - self.root = node - node.parent.parent, node.parent = node, node.parent.parent - - def lookup(self, key): - """Look up the given key in the tree. - - Returns the node with the requested key or `None`. - """ - # TODO: Utilize splay suitably. - node = self.root - while node is not None: - if node.key == key: - return node - if key < node.key: - node = node.left - else: - node = node.right - return None - - def insert(self, key): - """Insert key into the tree. - - If the key is already present, nothing happens. - """ - # TODO: Utilize splay suitably. - if self.root is None: - self.root = Node(key) - return - - node = self.root - while node.key != key: - if key < node.key: - if node.left is None: - node.left = Node(key, parent=node) - node = node.left - else: - if node.right is None: - node.right = Node(key, parent=node) - node = node.right - - def remove(self, key): - """Remove given key from the tree. - - It the key is not present, nothing happens. - """ - # TODO: Utilize splay suitably. - node = self.root - while node is not None and node.key != key: - if key < node.key: - node = node.left - else: - node = node.right - - if node is not None: - if node.left is not None and node.right is not None: - replacement = node.right - while replacement.left is not None: - replacement = replacement.left - node.key = replacement.key - node = replacement - - replacement = node.left if node.left is not None else node.right - if node.parent is not None: - if node.parent.left == node: node.parent.left = replacement - else: node.parent.right = replacement - else: - self.root = replacement - if replacement is not None: - replacement.parent = node.parent - - def splay(self, node): - """Splay the given node. - - If a single rotation needs to be performed, perform it as the last rotation - (i.e., to move the splayed node to the root of the tree). - """ - # TODO: Implement - raise NotImplementedError diff --git a/02-splay_operation/python/splay_operation_test.py b/02-splay_operation/python/splay_operation_test.py deleted file mode 100644 index b342520..0000000 --- a/02-splay_operation/python/splay_operation_test.py +++ /dev/null @@ -1,145 +0,0 @@ -#!/usr/bin/env python3 -import itertools -import math -import sys - -from splay_operation import Tree, Node - -def flatten(tree): - """Flatten given tree in ascending order.""" - L, R, F = 0, 1, 2 - - node, stack, flattened = tree.root, [L], [] - while node is not None: - if stack[-1] == L: - stack[-1] = R - if node.left is not None: - node = node.left - stack.append(L) - elif stack[-1] == R: - flattened.append(node.key) - stack[-1] = F - if node.right is not None: - node = node.right - stack.append(L) - else: - node = node.parent - stack.pop() - - return flattened - -def test_splay(): - def deserialize_tree(string): - def deserialize_node(i): - assert string[i] == "(" - i += 1 - if string[i] == ")": - return i + 1, None - else: - comma = string.find(",", i) - comma2, left = deserialize_node(comma + 1) - rparen, right = deserialize_node(comma2 + 1) - assert string[rparen] == ")" - return rparen + 1, Node(int(string[i : comma]), left=left, right=right) - - index, root = deserialize_node(0) - assert index == len(string) - return Tree(root) - - def compare(system, gold): - if system is None and gold is not None: - return "expected node with key {}, found None".format(gold.key) - elif system is not None and gold is None: - return "expected None, found node with key {}".format(system.key) - elif system is not None and gold is not None: - if system.key != gold.key: - return "expected node with key {}, found {}".format(gold.key, system.key) - return compare(system.left, gold.left) or compare(system.right, gold.right) - - with open("splay_tests.txt", "r") as splay_tests_file: - for line in splay_tests_file: - original_serialized, target_serialized, splayed_serialized = line.rstrip("\n").split() - original = deserialize_tree(original_serialized) - splayed = deserialize_tree(splayed_serialized) - target = int(target_serialized) - - node = original.root - while node is not None and node.key != target: - if target < node.key: node = node.left - else: node = node.right - assert node is not None - - original.splay(node) - error = compare(original.root, splayed.root) - assert not error, "Error running splay on key {} of {}: {}".format(node.key, original_serialized, error) - -def test_lookup(): - tree = Tree() - for elem in range(0, 100000, 2): - tree.insert(elem) - - # Find non-existing - for elem in range(1, 100000, 2): - for _ in range(10): - assert tree.lookup(elem) is None, "Non-existing element was found" - - # Find existing - for elem in range(0, 100000, 2): - for _ in range(10): - assert tree.lookup(elem) is not None, "Existing element was not found" - -def test_insert(): - # Test validity first - tree = Tree() - sequence = [pow(997, i, 1999) for i in range(1, 1999)] - for elem in sequence: - tree.insert(elem) - assert flatten(tree) == sorted(sequence), "Incorrect tree after a sequence of inserts" - - # Test speed - tree = Tree() - for elem in range(200000): - for _ in range(10): - tree.insert(elem) - -def test_remove(): - # Test validity first - tree = Tree() - for elem in range(2, 1999 * 2): - tree.insert(elem) - - sequence = [2 * pow(997, i, 1999) for i in range(1, 1999)] - for elem in sequence: - tree.remove(elem + 1) - assert flatten(tree) == sorted(sequence), "Incorrect tree after a sequence of removes" - - # Test speed - tree = Tree() - for elem in range(0, 100000, 2): - tree.insert(elem) - - # Non-existing elements - for elem in range(1, 100000, 2): - for _ in range(10): - tree.remove(elem) - - # Existing elements - for elem in range(2, 100000, 2): - tree.remove(elem) - -tests = [ - ("splay", test_splay), - ("lookup", test_lookup), - ("insert", test_insert), - ("remove", test_remove), -] - -if __name__ == "__main__": - for required_test in sys.argv[1:] or [name for name, _ in tests]: - for name, test in tests: - if name == required_test: - print("Running test {}".format(name), file=sys.stderr) - test() - break - else: - raise ValueError("Unknown test {}".format(name)) diff --git a/02-splay_operation/python/splay_tests.txt b/02-splay_operation/python/splay_tests.txt deleted file mode 100644 index 7f3259e..0000000 --- a/02-splay_operation/python/splay_tests.txt +++ /dev/null @@ -1,62 +0,0 @@ -(3,(1,(0,(),()),(2,(),())),(4,(),())) 4 (4,(3,(1,(0,(),()),(2,(),())),()),()) -(6,(5,(),()),(8,(7,(),()),(9,(),()))) 5 (5,(),(6,(),(8,(7,(),()),(9,(),())))) -(3,(1,(0,(),()),(2,(),())),(7,(5,(4,(),()),(6,(),())),(8,(),()))) 8 (8,(7,(3,(1,(0,(),()),(2,(),())),(5,(4,(),()),(6,(),()))),()),()) -(11,(9,(7,(6,(),()),(8,(),())),(10,(),())),(13,(12,(),()),(14,(),()))) 10 (10,(9,(7,(6,(),()),(8,(),())),()),(11,(),(13,(12,(),()),(14,(),())))) -(8,(6,(5,(),()),(7,(),())),(10,(9,(),()),(12,(11,(),()),(13,(),())))) 9 (9,(8,(6,(5,(),()),(7,(),())),()),(10,(),(12,(11,(),()),(13,(),())))) -(16,(12,(11,(),()),(14,(13,(),()),(15,(),()))),(18,(17,(),()),(19,(),()))) 11 (11,(),(12,(),(16,(14,(13,(),()),(15,(),())),(18,(17,(),()),(19,(),()))))) -(3,(1,(0,(),()),(2,(),())),(7,(5,(4,(),()),(6,(),())),(11,(9,(8,(),()),(10,(),())),(12,(),())))) 12 (12,(3,(1,(0,(),()),(2,(),())),(11,(7,(5,(4,(),()),(6,(),())),(9,(8,(),()),(10,(),()))),())),()) -(17,(11,(9,(8,(),()),(10,(),())),(15,(13,(12,(),()),(14,(),())),(16,(),()))),(19,(18,(),()),(20,(),()))) 16 (16,(15,(11,(9,(8,(),()),(10,(),())),(13,(12,(),()),(14,(),()))),()),(17,(),(19,(18,(),()),(20,(),())))) -(9,(7,(6,(),()),(8,(),())),(15,(13,(11,(10,(),()),(12,(),())),(14,(),())),(17,(16,(),()),(18,(),())))) 14 (14,(9,(7,(6,(),()),(8,(),())),(13,(11,(10,(),()),(12,(),())),())),(15,(),(17,(16,(),()),(18,(),())))) -(23,(19,(17,(15,(14,(),()),(16,(),())),(18,(),())),(21,(20,(),()),(22,(),()))),(25,(24,(),()),(26,(),()))) 18 (18,(17,(15,(14,(),()),(16,(),())),()),(23,(19,(),(21,(20,(),()),(22,(),()))),(25,(24,(),()),(26,(),())))) -(8,(6,(5,(),()),(7,(),())),(12,(10,(9,(),()),(11,(),())),(14,(13,(),()),(16,(15,(),()),(17,(),()))))) 13 (13,(8,(6,(5,(),()),(7,(),())),(12,(10,(9,(),()),(11,(),())),())),(14,(),(16,(15,(),()),(17,(),())))) -(22,(16,(14,(13,(),()),(15,(),())),(18,(17,(),()),(20,(19,(),()),(21,(),())))),(24,(23,(),()),(25,(),()))) 17 (17,(16,(14,(13,(),()),(15,(),())),()),(22,(18,(),(20,(19,(),()),(21,(),()))),(24,(23,(),()),(25,(),())))) -(14,(12,(11,(),()),(13,(),())),(20,(16,(15,(),()),(18,(17,(),()),(19,(),()))),(22,(21,(),()),(23,(),())))) 15 (15,(14,(12,(11,(),()),(13,(),())),()),(16,(),(20,(18,(17,(),()),(19,(),())),(22,(21,(),()),(23,(),()))))) -(28,(24,(20,(19,(),()),(22,(21,(),()),(23,(),()))),(26,(25,(),()),(27,(),()))),(30,(29,(),()),(31,(),()))) 19 (19,(),(28,(20,(),(24,(22,(21,(),()),(23,(),())),(26,(25,(),()),(27,(),())))),(30,(29,(),()),(31,(),())))) -(3,(1,(0,(),()),(2,(),())),(7,(5,(4,(),()),(6,(),())),(11,(9,(8,(),()),(10,(),())),(15,(13,(12,(),()),(14,(),())),(16,(),()))))) 16 (16,(7,(3,(1,(0,(),()),(2,(),())),(5,(4,(),()),(6,(),()))),(15,(11,(9,(8,(),()),(10,(),())),(13,(12,(),()),(14,(),()))),())),()) -(25,(15,(13,(12,(),()),(14,(),())),(19,(17,(16,(),()),(18,(),())),(23,(21,(20,(),()),(22,(),())),(24,(),())))),(27,(26,(),()),(28,(),()))) 24 (24,(15,(13,(12,(),()),(14,(),())),(23,(19,(17,(16,(),()),(18,(),())),(21,(20,(),()),(22,(),()))),())),(25,(),(27,(26,(),()),(28,(),())))) -(11,(9,(8,(),()),(10,(),())),(21,(15,(13,(12,(),()),(14,(),())),(19,(17,(16,(),()),(18,(),())),(20,(),()))),(23,(22,(),()),(24,(),())))) 20 (20,(11,(9,(8,(),()),(10,(),())),(19,(15,(13,(12,(),()),(14,(),())),(17,(16,(),()),(18,(),()))),())),(21,(),(23,(22,(),()),(24,(),())))) -(33,(29,(23,(21,(20,(),()),(22,(),())),(27,(25,(24,(),()),(26,(),())),(28,(),()))),(31,(30,(),()),(32,(),()))),(35,(34,(),()),(36,(),()))) 28 (28,(27,(23,(21,(20,(),()),(22,(),())),(25,(24,(),()),(26,(),()))),()),(29,(),(33,(31,(30,(),()),(32,(),())),(35,(34,(),()),(36,(),()))))) -(9,(7,(6,(),()),(8,(),())),(13,(11,(10,(),()),(12,(),())),(19,(17,(15,(14,(),()),(16,(),())),(18,(),())),(21,(20,(),()),(22,(),()))))) 18 (18,(13,(9,(7,(6,(),()),(8,(),())),(11,(10,(),()),(12,(),()))),(17,(15,(14,(),()),(16,(),())),())),(19,(),(21,(20,(),()),(22,(),())))) -(31,(21,(19,(18,(),()),(20,(),())),(27,(25,(23,(22,(),()),(24,(),())),(26,(),())),(29,(28,(),()),(30,(),())))),(33,(32,(),()),(34,(),()))) 26 (26,(21,(19,(18,(),()),(20,(),())),(25,(23,(22,(),()),(24,(),())),())),(31,(27,(),(29,(28,(),()),(30,(),()))),(33,(32,(),()),(34,(),())))) -(17,(15,(14,(),()),(16,(),())),(27,(23,(21,(19,(18,(),()),(20,(),())),(22,(),())),(25,(24,(),()),(26,(),()))),(29,(28,(),()),(30,(),())))) 22 (22,(17,(15,(14,(),()),(16,(),())),(21,(19,(18,(),()),(20,(),())),())),(27,(23,(),(25,(24,(),()),(26,(),()))),(29,(28,(),()),(30,(),())))) -(39,(35,(31,(29,(27,(26,(),()),(28,(),())),(30,(),())),(33,(32,(),()),(34,(),()))),(37,(36,(),()),(38,(),()))),(41,(40,(),()),(42,(),()))) 30 (30,(29,(27,(26,(),()),(28,(),())),()),(35,(31,(),(33,(32,(),()),(34,(),()))),(39,(37,(36,(),()),(38,(),())),(41,(40,(),()),(42,(),()))))) -(8,(6,(5,(),()),(7,(),())),(12,(10,(9,(),()),(11,(),())),(16,(14,(13,(),()),(15,(),())),(18,(17,(),()),(20,(19,(),()),(21,(),())))))) 17 (17,(12,(8,(6,(5,(),()),(7,(),())),(10,(9,(),()),(11,(),()))),(16,(14,(13,(),()),(15,(),())),())),(18,(),(20,(19,(),()),(21,(),())))) -(30,(20,(18,(17,(),()),(19,(),())),(24,(22,(21,(),()),(23,(),())),(26,(25,(),()),(28,(27,(),()),(29,(),()))))),(32,(31,(),()),(33,(),()))) 25 (25,(20,(18,(17,(),()),(19,(),())),(24,(22,(21,(),()),(23,(),())),())),(30,(26,(),(28,(27,(),()),(29,(),()))),(32,(31,(),()),(33,(),())))) -(16,(14,(13,(),()),(15,(),())),(26,(20,(18,(17,(),()),(19,(),())),(22,(21,(),()),(24,(23,(),()),(25,(),())))),(28,(27,(),()),(29,(),())))) 21 (21,(16,(14,(13,(),()),(15,(),())),(20,(18,(17,(),()),(19,(),())),())),(26,(22,(),(24,(23,(),()),(25,(),()))),(28,(27,(),()),(29,(),())))) -(38,(34,(28,(26,(25,(),()),(27,(),())),(30,(29,(),()),(32,(31,(),()),(33,(),())))),(36,(35,(),()),(37,(),()))),(40,(39,(),()),(41,(),()))) 29 (29,(28,(26,(25,(),()),(27,(),())),()),(34,(30,(),(32,(31,(),()),(33,(),()))),(38,(36,(35,(),()),(37,(),())),(40,(39,(),()),(41,(),()))))) -(14,(12,(11,(),()),(13,(),())),(18,(16,(15,(),()),(17,(),())),(24,(20,(19,(),()),(22,(21,(),()),(23,(),()))),(26,(25,(),()),(27,(),()))))) 19 (19,(18,(14,(12,(11,(),()),(13,(),())),(16,(15,(),()),(17,(),()))),()),(20,(),(24,(22,(21,(),()),(23,(),())),(26,(25,(),()),(27,(),()))))) -(36,(26,(24,(23,(),()),(25,(),())),(32,(28,(27,(),()),(30,(29,(),()),(31,(),()))),(34,(33,(),()),(35,(),())))),(38,(37,(),()),(39,(),()))) 27 (27,(26,(24,(23,(),()),(25,(),())),()),(36,(28,(),(32,(30,(29,(),()),(31,(),())),(34,(33,(),()),(35,(),())))),(38,(37,(),()),(39,(),())))) -(22,(20,(19,(),()),(21,(),())),(32,(28,(24,(23,(),()),(26,(25,(),()),(27,(),()))),(30,(29,(),()),(31,(),()))),(34,(33,(),()),(35,(),())))) 23 (23,(22,(20,(19,(),()),(21,(),())),()),(32,(24,(),(28,(26,(25,(),()),(27,(),())),(30,(29,(),()),(31,(),())))),(34,(33,(),()),(35,(),())))) -(44,(40,(36,(32,(31,(),()),(34,(33,(),()),(35,(),()))),(38,(37,(),()),(39,(),()))),(42,(41,(),()),(43,(),()))),(46,(45,(),()),(47,(),()))) 31 (31,(),(40,(32,(),(36,(34,(33,(),()),(35,(),())),(38,(37,(),()),(39,(),())))),(44,(42,(41,(),()),(43,(),())),(46,(45,(),()),(47,(),()))))) -(3,(1,(0,(),()),(2,(),())),(7,(5,(4,(),()),(6,(),())),(11,(9,(8,(),()),(10,(),())),(15,(13,(12,(),()),(14,(),())),(19,(17,(16,(),()),(18,(),())),(20,(),())))))) 20 (20,(3,(1,(0,(),()),(2,(),())),(11,(7,(5,(4,(),()),(6,(),())),(9,(8,(),()),(10,(),()))),(19,(15,(13,(12,(),()),(14,(),())),(17,(16,(),()),(18,(),()))),()))),()) -(37,(23,(21,(20,(),()),(22,(),())),(27,(25,(24,(),()),(26,(),())),(31,(29,(28,(),()),(30,(),())),(35,(33,(32,(),()),(34,(),())),(36,(),()))))),(39,(38,(),()),(40,(),()))) 36 (36,(27,(23,(21,(20,(),()),(22,(),())),(25,(24,(),()),(26,(),()))),(35,(31,(29,(28,(),()),(30,(),())),(33,(32,(),()),(34,(),()))),())),(37,(),(39,(38,(),()),(40,(),())))) -(15,(13,(12,(),()),(14,(),())),(29,(19,(17,(16,(),()),(18,(),())),(23,(21,(20,(),()),(22,(),())),(27,(25,(24,(),()),(26,(),())),(28,(),())))),(31,(30,(),()),(32,(),())))) 28 (28,(15,(13,(12,(),()),(14,(),())),(19,(17,(16,(),()),(18,(),())),(27,(23,(21,(20,(),()),(22,(),())),(25,(24,(),()),(26,(),()))),()))),(29,(),(31,(30,(),()),(32,(),())))) -(49,(45,(35,(33,(32,(),()),(34,(),())),(39,(37,(36,(),()),(38,(),())),(43,(41,(40,(),()),(42,(),())),(44,(),())))),(47,(46,(),()),(48,(),()))),(51,(50,(),()),(52,(),()))) 44 (44,(35,(33,(32,(),()),(34,(),())),(43,(39,(37,(36,(),()),(38,(),())),(41,(40,(),()),(42,(),()))),())),(49,(45,(),(47,(46,(),()),(48,(),()))),(51,(50,(),()),(52,(),())))) -(11,(9,(8,(),()),(10,(),())),(15,(13,(12,(),()),(14,(),())),(25,(19,(17,(16,(),()),(18,(),())),(23,(21,(20,(),()),(22,(),())),(24,(),()))),(27,(26,(),()),(28,(),()))))) 24 (24,(11,(9,(8,(),()),(10,(),())),(15,(13,(12,(),()),(14,(),())),(23,(19,(17,(16,(),()),(18,(),())),(21,(20,(),()),(22,(),()))),()))),(25,(),(27,(26,(),()),(28,(),())))) -(45,(31,(29,(28,(),()),(30,(),())),(41,(35,(33,(32,(),()),(34,(),())),(39,(37,(36,(),()),(38,(),())),(40,(),()))),(43,(42,(),()),(44,(),())))),(47,(46,(),()),(48,(),()))) 40 (40,(31,(29,(28,(),()),(30,(),())),(39,(35,(33,(32,(),()),(34,(),())),(37,(36,(),()),(38,(),()))),())),(45,(41,(),(43,(42,(),()),(44,(),()))),(47,(46,(),()),(48,(),())))) -(23,(21,(20,(),()),(22,(),())),(37,(33,(27,(25,(24,(),()),(26,(),())),(31,(29,(28,(),()),(30,(),())),(32,(),()))),(35,(34,(),()),(36,(),()))),(39,(38,(),()),(40,(),())))) 32 (32,(23,(21,(20,(),()),(22,(),())),(31,(27,(25,(24,(),()),(26,(),())),(29,(28,(),()),(30,(),()))),())),(33,(),(37,(35,(34,(),()),(36,(),())),(39,(38,(),()),(40,(),()))))) -(57,(53,(49,(43,(41,(40,(),()),(42,(),())),(47,(45,(44,(),()),(46,(),())),(48,(),()))),(51,(50,(),()),(52,(),()))),(55,(54,(),()),(56,(),()))),(59,(58,(),()),(60,(),()))) 48 (48,(47,(43,(41,(40,(),()),(42,(),())),(45,(44,(),()),(46,(),()))),()),(57,(49,(),(53,(51,(50,(),()),(52,(),())),(55,(54,(),()),(56,(),())))),(59,(58,(),()),(60,(),())))) -(9,(7,(6,(),()),(8,(),())),(13,(11,(10,(),()),(12,(),())),(17,(15,(14,(),()),(16,(),())),(23,(21,(19,(18,(),()),(20,(),())),(22,(),())),(25,(24,(),()),(26,(),())))))) 22 (22,(9,(7,(6,(),()),(8,(),())),(17,(13,(11,(10,(),()),(12,(),())),(15,(14,(),()),(16,(),()))),(21,(19,(18,(),()),(20,(),())),()))),(23,(),(25,(24,(),()),(26,(),())))) -(43,(29,(27,(26,(),()),(28,(),())),(33,(31,(30,(),()),(32,(),())),(39,(37,(35,(34,(),()),(36,(),())),(38,(),())),(41,(40,(),()),(42,(),()))))),(45,(44,(),()),(46,(),()))) 38 (38,(33,(29,(27,(26,(),()),(28,(),())),(31,(30,(),()),(32,(),()))),(37,(35,(34,(),()),(36,(),())),())),(43,(39,(),(41,(40,(),()),(42,(),()))),(45,(44,(),()),(46,(),())))) -(21,(19,(18,(),()),(20,(),())),(35,(25,(23,(22,(),()),(24,(),())),(31,(29,(27,(26,(),()),(28,(),())),(30,(),())),(33,(32,(),()),(34,(),())))),(37,(36,(),()),(38,(),())))) 30 (30,(21,(19,(18,(),()),(20,(),())),(25,(23,(22,(),()),(24,(),())),(29,(27,(26,(),()),(28,(),())),()))),(35,(31,(),(33,(32,(),()),(34,(),()))),(37,(36,(),()),(38,(),())))) -(55,(51,(41,(39,(38,(),()),(40,(),())),(47,(45,(43,(42,(),()),(44,(),())),(46,(),())),(49,(48,(),()),(50,(),())))),(53,(52,(),()),(54,(),()))),(57,(56,(),()),(58,(),()))) 46 (46,(41,(39,(38,(),()),(40,(),())),(45,(43,(42,(),()),(44,(),())),())),(55,(51,(47,(),(49,(48,(),()),(50,(),()))),(53,(52,(),()),(54,(),()))),(57,(56,(),()),(58,(),())))) -(17,(15,(14,(),()),(16,(),())),(21,(19,(18,(),()),(20,(),())),(31,(27,(25,(23,(22,(),()),(24,(),())),(26,(),())),(29,(28,(),()),(30,(),()))),(33,(32,(),()),(34,(),()))))) 26 (26,(17,(15,(14,(),()),(16,(),())),(21,(19,(18,(),()),(20,(),())),(25,(23,(22,(),()),(24,(),())),()))),(31,(27,(),(29,(28,(),()),(30,(),()))),(33,(32,(),()),(34,(),())))) -(51,(37,(35,(34,(),()),(36,(),())),(47,(43,(41,(39,(38,(),()),(40,(),())),(42,(),())),(45,(44,(),()),(46,(),()))),(49,(48,(),()),(50,(),())))),(53,(52,(),()),(54,(),()))) 42 (42,(37,(35,(34,(),()),(36,(),())),(41,(39,(38,(),()),(40,(),())),())),(51,(47,(43,(),(45,(44,(),()),(46,(),()))),(49,(48,(),()),(50,(),()))),(53,(52,(),()),(54,(),())))) -(29,(27,(26,(),()),(28,(),())),(43,(39,(35,(33,(31,(30,(),()),(32,(),())),(34,(),())),(37,(36,(),()),(38,(),()))),(41,(40,(),()),(42,(),()))),(45,(44,(),()),(46,(),())))) 34 (34,(29,(27,(26,(),()),(28,(),())),(33,(31,(30,(),()),(32,(),())),())),(39,(35,(),(37,(36,(),()),(38,(),()))),(43,(41,(40,(),()),(42,(),())),(45,(44,(),()),(46,(),()))))) -(63,(59,(55,(51,(49,(47,(46,(),()),(48,(),())),(50,(),())),(53,(52,(),()),(54,(),()))),(57,(56,(),()),(58,(),()))),(61,(60,(),()),(62,(),()))),(65,(64,(),()),(66,(),()))) 50 (50,(49,(47,(46,(),()),(48,(),())),()),(63,(55,(51,(),(53,(52,(),()),(54,(),()))),(59,(57,(56,(),()),(58,(),())),(61,(60,(),()),(62,(),())))),(65,(64,(),()),(66,(),())))) -(8,(6,(5,(),()),(7,(),())),(12,(10,(9,(),()),(11,(),())),(16,(14,(13,(),()),(15,(),())),(20,(18,(17,(),()),(19,(),())),(22,(21,(),()),(24,(23,(),()),(25,(),()))))))) 21 (21,(8,(6,(5,(),()),(7,(),())),(16,(12,(10,(9,(),()),(11,(),())),(14,(13,(),()),(15,(),()))),(20,(18,(17,(),()),(19,(),())),()))),(22,(),(24,(23,(),()),(25,(),())))) -(42,(28,(26,(25,(),()),(27,(),())),(32,(30,(29,(),()),(31,(),())),(36,(34,(33,(),()),(35,(),())),(38,(37,(),()),(40,(39,(),()),(41,(),())))))),(44,(43,(),()),(45,(),()))) 37 (37,(32,(28,(26,(25,(),()),(27,(),())),(30,(29,(),()),(31,(),()))),(36,(34,(33,(),()),(35,(),())),())),(42,(38,(),(40,(39,(),()),(41,(),()))),(44,(43,(),()),(45,(),())))) -(20,(18,(17,(),()),(19,(),())),(34,(24,(22,(21,(),()),(23,(),())),(28,(26,(25,(),()),(27,(),())),(30,(29,(),()),(32,(31,(),()),(33,(),()))))),(36,(35,(),()),(37,(),())))) 29 (29,(20,(18,(17,(),()),(19,(),())),(24,(22,(21,(),()),(23,(),())),(28,(26,(25,(),()),(27,(),())),()))),(34,(30,(),(32,(31,(),()),(33,(),()))),(36,(35,(),()),(37,(),())))) -(54,(50,(40,(38,(37,(),()),(39,(),())),(44,(42,(41,(),()),(43,(),())),(46,(45,(),()),(48,(47,(),()),(49,(),()))))),(52,(51,(),()),(53,(),()))),(56,(55,(),()),(57,(),()))) 45 (45,(40,(38,(37,(),()),(39,(),())),(44,(42,(41,(),()),(43,(),())),())),(54,(50,(46,(),(48,(47,(),()),(49,(),()))),(52,(51,(),()),(53,(),()))),(56,(55,(),()),(57,(),())))) -(16,(14,(13,(),()),(15,(),())),(20,(18,(17,(),()),(19,(),())),(30,(24,(22,(21,(),()),(23,(),())),(26,(25,(),()),(28,(27,(),()),(29,(),())))),(32,(31,(),()),(33,(),()))))) 25 (25,(16,(14,(13,(),()),(15,(),())),(20,(18,(17,(),()),(19,(),())),(24,(22,(21,(),()),(23,(),())),()))),(30,(26,(),(28,(27,(),()),(29,(),()))),(32,(31,(),()),(33,(),())))) -(50,(36,(34,(33,(),()),(35,(),())),(46,(40,(38,(37,(),()),(39,(),())),(42,(41,(),()),(44,(43,(),()),(45,(),())))),(48,(47,(),()),(49,(),())))),(52,(51,(),()),(53,(),()))) 41 (41,(36,(34,(33,(),()),(35,(),())),(40,(38,(37,(),()),(39,(),())),())),(50,(46,(42,(),(44,(43,(),()),(45,(),()))),(48,(47,(),()),(49,(),()))),(52,(51,(),()),(53,(),())))) -(28,(26,(25,(),()),(27,(),())),(42,(38,(32,(30,(29,(),()),(31,(),())),(34,(33,(),()),(36,(35,(),()),(37,(),())))),(40,(39,(),()),(41,(),()))),(44,(43,(),()),(45,(),())))) 33 (33,(28,(26,(25,(),()),(27,(),())),(32,(30,(29,(),()),(31,(),())),())),(38,(34,(),(36,(35,(),()),(37,(),()))),(42,(40,(39,(),()),(41,(),())),(44,(43,(),()),(45,(),()))))) -(62,(58,(54,(48,(46,(45,(),()),(47,(),())),(50,(49,(),()),(52,(51,(),()),(53,(),())))),(56,(55,(),()),(57,(),()))),(60,(59,(),()),(61,(),()))),(64,(63,(),()),(65,(),()))) 49 (49,(48,(46,(45,(),()),(47,(),())),()),(62,(54,(50,(),(52,(51,(),()),(53,(),()))),(58,(56,(55,(),()),(57,(),())),(60,(59,(),()),(61,(),())))),(64,(63,(),()),(65,(),())))) -(14,(12,(11,(),()),(13,(),())),(18,(16,(15,(),()),(17,(),())),(22,(20,(19,(),()),(21,(),())),(28,(24,(23,(),()),(26,(25,(),()),(27,(),()))),(30,(29,(),()),(31,(),())))))) 23 (23,(14,(12,(11,(),()),(13,(),())),(22,(18,(16,(15,(),()),(17,(),())),(20,(19,(),()),(21,(),()))),())),(24,(),(28,(26,(25,(),()),(27,(),())),(30,(29,(),()),(31,(),()))))) -(48,(34,(32,(31,(),()),(33,(),())),(38,(36,(35,(),()),(37,(),())),(44,(40,(39,(),()),(42,(41,(),()),(43,(),()))),(46,(45,(),()),(47,(),()))))),(50,(49,(),()),(51,(),()))) 39 (39,(38,(34,(32,(31,(),()),(33,(),())),(36,(35,(),()),(37,(),()))),()),(48,(40,(),(44,(42,(41,(),()),(43,(),())),(46,(45,(),()),(47,(),())))),(50,(49,(),()),(51,(),())))) -(26,(24,(23,(),()),(25,(),())),(40,(30,(28,(27,(),()),(29,(),())),(36,(32,(31,(),()),(34,(33,(),()),(35,(),()))),(38,(37,(),()),(39,(),())))),(42,(41,(),()),(43,(),())))) 31 (31,(26,(24,(23,(),()),(25,(),())),(30,(28,(27,(),()),(29,(),())),())),(40,(32,(),(36,(34,(33,(),()),(35,(),())),(38,(37,(),()),(39,(),())))),(42,(41,(),()),(43,(),())))) -(60,(56,(46,(44,(43,(),()),(45,(),())),(52,(48,(47,(),()),(50,(49,(),()),(51,(),()))),(54,(53,(),()),(55,(),())))),(58,(57,(),()),(59,(),()))),(62,(61,(),()),(63,(),()))) 47 (47,(46,(44,(43,(),()),(45,(),())),()),(60,(56,(48,(),(52,(50,(49,(),()),(51,(),())),(54,(53,(),()),(55,(),())))),(58,(57,(),()),(59,(),()))),(62,(61,(),()),(63,(),())))) -(22,(20,(19,(),()),(21,(),())),(26,(24,(23,(),()),(25,(),())),(36,(32,(28,(27,(),()),(30,(29,(),()),(31,(),()))),(34,(33,(),()),(35,(),()))),(38,(37,(),()),(39,(),()))))) 27 (27,(22,(20,(19,(),()),(21,(),())),(26,(24,(23,(),()),(25,(),())),())),(36,(28,(),(32,(30,(29,(),()),(31,(),())),(34,(33,(),()),(35,(),())))),(38,(37,(),()),(39,(),())))) -(56,(42,(40,(39,(),()),(41,(),())),(52,(48,(44,(43,(),()),(46,(45,(),()),(47,(),()))),(50,(49,(),()),(51,(),()))),(54,(53,(),()),(55,(),())))),(58,(57,(),()),(59,(),()))) 43 (43,(42,(40,(39,(),()),(41,(),())),()),(56,(52,(44,(),(48,(46,(45,(),()),(47,(),())),(50,(49,(),()),(51,(),())))),(54,(53,(),()),(55,(),()))),(58,(57,(),()),(59,(),())))) -(34,(32,(31,(),()),(33,(),())),(48,(44,(40,(36,(35,(),()),(38,(37,(),()),(39,(),()))),(42,(41,(),()),(43,(),()))),(46,(45,(),()),(47,(),()))),(50,(49,(),()),(51,(),())))) 35 (35,(34,(32,(31,(),()),(33,(),())),()),(44,(36,(),(40,(38,(37,(),()),(39,(),())),(42,(41,(),()),(43,(),())))),(48,(46,(45,(),()),(47,(),())),(50,(49,(),()),(51,(),()))))) -(68,(64,(60,(56,(52,(51,(),()),(54,(53,(),()),(55,(),()))),(58,(57,(),()),(59,(),()))),(62,(61,(),()),(63,(),()))),(66,(65,(),()),(67,(),()))),(70,(69,(),()),(71,(),()))) 51 (51,(),(68,(60,(52,(),(56,(54,(53,(),()),(55,(),())),(58,(57,(),()),(59,(),())))),(64,(62,(61,(),()),(63,(),())),(66,(65,(),()),(67,(),())))),(70,(69,(),()),(71,(),())))) diff --git a/02-splay_operation/task.md b/02-splay_operation/task.md deleted file mode 100644 index 1967c79..0000000 --- a/02-splay_operation/task.md +++ /dev/null @@ -1,9 +0,0 @@ -Given an implementation of a binary search tree including parent pointers: -- implement `splay` method, preferably utilizing the provided `rotate` operation - performing a single rotation; -- update `lookup`, `insert` and `remove` methods to utilize it correctly. - -You should submit the `splay_operation.*` file (but not the -`splay_operation_test.*`). - -Source code templates can be found in [git](https://gitlab.kam.mff.cuni.cz/datovky/assignments/-/tree/master). diff --git a/03-splay_experiment/cpp/Makefile b/03-splay_experiment/cpp/Makefile deleted file mode 100644 index a19778d..0000000 --- a/03-splay_experiment/cpp/Makefile +++ /dev/null @@ -1,22 +0,0 @@ -STUDENT_ID ?= PLEASE_SET_STUDENT_ID - -.PHONY: test -test: splay_experiment - @rm -rf out && mkdir out - @for test in sequential random subset ; do \ - for mode in std naive ; do \ - echo t-$$test-$$mode ; \ - ./splay_experiment $$test $(STUDENT_ID) $$mode >out/t-$$test-$$mode ; \ - done ; \ - done - -INCLUDE ?= . -CXXFLAGS=-std=c++11 -O2 -Wall -Wextra -g -Wno-sign-compare -I$(INCLUDE) - -splay_experiment: splay_operation.h splay_experiment.cpp $(INCLUDE)/random.h - $(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@ - -.PHONY: clean -clean:: - rm -f splay_experiment - rm -rf out diff --git a/03-splay_experiment/cpp/random.h b/03-splay_experiment/cpp/random.h deleted file mode 100644 index 5ef10ae..0000000 --- a/03-splay_experiment/cpp/random.h +++ /dev/null @@ -1,61 +0,0 @@ -#ifndef DS1_RANDOM_H -#define DS1_RANDOM_H - -#include <cstdint> - -/* - * This is the xoroshiro128+ random generator, designed in 2016 by David Blackman - * and Sebastiano Vigna, distributed under the CC-0 license. For more details, - * see http://vigna.di.unimi.it/xorshift/. - * - * Rewritten to C++ by Martin Mares, also placed under CC-0. - */ - -class RandomGen { - uint64_t state[2]; - - uint64_t rotl(uint64_t x, int k) - { - return (x << k) | (x >> (64 - k)); - } - - public: - // Initialize the generator, set its seed and warm it up. - RandomGen(unsigned int seed) - { - state[0] = seed * 0xdeadbeef; - state[1] = seed ^ 0xc0de1234; - for (int i=0; i<100; i++) - next_u64(); - } - - // Generate a random 64-bit number. - uint64_t next_u64(void) - { - uint64_t s0 = state[0], s1 = state[1]; - uint64_t result = s0 + s1; - s1 ^= s0; - state[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); - state[1] = rotl(s1, 36); - return result; - } - - // Generate a random 32-bit number. - uint32_t next_u32(void) - { - return next_u64() >> 11; - } - - // Generate a number between 0 and range-1. - unsigned int next_range(unsigned int range) - { - /* - * This is not perfectly uniform, unless the range is a power of two. - * However, for 64-bit random values and 32-bit ranges, the bias is - * insignificant. - */ - return next_u64() % range; - } -}; - -#endif diff --git a/03-splay_experiment/cpp/splay_experiment.cpp b/03-splay_experiment/cpp/splay_experiment.cpp deleted file mode 100644 index a47da8b..0000000 --- a/03-splay_experiment/cpp/splay_experiment.cpp +++ /dev/null @@ -1,208 +0,0 @@ -#include <algorithm> -#include <functional> -#include <string> -#include <utility> -#include <vector> -#include <iostream> -#include <cmath> - -#include "splay_operation.h" -#include "random.h" - -using namespace std; - -/* - * A modified Splay tree for benchmarking. - * - * We inherit the implementation of operations from the Tree class - * and extend it by keeping statistics on the number of splay operations - * and the total number of rotations. Also, if naive is turned on, - * splay uses only single rotations. - * - * Please make sure that your Tree class defines the rotate() and splay() - * methods as virtual. - */ - -class BenchmarkingTree : public Tree { -public: - int num_operations; - int num_rotations; - bool do_naive; - - BenchmarkingTree(bool naive=false) - { - do_naive = naive; - reset(); - } - - void reset() - { - num_operations = 0; - num_rotations = 0; - } - - void rotate(Node *node) override - { - num_rotations++; - Tree::rotate(node); - } - - void splay(Node *node) override - { - num_operations++; - if (do_naive) { - while (node->parent) - rotate(node); - } else { - Tree::splay(node); - } - } - - // Return the average number of rotations per operation. - double rot_per_op() - { - if (num_operations > 0) - return (double) num_rotations / num_operations; - else - return 0; - } -}; - -bool naive; // Use of naive rotations requested -RandomGen *rng; // Random generator object - -void test_sequential() -{ - for (int n=100; n<=3000; n+=100) { - BenchmarkingTree tree = BenchmarkingTree(naive); - - for (int x=0; x<n; x++) - tree.insert(x); - - for (int i=0; i<5; i++) - for (int x=0; x<n; x++) - tree.lookup(x); - - cout << n << " " << tree.rot_per_op() << endl; - } -} - -// An auxiliary function for generating a random permutation. -vector<int> random_permutation(int n) -{ - vector<int> perm; - for (int i=0; i<n; i++) - perm.push_back(i); - for (int i=0; i<n-1; i++) - swap(perm[i], perm[i + rng->next_range(n-i)]); - return perm; -} - -void test_random() -{ - for (int e=32; e<=64; e++) { - int n = (int) pow(2, e/4.); - BenchmarkingTree tree = BenchmarkingTree(naive); - - vector<int> perm = random_permutation(n); - for (int x : perm) - tree.insert(x); - - for (int i=0; i<5*n; i++) - tree.lookup(rng->next_range(n)); - - cout << n << " " << tree.rot_per_op() << endl; - } -} - -/* - * An auxiliary function for constructing arithmetic progressions. - * The vector seq will be modified to contain an arithmetic progression - * of elements in interval [A,B] starting from position s with step inc. - */ -void make_progression(vector<int> &seq, int A, int B, int s, int inc) -{ - for (int i=0; i<seq.size(); i++) - while (seq[i] >= A && seq[i] <= B && s + inc*(seq[i]-A) != i) - swap(seq[i], seq[s + inc*(seq[i] - A)]); -} - -void test_subset_s(int sub) -{ - for (int e=32; e<=64; e++) { - int n = (int) pow(2, e/4.); - if (n < sub) - continue; - - // We will insert elements in order, which contain several - // arithmetic progressions interspersed with random elements. - vector<int> seq = random_permutation(n); - make_progression(seq, n/4, n/4 + n/20, n/10, 1); - make_progression(seq, n/2, n/2 + n/20, n/10, -1); - make_progression(seq, 3*n/4, 3*n/4 + n/20, n/2, -4); - make_progression(seq, 17*n/20, 17*n/20 + n/20, 2*n/5, 5); - - BenchmarkingTree tree = BenchmarkingTree(naive); - for (int x : seq) - tree.insert(x); - tree.reset(); - - for (int i=0; i<10000; i++) - tree.lookup(seq[rng->next_range(sub)]); - - cout << sub << " " << n << " " << tree.rot_per_op() << endl; - } -} - -void test_subset() -{ - test_subset_s(10); - test_subset_s(100); - test_subset_s(1000); -} - -vector<pair<string, function<void()>>> tests = { - { "sequential", test_sequential }, - { "random", test_random }, - { "subset", test_subset }, -}; - -int main(int argc, char **argv) -{ - if (argc != 4) { - cerr << "Usage: " << argv[0] << " <test> <student-id> (std|naive)" << endl; - return 1; - } - - string which_test = argv[1]; - string id_str = argv[2]; - string mode = argv[3]; - - try { - rng = new RandomGen(stoi(id_str)); - } catch (...) { - cerr << "Invalid student ID" << endl; - return 1; - } - - if (mode == "std") - naive = false; - else if (mode == "naive") - naive = true; - else - { - cerr << "Last argument must be either 'std' or 'naive'" << endl; - return 1; - } - - for (const auto& test : tests) { - if (test.first == which_test) - { - cout.precision(12); - test.second(); - return 0; - } - } - cerr << "Unknown test " << which_test << endl; - return 1; -} diff --git a/03-splay_experiment/python/Makefile b/03-splay_experiment/python/Makefile deleted file mode 100644 index 4a5efbf..0000000 --- a/03-splay_experiment/python/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -STUDENT_ID ?= PLEASE_SET_STUDENT_ID - -.PHONY: test -test: splay_experiment.py splay_operation.py - @rm -rf out && mkdir out - @for test in sequential random subset ; do \ - for mode in std naive ; do \ - echo t-$$test-$$mode ; \ - ./splay_experiment.py $$test $(STUDENT_ID) $$mode >out/t-$$test-$$mode ; \ - done ; \ - done - -.PHONY: clean -clean:: - rm -rf out __pycache__ diff --git a/03-splay_experiment/python/splay_experiment.py b/03-splay_experiment/python/splay_experiment.py deleted file mode 100755 index 8cf3d6d..0000000 --- a/03-splay_experiment/python/splay_experiment.py +++ /dev/null @@ -1,127 +0,0 @@ -#!/usr/bin/env python3 - -import sys -import random - -from splay_operation import Tree - -class BenchmarkingTree(Tree): - """ A modified Splay tree for benchmarking. - - We inherit the implementation of operations from the Tree class - and extend it by keeping statistics on the number of splay operations - and the total number of rotations. Also, if naive is turned on, - splay uses only single rotations. - """ - - def __init__(self, naive=False): - Tree.__init__(self) - self.do_naive = naive - self.reset() - - def reset(self): - """Reset statistics.""" - self.num_rotations = 0; - self.num_operations = 0; - - def rotate(self, node): - self.num_rotations += 1 - Tree.rotate(self, node) - - def splay(self, node): - self.num_operations += 1 - if self.do_naive: - while node.parent is not None: - self.rotate(node) - else: - Tree.splay(self, node) - - def rot_per_op(self): - """Return the average number of rotations per operation.""" - if self.num_operations > 0: - return self.num_rotations / self.num_operations - else: - return 0 - -def test_sequential(): - for n in range(100, 3001, 100): - tree = BenchmarkingTree(naive) - for elem in range(n): - tree.insert(elem) - - for _ in range(5): - for elem in range(n): - tree.lookup(elem) - - print(n, tree.rot_per_op()) - -def test_random(): - for exp in range(32, 64): - n = int(2**(exp/4)) - tree = BenchmarkingTree(naive) - - for elem in random.sample(range(n), n): - tree.insert(elem) - - for _ in range(5*n): - tree.lookup(random.randrange(n)) - - print(n, tree.rot_per_op()) - -def make_progression(seq, A, B, s, inc): - """An auxiliary function for constructing arithmetic progressions. - - The array seq will be modified to contain an arithmetic progression - of elements in interval [A,B] starting from position s with step inc. - """ - for i in range(len(seq)): - while seq[i] >= A and seq[i] <= B and s + inc*(seq[i]-A) != i: - pos = s + inc*(seq[i]-A) - seq[i], seq[pos] = seq[pos], seq[i] - -def test_subset(): - for sub in [10, 100, 1000]: - for exp in range(32,64): - n = int(2**(exp/4)) - if n < sub: - continue - - # We will insert elements in order, which contain several - # arithmetic progressions interspersed with random elements. - seq = random.sample(range(n), n) - make_progression(seq, n//4, n//4 + n//20, n//10, 1) - make_progression(seq, n//2, n//2 + n//20, n//10, -1) - make_progression(seq, 3*n//4, 3*n//4 + n//20, n//2, -4) - make_progression(seq, 17*n//20, 17*n//20 + n//20, 2*n//5, 5) - - tree = BenchmarkingTree(naive) - for elem in seq: - tree.insert(elem) - tree.reset() - - for _ in range(10000): - tree.lookup(seq[random.randrange(sub)]) - - print(sub, n, tree.rot_per_op()) - -tests = { - "sequential": test_sequential, - "random": test_random, - "subset": test_subset, -} - -if len(sys.argv) == 4: - test, student_id = sys.argv[1], sys.argv[2] - if sys.argv[3] == "std": - naive = False - elif sys.argv[3] == "naive": - naive = True - else: - raise ValueError("Last argument must be either 'std' or 'naive'") - random.seed(student_id) - if test in tests: - tests[test]() - else: - raise ValueError("Unknown test {}".format(test)) -else: - raise ValueError("Usage: {} <test> <student-id> (std|naive)".format(sys.argv[0])) diff --git a/03-splay_experiment/task.md b/03-splay_experiment/task.md deleted file mode 100644 index 0d96811..0000000 --- a/03-splay_experiment/task.md +++ /dev/null @@ -1,87 +0,0 @@ -## Goal - -The goal of this assignment is to evaluate your implementation of Splay trees -experimentally and to compare it with a "naive" implementation which splays -using single rotations only. - -You are given a test program (`splay_experiment`) which calls your -implementation from the previous assignment to perform the following -experiments: - -- _Sequential test:_ Insert _n_ elements sequentially and then repeatedly - find them all in sequential order. -- _Random test:_ Insert _n_ elements in random order and then find _5n_ - random elements. -- _Subset test:_ Insert a sequence of _n_ elements, which contains arithmetic - progressions interspersed with random elements. Then repeatedly access - a small subset of these elements in random order. Try this with subsets of - different cardinalities. - -The program tries each experiment with different values of _n_. In each try, -it prints the average number of rotations per splay operation. - -You should perform these experiments and write a report, which contains the following -plots of the measured data. Each plot should show the dependence of the average -number of rotations on the set size _n_. - -- The sequential test: one curve for the standard implementation, one for the naive one. -- The random test: one curve for the standard implementation, one for the naive one. -- The subset test: three curves for the standard implementation with different sizes - of the subset, three for the naive implementation with the same sizes. - -The report should discuss the experimental results and try to explain the observed -behavior using theory from the lectures. (If you want, you can carry out further -experiments to gain better understanding of the data structure and include these -in the report. This is strictly optional.) - -You should submit a PDF file with the report (and no source code). -You will get 1 temporary point upon submission if the file is syntantically correct; -proper points will be assigned later. - -## Test program - -The test program is given three arguments: -- The name of the test (`sequential`, `random`, `subset`). -- The random seed: you should use the last 2 digits of your student ID (you can find - it in the Study Information System – just click on the Personal data icon). Please - include the random seed in your report. -- The implementation to test (`std` or `naive`). - -The output of the program contains one line per experiment, which consists of: -- For the sequential and random test: the set size and the average number of rotations. -- For the subset test: the subset size, the set size, and the average number of rotations - per find. The initial insertions of the full set are not counted. - -## Your implementation - -Please use your implementation from the previous exercise. Methods `splay()` -and `rotate()` will be augmented by the test program. If you are performing -a double rotation directly instead of composing it from single rotations, you -need to adjust the `BenchmarkingTree` class accordingly. - -## Hints - -The following tools can be useful for producing nice plots: -- [pandas](https://pandas.pydata.org/) -- [matplotlib](https://matplotlib.org/) -- [gnuplot](http://www.gnuplot.info/) - -A quick checklist for plots: -- Is there a caption explaining what is plotted? -- Are the axes clearly labelled? Do they have value ranges and units? -- Have you mentioned that this axis has logarithmic scale? (Logarithmic graphs - are more fitting in some cases, but you should tell.) -- Is it clear which curve means what? -- Is it clear what are the measured points and what is an interpolated - curve between them? -- Are there any overlaps? (E.g., the most interesting part of the curve - hidden underneath a label?) - -In your discussion, please distinguish the following kinds of claims. -It should be always clear which is which: -- Experimental results (i.e., the raw data you obtained from the experiments) -- Theoretical facts (i.e., claims we have proved mathematically) -- Your hypotheses (e.g., when you claim that the graph looks like something is true, - but you are not able to prove rigorously that it always holds) - -Source code templates can be found in [git](https://gitlab.kam.mff.cuni.cz/datovky/assignments/-/tree/master). diff --git a/04-ab_tree/cpp/Makefile b/04-ab_tree/cpp/Makefile deleted file mode 100644 index e6ab228..0000000 --- a/04-ab_tree/cpp/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -test: ab_tree_test - ./$< - -CXXFLAGS=-std=c++11 -O2 -Wall -Wextra -g -Wno-sign-compare - -ab_tree_test: ab_tree_test.cpp ab_tree.h test_main.cpp - $(CXX) $(CXXFLAGS) $^ -o $@ - -clean: - rm -f ab_tree_test - -.PHONY: clean test diff --git a/04-ab_tree/cpp/ab_tree.h b/04-ab_tree/cpp/ab_tree.h deleted file mode 100644 index e77374b..0000000 --- a/04-ab_tree/cpp/ab_tree.h +++ /dev/null @@ -1,135 +0,0 @@ -#include <limits> -#include <vector> -#include <tuple> -#include <iostream> - -using namespace std; - -// If the condition is not true, report an error and halt. -#define EXPECT(condition, message) do { if (!(condition)) expect_failed(message); } while (0) - -void expect_failed(const string& message); - -/*** One node ***/ - -class ab_node { - public: - // Keys stored in this node and the corresponding children - // The vectors are large enough to accomodate one extra entry - // in overflowing nodes. - vector<ab_node *> children; - vector<int> keys; - ab_node *parent; - - // If this node contains the given key, return true and set i to key's position. - // Otherwise return false and set i to the first key greater than the given one. - bool find_branch(int key, int &i) - { - i = 0; - while (i < keys.size() && keys[i] <= key) { - if (keys[i] == key) - return true; - i++; - } - return false; - } - - // Insert a new key at posision i and add a new child between keys i and i+1. - void insert_branch(int i, int key, ab_node *child) - { - keys.insert(keys.begin() + i, key); - children.insert(children.begin() + i + 1, child); - } - - // An auxiliary function for displaying a sub-tree under this node. - void show(int indent); -}; - -/*** Tree ***/ - -class ab_tree { - public: - int a; // Minimum allowed number of children - int b; // Maximum allowed number of children - ab_node *root; // Root node (even a tree with no keys has a root) - int num_nodes; // We keep track of how many nodes the tree has - - // Create a new node and return a pointer to it. - ab_node *new_node(ab_node* parent) - { - ab_node *n = new ab_node; - n->keys.reserve(b); - n->children.reserve(b+1); - n->parent = parent; - num_nodes++; - return n; - } - - // Delete a given node, assuming that its children have been already unlinked. - void delete_node(ab_node *n) - { - num_nodes--; - delete n; - } - - // Constructor: initialize an empty tree with just the root. - ab_tree(int a, int b) - { - EXPECT(a >= 2 && b >= 2*a - 1, "Invalid values of a,b"); - this->a = a; - this->b = b; - num_nodes = 0; - // The root has no keys and one null child pointer. - root = new_node(nullptr); - root->children.push_back(nullptr); - } - - // An auxiliary function for deleting a subtree recursively. - void delete_tree(ab_node *n) - { - for (int i=0; i < n->children.size(); i++) - if (n->children[i]) - delete_tree(n->children[i]); - delete_node(n); - } - - // Destructor: delete all nodes. - ~ab_tree() - { - delete_tree(root); - EXPECT(num_nodes == 0, "Memory leak detected: some nodes were not deleted"); - } - - // Find a key: returns true if it is present in the tree. - bool find(int key) - { - ab_node *n = root; - while (n) { - int i; - if (n->find_branch(key, i)) - return true; - n = n->children[i]; - } - return false; - } - - // Display the tree on standard output in human-readable form. - void show(); - - // Check that the data structure satisfies all invariants. - void audit(); - - // Split the node into two nodes: move some children of n into - // a newly created node such that n contains exactly size children in the end. - // Return the new node and the key separating n and the new node. - virtual pair<ab_node*, int> split_node(ab_node* n, int size) - { - // FIXME: Implement - } - - // Insert: add key to the tree (unless it was already present). - virtual void insert(int key) - { - // FIXME: Implement - } -}; diff --git a/04-ab_tree/cpp/ab_tree_test.cpp b/04-ab_tree/cpp/ab_tree_test.cpp deleted file mode 100644 index d718d87..0000000 --- a/04-ab_tree/cpp/ab_tree_test.cpp +++ /dev/null @@ -1,150 +0,0 @@ -#include <functional> -#include <cstdlib> -#include <vector> - -#include "ab_tree.h" - -// Debugging output: showing trees prettily on standard output. - -void ab_tree::show() -{ - root->show(0); - for (int i=0; i<70; i++) - cout << '='; - cout << endl; -} - -void ab_node::show(int indent) -{ - for (int i = children.size() - 1; i >= 0 ; i--) { - if (i < keys.size()) { - for (int j = 0; j < indent; j++) - cout << " "; - cout << keys[i] << endl; - } - if (children[i]) - children[i]->show(indent+1); - } -} - -// Invariant checks - -void audit_subtree(ab_tree *tree, ab_node *n, ab_node* parent, int key_min, int key_max, int depth, int &leaf_depth) -{ - if (!n) { - // Check that all leaves are on the same level. - if (leaf_depth < 0) - leaf_depth = depth; - else - EXPECT(depth == leaf_depth, "Leaves are not on the same level"); - return; - } - // Check consistency of parent pointers - EXPECT(n->parent == parent, "Inconsistent parent pointers"); - - // The number of children must be in the allowed range. - if (depth > 0) - EXPECT(n->children.size() >= tree->a, "Too few children"); - EXPECT(n->children.size() <= tree->b, "Too many children"); - - // We must have one more children than keys. - EXPECT(n->children.size() == n->keys.size() + 1, "Number of keys does not match number of children"); - - // Allow degenerate trees with 0 keys in the root. - if (n->children.size() == 1) - return; - - // Check order of keys: they must be increasing and bounded by the keys on the higher levels. - for (int i = 0; i < n->keys.size(); i++) { - EXPECT(n->keys[i] >= key_min && n->keys[i] <= key_max, "Wrong key order"); - EXPECT(i == 0 || n->keys[i-1] < n->keys[i], "Wrong key order"); - } - - // Call on children recursively. - for (int i = 0; i < n->children.size(); i++) { - int tmin, tmax; - if (i == 0) - tmin = key_min; - else - tmin = n->keys[i-1] + 1; - if (i < n->keys.size()) - tmax = n->keys[i] - 1; - else - tmax = key_max; - audit_subtree(tree, n->children[i], n, tmin, tmax, depth+1, leaf_depth); - } -} - -void ab_tree::audit() -{ - EXPECT(root, "Tree has no root"); - int leaf_depth = -1; - audit_subtree(this, root, nullptr, numeric_limits<int>::min(), numeric_limits<int>::max(), 0, leaf_depth); -} - -// A basic test: insert a couple of keys and show how the tree evolves. - -void test_basic() -{ - cout << "## Basic test" << endl; - - ab_tree t(2, 3); - vector<int> keys = { 3, 1, 4, 5, 9, 2, 6, 8, 7, 0 }; - for (int k : keys) { - t.insert(k); - t.show(); - t.audit(); - EXPECT(t.find(k), "Inserted key disappeared"); - } - - for (int k : keys) - EXPECT(t.find(k), "Some keys are missing at the end"); -} - -// The main test: inserting a lot of keys and checking that they are really there. -// We will insert num_items keys from the set {1,...,range-1}, where range is a prime. - -void test_main(int a, int b, int range, int num_items) -{ - // Create a new tree. - cout << "## Test: a=" << a << " b=" << b << " range=" << range << " num_items=" << num_items << endl; - ab_tree t(a, b); - - int key = 1; - int step = (int)(range * 1.618); - int audit_time = 1; - - // Insert keys. - for (int i=1; i <= num_items; i++) { - t.insert(key); - // Audit the tree occasionally. - if (i == audit_time || i == num_items) { - // cout << "== Audit at " << i << endl; - // t.show(); - t.audit(); - audit_time = (int)(audit_time * 1.33) + 1; - } - key = (key + step) % range; - } - - // Check that the tree contains exactly the items it should contain. - key = 1; - for (int i=1; i < range; i++) { - bool found = t.find(key); - // cout << "Step #" << i << ": find(" << key << ") = " << found << endl; - EXPECT(found == (i <= num_items), "Tree contains wrong keys"); - key = (key + step) % range; - } -} - -/*** A list of all tests ***/ - -vector<pair<string, function<void()>>> tests = { - { "basic", [] { test_basic(); } }, - { "small-2,3", [] { test_main(2, 3, 997, 700); } }, - { "small-2,4", [] { test_main(2, 4, 997, 700); } }, - { "big-2,3", [] { test_main(2, 3, 999983, 700000); } }, - { "big-2,4", [] { test_main(2, 4, 999983, 700000); } }, - { "big-10,20", [] { test_main(10, 20, 999983, 700000); } }, - { "big-100,200", [] { test_main(100, 200, 999983, 700000); } }, -}; diff --git a/04-ab_tree/cpp/test_main.cpp b/04-ab_tree/cpp/test_main.cpp deleted file mode 100644 index 3f4aff0..0000000 --- a/04-ab_tree/cpp/test_main.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include <cstdlib> -#include <functional> -#include <iostream> -#include <string> -#include <utility> -#include <vector> - -using namespace std; - -extern vector<pair<string, function<void()>>> tests; - -void expect_failed(const string& message) { - cerr << "Test error: " << message << endl; - exit(1); -} - -int main(int argc, char* argv[]) { - vector<string> required_tests; - - if (argc > 1) { - required_tests.assign(argv + 1, argv + argc); - } else { - for (const auto& test : tests) - required_tests.push_back(test.first); - } - - for (const auto& required_test : required_tests) { - bool found = false; - for (const auto& test : tests) - if (required_test == test.first) { - cerr << "Running test " << required_test << endl; - test.second(); - found = true; - break; - } - if (!found) { - cerr << "Unknown test " << required_test << endl; - return 1; - } - } - - return 0; -} diff --git a/04-ab_tree/python/ab_tree.py b/04-ab_tree/python/ab_tree.py deleted file mode 100644 index baae16f..0000000 --- a/04-ab_tree/python/ab_tree.py +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env python3 - -class ABNode: - """Single node in an ABTree. - - Each node contains keys and children - (with one more children than there are keys). - We also store a pointer to node's parent (None for root). - """ - def __init__(self, keys = None, children = None, parent = None): - self.keys = keys if keys is not None else [] - self.children = children if children is not None else [] - self.parent = parent - - def find_branch(self, key): - """ Try finding given key in this node. - - If this node contains the given key, returns (True, key_position). - If not, returns (False, first_position_with_key_greater_than_the_given). - """ - i = 0 - while (i < len(self.keys) and self.keys[i] < key): - i += 1 - - return (i < len(self.keys) and self.keys[i] == key, i) - - def insert_branch(self, i, key, child): - """ Insert a new key and a given child between keys i and i+1.""" - self.keys.insert(i, key) - self.children.insert(i + 1, child) - -class ABTree: - """A class representing the whole ABTree.""" - def __init__(self, a, b): - assert a >= 2 and b >= 2 * a - 1, "Invalid values of a, b: {}, {}".format(a, b) - self.a = a - self.b = b - self.root = ABNode(children=[None]) - - def find(self, key): - """Find a key in the tree. - - Returns True if the key is present, False otherwise. - """ - node = self.root - while node: - found, i = node.find_branch(key) - if found: return True - node = node.children[i] - return False - - def split_node(self, node, size): - """Helper function for insert - - Split node into two nodes such that original node contains first _size_ children. - Return new node and the key separating nodes. - """ - # TODO: Implement and use in insert method - raise NotImplementedError - - def insert(self, key): - """Add a given key to the tree, unless already present.""" - # TODO: Implement - raise NotImplementedError diff --git a/04-ab_tree/python/ab_tree_test.py b/04-ab_tree/python/ab_tree_test.py deleted file mode 100644 index 110553c..0000000 --- a/04-ab_tree/python/ab_tree_test.py +++ /dev/null @@ -1,111 +0,0 @@ -#!/usr/bin/env python3 -import math -import sys - -from ab_tree import ABNode, ABTree - -def show(tree): - """Show a tree.""" - def show_node(node, indent): - for i in reversed(range(len(node.children))): - if i < len(node.keys): - print(" " * indent, node.keys[i], sep="") - if node.children[i]: - show_node(node.children[i], indent + 1) - - show_node(tree.root, 0) - print("=" * 70) - -def audit(tree): - """Invariant check for the given tree.""" - def audit_node(node, parent, key_min, key_max, depth, leaf_depth): - if not node: - # Check that all leaves are on the same level. - if leaf_depth is None: - leaf_depth = depth - assert depth == leaf_depth, "Leaves are not on the same level" - - else: - # Check consistency of parent pointers - assert node.parent == parent, "Inconsistent parent pointers" - - # The number of children must be in the allowed range. - assert depth == 0 or len(node.children) >= tree.a, "Too few children" - assert len(node.children) <= tree.b, "Too many children" - - # We must have one more children than keys - assert len(node.children) == len(node.keys) + 1, "Number of keys does not match number of children" - - # Check that keys are increasing and in (key_min, key_max) range. - for i in range(len(node.keys)): - assert node.keys[i] > key_min and node.keys[i] < key_max, "Wrong key order" - assert i == 0 or node.keys[i - 1] < node.keys[i], "Wrong key order" - - # Check children recursively - for i in range(len(node.children)): - child_min = node.keys[i - 1] if i > 0 else key_min - child_max = node.keys[i] if i < len(node.keys) else key_max - leaf_depth = audit_node(node.children[i], node, child_min, child_max, depth + 1, leaf_depth) - - return leaf_depth - - assert tree.root, "Tree has no root" - audit_node(tree.root, None, -math.inf, math.inf, 0, None) - -def test_basic(): - """Insert a couple of keys and show how the tree evolves.""" - print("## Basic test") - - tree = ABTree(2, 3) - keys = [3, 1, 4, 5, 9, 2, 6, 8, 7, 0] - for key in keys: - tree.insert(key) - show(tree) - audit(tree) - assert tree.find(key), "Inserted key disappeared" - - for key in keys: - assert tree.find(key), "Some keys are missing at the end" - -def test_main(a, b, limit, num_items): - print("## Test: a={} b={} range={} num_items={}".format(a, b, limit, num_items)) - - tree = ABTree(a, b) - - # Insert keys - step = int(limit * 1.618) - key, audit_time = 1, 1 - for i in range(num_items): - tree.insert(key) - key = (key + step) % limit - - # Audit the tree occasionally - if i == audit_time or i + 1 == num_items: - audit(tree) - audit_time = int(audit_time * 1.33) + 1 - - # Check the content of the tree - key = 1 - for i in range(limit): - assert tree.find(key) == (i < num_items), "Tree contains wrong keys" - key = (key + step) % limit - -tests = [ - ("basic", test_basic), - ("small-2,3", lambda: test_main(2, 3, 997, 700)), - ("small-2,4", lambda: test_main(2, 4, 997, 700)), - ("big-2,3", lambda: test_main(2, 3, 99991, 70000)), - ("big-2,4", lambda: test_main(2, 4, 99991, 70000)), - ("big-10,20", lambda: test_main(10, 20, 99991, 70000)), - ("big-100,200", lambda: test_main(100, 200, 99991, 70000)), -] - -if __name__ == "__main__": - for required_test in sys.argv[1:] or [name for name, _ in tests]: - for name, test in tests: - if name == required_test: - print("Running test {}".format(name), file=sys.stderr) - test() - break - else: - raise ValueError("Unknown test {}".format(name)) diff --git a/04-ab_tree/task.md b/04-ab_tree/task.md deleted file mode 100644 index eb23ec5..0000000 --- a/04-ab_tree/task.md +++ /dev/null @@ -1,15 +0,0 @@ -You are given a representation of _(a, b)-tree_ with a `find` operation, -and a representation of an _(a, b)-tree node_. - -Your goal is to implement an `insert` operation, which inserts the given -key in the tree (or does nothing if the key is already present). Preferably, -you should also implement `split_node` method and use it properly in -your `insert` implementation. - -The implementation uses the variant of (a,b)-trees from lecture notes by [Martin Mares, -Chapter 3](http://mj.ucw.cz/vyuka/dsnotes/03-abtree.pdf) where the actual values are -stored also in the internal nodes of the tree and not only in leaves. - -You should submit the `ab_tree.*` file (but not `ab_tree_test.*` files). - -Source code templates can be found in [git](https://gitlab.kam.mff.cuni.cz/datovky/assignments/-/tree/master). diff --git a/05-ab_experiment/cpp/Makefile b/05-ab_experiment/cpp/Makefile deleted file mode 100644 index 967fad4..0000000 --- a/05-ab_experiment/cpp/Makefile +++ /dev/null @@ -1,22 +0,0 @@ -STUDENT_ID ?= PLEASE_SET_STUDENT_ID - -.PHONY: test -test: ab_experiment - @rm -rf out && mkdir out - @for test in insert min random ; do \ - for mode in '2-3' '2-4' ; do \ - echo t-$$test-$$mode ; \ - ./ab_experiment $$test $(STUDENT_ID) $$mode >out/t-$$test-$$mode ; \ - done ; \ - done - -INCLUDE ?= . -CXXFLAGS=-std=c++11 -O2 -Wall -Wextra -g -Wno-sign-compare -I$(INCLUDE) - -ab_experiment: ab_tree.h ab_experiment.cpp $(INCLUDE)/random.h - $(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@ - -.PHONY: clean -clean:: - rm -f ab_experiment - rm -rf out diff --git a/05-ab_experiment/cpp/ab_experiment.cpp b/05-ab_experiment/cpp/ab_experiment.cpp deleted file mode 100644 index 31b520e..0000000 --- a/05-ab_experiment/cpp/ab_experiment.cpp +++ /dev/null @@ -1,389 +0,0 @@ -#include <algorithm> -#include <functional> -#include <string> -#include <utility> -#include <vector> -#include <iostream> -#include <cmath> - -#include "ab_tree.h" -#include "random.h" - -using namespace std; - -void expect_failed(const string& message) { - cerr << "Test error: " << message << endl; - exit(1); -} - -/* - * A modified Splay tree for benchmarking. - * - * We inherit the implementation of operations from the Tree class - * and extend it by keeping statistics on the number of splay operations - * and the total number of rotations. Also, if naive is turned on, - * splay uses only single rotations. - * - * Please make sure that your Tree class defines the rotate() and splay() - * methods as virtual. - */ - -class BenchmarkingABTree : public ab_tree { -public: - int num_operations; - int num_struct_changes; - - BenchmarkingABTree(int a, int b) : ab_tree(a,b) - { - reset(); - } - - void reset() - { - num_operations = 0; - num_struct_changes = 0; - } - - pair<ab_node*, int> split_node(ab_node *node, int size) override - { - num_struct_changes++; - return ab_tree::split_node(node, size); - } - - void insert(int key) override - { - num_operations++; - ab_tree::insert(key); - } - - // Return the average number of rotations per operation. - double struct_changes_per_op() - { - if (num_operations > 0) - return (double) num_struct_changes / num_operations; - else - return 0; - } - - // Delete key from the tree. Does nothing if the key is not in the tree. - void remove(int key){ - num_operations += 1; - - // Find the key to be deleted - ab_node *node = root; - int i; - bool found = node->find_branch(key, i); - while(!found){ - node = node->children[i]; - if (!node) return; // Key is not in the tree - found = node->find_branch(key, i); - } - - // If node is not a leaf, we need to swap the key with its successor - if (node->children[0] != nullptr){ // Only leaves have nullptr as children - // Successor is leftmost key in the right subtree of key - ab_node *succ = min(node->children[i+1]); - swap(node->keys[i], succ->keys[0]); - node = succ; - } - - // Now run the main part of the delete - remove_leaf(key, node); - } - -private: - // Main part of the remove - void remove_leaf(int key, ab_node* node) - { - EXPECT(node != nullptr, "Trying to delete key from nullptr"); - EXPECT(node->children[0] == nullptr, "Leaf's child must be nullptr"); - - while(1){ - // Find the key in the node - int key_position; - bool found = node->find_branch(key, key_position); - EXPECT(found, "Trying to delete key that is not in the node."); - - // Start with the deleting itself - node->keys.erase(node->keys.cbegin() + key_position); - node->children.erase(node->children.cbegin() + key_position + 1); - - // No underflow means we are done - if (node->children.size() >= a) return; - - // Root may underflow, but cannot have just one child (unless tree is empty) - if (node == root){ - if ((node->children.size() == 1) && (root->children[0] != nullptr)){ - ab_node *old_root = root; - root = root->children[0]; - root->parent = nullptr; - delete_node(old_root); - } - return; - } - - ab_node *brother; - int separating_key_pos; - bool tmp; - tie(brother, separating_key_pos, tmp) = get_brother(node); - int separating_key = node->parent->keys[separating_key_pos]; - - // First check whether we can steal brother's child - if (brother->children.size() > a){ - steal_child(node); - return; - } - - // If the brother is too small, we merge with him and propagate the delete - node = merge_node(node); - node = node->parent; - key = separating_key; - key_position = separating_key_pos; - } - } - - // Return the leftmost node of a subtree rooted at node. - ab_node* min(ab_node *node) - { - EXPECT(node != nullptr, "Trying to search for minimum of nullptr"); - while (node->children[0]) { - node = node->children[0]; - } - return node; - } - - // Return the left brother if it exists, otherwise return right brother. - // Returns tuple (brother, key_position, is_left_brother), where - // key_position is a position of the key that separates node and brother in their parent. - tuple<ab_node*, int, bool> get_brother(ab_node* node) - { - ab_node *parent = node->parent; - EXPECT(parent != nullptr, "Node without parent has no brother"); - - // Find node in parent's child list - int i; - for(i = 0; i < parent->children.size(); ++i){ - ab_node *c = parent->children[i]; - if (c == node) break; - } - EXPECT(i < parent->children.size(), "Node is not inside its parent"); - - if (i == 0){ - return make_tuple(parent->children[1], 0, false); - } - else{ - return make_tuple(parent->children[i - 1], i - 1, true); - } - } - - // Transfer one child from node's left brother to the node. - // If node has no left brother, use right brother instead. - void steal_child(ab_node* node) - { - ab_node *brother; - int separating_key_pos; - bool is_left_brother; - tie(brother, separating_key_pos, is_left_brother) = get_brother(node); - int separating_key = node->parent->keys[separating_key_pos]; - - EXPECT(brother->children.size() > a, "Stealing child causes underflow in brother!"); - EXPECT(node->children.size() < b, "Stealing child causes overflow in the node!"); - - // We steal either from front or back - int steal_position, target_position; - if (is_left_brother){ - steal_position = brother->children.size()-1; - target_position = 0; - } - else{ - steal_position = 0; - target_position = node->children.size(); - } - // Steal the child - ab_node *stolen_child = brother->children[steal_position]; - if (stolen_child != nullptr){ - stolen_child->parent = node; - } - node->children.insert(node->children.cbegin() + target_position, stolen_child); - brother->children.erase(brother->children.cbegin() + steal_position); - - // List of keys is shorter than list of children - if (is_left_brother) steal_position -= 1; - else target_position -= 1; - - // Update keys - node->keys.insert(node->keys.cbegin() + target_position, separating_key); - node->parent->keys[separating_key_pos] = brother->keys[steal_position]; - brother->keys.erase(brother->keys.cbegin() + steal_position); - } - -public: - // Merge node with its left brother and destroy the node. Must not cause overflow! - // Returns result of the merge. - // If node has no left brother, use right brother instead. - ab_node* merge_node(ab_node* node){ - num_struct_changes += 1; - - ab_node *brother; - int separating_key_pos; - bool is_left_brother; - tie(brother, separating_key_pos, is_left_brother) = get_brother(node); - int separating_key = node->parent->keys[separating_key_pos]; - - // We swap brother and node if necessary so that the node is always on the right - if (!is_left_brother) swap(brother, node); - - for (auto c: node->children) - brother->children.push_back(c); - brother->keys.push_back(separating_key); - for (auto k: node->keys) - brother->keys.push_back(k); - - EXPECT(brother->children.size() <= b, "Merge caused overflow!"); - - // Update parent pointers in non-leaf - if (brother->children[0] != nullptr){ - for (auto c : brother->children) - c->parent = brother; - } - - delete_node(node); - return brother; - } -}; - -int a, b; -RandomGen *rng; // Random generator object - -// An auxiliary function for generating a random permutation. -vector<int> random_permutation(int n) -{ - vector<int> perm; - for (int i=0; i<n; i++) - perm.push_back(i); - for (int i=0; i<n-1; i++) - swap(perm[i], perm[i + rng->next_range(n-i)]); - return perm; -} - -void test_insert() -{ - for (int e=32; e<=64; e++) { - int n = (int) pow(2, e/4.); - BenchmarkingABTree tree = BenchmarkingABTree(a,b); - - vector<int> perm = random_permutation(n); - for (int x : perm) - tree.insert(x); - - cout << n << " " << tree.struct_changes_per_op() << endl; - } -} - -void test_random() -{ - for (int e=32; e<=64; e++) { - int n = (int) pow(2, e/4.); - BenchmarkingABTree tree = BenchmarkingABTree(a,b); - - // We keep track of elements present and not present in the tree - vector<int> elems; - vector<int> anti_elems; - elems.reserve(n); - anti_elems.reserve(n+1); - - for (int x = 0; x < 2*n; x+=2){ - tree.insert(x); - elems.push_back(x); - } - - for (int i = -1; i <2*n + 1; i+=2) - anti_elems.push_back(i); - - for (int i=0; i<n; i++){ - int r, x; - // Delete random element - r = rng->next_range(elems.size()); - x = elems[r]; - tree.remove(x); - elems.erase(elems.cbegin() + r); - anti_elems.push_back(x); - - // Insert random "anti-element" - r = rng->next_range(anti_elems.size()); - x = anti_elems[r]; - tree.insert(x); - elems.push_back(x); - anti_elems.erase(anti_elems.cbegin() + r); - } - - cout << n << " " << tree.struct_changes_per_op() << endl; - } -} - -void test_min() -{ - for (int e=32; e<=64; e++) { - int n = (int) pow(2, e/4.); - BenchmarkingABTree tree = BenchmarkingABTree(a,b); - - for (int x = 0; x < n; x++) - tree.insert(x); - - for (int i=0; i<n; i++){ - tree.remove(0); - tree.insert(0); - } - - cout << n << " " << tree.struct_changes_per_op() << endl; - } -} - -vector<pair<string, function<void()>>> tests = { - { "insert", test_insert }, - { "random", test_random }, - { "min", test_min }, -}; - -int main(int argc, char **argv) -{ - if (argc != 4) { - cerr << "Usage: " << argv[0] << " <test> <student-id> (2-3|2-4)" << endl; - return 1; - } - - string which_test = argv[1]; - string id_str = argv[2]; - string mode = argv[3]; - - try { - rng = new RandomGen(stoi(id_str)); - } catch (...) { - cerr << "Invalid student ID" << endl; - return 1; - } - - a = 2; - if (mode == "2-3") - b = 3; - else if (mode == "2-4") - b = 4; - else - { - cerr << "Last argument must be either '2-3' or '2-4'" << endl; - return 1; - } - - for (const auto& test : tests) { - if (test.first == which_test) - { - cout.precision(12); - test.second(); - return 0; - } - } - cerr << "Unknown test " << which_test << endl; - return 1; - - return 0; -} diff --git a/05-ab_experiment/cpp/random.h b/05-ab_experiment/cpp/random.h deleted file mode 100644 index 7d18ab6..0000000 --- a/05-ab_experiment/cpp/random.h +++ /dev/null @@ -1,59 +0,0 @@ -#define DS1_RANDOM_H - -#include <cstdint> - -/* - * This is the xoroshiro128+ random generator, designed in 2016 by David Blackman - * and Sebastiano Vigna, distributed under the CC-0 license. For more details, - * see http://vigna.di.unimi.it/xorshift/. - * - * Rewritten to C++ by Martin Mares, also placed under CC-0. - */ - -class RandomGen { - uint64_t state[2]; - - uint64_t rotl(uint64_t x, int k) - { - return (x << k) | (x >> (64 - k)); - } - - public: - // Initialize the generator, set its seed and warm it up. - RandomGen(unsigned int seed) - { - state[0] = seed * 0xdeadbeef; - state[1] = seed ^ 0xc0de1234; - for (int i=0; i<100; i++) - next_u64(); - } - - // Generate a random 64-bit number. - uint64_t next_u64(void) - { - uint64_t s0 = state[0], s1 = state[1]; - uint64_t result = s0 + s1; - s1 ^= s0; - state[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); - state[1] = rotl(s1, 36); - return result; - } - - // Generate a random 32-bit number. - uint32_t next_u32(void) - { - return next_u64() >> 11; - } - - // Generate a number between 0 and range-1. - unsigned int next_range(unsigned int range) - { - /* - * This is not perfectly uniform, unless the range is a power of two. - * However, for 64-bit random values and 32-bit ranges, the bias is - * insignificant. - */ - return next_u64() % range; - } -}; - diff --git a/05-ab_experiment/python/Makefile b/05-ab_experiment/python/Makefile deleted file mode 100644 index 48e36ae..0000000 --- a/05-ab_experiment/python/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -STUDENT_ID ?= PLEASE_SET_STUDENT_ID - -.PHONY: test -test: ab_experiment.py ab_tree.py - @rm -rf out && mkdir out - @for test in insert min random ; do \ - for mode in '2-3' '2-4' ; do \ - echo t-$$test-$$mode ; \ - ./ab_experiment.py $$test $(STUDENT_ID) $$mode >out/t-$$test-$$mode ; \ - done ; \ - done - -.PHONY: clean -clean:: - rm -rf out __pycache__ diff --git a/05-ab_experiment/python/ab_experiment.py b/05-ab_experiment/python/ab_experiment.py deleted file mode 100755 index bc116e3..0000000 --- a/05-ab_experiment/python/ab_experiment.py +++ /dev/null @@ -1,259 +0,0 @@ -#!/usr/bin/env python3 - -import sys -import random - -from ab_tree import ABTree - -class BenchmarkingABTree(ABTree): - """A modified ABTree for benchmarking. - - We inherit the implementation of operations from the ABTree class - and extend it by delete operation and by keeping statistics on the number - of operations and the total number of structural changes. - """ - def __init__(self, a, b): - ABTree.__init__(self, a, b) - self.reset() - - def reset(self): - """ Reset statistics """ - self.num_operations = 0 - self.num_struct_changes = 0 - - def struct_changes_per_op(self): - """Return the average number of struct. changes per operation.""" - if self.num_operations > 0: - return self.num_struct_changes / self.num_operations - else: - return 0 - - def insert(self, key): - self.num_operations += 1 - ABTree.insert(self, key) - - def split_node(self, node, size): - self.num_struct_changes += 1 - return ABTree.split_node(self, node, size) - - def remove(self, key): - """ Delete key from the tree. Does nothing if the key is not in the tree. """ - self.num_operations += 1 - - # Find the key to be deleted - node = self.root - found, i = node.find_branch(key) - while not found: - node = node.children[i] - if not node: return # Key is not in the tree - found, i = node.find_branch(key) - - # If node is not a leaf, we need to swap the key with its successor - if node.children[0] is not None: # Only leaves have None as children - # Successor is leftmost key in the right subtree of key - succ = self._min(node.children[i+1]) - node.keys[i], succ.keys[0] = succ.keys[0], node.keys[i] - node = succ - - # Now run the main part of the delete - self._remove_leaf(key, node) - - def _remove_leaf(self, key, node): - """ Main part of the delete. - """ - assert node is not None, "Trying to delete key from None" - assert node.children[0] is None, "Leaf's child must be None" - - while True: - # Find the key in the node - found, key_position = node.find_branch(key) - assert found, "Trying to delete key that is not in the node." - - # Start with the deleting itself - del node.keys[key_position] - del node.children[key_position + 1] - - # No underflow means we are done - if len(node.children) >= self.a: return - - # Root may underflow, but cannot have just one child (unless tree is empty) - if node == self.root: - if (len(node.children) == 1) and (self.root.children[0] is not None): - self.root = self.root.children[0] - self.root.parent = None - return - - brother, separating_key_pos, _ = self._get_brother(node) - separating_key = node.parent.keys[separating_key_pos] - - # First check whether we can steal brother's child - if len(brother.children) > self.a: - self._steal_child(node) - return - - # If the brother is too small, we merge with him and propagate the delete - node = self.merge_node(node) - node, key, key_position = node.parent, separating_key, separating_key_pos - - def _min(self, node): - """ Return the leftmost node of a subtree rooted at node.""" - assert node is not None - while node.children[0] is not None: - node = node.children[0] - return node - - def _get_brother(self, node): - """ Return the left brother if it exists, otherwise return right brother. - returns tuple (brother, key_position, is_left_brother), where - key_position is a position of the key that separates node and brother in their parent. - """ - parent = node.parent - assert parent is not None, "Node without parent has no brother" - - # Find node in parent's child list - i = 0 - for c in parent.children: - if c is node: break - else: i += 1 - assert i < len(parent.children), "Node is not inside its parent" - - if i == 0: - return parent.children[1], 0, False - else: - return parent.children[i - 1], i - 1, True - - def _steal_child(self, node): - """ Transfer one child from node's left brother to the node. - If node has no left brother, use right brother instead. - """ - brother, separating_key_pos, is_left_brother = self._get_brother(node) - separating_key = node.parent.keys[separating_key_pos] - - assert len(brother.children) > self.a, "Stealing child causes underflow in brother!" - assert len(node.children) < self.b, "Stealing child causes overflow in the node!" - - # We steal either from front or back - if is_left_brother: - steal_position = len(brother.children)-1 - target_position = 0 - else: - steal_position = 0 - target_position = len(node.children) - # Steal the child - stolen_child = brother.children[steal_position] - if stolen_child is not None: - stolen_child.parent = node - node.children.insert(target_position, stolen_child) - del brother.children[steal_position] - - # List of keys is shorter than list of children - if is_left_brother: - steal_position -= 1 - else: - target_position -= 1 - # Update keys - node.keys.insert(target_position, separating_key) - node.parent.keys[separating_key_pos] = brother.keys[steal_position] - del brother.keys[steal_position] - - def merge_node(self, node): - """ Merge node with its left brother and destroy the node. Must not cause overflow! - - Returns result of the merge. - If node has no left brother, use right brother instead. - """ - self.num_struct_changes += 1 - - brother, separating_key_pos, is_left_brother = self._get_brother(node) - separating_key = node.parent.keys[separating_key_pos] - - # We swap brother and node if necessary so that the node is always on the right - if not is_left_brother: - brother, node = node, brother - - brother.children.extend(node.children) - brother.keys.append(separating_key) - brother.keys.extend(node.keys) - - assert len(brother.children) <= self.b, "Merge caused overflow!" - - # Update parent pointers in non-leaf - if brother.children[0] is not None: - for c in brother.children: - c.parent = brother - return brother - -def test_insert(): - for exp in range(32, 64): - n = int(2**(exp/4)) - tree = BenchmarkingABTree(a, b) - - for elem in random.sample(range(n), n): - tree.insert(elem) - - print(n, tree.struct_changes_per_op()) - -def test_random(): - for exp in range(32, 64): - n = int(2**(exp/4)) - tree = BenchmarkingABTree(a, b) - - for elem in range(0, 2*n, 2): - tree.insert(elem) - - # We keep track of elements present and not present in the tree - elems = list(range(0, n, 2)) - anti_elems = list(range(-1, 2*n+1, 2)) - - for _ in range(n): - # Delete random element - elem = random.choice(elems) - tree.remove(elem) - elems.remove(elem) - anti_elems.append(elem) - - # Insert random "anti-element" - elem = random.choice(anti_elems) - tree.insert(elem) - elems.append(elem) - anti_elems.remove(elem) - - print(n, tree.struct_changes_per_op()) - -def test_min(): - for exp in range(32, 64): - n = int(2 ** (exp / 4)) - tree = BenchmarkingABTree(a, b) - - for i in range(n): - tree.insert(i) - - for _ in range(n): - tree.remove(0) - tree.insert(0) - - print(n, tree.struct_changes_per_op()) - -tests = { - "min": test_min, - "insert": test_insert, - "random": test_random, -} - -if __name__ == '__main__': - if len(sys.argv) == 4: - test, student_id = sys.argv[1], sys.argv[2] - a = 2 - if sys.argv[3] == "2-3": - b = 3 - elif sys.argv[3] == "2-4": - b = 4 - else: - raise ValueError("Last argument must be either '2-3' or '2-4'") - random.seed(student_id) - if test in tests: - tests[test]() - else: - raise ValueError("Unknown test {}".format(test)) - else: - raise ValueError("Usage: {} <test> <student-id> (2-3|2-4)".format(sys.argv[0])) diff --git a/05-ab_experiment/task.md b/05-ab_experiment/task.md deleted file mode 100644 index cd8e1e4..0000000 --- a/05-ab_experiment/task.md +++ /dev/null @@ -1,84 +0,0 @@ -## Goal - -The goal of this assignment is to evaluate your implementation of (a,b)-trees -experimentally and compare performance of (2,3) and (2,4)-trees. - -You are given a test program (`ab_experiment`) which is used to evaluate your -implementation of the previous assignment. The test program auguments your implementation -by implementing a `remove` method and it performs the following experiments: - -- _Insert test:_ Insert _n_ elements in random order. -- _Min test:_ Insert _n_ elements sequentially and then _n_ times repeat: remove the minimal - element in the tree and then insert it back. -- _Random test:_ Insert _n_ elements sequentially and then _n_ times repeat: remove random - element from the tree and then insert random element into the tree. Removed element is - always present in the tree and inserted element is always *not* present in the tree. - - -The program tries each experiment with different values of _n_. In each try, -it prints the average number of _structural changes_ per operation. Structural change is -either a node split (in insert) or merging of two nodes (in delete). - -You should perform these experiments and write a report, which contains the following -plots of the measured data. Each plot should show the dependence of the average -number of structural changes on the set size _n_. - -- The insert test: one curve for (2,3) tree, one for (2,4) tree. -- The min test: one curve for (2,3) tree, one for (2,4) tree. -- The random test: one curve for (2,3) tree, one for (2,4) tree. - -The report should discuss the experimental results and try to explain the observed -behavior using theory from the lectures. (If you want, you can carry out further -experiments to gain better understanding of the data structure and include these -in the report. This is strictly optional.) - -You should submit a PDF file with the report (and no source code). -You will get 1 temporary point upon submission if the file is syntantically correct; -proper points will be assigned later. - -## Test program - -The test program is given three arguments: -- The name of the test (`insert`, `min`, `random`). -- The random seed: you should use the last 2 digits of your student ID (you can find - it in the Study Information System – just click on the Personal data icon). Please - include the random seed in your report. -- The type of the tree to test (`2-3` or `2-4`). - -The output of the program contains one line per experiment, which consists of _n_ and the -average number of structural changes. - -## Your implementation - -Please use your implementation from the previous exercise. Methods `split_node(...)` -and `insert()` will be augmented by the test program. If you are performing -a node splits directly instead of using `split_node(...)` method, you -need to adjust the `BenchmarkingABTree` class accordingly. - -## Hints - -The following tools can be useful for producing nice plots: -- [pandas](https://pandas.pydata.org/) -- [matplotlib](https://matplotlib.org/) -- [gnuplot](http://www.gnuplot.info/) - -A quick checklist for plots: -- Is there a caption explaining what is plotted? -- Are the axes clearly labelled? Do they have value ranges and units? -- Have you mentioned that this axis has logarithmic scale? (Logarithmic graphs - are more fitting in some cases, but you should tell.) -- Is it clear which curve means what? -- Is it clear what are the measured points and what is an interpolated - curve between them? -- Are there any overlaps? (E.g., the most interesting part of the curve - hidden underneath a label?) - -In your discussion, please distinguish the following kinds of claims. -It should be always clear which is which: -- Experimental results (i.e., the raw data you obtained from the experiments) -- Theoretical facts (i.e., claims we have proved mathematically) -- Your hypotheses (e.g., when you claim that the graph looks like something is true, - but you are not able to prove rigorously that it always holds) - - -Source code templates can be found in [git](https://gitlab.kam.mff.cuni.cz/datovky/assignments/-/tree/master). diff --git a/06-matrix_transpose/cpp/Makefile b/06-matrix_transpose/cpp/Makefile deleted file mode 100644 index d21f424..0000000 --- a/06-matrix_transpose/cpp/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -test: matrix_transpose_test - ./$< - -CXXFLAGS=-std=c++11 -O2 -Wall -Wextra -g -Wno-sign-compare - -matrix_transpose_test: matrix_transpose_test.cpp matrix_transpose.h matrix_tests.h test_main.cpp - $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ - -clean: - rm -f matrix_transpose_test - -.PHONY: clean test diff --git a/06-matrix_transpose/cpp/matrix_tests.h b/06-matrix_transpose/cpp/matrix_tests.h deleted file mode 100644 index 6110f8e..0000000 --- a/06-matrix_transpose/cpp/matrix_tests.h +++ /dev/null @@ -1,213 +0,0 @@ -#include <string> -#include <vector> -#include <iostream> -#include <algorithm> - -/* A matrix stored in a simulated cache */ - -class CachedMatrix { - unsigned B; // Block size - unsigned mem_blocks; // Memory size in blocks - unsigned cache_blocks; // Cache size in blocks - unsigned cache_used; // How many blocks of cache we already used - - // We store the matrix as a one-dimensional array - vector<unsigned> items; - unsigned pos(unsigned i, unsigned j) { return i*N + j; } - - /* - * For each memory block, we keep the following structure. - * If the block is currently cached, we set cached == true - * and lru_prev/next point to neighboring blocks in the cyclic LRU list. - * Otherwise, cached == false and the block is not in the LRU. - */ - - class MemoryBlock { - public: - unsigned lru_prev, lru_next; - bool cached; - MemoryBlock() - { - lru_prev = lru_next = 0; - cached = false; - } - }; - - vector<MemoryBlock> blocks; - - // One block at the end of "blocks" serves as a head of the LRU list. - unsigned lru_head; - - public: - // Number of rows and columns of the matrix - unsigned N; - - int debug_level; // Verbosity - - CachedMatrix(unsigned N, unsigned M, unsigned B, int debug_level=0) - { - EXPECT(N > 0, "CachedMatrix must be non-empty."); - EXPECT(B > 0, "Blocks must be non-empty."); - EXPECT(!(M % B), "Cache size must be divisible by block size."); - EXPECT(M >= 2*B, "Cache must have at least 2 blocks."); - - unsigned NN = N*N; - items.resize(NN, 0); - - this->N = N; - this->B = B; - this->debug_level = debug_level; - mem_blocks = (NN+B-1) / B; - cache_blocks = M / B; - cache_used = 0; - - // Initialize the LRU list - blocks.resize(mem_blocks + 1); - lru_head = mem_blocks; - blocks[lru_head].lru_prev = lru_head; - blocks[lru_head].lru_next = lru_head; - - if (debug_level > 0) - cout << "\tMemory: " << mem_blocks << " blocks of " << B << " items, " << cache_blocks << " cached\n"; - } - - // Read value at position (i,j), used only in testing code - unsigned read(unsigned i, unsigned j) - { - EXPECT(i < N && j < N, "Read out of range: " + coord_string(i, j) + "."); - unsigned addr = pos(i, j); - access(addr); - return items[addr]; - } - - // Write value at position (i,j), used only in testing code - void write(unsigned i, unsigned j, unsigned data) - { - EXPECT(i < N && j < N, "Write out of range: " + coord_string(i, j) + "."); - unsigned addr = pos(i, j); - access(addr); - items[addr] = data; - } - - // Swap items (i1,j1) and (i2,j2) - void swap(unsigned i1, unsigned j1, unsigned i2, unsigned j2) - { - EXPECT(i1 < N && j1 < N && i2 < N && j2 < N, "Swap out of range: " + coord_string(i1, j1) + " with " + coord_string(i2, j2) + "."); - if (debug_level > 1) - cout << "\tSwap " << coord_string(i1, j1) << " " << coord_string(i2, j2) << endl; - unsigned addr1 = pos(i1, j1), addr2 = pos(i2, j2); - access(addr1); - access(addr2); - std::swap(items[addr1], items[addr2]); - } - - unsigned stat_cache_misses; - unsigned stat_accesses; - - // Reset statistic counters. - void reset_stats() - { - stat_cache_misses = 0; - stat_accesses = 0; - } - - static string coord_string(unsigned i, unsigned j) - { - return "(" + to_string(i) + "," + to_string(j) + ")"; - } - -#include "matrix_transpose.h" - - private: - // Bring the given address to the cache. - void access(unsigned addr) - { - int i = addr / B; // Which block to bring - if (blocks[i].cached) { - lru_remove(i); - } else { - if (cache_used < cache_blocks) { - // We still have room in the cache. - cache_used++; - if (debug_level > 1) - cout << "\t\tLoading block " << i << endl; - } else { - // We need to evict the least-recently used block to make space. - unsigned replace = blocks[lru_head].lru_prev; - lru_remove(replace); - EXPECT(blocks[replace].cached, "Internal error: Buggy LRU list."); - blocks[replace].cached = false; - if (debug_level > 1) - cout << "\t\tLoading block " << i << ", replacing " << replace << endl; - } - blocks[i].cached = true; - stat_cache_misses++; - } - lru_add_after(i, lru_head); - stat_accesses++; - } - - // Remove block from the LRU list. - void lru_remove(unsigned i) - { - unsigned prev = blocks[i].lru_prev; - unsigned next = blocks[i].lru_next; - blocks[prev].lru_next = next; - blocks[next].lru_prev = prev; - } - - // Add block at the given position in the LRU list. - void lru_add_after(unsigned i, unsigned after) - { - unsigned next = blocks[after].lru_next; - blocks[next].lru_prev = i; - blocks[after].lru_next = i; - blocks[i].lru_next = next; - blocks[i].lru_prev = after; - } -}; - -/* A cached matrix extended by methods for testing */ - -class TestMatrix : public CachedMatrix { - public: - TestMatrix(unsigned N, unsigned M, unsigned B, int debug_level = 0) : CachedMatrix(N, M, B, debug_level) { } - - // Fill matrix with a testing pattern. - void fill_matrix() - { - if (debug_level > 1) - cout << "\tInitializing\n"; - for (unsigned i = 0; i < N; i++) - for (unsigned j = 0; j < N; j++) - write(i, j, i*N + j); - } - - // Check that the pattern corresponds to the properly transposed matrix. - void check_result() - { - if (debug_level > 1) - cout << "\tChecking\n"; - for (unsigned i = 0; i < N; i++) { - for (unsigned j = 0; j < N; j++) { - unsigned want = j*N + i; - unsigned found = read(i, j); - unsigned found_i = found / N; - unsigned found_j = found % N; - EXPECT(found == want, - "Mismatch at position " + coord_string(i, j) + - ": expected element from " + coord_string(j, i) + - ", found element from " + coord_string(found_i, found_j) + - "."); - } - } - } - - // Transpose the matrix naively. - void naive_transpose() - { - for (unsigned i=0; i<N; i++) - for (unsigned j=0; j<i; j++) - swap(i, j, j, i); - } -}; diff --git a/06-matrix_transpose/cpp/matrix_transpose.h b/06-matrix_transpose/cpp/matrix_transpose.h deleted file mode 100644 index ddc58b5..0000000 --- a/06-matrix_transpose/cpp/matrix_transpose.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * This file is #include'd inside the definition of a matrix class - * like this: - * - * class ClassName { - * // Number of rows and columns of the matrix - * unsigned N; - * - * // Swap elements (i1,j1) and (i2,j2) - * void swap(unsigned i1, unsigned j1, unsigned i2, unsigned j2); - * - * // Your code - * #include "matrix_transpose.h" - * } - */ - -void transpose() -{ - // TODO: Implement this efficiently - - for (unsigned i=0; i<N; i++) - for (unsigned j=0; j<i; j++) - swap(i, j, j, i); -} diff --git a/06-matrix_transpose/cpp/matrix_transpose_test.cpp b/06-matrix_transpose/cpp/matrix_transpose_test.cpp deleted file mode 100644 index cbbfff1..0000000 --- a/06-matrix_transpose/cpp/matrix_transpose_test.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#include <functional> -#include <vector> -#include <iostream> -#include <iomanip> - -using namespace std; - -// If the condition is not true, report an error and halt. -#define EXPECT(condition, message) do { if (!(condition)) expect_failed(message); } while (0) - -void expect_failed(const string& message); - -#include "matrix_tests.h" - -void generic_test(unsigned N, unsigned M, unsigned B, double max_ratio, int debug_level) -{ - TestMatrix m(N, M, B, debug_level); - m.fill_matrix(); - m.reset_stats(); - m.transpose(); - - cout << "\t" << m.stat_cache_misses << " misses in " << m.stat_accesses << " accesses\n"; - if (m.stat_accesses) { - double mpa = (double) m.stat_cache_misses / m.stat_accesses; - double lb = 1. / B; - double ratio = mpa / lb; - cout << "\t" << - std::fixed << std::setprecision(6) << - mpa << " misses/access (lower bound is " << lb << - " => ratio " << ratio << - ", need " << max_ratio << ")\n"; - EXPECT(ratio <= max_ratio, "Algorithm did too many I/O operations."); - } - - m.check_result(); -} - -/*** A list of all tests ***/ - -vector<pair<string, function<void()>>> tests = { -// name N M B max_ratio debug_level - { "small2k", [] { generic_test( 8, 32, 8, 8, 2 ); } }, - { "small", [] { generic_test( 13, 64, 8, 4, 2 ); } }, - { "n100b16", [] { generic_test( 100, 1024, 16, 3, 1 ); } }, - { "n1000b16", [] { generic_test(1000, 1024, 16, 3, 1 ); } }, - { "n1000b64", [] { generic_test(1000, 8192, 64, 3, 1 ); } }, - { "n1000b256", [] { generic_test(1000, 65536, 256, 5, 1 ); } }, - { "n1000b4096", [] { generic_test(1000, 65536, 4096, 50, 1 ); } }, -}; diff --git a/06-matrix_transpose/cpp/test_main.cpp b/06-matrix_transpose/cpp/test_main.cpp deleted file mode 100644 index 3f4aff0..0000000 --- a/06-matrix_transpose/cpp/test_main.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include <cstdlib> -#include <functional> -#include <iostream> -#include <string> -#include <utility> -#include <vector> - -using namespace std; - -extern vector<pair<string, function<void()>>> tests; - -void expect_failed(const string& message) { - cerr << "Test error: " << message << endl; - exit(1); -} - -int main(int argc, char* argv[]) { - vector<string> required_tests; - - if (argc > 1) { - required_tests.assign(argv + 1, argv + argc); - } else { - for (const auto& test : tests) - required_tests.push_back(test.first); - } - - for (const auto& required_test : required_tests) { - bool found = false; - for (const auto& test : tests) - if (required_test == test.first) { - cerr << "Running test " << required_test << endl; - test.second(); - found = true; - break; - } - if (!found) { - cerr << "Unknown test " << required_test << endl; - return 1; - } - } - - return 0; -} diff --git a/06-matrix_transpose/python/matrix_tests.py b/06-matrix_transpose/python/matrix_tests.py deleted file mode 100644 index f002701..0000000 --- a/06-matrix_transpose/python/matrix_tests.py +++ /dev/null @@ -1,165 +0,0 @@ -from matrix_transpose import Matrix -import numpy - -# Description of memory blocks is stored in an array blocks[block_index][B_xxx]. -# If the block is cached, B_CACHED is 1 and B_LRU_NEXT and B_LRU_PREV point to -# neighboring blocks in the cyclic LRU list. Otherwise, B_CACHED is 0 and the block -# is not in the LRU. -B_CACHED = 0 -B_LRU_NEXT = 1 -B_LRU_PREV = 2 - -class CachedMatrix(Matrix): - """A matrix stored in simulated cache""" - - def __init__(self, N, M, B, debug_level=0): - assert N>0, "CachedMatrix must be non-empty." - assert B>0, "Blocks must be non-empty." - assert M%B == 0, "Cache size must be divisible by block size." - assert M >= 2*B, "Cache must have at least 2 blocks." - - Matrix.__init__(self, N) - - NN = N*N - self.items = numpy.zeros(shape=NN, dtype=numpy.int32, order="C") - - self.B = B - self.M = M - self.debug_level = debug_level - self.mem_blocks = (NN+B-1) // B - self.cache_blocks = M//B - self.cache_used = 0 - - # Initialize the LRU list. There is a virtual block right after the last real block, - # which serves as a head of the cyclic LRU list. - self.blocks = numpy.zeros(shape=(self.mem_blocks+1, 3), dtype=numpy.int32, order="C") - self.lru_head = self.mem_blocks - self.blocks[self.lru_head, B_LRU_NEXT] = self.lru_head - self.blocks[self.lru_head, B_LRU_PREV] = self.lru_head - - self.reset_stats() - - if debug_level > 0: - print("\tMemory: {} blocks of {} items, {} cached".format(self.mem_blocks, self.B, self.cache_blocks)) - - def _pos(self, i, j): - """Convert position in matrix to linear address in simulated memory.""" - - return i*self.N + j - - def read(self, i, j): - """Read value at position (i,j), used only in testing code.""" - - assert i >= 0 and i < self.N and j >= 0 and j < self.N, "Read out of range: ({},{})".format(i, j) - addr = self._pos(i, j) - self._access(addr) - return self.items[addr] - - def write(self, i, j, value): - """Write value at position (i,j), used only in testing code.""" - - assert i >= 0 and i < self.N and j >= 0 and j < self.N, "Write out of range: ({},{})".format(i, j) - addr = self._pos(i, j) - self._access(addr) - self.items[addr] = value - - def swap(self, i1, j1, i2, j2): - """Swap items (i1,j1) and (i2,j2).""" - - assert i1 >= 0 and i1 < self.N and j1 >= 0 and j1 < self.N and \ - i2 >= 0 and i2 < self.N and j2 >= 0 and j2 < self.N, \ - "Swap out of range: ({},{}) with ({},{})".format(i1, j1, i2, j2) - if self.debug_level > 1: - print("\tSwap ({},{}) ({},{})".format(i1, j1, i2, j2)) - addr1 = self._pos(i1, j1) - addr2 = self._pos(i2, j2) - self._access(addr1) - self._access(addr2) - items = self.items - items[addr1], items[addr2] = items[addr2], items[addr1] - - def reset_stats(self): - """Reset statistic counters.""" - - self.stat_cache_misses = 0 - self.stat_accesses = 0 - - def _access(self, addr): - """Bring the given address to the cache.""" - - blocks = self.blocks - i = addr // self.B # Which block to bring - if blocks[i, B_CACHED] > 0: - self._lru_remove(i) - else: - if self.cache_used < self.cache_blocks: - # We still have room in the cache. - self.cache_used += 1 - if self.debug_level > 1: - print("\t\tLoading block {}".format(i)) - else: - # We need to evict the least-recently used block to make space. - replace = blocks[self.lru_head, B_LRU_PREV] - self._lru_remove(replace) - assert blocks[replace, B_CACHED] > 0, "Internal error: Buggy LRU list" - blocks[replace, B_CACHED] = 0 - if self.debug_level > 1: - print("\t\tLoading block {}, replacing {}".format(i, replace)) - blocks[i, B_CACHED] = 1 - self.stat_cache_misses += 1 - self._lru_add_after(i, self.lru_head) - self.stat_accesses += 1 - - def _lru_remove(self, i): - """Remove block from the LRU list.""" - - blocks = self.blocks - prev, next = blocks[i, B_LRU_PREV], blocks[i, B_LRU_NEXT] - blocks[prev, B_LRU_NEXT] = next - blocks[next, B_LRU_PREV] = prev - - def _lru_add_after(self, i, after): - """Add block at the given position in the LRU list.""" - - blocks = self.blocks - next = blocks[after, B_LRU_NEXT] - blocks[after, B_LRU_NEXT] = i - blocks[next, B_LRU_PREV] = i - blocks[i, B_LRU_NEXT] = next - blocks[i, B_LRU_PREV] = after - -class TestMatrix(CachedMatrix): - """A cached matrix extended by methods for testing.""" - - # Constructor is inherited - - def fill_matrix(self): - """Fill matrix with a testing pattern.""" - - if self.debug_level > 1: - print("\tInitializing") - N = self.N - for i in range(N): - for j in range(N): - self.write(i, j, i*N + j) - - def check_result(self): - """Check that the pattern corresponds to the properly transposed matrix.""" - - if self.debug_level > 1: - print("\tChecking") - N = self.N - for i in range(N): - for j in range(N): - want = j*N + i - have = self.read(i, j) - have_i = have // N - have_j = have % N - assert have == want, "Mismatch at position ({},{}): expected element from ({},{}), found element from ({},{})".format(i, j, j, i, have_i, have_j) - - def naive_transpose(self): - """Transpose the matrix naively.""" - - for i in range(self.N): - for j in range(i): - self.swap(i, j, j, i) diff --git a/06-matrix_transpose/python/matrix_transpose.py b/06-matrix_transpose/python/matrix_transpose.py deleted file mode 100644 index 77570f4..0000000 --- a/06-matrix_transpose/python/matrix_transpose.py +++ /dev/null @@ -1,24 +0,0 @@ -class Matrix: - """Interface of a matrix. - - This class provides only the matrix size N and a method for swapping - two items. The actual storage of the matrix in memory is provided by - subclasses in testing code. - """ - - def __init__(self, N): - self.N = N - - def swap(self, i1, j1, i2, j2): - """Swap elements (i1,j1) and (i2,j2).""" - - # Overridden in subclasses - raise NotImplementedError - - def transpose(self): - """Transpose the matrix.""" - - # TODO: Implement more efficiently - for i in range(self.N): - for j in range(i): - self.swap(i, j, j, i) diff --git a/06-matrix_transpose/python/matrix_transpose_test.py b/06-matrix_transpose/python/matrix_transpose_test.py deleted file mode 100644 index ba5c0ec..0000000 --- a/06-matrix_transpose/python/matrix_transpose_test.py +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env python3 -import math -import sys - -from matrix_tests import TestMatrix - -def generic_test(N, M, B, max_ratio, debug_level): - m = TestMatrix(N, M, B, debug_level) - m.fill_matrix() - m.reset_stats() - m.transpose() - - print("\t{} misses in {} accesses".format(m.stat_cache_misses, m.stat_accesses)) - if m.stat_accesses: - mpa = m.stat_cache_misses / m.stat_accesses - lb = 1 / B - ratio = mpa / lb - print("\t{:.6f} misses/access (lower bound is {:.6f} => ratio {:.6f}, need {:.6f})".format(mpa, lb, ratio, max_ratio)) - assert ratio <= max_ratio, "Algorithm did too many I/O operations." - - m.check_result() - -# A list of all tests -tests = [ - # name N M B max_ratio debug_level - ("small2k", lambda: generic_test( 8, 32, 8, 8, 2 )), - ("small", lambda: generic_test( 13, 64, 8, 4, 2 )), - ("n100b16", lambda: generic_test( 100, 1024, 16, 3, 1 )), - ("n1000b16", lambda: generic_test(1000, 1024, 16, 3, 1 )), - ("n1000b64", lambda: generic_test(1000, 8192, 64, 3, 1 )), - ("n1000b256", lambda: generic_test(1000, 65536, 256, 5, 1 )), - ("n1000b4096", lambda: generic_test(1000, 65536, 4096, 50, 1 )), -] - -if __name__ == "__main__": - for required_test in sys.argv[1:] or [name for name, _ in tests]: - for name, test in tests: - if name == required_test: - print("Running test {}".format(name), file=sys.stderr) - test() - break - else: - raise ValueError("Unknown test {}".format(name)) diff --git a/06-matrix_transpose/task.md b/06-matrix_transpose/task.md deleted file mode 100644 index 55f1d57..0000000 --- a/06-matrix_transpose/task.md +++ /dev/null @@ -1,6 +0,0 @@ -Implement cache-oblivious transposition of square matrices. - -The Python version requires the NumPy module for storing the matrix -in memory efficiently. - -Source code templates can be found in [git](https://gitlab.kam.mff.cuni.cz/datovky/assignments/-/tree/master). diff --git a/07-matrix_experiment/cpp/Makefile b/07-matrix_experiment/cpp/Makefile deleted file mode 100644 index ce3b51d..0000000 --- a/07-matrix_experiment/cpp/Makefile +++ /dev/null @@ -1,26 +0,0 @@ -.PHONY: test -test: matrix_experiment_sim matrix_experiment_real - @rm -rf out && mkdir out - @for exp in m1024-b16 m8192-b64 m65536-b256 m65536-b4096 ; do \ - for impl in smart naive ; do \ - echo "t-sim-$$exp-$$impl" ; \ - ./matrix_experiment_sim $$exp $$impl >out/t-sim-$$exp-$$impl ; \ - done ; \ - done - @for impl in smart naive ; do \ - echo "t-real-$$impl" ; \ - ./matrix_experiment_real $$impl >out/t-real-$$impl ; \ - done - -CXXFLAGS=-std=c++11 -O3 -Wall -Wextra -g -Wno-sign-compare - -matrix_experiment_sim: matrix_transpose.h matrix_tests.h matrix_experiment_sim.cpp - $(CXX) $(CPPFLAGS) $(CXXFLAGS) matrix_experiment_sim.cpp -o $@ - -matrix_experiment_real: matrix_transpose.h matrix_tests.h matrix_experiment_real.cpp - $(CXX) $(CPPFLAGS) $(CXXFLAGS) matrix_experiment_real.cpp -o $@ - -.PHONY: clean -clean:: - rm -f matrix_experiment_sim matrix_experiment_real - rm -rf out diff --git a/07-matrix_experiment/cpp/matrix_experiment_real.cpp b/07-matrix_experiment/cpp/matrix_experiment_real.cpp deleted file mode 100644 index f46c38a..0000000 --- a/07-matrix_experiment/cpp/matrix_experiment_real.cpp +++ /dev/null @@ -1,90 +0,0 @@ -#include <functional> -#include <string> -#include <vector> -#include <cstdio> -#include <cmath> -#include <iostream> - -#include <time.h> - -using namespace std; - -// If the condition is not true, report an error and halt. -#define EXPECT(condition, message) do { if (!(condition)) expect_failed(message); } while (0) - -void expect_failed(const string& message) { - cerr << "Test error: " << message << endl; - exit(1); -} - -class Matrix { - vector<unsigned> items; - unsigned &item(unsigned i, unsigned j) { return items[i*N + j]; } - public: - unsigned N; - Matrix(unsigned N) { this->N = N; items.resize(N*N, 0); } - - void swap(unsigned i1, unsigned j1, unsigned i2, unsigned j2) - { - // EXPECT(i1 < N && j1 < N && i2 < N && j2 < N, "Swap out of range: " + coord_string(i1, j1) + " with " + coord_string(i2, j2) + "."); - std::swap(item(i1, j1), item(i2, j2)); - } - - void naive_transpose() - { - for (unsigned i=0; i<N; i++) - for (unsigned j=0; j<i; j++) - swap(i, j, j, i); - } - -#include "matrix_transpose.h" -}; - -void real_test(bool naive) -{ - for (int e=40; e<=112; e++) { - unsigned N = (unsigned) pow(2, e/8.); - Matrix m(N); - - clock_t start_time, stop_time; - unsigned tries = 1; - do { - start_time = clock(); - for (unsigned t=0; t < tries; t++) { - if (naive) - m.naive_transpose(); - else - m.transpose(); - } - stop_time = clock(); - tries *= 2; - } while (stop_time - start_time < CLOCKS_PER_SEC/10); - // It is guaranteed that the total number of tries is odd :) - - double ns_per_item = (double)(stop_time - start_time) / CLOCKS_PER_SEC / (N*(N-1)) / tries * 1e9; - printf("%d\t%.6f\n", N, ns_per_item); - } -} - -int main(int argc, char **argv) -{ - if (argc != 2) { - fprintf(stderr, "Usage: %s (smart|naive)\n", argv[0]); - return 1; - } - - std::string mode = argv[1]; - - bool naive; - if (mode == "smart") - naive = false; - else if (mode == "naive") - naive = true; - else { - fprintf(stderr, "The argument must be either 'smart' or 'naive'\n"); - return 1; - } - - real_test(naive); - return 0; -} diff --git a/07-matrix_experiment/cpp/matrix_experiment_sim.cpp b/07-matrix_experiment/cpp/matrix_experiment_sim.cpp deleted file mode 100644 index 316db52..0000000 --- a/07-matrix_experiment/cpp/matrix_experiment_sim.cpp +++ /dev/null @@ -1,80 +0,0 @@ -#include <functional> -#include <string> -#include <vector> -#include <cstdio> -#include <cmath> -#include <string> -#include <iostream> - -#include <time.h> - -using namespace std; - -// If the condition is not true, report an error and halt. -#define EXPECT(condition, message) do { if (!(condition)) expect_failed(message); } while (0) - -void expect_failed(const string& message) { - cerr << "Test error: " << message << endl; - exit(1); -} - -#include "matrix_tests.h" - -void simulated_test(unsigned M, unsigned B, bool naive) -{ - for (int e=20; e<=52; e++) { - unsigned N = (unsigned) pow(2, e/4.); - TestMatrix m(N, M, B, 0); - m.fill_matrix(); - m.reset_stats(); - if (naive) - m.naive_transpose(); - else - m.transpose(); - - double misses_per_item = (double) m.stat_cache_misses / (N*(N-1)); - printf("%d\t%.6f\n", N, misses_per_item); - - m.check_result(); - } -} - -vector<pair<string, function<void(bool n)>>> tests = { -// M B - { "m1024-b16", [](bool n) { simulated_test( 1024, 16, n); } }, - { "m8192-b64", [](bool n) { simulated_test( 8192, 64, n); } }, - { "m65536-b256", [](bool n) { simulated_test(65536, 256, n); } }, - { "m65536-b4096", [](bool n) { simulated_test(65536, 4096, n); } }, -}; - -int main(int argc, char **argv) -{ - if (argc != 3) { - fprintf(stderr, "Usage: %s <test> (smart|naive)\n", argv[0]); - return 1; - } - - std::string which_test = argv[1]; - std::string mode = argv[2]; - - bool naive; - if (mode == "smart") - naive = false; - else if (mode == "naive") - naive = true; - else - { - fprintf(stderr, "Last argument must be either 'smart' or 'naive'\n"); - return 1; - } - - for (const auto& test : tests) { - if (test.first == which_test) { - test.second(naive); - return 0; - } - } - - fprintf(stderr, "Unknown test %s\n", which_test.c_str()); - return 1; -} diff --git a/07-matrix_experiment/cpp/matrix_tests.h b/07-matrix_experiment/cpp/matrix_tests.h deleted file mode 100644 index 6110f8e..0000000 --- a/07-matrix_experiment/cpp/matrix_tests.h +++ /dev/null @@ -1,213 +0,0 @@ -#include <string> -#include <vector> -#include <iostream> -#include <algorithm> - -/* A matrix stored in a simulated cache */ - -class CachedMatrix { - unsigned B; // Block size - unsigned mem_blocks; // Memory size in blocks - unsigned cache_blocks; // Cache size in blocks - unsigned cache_used; // How many blocks of cache we already used - - // We store the matrix as a one-dimensional array - vector<unsigned> items; - unsigned pos(unsigned i, unsigned j) { return i*N + j; } - - /* - * For each memory block, we keep the following structure. - * If the block is currently cached, we set cached == true - * and lru_prev/next point to neighboring blocks in the cyclic LRU list. - * Otherwise, cached == false and the block is not in the LRU. - */ - - class MemoryBlock { - public: - unsigned lru_prev, lru_next; - bool cached; - MemoryBlock() - { - lru_prev = lru_next = 0; - cached = false; - } - }; - - vector<MemoryBlock> blocks; - - // One block at the end of "blocks" serves as a head of the LRU list. - unsigned lru_head; - - public: - // Number of rows and columns of the matrix - unsigned N; - - int debug_level; // Verbosity - - CachedMatrix(unsigned N, unsigned M, unsigned B, int debug_level=0) - { - EXPECT(N > 0, "CachedMatrix must be non-empty."); - EXPECT(B > 0, "Blocks must be non-empty."); - EXPECT(!(M % B), "Cache size must be divisible by block size."); - EXPECT(M >= 2*B, "Cache must have at least 2 blocks."); - - unsigned NN = N*N; - items.resize(NN, 0); - - this->N = N; - this->B = B; - this->debug_level = debug_level; - mem_blocks = (NN+B-1) / B; - cache_blocks = M / B; - cache_used = 0; - - // Initialize the LRU list - blocks.resize(mem_blocks + 1); - lru_head = mem_blocks; - blocks[lru_head].lru_prev = lru_head; - blocks[lru_head].lru_next = lru_head; - - if (debug_level > 0) - cout << "\tMemory: " << mem_blocks << " blocks of " << B << " items, " << cache_blocks << " cached\n"; - } - - // Read value at position (i,j), used only in testing code - unsigned read(unsigned i, unsigned j) - { - EXPECT(i < N && j < N, "Read out of range: " + coord_string(i, j) + "."); - unsigned addr = pos(i, j); - access(addr); - return items[addr]; - } - - // Write value at position (i,j), used only in testing code - void write(unsigned i, unsigned j, unsigned data) - { - EXPECT(i < N && j < N, "Write out of range: " + coord_string(i, j) + "."); - unsigned addr = pos(i, j); - access(addr); - items[addr] = data; - } - - // Swap items (i1,j1) and (i2,j2) - void swap(unsigned i1, unsigned j1, unsigned i2, unsigned j2) - { - EXPECT(i1 < N && j1 < N && i2 < N && j2 < N, "Swap out of range: " + coord_string(i1, j1) + " with " + coord_string(i2, j2) + "."); - if (debug_level > 1) - cout << "\tSwap " << coord_string(i1, j1) << " " << coord_string(i2, j2) << endl; - unsigned addr1 = pos(i1, j1), addr2 = pos(i2, j2); - access(addr1); - access(addr2); - std::swap(items[addr1], items[addr2]); - } - - unsigned stat_cache_misses; - unsigned stat_accesses; - - // Reset statistic counters. - void reset_stats() - { - stat_cache_misses = 0; - stat_accesses = 0; - } - - static string coord_string(unsigned i, unsigned j) - { - return "(" + to_string(i) + "," + to_string(j) + ")"; - } - -#include "matrix_transpose.h" - - private: - // Bring the given address to the cache. - void access(unsigned addr) - { - int i = addr / B; // Which block to bring - if (blocks[i].cached) { - lru_remove(i); - } else { - if (cache_used < cache_blocks) { - // We still have room in the cache. - cache_used++; - if (debug_level > 1) - cout << "\t\tLoading block " << i << endl; - } else { - // We need to evict the least-recently used block to make space. - unsigned replace = blocks[lru_head].lru_prev; - lru_remove(replace); - EXPECT(blocks[replace].cached, "Internal error: Buggy LRU list."); - blocks[replace].cached = false; - if (debug_level > 1) - cout << "\t\tLoading block " << i << ", replacing " << replace << endl; - } - blocks[i].cached = true; - stat_cache_misses++; - } - lru_add_after(i, lru_head); - stat_accesses++; - } - - // Remove block from the LRU list. - void lru_remove(unsigned i) - { - unsigned prev = blocks[i].lru_prev; - unsigned next = blocks[i].lru_next; - blocks[prev].lru_next = next; - blocks[next].lru_prev = prev; - } - - // Add block at the given position in the LRU list. - void lru_add_after(unsigned i, unsigned after) - { - unsigned next = blocks[after].lru_next; - blocks[next].lru_prev = i; - blocks[after].lru_next = i; - blocks[i].lru_next = next; - blocks[i].lru_prev = after; - } -}; - -/* A cached matrix extended by methods for testing */ - -class TestMatrix : public CachedMatrix { - public: - TestMatrix(unsigned N, unsigned M, unsigned B, int debug_level = 0) : CachedMatrix(N, M, B, debug_level) { } - - // Fill matrix with a testing pattern. - void fill_matrix() - { - if (debug_level > 1) - cout << "\tInitializing\n"; - for (unsigned i = 0; i < N; i++) - for (unsigned j = 0; j < N; j++) - write(i, j, i*N + j); - } - - // Check that the pattern corresponds to the properly transposed matrix. - void check_result() - { - if (debug_level > 1) - cout << "\tChecking\n"; - for (unsigned i = 0; i < N; i++) { - for (unsigned j = 0; j < N; j++) { - unsigned want = j*N + i; - unsigned found = read(i, j); - unsigned found_i = found / N; - unsigned found_j = found % N; - EXPECT(found == want, - "Mismatch at position " + coord_string(i, j) + - ": expected element from " + coord_string(j, i) + - ", found element from " + coord_string(found_i, found_j) + - "."); - } - } - } - - // Transpose the matrix naively. - void naive_transpose() - { - for (unsigned i=0; i<N; i++) - for (unsigned j=0; j<i; j++) - swap(i, j, j, i); - } -}; diff --git a/07-matrix_experiment/python/Makefile b/07-matrix_experiment/python/Makefile deleted file mode 100644 index 26bbb01..0000000 --- a/07-matrix_experiment/python/Makefile +++ /dev/null @@ -1,17 +0,0 @@ -TESTS=m1024-b16 m8192-b64 m65536-b256 m65536-b4096 -TESTFILES=$(addprefix out/t-sim-,$(TESTS)) - -.PHONY: test -test: $(addsuffix -smart,$(TESTFILES)) $(addsuffix -naive,$(TESTFILES)) - -out/t-sim-%-naive: - @mkdir -p out - ./matrix_experiment_sim.py $* naive >$@ - -out/t-sim-%-smart: - @mkdir -p out - ./matrix_experiment_sim.py $* smart >$@ - -.PHONY: clean -clean:: - rm -rf out __pycache__ diff --git a/07-matrix_experiment/python/matrix_experiment_sim.py b/07-matrix_experiment/python/matrix_experiment_sim.py deleted file mode 100755 index 5f326f1..0000000 --- a/07-matrix_experiment/python/matrix_experiment_sim.py +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env python3 -import sys - -from matrix_tests import TestMatrix - -def simulated_test(M, B, naive): - for e in range(10, 25): - N = int(2 ** (e/2)) - print(" ", N, M, B, file=sys.stderr) - m = TestMatrix(N, M, B, 0) - m.fill_matrix() - m.reset_stats() - if naive: - m.naive_transpose() - else: - m.transpose() - misses_per_item = m.stat_cache_misses / (N*(N-1)) - print(N, misses_per_item, flush=True) - m.check_result() - -tests = { -# M B - "m1024-b16": lambda n: simulated_test( 1024, 16, n), - "m8192-b64": lambda n: simulated_test( 8192, 64, n), - "m65536-b256": lambda n: simulated_test(65536, 256, n), - "m65536-b4096": lambda n: simulated_test(65536, 4096, n), -} - -if len(sys.argv) == 3: - test = sys.argv[1] - if sys.argv[2] == "smart": - naive = False - elif sys.argv[2] == "naive": - naive = True - else: - raise ValueError("Last argument must be either 'smart' or 'naive'") - if test in tests: - tests[test](naive) - else: - raise ValueError("Unknown test {}".format(test)) -else: - raise ValueError("Usage: {} <test> (smart|naive)".format(sys.argv[0])) diff --git a/07-matrix_experiment/python/matrix_tests.py b/07-matrix_experiment/python/matrix_tests.py deleted file mode 100644 index f002701..0000000 --- a/07-matrix_experiment/python/matrix_tests.py +++ /dev/null @@ -1,165 +0,0 @@ -from matrix_transpose import Matrix -import numpy - -# Description of memory blocks is stored in an array blocks[block_index][B_xxx]. -# If the block is cached, B_CACHED is 1 and B_LRU_NEXT and B_LRU_PREV point to -# neighboring blocks in the cyclic LRU list. Otherwise, B_CACHED is 0 and the block -# is not in the LRU. -B_CACHED = 0 -B_LRU_NEXT = 1 -B_LRU_PREV = 2 - -class CachedMatrix(Matrix): - """A matrix stored in simulated cache""" - - def __init__(self, N, M, B, debug_level=0): - assert N>0, "CachedMatrix must be non-empty." - assert B>0, "Blocks must be non-empty." - assert M%B == 0, "Cache size must be divisible by block size." - assert M >= 2*B, "Cache must have at least 2 blocks." - - Matrix.__init__(self, N) - - NN = N*N - self.items = numpy.zeros(shape=NN, dtype=numpy.int32, order="C") - - self.B = B - self.M = M - self.debug_level = debug_level - self.mem_blocks = (NN+B-1) // B - self.cache_blocks = M//B - self.cache_used = 0 - - # Initialize the LRU list. There is a virtual block right after the last real block, - # which serves as a head of the cyclic LRU list. - self.blocks = numpy.zeros(shape=(self.mem_blocks+1, 3), dtype=numpy.int32, order="C") - self.lru_head = self.mem_blocks - self.blocks[self.lru_head, B_LRU_NEXT] = self.lru_head - self.blocks[self.lru_head, B_LRU_PREV] = self.lru_head - - self.reset_stats() - - if debug_level > 0: - print("\tMemory: {} blocks of {} items, {} cached".format(self.mem_blocks, self.B, self.cache_blocks)) - - def _pos(self, i, j): - """Convert position in matrix to linear address in simulated memory.""" - - return i*self.N + j - - def read(self, i, j): - """Read value at position (i,j), used only in testing code.""" - - assert i >= 0 and i < self.N and j >= 0 and j < self.N, "Read out of range: ({},{})".format(i, j) - addr = self._pos(i, j) - self._access(addr) - return self.items[addr] - - def write(self, i, j, value): - """Write value at position (i,j), used only in testing code.""" - - assert i >= 0 and i < self.N and j >= 0 and j < self.N, "Write out of range: ({},{})".format(i, j) - addr = self._pos(i, j) - self._access(addr) - self.items[addr] = value - - def swap(self, i1, j1, i2, j2): - """Swap items (i1,j1) and (i2,j2).""" - - assert i1 >= 0 and i1 < self.N and j1 >= 0 and j1 < self.N and \ - i2 >= 0 and i2 < self.N and j2 >= 0 and j2 < self.N, \ - "Swap out of range: ({},{}) with ({},{})".format(i1, j1, i2, j2) - if self.debug_level > 1: - print("\tSwap ({},{}) ({},{})".format(i1, j1, i2, j2)) - addr1 = self._pos(i1, j1) - addr2 = self._pos(i2, j2) - self._access(addr1) - self._access(addr2) - items = self.items - items[addr1], items[addr2] = items[addr2], items[addr1] - - def reset_stats(self): - """Reset statistic counters.""" - - self.stat_cache_misses = 0 - self.stat_accesses = 0 - - def _access(self, addr): - """Bring the given address to the cache.""" - - blocks = self.blocks - i = addr // self.B # Which block to bring - if blocks[i, B_CACHED] > 0: - self._lru_remove(i) - else: - if self.cache_used < self.cache_blocks: - # We still have room in the cache. - self.cache_used += 1 - if self.debug_level > 1: - print("\t\tLoading block {}".format(i)) - else: - # We need to evict the least-recently used block to make space. - replace = blocks[self.lru_head, B_LRU_PREV] - self._lru_remove(replace) - assert blocks[replace, B_CACHED] > 0, "Internal error: Buggy LRU list" - blocks[replace, B_CACHED] = 0 - if self.debug_level > 1: - print("\t\tLoading block {}, replacing {}".format(i, replace)) - blocks[i, B_CACHED] = 1 - self.stat_cache_misses += 1 - self._lru_add_after(i, self.lru_head) - self.stat_accesses += 1 - - def _lru_remove(self, i): - """Remove block from the LRU list.""" - - blocks = self.blocks - prev, next = blocks[i, B_LRU_PREV], blocks[i, B_LRU_NEXT] - blocks[prev, B_LRU_NEXT] = next - blocks[next, B_LRU_PREV] = prev - - def _lru_add_after(self, i, after): - """Add block at the given position in the LRU list.""" - - blocks = self.blocks - next = blocks[after, B_LRU_NEXT] - blocks[after, B_LRU_NEXT] = i - blocks[next, B_LRU_PREV] = i - blocks[i, B_LRU_NEXT] = next - blocks[i, B_LRU_PREV] = after - -class TestMatrix(CachedMatrix): - """A cached matrix extended by methods for testing.""" - - # Constructor is inherited - - def fill_matrix(self): - """Fill matrix with a testing pattern.""" - - if self.debug_level > 1: - print("\tInitializing") - N = self.N - for i in range(N): - for j in range(N): - self.write(i, j, i*N + j) - - def check_result(self): - """Check that the pattern corresponds to the properly transposed matrix.""" - - if self.debug_level > 1: - print("\tChecking") - N = self.N - for i in range(N): - for j in range(N): - want = j*N + i - have = self.read(i, j) - have_i = have // N - have_j = have % N - assert have == want, "Mismatch at position ({},{}): expected element from ({},{}), found element from ({},{})".format(i, j, j, i, have_i, have_j) - - def naive_transpose(self): - """Transpose the matrix naively.""" - - for i in range(self.N): - for j in range(i): - self.swap(i, j, j, i) diff --git a/07-matrix_experiment/task.md b/07-matrix_experiment/task.md deleted file mode 100644 index 2368111..0000000 --- a/07-matrix_experiment/task.md +++ /dev/null @@ -1,90 +0,0 @@ -## Goal - -The goal of this assignment is to evaluate your implementation of cache-oblivious -matrix transposition experimentally and to compare it with the trivial algorithm -which transposes by definition. - -You are given a test program (`matrix_experiment_sim`) which evaluates your -implementation from the previous assignment on different simulated caches and -matrices of different sizes. For each experiment, the average number of cache -misses per item is reported (the diagonal items which do not move are not -counted). The program also evaluates performance of the trivial transposition algorithm. -The simulated cache is fully associative and uses LRU replacement strategy. - -You should run these experiments and write a report, which contains one plot of -the measured data for each cache type, showing dependency of the average number of -misses per item on the matrix size. There should be two curves in each plot: one for your -algorithm, another for the trivial one. - -The report should discuss the experimental results and try to explain the observed -behavior (including any strange anomalies) using theory from the lectures. -If you want, you can carry out further experiments to gain better understanding -of the algorithm and include these in the report. - -You should submit a PDF file with the report (and no source code). -You will get 1 temporary point upon submission if the file is syntactically correct; -proper points will be assigned later. - -## Test program - -The test program is given two arguments: -- Cache type: - - `m1024-b16` – cache of 1024 items organized in 16-item blocks - - `m8192-b64` – cache of 8192 items organized in 64-item blocks - - `m65536-b256` – cache of 65536 items organized on 256-item blocks - - `m65536-b4096` – cache of 65536 items organized in 4096-item blocks -- The implementation to test (`smart` or `naive`). - -The output of the program contains one line per experiment, which consists of -the matrix size and the average number of cache misses per item. - -*Warning:* The Python tests are slow, even though they use only a subset of the -matrix sizes. They can take about one hour to complete. -If your machine has multiple processors or cores, you can try `make -j` -to run the tests in parallel. - -## Optional: Tests on real hardware (for 5 extra points) - -You can also test your transposition algorithm on real hardware -using the `matrix_experiment_real` program. The matrix is stored in row-major -order, each item takes 4 bytes. - -The program takes one parameter, the implementation to test: `smart` or `naive`. -Its output contains one line per experiment, which consists of the matrix size -and the average time per item in nanoseconds. - -However, the program is available only for C++, because general slowness of -Python completely hides all cache effects. - -Again, your report should show a plot of the measured data and discuss the observed -effects. You should also include the configuration of caches in your machine. -(If you are using Linux, you can try the `machinfo` script from -[this repository](https://gitlab.kam.mff.cuni.cz/mj/aim.git).) - -## Hints - -The following tools can be useful for producing nice plots: -- [pandas](https://pandas.pydata.org/) -- [matplotlib](https://matplotlib.org/) -- [gnuplot](http://www.gnuplot.info/) - -A quick checklist for plots: -- Is there a caption explaining what is plotted? -- Are the axes clearly labelled? Do they have value ranges and units? -- Have you mentioned that this axis has logarithmic scale? (Logarithmic graphs - are more fitting in some cases, but you should tell.) -- Is it clear which curve means what? -- Is it clear what are the measured points and what is an interpolated - curve between them? -- Are there any overlaps? (E.g., the most interesting part of the curve - hidden underneath a label?) -- **Is the graph distorted by compression artifacts?** (No, you shouldn't use JPEG for plots!) - -In your discussion, please distinguish the following kinds of claims. -It should be always clear which is which: -- Experimental results (i.e., the raw data you obtained from the experiments) -- Theoretical facts (i.e., claims we have proved mathematically) -- Your hypotheses (e.g., when you claim that the graph looks like something is true, - but you are not able to prove rigorously that it always holds) - -Source code templates can be found in [git](https://gitlab.kam.mff.cuni.cz/datovky/assignments/-/tree/master). diff --git a/08-cuckoo_hash/cpp/Makefile b/08-cuckoo_hash/cpp/Makefile deleted file mode 100644 index f32e87a..0000000 --- a/08-cuckoo_hash/cpp/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -test: cuckoo_hash_test - ./$< - -INCLUDE ?= . -CXXFLAGS=-std=c++11 -O2 -Wall -Wextra -g -Wno-sign-compare -I$(INCLUDE) - -cuckoo_hash_test: cuckoo_hash_test.cpp cuckoo_hash.h test_main.cpp $(INCLUDE)/random.h - $(CXX) $(CXXFLAGS) $^ -o $@ - -clean: - rm -f cuckoo_hash_test - -.PHONY: clean test diff --git a/08-cuckoo_hash/cpp/cuckoo_hash.h b/08-cuckoo_hash/cpp/cuckoo_hash.h deleted file mode 100644 index 32a5660..0000000 --- a/08-cuckoo_hash/cpp/cuckoo_hash.h +++ /dev/null @@ -1,103 +0,0 @@ -#include <string> -#include <vector> -#include <cstdint> -#include <iostream> - -#include "random.h" - -using namespace std; - -// If the condition is not true, report an error and halt. -#define EXPECT(condition, message) do { if (!(condition)) expect_failed(message); } while (0) - -void expect_failed(const string& message); - -class TabulationHash { - /* - * Hash function for hashing by tabulation. - * - * The 32-bit key is split to four 8-bit parts. Each part indexes - * a separate table of 256 randomly generated values. Obtained values - * are XORed together. - */ - - unsigned num_buckets; - uint32_t tables[4][256]; - -public: - TabulationHash(unsigned num_buckets, RandomGen *random_gen) - { - this->num_buckets = num_buckets; - for (int i=0; i<4; i++) - for (int j=0; j<256; j++) - tables[i][j] = random_gen->next_u32(); - } - - uint32_t hash(uint32_t key) - { - unsigned h0 = key & 0xff; - unsigned h1 = (key >> 8) & 0xff; - unsigned h2 = (key >> 16) & 0xff; - unsigned h3 = (key >> 24) & 0xff; - return (tables[0][h0] ^ tables[1][h1] ^ tables[2][h2] ^ tables[3][h3]) % num_buckets; - } -}; - -class CuckooTable { - /* - * Hash table with Cuckoo hashing. - * - * We have two hash functions, which map 32-bit keys to buckets of a common - * hash table. Unused buckets contain 0xffffffff. - */ - - const uint32_t UNUSED = 0xffffffff; - - // The array of buckets - vector<uint32_t> table; - unsigned num_buckets; - - // Hash functions and the random generator used to create them - TabulationHash *hashes[2]; - RandomGen *random_gen; - -public: - - CuckooTable(unsigned num_buckets) - { - // Initialize the table with the given number of buckets. - // The number of buckets is expected to stay constant. - - this->num_buckets = num_buckets; - table.resize(num_buckets, UNUSED); - - // Obtain two fresh hash functions. - random_gen = new RandomGen(42); - for (int i=0; i<2; i++) - hashes[i] = new TabulationHash(num_buckets, random_gen); - } - - ~CuckooTable() - { - for (int i=0; i<2; i++) - delete hashes[i]; - delete random_gen; - } - - bool lookup(uint32_t key) - { - // Check if the table contains the given key. Returns True or False. - unsigned h0 = hashes[0]->hash(key); - unsigned h1 = hashes[1]->hash(key); - return (table[h0] == key || table[h1] == key); - } - - void insert(uint32_t key) - { - // Insert a new key to the table. Assumes that the key is not present yet. - EXPECT(key != UNUSED, "Keys must differ from UNUSED."); - - // TODO: Implement - } - -}; diff --git a/08-cuckoo_hash/cpp/cuckoo_hash_test.cpp b/08-cuckoo_hash/cpp/cuckoo_hash_test.cpp deleted file mode 100644 index 84ececb..0000000 --- a/08-cuckoo_hash/cpp/cuckoo_hash_test.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include <functional> -#include <cstdlib> -#include <vector> - -#include "cuckoo_hash.h" - -void simple_test(unsigned n, unsigned table_size_percentage) -{ - CuckooTable table(n * table_size_percentage / 100); - - for (unsigned i=0; i < n; i++) - table.insert(37*i); - - for (unsigned i=0; i < n; i++) { - EXPECT(table.lookup(37*i), "Item not present in table, but it should be."); - EXPECT(!table.lookup(37*i+1), "Item present in table, even though it should not be."); - } -} - -void multiple_test(unsigned min_n, unsigned max_n, unsigned step_n, unsigned table_size_percentage) -{ - for (unsigned n=min_n; n < max_n; n += step_n) { - printf("\tn=%u\n", n); - simple_test(n, table_size_percentage); - } -} - -/*** A list of all tests ***/ - -vector<pair<string, function<void()>>> tests = { - { "small", [] { simple_test(100, 400); } }, - { "middle", [] { simple_test(31415, 300); } }, - { "big", [] { simple_test(1000000, 300); } }, - { "tight", [] { multiple_test(20000, 40000, 500, 205); } }, -}; diff --git a/08-cuckoo_hash/cpp/random.h b/08-cuckoo_hash/cpp/random.h deleted file mode 100644 index 7d18ab6..0000000 --- a/08-cuckoo_hash/cpp/random.h +++ /dev/null @@ -1,59 +0,0 @@ -#define DS1_RANDOM_H - -#include <cstdint> - -/* - * This is the xoroshiro128+ random generator, designed in 2016 by David Blackman - * and Sebastiano Vigna, distributed under the CC-0 license. For more details, - * see http://vigna.di.unimi.it/xorshift/. - * - * Rewritten to C++ by Martin Mares, also placed under CC-0. - */ - -class RandomGen { - uint64_t state[2]; - - uint64_t rotl(uint64_t x, int k) - { - return (x << k) | (x >> (64 - k)); - } - - public: - // Initialize the generator, set its seed and warm it up. - RandomGen(unsigned int seed) - { - state[0] = seed * 0xdeadbeef; - state[1] = seed ^ 0xc0de1234; - for (int i=0; i<100; i++) - next_u64(); - } - - // Generate a random 64-bit number. - uint64_t next_u64(void) - { - uint64_t s0 = state[0], s1 = state[1]; - uint64_t result = s0 + s1; - s1 ^= s0; - state[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); - state[1] = rotl(s1, 36); - return result; - } - - // Generate a random 32-bit number. - uint32_t next_u32(void) - { - return next_u64() >> 11; - } - - // Generate a number between 0 and range-1. - unsigned int next_range(unsigned int range) - { - /* - * This is not perfectly uniform, unless the range is a power of two. - * However, for 64-bit random values and 32-bit ranges, the bias is - * insignificant. - */ - return next_u64() % range; - } -}; - diff --git a/08-cuckoo_hash/cpp/test_main.cpp b/08-cuckoo_hash/cpp/test_main.cpp deleted file mode 100644 index 3f4aff0..0000000 --- a/08-cuckoo_hash/cpp/test_main.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include <cstdlib> -#include <functional> -#include <iostream> -#include <string> -#include <utility> -#include <vector> - -using namespace std; - -extern vector<pair<string, function<void()>>> tests; - -void expect_failed(const string& message) { - cerr << "Test error: " << message << endl; - exit(1); -} - -int main(int argc, char* argv[]) { - vector<string> required_tests; - - if (argc > 1) { - required_tests.assign(argv + 1, argv + argc); - } else { - for (const auto& test : tests) - required_tests.push_back(test.first); - } - - for (const auto& required_test : required_tests) { - bool found = false; - for (const auto& test : tests) - if (required_test == test.first) { - cerr << "Running test " << required_test << endl; - test.second(); - found = true; - break; - } - if (!found) { - cerr << "Unknown test " << required_test << endl; - return 1; - } - } - - return 0; -} diff --git a/08-cuckoo_hash/python/cuckoo_hash.py b/08-cuckoo_hash/python/cuckoo_hash.py deleted file mode 100644 index 72a415c..0000000 --- a/08-cuckoo_hash/python/cuckoo_hash.py +++ /dev/null @@ -1,56 +0,0 @@ -import random -import math - -class TabulationHash: - """Hash function for hashing by tabulation. - - The 32-bit key is split to four 8-bit parts. Each part indexes - a separate table of 256 randomly generated values. Obtained values - are XORed together. - """ - - def __init__(self, num_buckets): - self.tables = [None] * 4 - for i in range(4): - self.tables[i] = [random.randint(0, 0xffffffff) for _ in range(256)] - self.num_buckets = num_buckets - - def hash(self, key): - h0 = key & 0xff - h1 = (key >> 8) & 0xff - h2 = (key >> 16) & 0xff - h3 = (key >> 24) & 0xff - t = self.tables - return (t[0][h0] ^ t[1][h1] ^ t[2][h2] ^ t[3][h3]) % self.num_buckets - -class CuckooTable: - """Hash table with Cuckoo hashing. - - We have two hash functions, which map 32-bit keys to buckets of a common - hash table. Unused buckets contain None. - """ - - def __init__(self, num_buckets): - """Initialize the table with the given number of buckets. - The number of buckets is expected to stay constant.""" - - # The array of buckets - self.num_buckets = num_buckets - self.table = [None] * num_buckets - - # Create two fresh hash functions - self.hashes = [TabulationHash(num_buckets), TabulationHash(num_buckets)] - - def lookup(self, key): - """Check if the table contains the given key. Returns True or False.""" - - b0 = self.hashes[0].hash(key) - b1 = self.hashes[1].hash(key) - # print("## Lookup key={} b0={} b1={}".format(key, b0, b1)) - return self.table[b0] == key or self.table[b1] == key - - def insert(self, key): - """Insert a new key to the table. Assumes that the key is not present yet.""" - - # TODO: Implement - raise NotImplementedError diff --git a/08-cuckoo_hash/python/cuckoo_hash_test.py b/08-cuckoo_hash/python/cuckoo_hash_test.py deleted file mode 100755 index f9137c4..0000000 --- a/08-cuckoo_hash/python/cuckoo_hash_test.py +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env python3 -import sys -import random - -from cuckoo_hash import CuckooTable - -def simple_test(n, table_size_percentage): - random.seed(42) - table = CuckooTable(n*table_size_percentage//100) - - # Insert an arithmetic progression - for i in range(n): - table.insert(37*i) - - # Verify contents of the table - for i in range(n): - assert table.lookup(37*i), "Item not present in table, but it should be." - assert not table.lookup(37*i+1), "Item present in table, even though it should not be." - -def multiple_test(min_n, max_n, step_n, table_size_percentage): - for n in range(min_n, max_n, step_n): - print("\tn={}".format(n)) - simple_test(n, table_size_percentage) - -# A list of all tests -tests = [ - ("small", lambda: simple_test(100, 400)), - ("middle", lambda: simple_test(31415, 300)), - ("big", lambda: simple_test(1000000, 300)), - ("tight", lambda: multiple_test(20000, 40000, 500, 205)), -] - -if __name__ == "__main__": - for required_test in sys.argv[1:] or [name for name, _ in tests]: - for name, test in tests: - if name == required_test: - print("Running test {}".format(name), file=sys.stderr) - test() - break - else: - raise ValueError("Unknown test {}".format(name)) diff --git a/08-cuckoo_hash/task.md b/08-cuckoo_hash/task.md deleted file mode 100644 index 87f378c..0000000 --- a/08-cuckoo_hash/task.md +++ /dev/null @@ -1,14 +0,0 @@ -Implement Cuckoo hash table with simple tabulation hashing. - -You are given a skeleton code which defines the table, implements -`lookup()`, and provides hash functions. You have to add an `insert()` -method. - -If too many elements are moved during a single insert, the table must -be rehashed with new hash functions. See lecture notes for the particular -bounds. - -The size of the table should stay constant -throughout the existence of the data structure. - -Source code templates can be found in [git](https://gitlab.kam.mff.cuni.cz/datovky/assignments/-/tree/master). \ No newline at end of file diff --git a/09-hash_experiment/cpp/Makefile b/09-hash_experiment/cpp/Makefile deleted file mode 100644 index 8745327..0000000 --- a/09-hash_experiment/cpp/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -INCLUDE ?= . -CXXFLAGS=-std=c++11 -O2 -Wall -Wextra -g -Wno-sign-compare -I$(INCLUDE) -STUDENT_ID ?= PLEASE_SET_STUDENT_ID - -HASHFUNCS=ms-low ms-high poly-1 poly-2 tab - -.PHONY: test -test: $(addprefix out/t-grow-, $(HASHFUNCS)) $(addprefix out/t-usage-, $(HASHFUNCS)) - -out/t-%: hash_experiment - @mkdir -p out - ./hash_experiment $* $(STUDENT_ID) >$@ - -hash_experiment: hash_experiment.cpp $(INCLUDE)/random.h - $(CXX) $(CPPFLAGS) $(CXXFLAGS) hash_experiment.cpp -o $@ - -.PHONY: clean -clean: - rm -f hash_experiment - rm -rf out diff --git a/09-hash_experiment/cpp/hash_experiment.cpp b/09-hash_experiment/cpp/hash_experiment.cpp deleted file mode 100644 index 7eac12f..0000000 --- a/09-hash_experiment/cpp/hash_experiment.cpp +++ /dev/null @@ -1,314 +0,0 @@ -#include <vector> -#include <functional> -#include <algorithm> -#include <utility> -#include <stdexcept> -#include <stdio.h> -#include <stdint.h> -#include <math.h> -#include "random.h" - -using namespace std; - -RandomGen rng(42); - -typedef uint32_t uint; - -typedef function<uint(uint)> HashFunction; -typedef function<HashFunction(unsigned num_buckets)> HashFunctionFactory; - -/* - * Hash function for hashing by tabulation. - * - * The 32-bit key is split to four 8-bit parts. Each part indexes - * a separate table of 256 randomly generated values. Obtained values - * are XORed together. - */ -class TabulationHash { - unsigned num_buckets; - vector<uint> tables; - - TabulationHash(unsigned num_buckets) : num_buckets(num_buckets), tables(4 * 256) { - for (uint& x : tables) x = rng.next_u32(); - } - - public: - static HashFunction factory(unsigned num_buckets) { - return HashFunction(TabulationHash(num_buckets)); - } - - uint operator()(uint key) { - return ( - tables[key & 0xff] ^ - tables[((key >> 8) & 0xff) | 0x100] ^ - tables[((key >> 16) & 0xff) | 0x200] ^ - tables[((key >> 24) & 0xff) | 0x300] - ) % num_buckets; - } -}; - -// Hash function using polynomial modulo a prime. -template < int degree, uint prime = 2147483647 > -class PolynomialHash { - unsigned num_buckets; - vector<uint> coefs; - - PolynomialHash(unsigned num_buckets) : num_buckets(num_buckets), coefs(degree + 1) { - for (uint& x : coefs) x = rng.next_u32(); - } - - public: - static HashFunction factory(unsigned num_buckets) { - return HashFunction(PolynomialHash(num_buckets)); - } - - uint operator()(uint key) { - uint64_t acc = 0; - for (uint c : coefs) acc = (acc * key + c) % prime; - return (uint)(acc % num_buckets); - } -}; - -typedef PolynomialHash<1> LinearHash; -typedef PolynomialHash<2> QuadraticHash; - -// Multiply-shift hash function taking top bits of 32-bit word -class MultiplyShiftLowHash { - uint mult; - uint mask; - int shift = 0; - - MultiplyShiftLowHash(unsigned num_buckets) { - mult = rng.next_u32() | 0x1; - mask = num_buckets - 1; - - if (mask & num_buckets) - throw runtime_error("MultiplyShiftLowHash: num_buckets must be power of 2"); - - unsigned tmp = num_buckets - 1; - while ((0x80000000U & tmp) == 0) { - tmp <<= 1; - shift++; - } - } - - public: - static HashFunction factory(unsigned num_buckets) { - return HashFunction(MultiplyShiftLowHash(num_buckets)); - } - - uint operator()(uint key) { - return ((key * mult) >> shift) & mask; - } -}; - -// Multiply-shift hash function taking low bits of upper half of 64-bit word -class MultiplyShiftHighHash { - uint mask; - uint64_t mult; - - MultiplyShiftHighHash(unsigned num_buckets) { - mult = rng.next_u64() | 0x1; - mask = num_buckets - 1; - - if (mask & num_buckets) - throw runtime_error("MultiplyShiftHighHash: num_buckets must be power of 2"); - } - - public: - static HashFunction factory(unsigned num_buckets) { - return HashFunction(MultiplyShiftHighHash(num_buckets)); - } - - uint operator()(uint key) { - return ((key * mult) >> 32) & mask; - } -}; - - -// Hash table with linear probing -class HashTable { - HashFunction hash; - vector<uint> table; - unsigned size = 0; - - unsigned ops; - unsigned max_; - uint64_t steps; - - public: - // We reserve one integer to mark unused buckets. This integer - // cannot be stored in the table. - static constexpr uint UNUSED = ~((uint)0); - - HashTable(const HashFunctionFactory& factory, unsigned num_buckets) : - hash(factory(num_buckets)), table(num_buckets, +UNUSED) { - reset_counter(); - } - - // Check whether key is present in the table. - bool lookup(uint key) { - if (key == UNUSED) throw runtime_error("Cannot lookup UNUSED"); - - bool ret = false; - unsigned steps = 1; - - uint b = hash(key); - while (table[b] != UNUSED) { - if (table[b] == key) { - ret = true; - break; - } - steps++; - b = next_bucket(b); - } - - update_counter(steps); - return ret; - } - - // Add the key in the table. - void insert(uint key) { - if (key == UNUSED) throw runtime_error("Cannot insert UNUSED"); - if (size >= table.size()) throw runtime_error("Insert: Table is full"); - - unsigned steps = 1; - uint b = hash(key); - - while (table[b] != UNUSED) { - if (table[b] == key) goto key_found; - steps++; - b = next_bucket(b); - } - - table[b] = key; - size++; - - key_found: - update_counter(steps); - } - - void reset_counter() { ops = steps = max_ = 0; } - double report_avg() { return ((double)steps) / max(1U, ops); } - double report_max() { return max_; } - - private: - void update_counter(unsigned steps) { - ops++; - this->steps += steps; - max_ = max(steps, max_); - } - - unsigned next_bucket(unsigned b) { return (b + 1) % table.size(); } -}; - -void usage_test(HashFunctionFactory factory, int max_usage = 90, int retry = 40) { - vector<double> avg(max_usage, 0.0); - vector<double> avg2(max_usage, 0.0); - - unsigned N = 1 << 20; - unsigned step_size = N / 100; - - vector<uint> elements(N); - for (unsigned i = 0; i < N; i++) elements[i] = i; - - for (int t = 0; t < retry; t++) { - HashTable H(factory, N); - for (unsigned i = 0; i < N-1; i++) - swap(elements[i], elements[i + (rng.next_u32() % (N-i))]); - - for (int s = 0; s < max_usage; s++) { - H.reset_counter(); - for (unsigned i = 0; i < step_size; i++) - H.insert(elements[s*step_size + i]); - - avg[s] += H.report_avg(); - avg2[s] += H.report_avg() * H.report_avg(); - } - } - - for (int i = 0; i < max_usage; i++) { - avg[i] /= retry; - avg2[i] /= retry; - double std_dev = sqrt(avg2[i] - avg[i]*avg[i]); - - printf("%i %.03lf %.03lf\n", i+1, avg[i], std_dev); - } -} - - -void grow_test(HashFunctionFactory factory, int usage = 60, int retry = 40, - int begin = 7, int end = 22) { - - for (int n = begin; n < end; n++) { - double avg = 0; - double avg2 = 0; - unsigned N = 1 << n; - - vector<uint> elements(N); - for (unsigned i = 0; i < N; i++) elements[i] = i; - - for (int t = 0; t < retry; t++) { - HashTable H(factory, N); - for (unsigned i = 0; i < N-1; i++) - swap(elements[i], elements[i + (rng.next_u32() % (N-i))]); - - for (unsigned i = 0; i < ((uint64_t)N) * usage / 100; i++) - H.insert(elements[i]); - - for (unsigned i = 0; i < N; i++) - H.lookup(i); - - avg += H.report_avg(); - avg2 += H.report_avg() * H.report_avg(); - } - - avg /= retry; - avg2 /= retry; - double std_dev = sqrt(avg2 - avg*avg); - - printf("%i %.03lf %.03lf\n", N, avg, std_dev); - } -} - -int main(int argc, char** argv) { - vector<pair<string, HashFunctionFactory>> grow_tests = { - {"grow-ms-low", MultiplyShiftLowHash::factory}, - {"grow-ms-high", MultiplyShiftHighHash::factory}, - {"grow-poly-1", LinearHash::factory}, - {"grow-poly-2", QuadraticHash::factory}, - {"grow-tab", TabulationHash::factory} - }; - vector<pair<string, HashFunctionFactory>> usage_tests = { - {"usage-ms-low", MultiplyShiftLowHash::factory}, - {"usage-ms-high", MultiplyShiftHighHash::factory}, - {"usage-poly-1", LinearHash::factory}, - {"usage-poly-2", QuadraticHash::factory}, - {"usage-tab", TabulationHash::factory} - }; - - if (argc != 3) goto fail; - - rng = RandomGen(atoi(argv[2])); - - for (auto t : grow_tests) { - if (t.first == argv[1]) { - grow_test(t.second); - return 0; - } - } - - for (auto t : usage_tests) { - if (t.first == argv[1]) { - usage_test(t.second); - return 0; - } - } - - fail: - printf("Usage: %s <test> <seed>\nAvailable tests are:", argv[0]); - for (auto t : grow_tests) printf(" %s", t.first.c_str()); - for (auto t : usage_tests) printf(" %s", t.first.c_str()); - return 1; -} - diff --git a/09-hash_experiment/cpp/random.h b/09-hash_experiment/cpp/random.h deleted file mode 100644 index 5ef10ae..0000000 --- a/09-hash_experiment/cpp/random.h +++ /dev/null @@ -1,61 +0,0 @@ -#ifndef DS1_RANDOM_H -#define DS1_RANDOM_H - -#include <cstdint> - -/* - * This is the xoroshiro128+ random generator, designed in 2016 by David Blackman - * and Sebastiano Vigna, distributed under the CC-0 license. For more details, - * see http://vigna.di.unimi.it/xorshift/. - * - * Rewritten to C++ by Martin Mares, also placed under CC-0. - */ - -class RandomGen { - uint64_t state[2]; - - uint64_t rotl(uint64_t x, int k) - { - return (x << k) | (x >> (64 - k)); - } - - public: - // Initialize the generator, set its seed and warm it up. - RandomGen(unsigned int seed) - { - state[0] = seed * 0xdeadbeef; - state[1] = seed ^ 0xc0de1234; - for (int i=0; i<100; i++) - next_u64(); - } - - // Generate a random 64-bit number. - uint64_t next_u64(void) - { - uint64_t s0 = state[0], s1 = state[1]; - uint64_t result = s0 + s1; - s1 ^= s0; - state[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); - state[1] = rotl(s1, 36); - return result; - } - - // Generate a random 32-bit number. - uint32_t next_u32(void) - { - return next_u64() >> 11; - } - - // Generate a number between 0 and range-1. - unsigned int next_range(unsigned int range) - { - /* - * This is not perfectly uniform, unless the range is a power of two. - * However, for 64-bit random values and 32-bit ranges, the bias is - * insignificant. - */ - return next_u64() % range; - } -}; - -#endif diff --git a/09-hash_experiment/python/Makefile b/09-hash_experiment/python/Makefile deleted file mode 100644 index e9373dc..0000000 --- a/09-hash_experiment/python/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -STUDENT_ID ?= PLEASE_SET_STUDENT_ID - -HASHFUNCS=ms-low ms-high poly-1 poly-2 tab - -.PHONY: test -test: $(addprefix out/t-grow-, $(HASHFUNCS)) $(addprefix out/t-usage-, $(HASHFUNCS)) - -out/t-%: hash_experiment.py - @mkdir -p out - ./hash_experiment.py $* $(STUDENT_ID) >$@ - -.PHONY: clean -clean: - rm -rf out diff --git a/09-hash_experiment/python/hash_experiment.py b/09-hash_experiment/python/hash_experiment.py deleted file mode 100644 index 9de266e..0000000 --- a/09-hash_experiment/python/hash_experiment.py +++ /dev/null @@ -1,217 +0,0 @@ -#!/usr/bin/env python3 - -import random, sys -from math import sqrt - -# Our wrapper of random so we can substitute it with another random generator -rng_init = lambda x: random.seed(x) -rng_next_u32 = lambda: random.randint(0, 2**32 - 1) - -class TabulationHash: - """Hash function for hashing by tabulation. - - The 32-bit key is split to four 8-bit parts. Each part indexes - a separate table of 256 randomly generated values. Obtained values - are XORed together. - """ - - def __init__(self, num_buckets): - self.num_buckets = num_buckets - self.tables = [None] * 4 - for i in range(4): - self.tables[i] = [ rng_next_u32() for _ in range(256) ] - - def __call__(self, key): - h0 = key & 0xff; - h1 = (key >> 8) & 0xff; - h2 = (key >> 16) & 0xff; - h3 = (key >> 24) & 0xff; - t = self.tables - return (t[0][h0] ^ t[1][h1] ^ t[2][h2] ^ t[3][h3]) % self.num_buckets - -class PolynomialHash: - """Hash function using polynomial modulo a prime.""" - - def __init__(self, num_buckets, degree, prime = 2147483647): - self.num_buckets = num_buckets - self.prime = prime - self.coefs = [ rng_next_u32() for _ in range(degree + 1) ] - - def __call__(self, key): - acc = 0 - for c in self.coefs: - acc = (acc * key + c) % self.prime - return acc % self.num_buckets - -LinearHash = lambda num_buckets: PolynomialHash(num_buckets, 1) -QuadraticHash = lambda num_buckets: PolynomialHash(num_buckets, 2) - -class MultiplyShiftLowHash: - """Multiply-shift hash function taking top bits of 32-bit word""" - - def __init__(self, num_buckets): - self.mask = num_buckets - 1 - assert (num_buckets & self.mask == 0), \ - "MultiplyShiftLowHash: num_buckets must be power of 2" - - self.mult = rng_next_u32() | 0x1 - self.shift = 0; - tmp = num_buckets - 1 - while 0x80000000 & tmp == 0: - tmp <<= 1 - self.shift += 1 - - def __call__(self, key): - return ((key * self.mult) >> self.shift) & self.mask - -class MultiplyShiftHighHash: - """Multiply-shift hash function taking low bits of upper half of 64-bit word""" - - def __init__(self, num_buckets): - self.mask = num_buckets - 1 - assert (num_buckets & self.mask == 0), \ - "MultiplyShiftLowHash: num_buckets must be power of 2" - self.mult = (rng_next_u32() << 32) | rng_next_u32() | 0x1 - - def __call__(self, key): - return ((key * self.mult) >> 32) & self.mask - -class HashTable: - """Hash table with linear probing""" - - def __init__(self, hash_fun_factory, num_buckets): - self._hash = hash_fun_factory(num_buckets) - self._num_buckets = num_buckets - self._table = [None] * num_buckets - self._size = 0 - self.reset_counter() - - def _next_bucket(self, b): - return (b + 1) % self._num_buckets - - def lookup(self, key): - """Check whether key is present in the table.""" - ret = False - steps = 1 - - b = self._hash(key) - while self._table[b] is not None: - if self._table[b] == key: - ret = True - break - steps += 1 - b = self._next_bucket(b) - - self._update_counter(steps) - return ret - - def insert(self, key): - """Add the key in the table.""" - assert self._size < self._num_buckets, "Cannot insert into a full table." - steps = 1 - - b = self._hash(key) - while self._table[b] is not None: - if self._table[b] == key: break - steps += 1 - b = self._next_bucket(b) - else: - self._table[b] = key - - self._update_counter(steps) - - def _update_counter(self, steps): - self._ops += 1 - self._steps += steps - self._max = max(self._max, steps) - - def reset_counter(self): - self._steps = 0 - self._ops = 0 - self._max = 0 - - def report_avg(self): return self._steps / max(1, self._ops) - def report_max(self): return self._max - -def permute_list(l): - N = len(l) - for i in range(N - 1): - dst = i + (rng_next_u32() % (N-i)) - l[i], l[dst] = l[dst], l[i] - -def usage_test(hash_fun_factory, max_usage = 90, retry = 40): - avg = [0.0] * max_usage - avg2 = [0.0] * max_usage - - N = 2**19 - step_size = N // 100 - elements = list(range(N)) - - for _ in range(retry): - H = HashTable(hash_fun_factory, N) - permute_list(elements) - - for s in range(max_usage): - H.reset_counter() - for i in range(step_size): - H.insert(s*step_size + i) - avg[s] += H.report_avg() - avg2[s] += H.report_avg() ** 2 - - for i in range(max_usage): - avg[i] /= retry; - avg2[i] /= retry; - std_dev = sqrt(avg2[i] - avg[i]**2) - - print("%i %.03f %.03f" % ((i + 1), avg[i], std_dev)) - -def grow_test(hash_fun_factory, usage = 60, retry = 40, begin = 7, end = 21): - for n in range(begin, end): - avg = 0.0 - avg2 = 0.0 - N = 2 ** n - elements = list(range(N)) - - for _ in range(retry): - H = HashTable(hash_fun_factory, N) - permute_list(elements) - - for x in elements[:N * usage // 100]: - H.insert(x) - - for i in range(N): - H.lookup(i) - - avg += H.report_avg() - avg2 += H.report_avg() ** 2 - - avg /= retry - avg2 /= retry - std_dev = sqrt(avg2 - avg**2) - - print("%i %.03f %.03f" % (N, avg, std_dev)) - -tests = { - "usage-ms-low": lambda: usage_test(MultiplyShiftLowHash), - "usage-ms-high": lambda: usage_test(MultiplyShiftHighHash), - "usage-poly-1": lambda: usage_test(LinearHash), - "usage-poly-2": lambda: usage_test(QuadraticHash), - "usage-tab": lambda: usage_test(TabulationHash), - - "grow-ms-low": lambda: grow_test(MultiplyShiftLowHash), - "grow-ms-high": lambda: grow_test(MultiplyShiftHighHash), - "grow-poly-1": lambda: grow_test(LinearHash), - "grow-poly-2": lambda: grow_test(QuadraticHash), - "grow-tab": lambda: grow_test(TabulationHash), -} - -if len(sys.argv) == 3: - test, student_id = sys.argv[1], sys.argv[2] - rng_init(int(student_id)) - if test in tests: - tests[test]() - else: - raise ValueError("Unknown test {}".format(test)) -else: - raise ValueError("Usage: {} <test> <student-id>".format(sys.argv[0])) - diff --git a/09-hash_experiment/task.md b/09-hash_experiment/task.md deleted file mode 100644 index b5d758f..0000000 --- a/09-hash_experiment/task.md +++ /dev/null @@ -1,76 +0,0 @@ -## Goal - -The goal of this assignment is to experimentally evaluate Linear probing -hash table with different systems of hash functions. - -You are given a test program (`hash_experiment`) which implements everything -needed to perform the following experiments: - -- _Grow experiment:_ This experiment tries different sizes $N$ of the hash table and for each size - it inserts small keys in random order until 60% of the table is used - and then it performs lookup operation for keys $0,\ldots,N-1$. -- _Usage experiment:_ This experiment uses hash table of size $2^{20}$. It performs insertions - to increase usage of the table by 1%, reports efficiency of the insert operation, - and repeats until usage of the table reaches 90%. - -Both experiments measure number of probed buckets per operation, are repeated 40 times -and report average and standard deviation. Note that even with 40 repetitions -the reported numbers still depend quite a lot on the random seed used. - -You should perform these experiments for 5 different classes of hash functions – -tabulation, multiply-shift which uses top bits of 32-bit word (`ms-low`), -multiply-shift which uses low bits of upper half of 64-bit word (`ms-high`), -and polynomial hash function of degree 1 and 2 – and write a report, which contains two -plots of the measured data for each experiment. The first plot should contain average -complexity of operations and the second one the standard deviation. - -Each plot should show the dependence of the average number of probed buckets -either on size of the hash table (the grow experiment) or the usage of the hash table -(the usage experiment). - -The report should discuss the experimental results and try to explain the observed -behavior using theory from the lectures. (If you want, you can carry out further -experiments to gain better understanding of the data structure and include these -in the report. This is strictly optional.) - -You should submit a PDF file with the report (and no source code). -You will get 1 temporary point upon submission if the file is syntactically correct; -proper points will be assigned later. - -## Test program - -The test program is given two arguments: -- The name of the test (`{grow,usage}-{ms-low,ms-high,poly-1,poly-2,tab}`). -- The random seed: you should use the last 2 digits of your student ID (you can find - it in the Study Information System – just click on the Personal data icon). Please - include the random seed in your report. - -The output of the program contains one line per experiment, which consists of -the set size and the average number of structural changes. - -## Hints - -The following tools can be useful for producing nice plots: -- [pandas](https://pandas.pydata.org/) -- [matplotlib](https://matplotlib.org/) -- [gnuplot](http://www.gnuplot.info/) - -A quick checklist for plots: -- Is there a caption explaining what is plotted? -- Are the axes clearly labelled? Do they have value ranges and units? -- Have you mentioned that this axis has logarithmic scale? (Logarithmic graphs - are more fitting in some cases, but you should tell.) -- Is it clear which curve means what? -- Is it clear what are the measured points and what is an interpolated - curve between them? -- Are there any overlaps? (E.g., the most interesting part of the curve - hidden underneath a label?) - -In your discussion, please distinguish the following kinds of claims. -It should be always clear which is which: -- Experimental results (i.e., the raw data you obtained from the experiments) -- Theoretical facts (i.e., claims we have proved mathematically) -- Your hypotheses (e.g., when you claim that the graph looks like something is true, - but you are not able to prove rigorously that it always holds) - -Source code templates can be found in [git](https://gitlab.kam.mff.cuni.cz/datovky/assignments/-/tree/master). diff --git a/10-find_duplicates/cpp/Makefile b/10-find_duplicates/cpp/Makefile deleted file mode 100644 index 015ffbf..0000000 --- a/10-find_duplicates/cpp/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -test: find_duplicates_test - ./$< - -INCLUDE ?= . -CXXFLAGS=-std=c++11 -O2 -Wall -Wextra -g -Wno-sign-compare -I$(INCLUDE) - -find_duplicates_test: find_duplicates_test.cpp find_duplicates.h test_main.cpp - $(CXX) $(CXXFLAGS) $(filter %.cpp,$^) -o $@ - -clean: - rm -f find_duplicates_test - -.PHONY: clean test diff --git a/10-find_duplicates/cpp/find_duplicates.h b/10-find_duplicates/cpp/find_duplicates.h deleted file mode 100644 index ead255c..0000000 --- a/10-find_duplicates/cpp/find_duplicates.h +++ /dev/null @@ -1,19 +0,0 @@ -#include <unordered_map> - -vector<string> find_duplicates(DataGenerator& generator) { - /* - * Find duplicates in the given data. - * - * The `generator` provides a forward iterator over strings - * for traversing the data, so it can be iterated for example - * using a `for` cycle: - * - * for (const string& item : generator) {...} - * - * The `generator` can be traversed multiple times. - * - * The goal is to return a vector of duplicated entries, - * reporting each duplicated entry only once. - */ - return vector<string>(); -} diff --git a/10-find_duplicates/cpp/find_duplicates_test.cpp b/10-find_duplicates/cpp/find_duplicates_test.cpp deleted file mode 100644 index 32b0001..0000000 --- a/10-find_duplicates/cpp/find_duplicates_test.cpp +++ /dev/null @@ -1,212 +0,0 @@ -#include <cmath> -#include <functional> -#include <iterator> -#include <iostream> -#include <string> -#include <vector> -#include <algorithm> -#include <type_traits> - -using namespace std; - -// If the condition is not true, report an error and halt. -#define EXPECT(condition, message) do { if (!(condition)) expect_failed(message); } while (0) - -void expect_failed(const string& message); - -template < typename Impl > -class IteratorHelper : iterator<input_iterator_tag, typename Impl::T> { - public: - IteratorHelper() {} - - template < typename ... Args > - IteratorHelper(Args... args) : impl(args...) { finished = !impl.next(); } - - IteratorHelper& operator++() { - finished = !impl.next(); - return *this; - } - - IteratorHelper operator++(int) { - IteratorHelper tmp(*this); - operator++(); - return tmp; - } - - bool operator==(const IteratorHelper& other) const { return other.finished && finished; } - bool operator!=(const IteratorHelper& other) const { return !(*this == other); } - auto operator*() -> typename Impl::T { return impl.get(); } - - private: - bool finished = true; - Impl impl; -}; - - -class DataGenerator { - public: - struct Gen { - uint64_t state; - uint64_t mul; - uint64_t mod; - - uint64_t next() { - uint64_t ret = state; - state = (state * mul) % mod; - return ret; - } - }; - - struct IteratorImpl { - DataGenerator* dg = nullptr; - bool only_dups; - Gen rng, fw_gen, bw_gen; - int fw_steps = 0; - int bw_steps = 0; - uint64_t val; - string ret; - - using T = string; - - IteratorImpl() {} - - IteratorImpl(DataGenerator *dg, bool only_dups) : dg(dg), only_dups(only_dups) { - rng = { (dg->seed * 311) % dg->prime, 78403, dg->prime }; - fw_gen = { dg->seed, dg->step, dg->prime }; - bw_gen = { dg->rev_seed, dg->rev_step, dg->prime }; - } - - bool next() { - repeat: - if (fw_steps >= dg->length) return false; - - if (rng.next() < dg->prime * (dg->repeat_prob / (dg->repeat_prob + 1))) { - while (rng.next() < dg->prime * (1 - dg->repeat_prob)) { - bw_gen.next(); - bw_steps++; - } - - if (only_dups && bw_steps >= dg->length) return false; - - bw_steps++; - val = bw_gen.next(); - return true; - } else { - fw_steps++; - if (!only_dups) { - val = fw_gen.next(); - return true; - } - goto repeat; - } - } - - string get() { - constexpr char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"; - constexpr uint64_t p = (1 << 21) - 19; - static_assert(sizeof(alphabet) == 65); // +1 due to '\0' at the end - - ret = string(dg->str_len, ' '); - uint64_t state = val; - int i = 0; - - while (i < dg->str_len) { - for (int j = 0; j < 5 && i < dg->str_len; j++) - ret[i++] = alphabet[(state >> (6*j)) & 0x3F]; - state = state * p + 11; - } - - return ret; - } - }; - - using Iterator = IteratorHelper<IteratorImpl>; - - Iterator begin() { return Iterator(this, false); } - Iterator end() { return Iterator(); } - - DataGenerator(int _seed, int _length, double _repeat_prob, int _str_len) { - prime = (1ULL << 30) - 101; - - seed = _seed + 101 + _length; - for (int i = 0; i < 100; i++) seed = (seed * 54321) % prime; - - repeat_prob = _repeat_prob; - length = _length; - - step = 23987; - - uint64_t x = pow_mod(step, length - 1, prime); - rev_seed = (x * seed) % prime; - rev_step = mult_inverse(step, prime); - - str_len = _str_len; - }; - - private: - string alphabet; - uint64_t seed, rev_seed, step, rev_step, prime; - int length, str_len; - double repeat_prob; - - Iterator dups() { return Iterator(this, true); } - - uint64_t pow_mod(uint64_t x, uint64_t n, uint64_t mod) { - if (n == 0) return 1; - if (n == 1) return x % mod; - - uint64_t rec = pow_mod(x, n / 2, mod); - rec = (rec * rec) % mod; - - if (n % 2 == 1) return (rec * x) % mod; - return rec; - } - - uint64_t mult_inverse(uint64_t x, uint64_t mod) { - // works only for prime mod - return pow_mod(x, mod - 2, mod); - } - - friend void test_duplicates(int, int, double, int); -}; - -#include "find_duplicates.h" - -#ifdef __linux__ -#include <sys/time.h> -#include <sys/resource.h> -#endif - -void test_duplicates(int seed, int length, double repeat_prob, int str_len) { -#ifdef __linux__ - rlimit data_limit; - data_limit.rlim_cur = data_limit.rlim_max = 64 << 20; - setrlimit(RLIMIT_DATA, &data_limit); -#endif - - DataGenerator generator(seed, length, repeat_prob, str_len); - auto results = find_duplicates(generator); - - vector<string> correct; - for (auto it = generator.dups(); it != generator.end(); ++it) - correct.push_back(*it); - - EXPECT(results.size() == correct.size(), - "Wrong number of generated duplicates, got " + to_string(results.size()) + - " and expected " + to_string(correct.size())); - - sort(correct.begin(), correct.end()); - sort(results.begin(), results.end()); - - for (int i = 0; i < int(results.size()); i++) - EXPECT(results[i] == correct[i], - "Wrong generated duplicate, got " + results[i] + " and expected " + correct[i]); -} - -vector<pair<string, function<void()>>> tests = { - {"10k", [] { test_duplicates(43, 10*1000, 0.01, 13); }}, - {"100k", [] { test_duplicates(43, 100*1000, 0.01, 20); }}, - {"1M", [] { test_duplicates(43, 1000*1000, 0.001, 40); }}, - {"10M", [] { test_duplicates(43, 10*1000*1000, 0.0001, 160); }}, - {"16M", [] { test_duplicates(43, 16*1000*1000, 0.0001, 360); }}, -}; diff --git a/10-find_duplicates/cpp/test_main.cpp b/10-find_duplicates/cpp/test_main.cpp deleted file mode 100644 index 3f4aff0..0000000 --- a/10-find_duplicates/cpp/test_main.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include <cstdlib> -#include <functional> -#include <iostream> -#include <string> -#include <utility> -#include <vector> - -using namespace std; - -extern vector<pair<string, function<void()>>> tests; - -void expect_failed(const string& message) { - cerr << "Test error: " << message << endl; - exit(1); -} - -int main(int argc, char* argv[]) { - vector<string> required_tests; - - if (argc > 1) { - required_tests.assign(argv + 1, argv + argc); - } else { - for (const auto& test : tests) - required_tests.push_back(test.first); - } - - for (const auto& required_test : required_tests) { - bool found = false; - for (const auto& test : tests) - if (required_test == test.first) { - cerr << "Running test " << required_test << endl; - test.second(); - found = true; - break; - } - if (!found) { - cerr << "Unknown test " << required_test << endl; - return 1; - } - } - - return 0; -} diff --git a/10-find_duplicates/python/find_duplicates.py b/10-find_duplicates/python/find_duplicates.py deleted file mode 100644 index 424ed3d..0000000 --- a/10-find_duplicates/python/find_duplicates.py +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env python3 -import sys - -def find_duplicates(data_generator): - """Find duplicates in the given data. - - The `data_generator` is an iterable over strings, so it can be - iterated for example using a `for` cycle: - - for item in data_generator: ... - - It can be iterated multiple times. - - The goal is to return a list of duplicated entries, reporting each duplicated - entry only once. - """ - - raise NotImplementedError() diff --git a/10-find_duplicates/python/find_duplicates_test.py b/10-find_duplicates/python/find_duplicates_test.py deleted file mode 100644 index f2233cc..0000000 --- a/10-find_duplicates/python/find_duplicates_test.py +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env python3 -import gc -import itertools -import sys -import string - -from find_duplicates import find_duplicates - -class DataGenerator(): - def __init__(self, seed, length, repeat_prob, str_len): - self.prime = 2**30 - 101 - - self.seed = seed + 101 + length - for _ in range(100): self.seed = (self.seed * 54321) % self.prime - - self.repeat_prob = float(repeat_prob) - self.length = length - - self.step = 23987 - - x = self._pow_mod(self.step, self.length - 1, self.prime) - self.rev_seed = (x * self.seed) % self.prime - self.rev_step = self._mult_inverse(self.step, self.prime) - - self.str_len = str_len - - def _generator(self, only_dups=False): - def gen(seed, step): - state = seed - while True: - yield state - state = (state * step) % self.prime - - rng = gen((self.seed * 311) % self.prime, 78403) - fw_gen = gen(self.seed, self.step) - bw_gen = gen(self.rev_seed, self.rev_step) - - fw_steps = 0 - bw_steps = 0 - while fw_steps < self.length: - if next(rng) < self.prime * (self.repeat_prob / (self.repeat_prob + 1)): - while next(rng) < self.prime * (1 - self.repeat_prob): - next(bw_gen) - bw_steps += 1 - - if only_dups and bw_steps >= self.length: return - - bw_steps += 1 - yield self._make_string(next(bw_gen)) - else: - fw_steps += 1 - if not only_dups: - yield self._make_string(next(fw_gen)) - - def _make_string(self, x): - alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" - assert(len(alphabet) == 64) - long_strings = [ - "hn7fHKPgyw6GiGu3dRx8NpDPIK1eB2", - "YPBhODY2UU7KTntxAI9YbK4JNPCPJj", - "5qh0uhJW3ZheD65ZnNThGeeB6ds7pI", - "wW8jgWM7cEkEmNWOsyEmOQezHGOGnf", - "JAL6lzo1W3viaHhBrAPC992YIBdQHS", - "Y7OtykNRwyNaZvHsLtFBYoVSJac9xM", - "xIHUKmJFH663fuzs37PXSC8AwL9inq", - ] - p = 2**21 - 19 - - ret = [] - state = x - i = 0 - - for j in range(0, 30, 6): - if i >= self.str_len: break - ret.append(alphabet[(state >> j) & 0x3F]) - i += 1 - - state = state * p + 11; - - while i < self.str_len: - ret.append(long_strings[state % len(long_strings)]) - state = state * p + 11; - i += len(ret[-1]) - - while i < self.str_len: - for j in range(0, 30, 6): - if i >= self.str_len: break - ret.append(alphabet[(state >> j) & 0x3F]) - i += 1 - state = state * p + 11; - - return "".join(ret) - - def __iter__(self): - return self._generator() - - def _pow_mod(self, x, n, mod): - if n == 0: return 1 - if n == 1: return x % mod - rec = self._pow_mod(x, n // 2, mod) - rec = (rec * rec) % mod - if n % 2 == 1: - return (rec * x) % mod - else: - return rec - - def _mult_inverse(self, x, mod): - # works only for prime mod - return self._pow_mod(x, mod - 2, mod) - -def test_duplicates(seed, length, repeat_prob, str_len): - generator = DataGenerator(seed, length, repeat_prob, str_len) - results = find_duplicates(generator) - gc.collect() - - correct = list(generator._generator(only_dups=True)) - assert len(results) == len(correct), \ - "Wrong number of generated duplicates, got %i and expected %i" % (len(results), len(correct)) - assert sorted(results) == sorted(correct), \ - "The generates list of duplicates is not correct, got {} and expected {}".format(results, correct) - -tests = [ - ("10k", lambda: test_duplicates(42, 10**4, 0.01, 14)), - ("100k", lambda: test_duplicates(10, 10**5, 0.01, 20)), - ("1M", lambda: test_duplicates(10, 10**6, 0.001, 340)), - ("10M", lambda: True), - ("16M", lambda: True), -] - -if __name__ == "__main__": - try: - import resource - resource.setrlimit(resource.RLIMIT_DATA, (12<<20, 12<<20)) - except: - pass - - for required_test in sys.argv[1:] or [name for name, _ in tests]: - for name, test in tests: - if name == required_test: - print("Running test {}".format(name), file=sys.stderr) - test() - break - else: - raise ValueError("Unknown test {}".format(name)) diff --git a/10-find_duplicates/task.md b/10-find_duplicates/task.md deleted file mode 100644 index 4953643..0000000 --- a/10-find_duplicates/task.md +++ /dev/null @@ -1,27 +0,0 @@ -In this assignment, you are given a large file on input. Your goal is to find -duplicated lines and return every duplicated line once. - -The challenging part of this assignment is the fact, that your program has to -run in a limited memory, using at most `64MB` for C++ and `12MB` for Python -(and Python itself requires about 5MB), and the input file can be considerably -larger than this memory limit. However, you can rely on the fact that the -number of duplicated lines is considerably smaller (so that all duplicated -lines fit in the memory at the same time). - -Instead of handling a real file, you are given a data generator (an `iterator` -in C++ and a `generator` in Python). Note that limiting memory during the -tests works only on Linux (and not on Windows), and of course also in ReCodEx. - -You can use full standard library of Python and C++ in this assignment, -including data structure implementations (also, `bytearray` might come handy). -Your solution must also work on other input data of the same size with similar -number of duplicates. Hence solutions depending on the fact that each string is -uniquely determined by some its substring or similar properties of the input -will not be accepted. - -As usual, you should submit only the `find_duplicates.{h,py}` file. - -Note that due to the space constraints of the Python solutions, tests `10M` and `16M` are -not used and are always considered successful by ReCodEx. - -Source code templates can be found in [git](https://gitlab.kam.mff.cuni.cz/datovky/assignments/-/tree/master). diff --git a/11-kgrams/cpp/Makefile b/11-kgrams/cpp/Makefile deleted file mode 100644 index 7046027..0000000 --- a/11-kgrams/cpp/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -test: kgrams_test - ./$< - -INCLUDE ?= . -CXXFLAGS=-std=c++11 -O2 -Wall -Wextra -g -Wno-sign-compare -I$(INCLUDE) - -kgrams_test: kgrams_test.cpp kgrams.h test_main.cpp - $(CXX) $(CXXFLAGS) $(filter %.cpp,$^) -o $@ - -clean: - rm -f kgrams_test - -.PHONY: clean test diff --git a/11-kgrams/cpp/kgrams.h b/11-kgrams/cpp/kgrams.h deleted file mode 100644 index bdcd4e8..0000000 --- a/11-kgrams/cpp/kgrams.h +++ /dev/null @@ -1,59 +0,0 @@ -#include <vector> -#include <iostream> -#include <algorithm> -#include <string> -#include <functional> - -using namespace std; - -class SuffixArray { - public: - string text; - int n; // Length of text - vector<int> S; // Permutation which sorts suffixes - vector<int> R; // Ranking array (an inverse of S) - - // Construct suffix array and ranking array for the given string - // using the doubling algorithm. - SuffixArray(const string &orig_text) - { - text = orig_text; - n = text.size(); - S.resize(n+1); - R.resize(n+1); - - sort_and_rank([this](int a, int b) -> bool { return text[a] < text[b]; }); - - for (int k=1; k<n; k*=2) { - sort_and_rank([this,k](int a, int b) -> bool { - pair<int,int> pa(R[a], (a+k < n) ? R[a+k] : -1); - pair<int,int> pb(R[b], (b+k < n) ? R[b+k] : -1); - return (pa < pb); - }); - } - } - - // An auxiliary function used in the doubling algorithm. - void sort_and_rank(function<bool(int a, int b)> comp) - { - for (size_t i=0; i<S.size(); i++) - S[i] = i; - - sort(S.begin(), S.end(), comp); - - vector<int> R2(S.size()); - for (size_t i=0; i<S.size(); i++) { - if (!i || comp(S[i-1], S[i]) || comp(S[i], S[i-1])) - R2[S[i]] = i; - else - R2[S[i]] = R2[S[i-1]]; - } - R.swap(R2); - } - - // Return the number of distinct k-grams in the string. - int num_kgrams(int k) - { - // TODO: Implement - } -}; diff --git a/11-kgrams/cpp/kgrams_test.cpp b/11-kgrams/cpp/kgrams_test.cpp deleted file mode 100644 index bb90a32..0000000 --- a/11-kgrams/cpp/kgrams_test.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include <algorithm> -#include <functional> -#include <string> -#include <vector> -#include <cstdint> - -using namespace std; - -// If the condition is not true, report an error and halt. -#define EXPECT(condition, message) do { if (!(condition)) expect_failed(message); } while (0) -void expect_failed(const string& message); - -#include "kgrams.h" - -void test_generic(const string& text, int k, int expected_kg) -{ - SuffixArray sa(text); - int num_kg = sa.num_kgrams(k); - EXPECT(num_kg == expected_kg, "Expected " + to_string(expected_kg) + " " + to_string(k) + "-grams, found " + to_string(num_kg) + "."); - -} - -// Test on a fixed string. -void test_explicit(int k, int expected_kg) -{ - test_generic("annbansbananas", k, expected_kg); -} - -// Test on a very non-uniform random string. -void test_random(int n, int k, int expected_kg) -{ - string s(n, ' '); - uint32_t state = n; - - for (int i=0; i<n; i++) { - state = state*2654289733 + 7; - unsigned x = (state >> 28) % 16; - char next = "aaaaaaaaaaaabbbc"[x]; - s[i] = next; - } - - test_generic(s, k, expected_kg); -} - -// Test on an almost-constant string. -void test_trivial(int n, int k, int expected_kg) -{ - string s(n, ' '); - - for (int i=0; i<n; i++) { - if (i == n/2) - s[i] = 'b'; - else - s[i] = 'a'; - } - - test_generic(s, k, expected_kg); -} - -vector<pair<string, function<void()>>> tests = { - {"basic-1", [] { test_explicit(1, 4); }}, - {"basic-2", [] { test_explicit(2, 8); }}, - {"basic-3", [] { test_explicit(3, 10); }}, - {"basic-4", [] { test_explicit(4, 11); }}, - {"basic-14", [] { test_explicit(14, 1); }}, - - {"short-5", [] { test_random(1000, 5, 107); }}, - {"short-33", [] { test_random(1000, 33, 968); }}, - {"short-500", [] { test_random(1000, 500, 501); }}, - - {"long-5", [] { test_random(100000, 5, 230); }}, - {"long-33", [] { test_random(100000, 33, 99767); }}, - {"long-5000", [] { test_random(100000, 5000, 95001); }}, - - {"triv-1", [] { test_trivial(1000000, 1, 2); }}, - {"triv-5", [] { test_trivial(1000000, 5, 6); }}, - {"triv-3333", [] { test_trivial(1000000, 3333, 3334); }}, - {"triv-500000", [] { test_trivial(1000000, 500000, 500001); }}, -}; diff --git a/11-kgrams/cpp/test_main.cpp b/11-kgrams/cpp/test_main.cpp deleted file mode 100644 index 3f4aff0..0000000 --- a/11-kgrams/cpp/test_main.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include <cstdlib> -#include <functional> -#include <iostream> -#include <string> -#include <utility> -#include <vector> - -using namespace std; - -extern vector<pair<string, function<void()>>> tests; - -void expect_failed(const string& message) { - cerr << "Test error: " << message << endl; - exit(1); -} - -int main(int argc, char* argv[]) { - vector<string> required_tests; - - if (argc > 1) { - required_tests.assign(argv + 1, argv + argc); - } else { - for (const auto& test : tests) - required_tests.push_back(test.first); - } - - for (const auto& required_test : required_tests) { - bool found = false; - for (const auto& test : tests) - if (required_test == test.first) { - cerr << "Running test " << required_test << endl; - test.second(); - found = true; - break; - } - if (!found) { - cerr << "Unknown test " << required_test << endl; - return 1; - } - } - - return 0; -} diff --git a/11-kgrams/python/kgrams.py b/11-kgrams/python/kgrams.py deleted file mode 100644 index 0eefda3..0000000 --- a/11-kgrams/python/kgrams.py +++ /dev/null @@ -1,43 +0,0 @@ -class SuffixArray: - def __init__(self, text): - self.text = text - # S is the suffix array (a permutation which sorts suffixes) - # R is the ranking array (the inverse of S) - self.R, self.S = self._build_suffix_array(text) - - def _build_suffix_array(self, text): - """ - Construct the suffix array and ranking array for the given string - using the doubling algorithm. - """ - - n = len(text) - R = [None] * (n+1) - S = [None] * (n+1) - - R = self._sort_and_rank(S, lambda a: ord(text[a]) if a < len(text) else -1) - - k = 1 - while k < n: - R = self._sort_and_rank(S, lambda a: (R[a], (R[a+k] if a+k < n else -1))) - k *= 2 - - return (tuple(R), tuple(S)) - - # An auxiliary function used in the doubling algorithm. - def _sort_and_rank(self, S, key): - for i in range(len(S)): S[i] = i - S.sort(key = key) - - R = [None] * len(S) - for i, s in enumerate(S): - prev_s = S[i-1] - if i == 0 or key(prev_s) != key(s): R[s] = i - else: R[s] = R[prev_s] - return R - - def num_kgrams(self, k): - """Return the number of distinct k-grams in the string.""" - - raise NotImplementedError - diff --git a/11-kgrams/python/kgrams_test.py b/11-kgrams/python/kgrams_test.py deleted file mode 100644 index 18646f4..0000000 --- a/11-kgrams/python/kgrams_test.py +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/python - -import sys -from kgrams import SuffixArray - -def test_generic(text, k, expected_count): - sa = SuffixArray(text) - count = sa.num_kgrams(k) - assert count == expected_count, \ - "Expected %i %i-grams, found %i." % (expected_count, k, count) - -def test_explicit(k, expected_count): - test_generic("annbansbananas", k, expected_count) - -def test_random(n, k, expected_count): - b = bytearray(n) - state = n - for i in range(n): - state = (state*2654289733 + 7) % (1 << 32) - x = (state >> 28) % 16 - next = "aaaaaaaaaaaabbbc"[x] - b[i] = ord(next) - - test_generic(b.decode(), k, expected_count) - -def test_trivial(n, k, expected_count): - s = "".join( "b" if i == n // 2 else "a" for i in range (n) ) - test_generic(s, k, expected_count) - -tests = [ - ("basic-1", lambda: test_explicit(1, 4)), - ("basic-2", lambda: test_explicit(2, 8)), - ("basic-3", lambda: test_explicit(3, 10)), - ("basic-4", lambda: test_explicit(4, 11)), - ("basic-14", lambda: test_explicit(14, 1)), - - ("short-5", lambda: test_random(1000, 5, 107)), - ("short-33", lambda: test_random(1000, 33, 968)), - ("short-500", lambda: test_random(1000, 500, 501)), - - ("long-5", lambda: test_random(100000, 5, 230)), - ("long-33", lambda: test_random(100000, 33, 99767)), - ("long-5000", lambda: test_random(100000, 5000, 95001)), - - ("triv-1", lambda: test_trivial(1000000, 1, 2)), - ("triv-5", lambda: test_trivial(1000000, 5, 6)), - ("triv-3333", lambda: test_trivial(1000000, 3333, 3334)), - ("triv-500000", lambda: test_trivial(1000000, 500000, 500001)), -] - -if __name__ == "__main__": - for required_test in sys.argv[1:] or [name for name, _ in tests]: - for name, test in tests: - if name == required_test: - print("Running test {}".format(name), file=sys.stderr) - test() - break - else: - raise ValueError("Unknown test {}".format(name)) - diff --git a/11-kgrams/task.md b/11-kgrams/task.md deleted file mode 100644 index 22800b4..0000000 --- a/11-kgrams/task.md +++ /dev/null @@ -1,10 +0,0 @@ -Your task is to write a function which takes a string and an integer K -and it reports how many different K-grams (K-character substrings) the -string has. - -You are given an algorithm for construction of the suffix array. For -simplicity, this algorithm has time complexity $O(n \log^2 n)$. Except -for constructing the suffix array, your algorithm should run in linear -time. - -Source code templates can be found in [git](https://gitlab.kam.mff.cuni.cz/datovky/assignments/-/tree/master). diff --git a/12-range_tree/cpp/Makefile b/12-range_tree/cpp/Makefile deleted file mode 100644 index 38c4dcd..0000000 --- a/12-range_tree/cpp/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -test: range_tree_test - ./$< - -INCLUDE ?= . -CXXFLAGS=-std=c++11 -O2 -Wall -Wextra -g -Wno-sign-compare -I$(INCLUDE) - -range_tree_test: range_tree_test.cpp range_tree.h test_main.cpp - $(CXX) $(CXXFLAGS) $(filter %.cpp,$^) -o $@ - -clean: - rm -f range_tree_test - -.PHONY: clean test diff --git a/12-range_tree/cpp/range_tree.h b/12-range_tree/cpp/range_tree.h deleted file mode 100644 index a5ca594..0000000 --- a/12-range_tree/cpp/range_tree.h +++ /dev/null @@ -1,177 +0,0 @@ -#include <cstdint> -#include <limits> - -// A node of the tree -class Node { - public: - int64_t key; - int64_t value; - Node* left; - Node* right; - Node* parent; - - // Constructor - Node(int64_t key, int64_t value, Node* parent=nullptr, Node* left=nullptr, Node* right=nullptr) { - this->key = key; - this->value = value; - this->parent = parent; - this->left = left; - this->right = right; - if (left) left->parent = this; - if (right) right->parent = this; - } - -}; - -// Splay tree -class Tree { - public: - // Pointer to root of the tree; nullptr if the tree is empty. - Node* root; - - Tree(Node* root=nullptr) { - this->root = root; - } - - // Rotate the given `node` up. Perform a single rotation of the edge - // between the node and its parent, choosing left or right rotation - // appropriately. - virtual void rotate(Node* node) { - if (node->parent) { - if (node->parent->left == node) { - if (node->right) node->right->parent = node->parent; - node->parent->left = node->right; - node->right = node->parent; - } else { - if (node->left) node->left->parent = node->parent; - node->parent->right = node->left; - node->left = node->parent; - } - if (node->parent->parent) { - if (node->parent->parent->left == node->parent) - node->parent->parent->left = node; - else - node->parent->parent->right = node; - } else { - root = node; - } - - Node* original_parent = node->parent; - node->parent = node->parent->parent; - original_parent->parent = node; - } - } - - // Splay the given node. - virtual void splay(Node* node) { - while (node->parent && node->parent->parent) { - if ((node->parent->right == node && node->parent->parent->right == node->parent) || - (node->parent->left == node && node->parent->parent->left == node->parent)) { - rotate(node->parent); - rotate(node); - } else { - rotate(node); - rotate(node); - } - } - if (node->parent) - rotate(node); - } - - // Look up the given key in the tree, returning the - // the node with the requested key or nullptr. - Node* lookup(int64_t key) { - Node* node = root; - Node* node_last = nullptr; - while (node) { - node_last = node; - if (node->key == key) - break; - if (key < node->key) - node = node->left; - else - node = node->right; - } - if (node_last) - splay(node_last); - return node; - } - - // Insert a (key, value) into the tree. - // If the key is already present, nothing happens. - void insert(int64_t key, int64_t value) { - if (!root) { - root = new Node(key, value); - return; - } - - Node* node = root; - while (node->key != key) { - if (key < node->key) { - if (!node->left) - node->left = new Node(key, value, node); - node = node->left; - } else { - if (!node->right) - node->right = new Node(key, value, node); - node = node->right; - } - } - splay(node); - } - - // Delete given key from the tree. - // It the key is not present, do nothing. - // - // The implementation first splays the element to be removed to - // the root, and if it has both children, splays the largest element - // in the left subtree and links it to the original right subtree. - void remove(int64_t key) { - if (lookup(key)) { - Node* right = root->right; - root = root->left; - if (!root) { - root = right; - right = nullptr; - } - if (root) - root->parent = nullptr; - - if (right) { - Node* node = root; - while (node->right) - node = node->right; - splay(node); - root->right = right; - right->parent = root; - } - } - } - - // Return the sum of elements with keys in range [left, right]. - // - // Given a closed range [left, right], return the sum of values of elements - // in the range, i.e., sum(value | (key, value) in tree, left <= key <= right). - int64_t range_sum(int64_t left, int64_t right) { - // TODO: Implement - } - - // Destructor to free all allocated memory. - ~Tree() { - Node* node = root; - while (node) { - Node* next; - if (node->left) { - next = node->left; - node->left = nullptr; - } else if (node->right) { - next = node->right; - node->right = nullptr; - } else { - next = node->parent; - delete node; - } - node = next; - } - } -}; diff --git a/12-range_tree/cpp/range_tree_test.cpp b/12-range_tree/cpp/range_tree_test.cpp deleted file mode 100644 index 95d0169..0000000 --- a/12-range_tree/cpp/range_tree_test.cpp +++ /dev/null @@ -1,81 +0,0 @@ -#include <algorithm> -#include <functional> -#include <string> -#include <utility> -#include <vector> - -using namespace std; - -// If the condition is not true, report an error and halt. -#define EXPECT(condition, message) do { if (!(condition)) expect_failed(message); } while (0) -void expect_failed(const string& message); - -#include "range_tree.h" - -void create_test_tree(int64_t size, bool ascending, Tree& tree) { - vector<int64_t> sequence = {7}; - for (int64_t i = 2; i < size; i++) - sequence.push_back((sequence.back() * sequence.front()) % size); - if (ascending) - sort(sequence.begin(), sequence.end()); - - for (int64_t element : sequence) - tree.insert(element, element); -} - -void test_missing(int64_t size, bool ascending) { - Tree tree; - create_test_tree(size, ascending, tree); - - int64_t values = 0; - for (int64_t i = 0; i < size; i++) - values += tree.range_sum(-size, 0) + tree.range_sum(size, 2 * size); - EXPECT(values == 0, "Expected no values in an empty range"); -} - -void test_suffixes(int64_t size, bool ascending) { - Tree tree; - create_test_tree(size, ascending, tree); - - for (int64_t left = 1; left < size; left++) { - int64_t values = tree.range_sum(left, size - 1); - int64_t expected = size * (size - 1) / 2 - left * (left - 1) / 2; - EXPECT(values == expected, - "Expected " + to_string(expected) + " for range [" + to_string(left) + - ", " + to_string(size - 1) + "], got " + to_string(values)); - } -} - -void test_updates(int64_t size, bool ascending) { - Tree tree; - create_test_tree(size, ascending, tree); - - for (int64_t left = 1; left < size; left++) { - tree.remove(left); - tree.insert(left + size - 1, left + size - 1); - int64_t values = tree.range_sum(left + 1, size + left); - int64_t expected = (size + left) * (size + left - 1) / 2 - (left + 1) * left / 2; - EXPECT(values == expected, - "Expected " + to_string(expected) + " for range [" + to_string(left + 1) + - ", " + to_string(size + left) + "], got " + to_string(values)); - } -} - - -vector<pair<string, function<void()>>> tests = { - {"random_missing", [] { test_missing(13, false); }}, - {"random_suffixes", [] { test_suffixes(13, false); }}, - {"random_updates", [] { test_updates(13, false); }}, - - {"path_missing", [] { test_missing(13, true); }}, - {"path_suffixes", [] { test_suffixes(13, true); }}, - {"path_updates", [] { test_updates(13, true); }}, - - {"random_missing_big", [] { test_missing(199999, false); }}, - {"random_suffixes_big", [] { test_suffixes(199999, false); }}, - {"random_updates_big", [] { test_updates(199999, false); }}, - - {"path_missing_big", [] { test_missing(199999, true); }}, - {"path_suffixes_big", [] { test_suffixes(199999, true); }}, - {"path_updates_big", [] { test_updates(199999, true); }}, -}; diff --git a/12-range_tree/cpp/test_main.cpp b/12-range_tree/cpp/test_main.cpp deleted file mode 100644 index 3f4aff0..0000000 --- a/12-range_tree/cpp/test_main.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include <cstdlib> -#include <functional> -#include <iostream> -#include <string> -#include <utility> -#include <vector> - -using namespace std; - -extern vector<pair<string, function<void()>>> tests; - -void expect_failed(const string& message) { - cerr << "Test error: " << message << endl; - exit(1); -} - -int main(int argc, char* argv[]) { - vector<string> required_tests; - - if (argc > 1) { - required_tests.assign(argv + 1, argv + argc); - } else { - for (const auto& test : tests) - required_tests.push_back(test.first); - } - - for (const auto& required_test : required_tests) { - bool found = false; - for (const auto& test : tests) - if (required_test == test.first) { - cerr << "Running test " << required_test << endl; - test.second(); - found = true; - break; - } - if (!found) { - cerr << "Unknown test " << required_test << endl; - return 1; - } - } - - return 0; -} diff --git a/12-range_tree/python/range_tree.py b/12-range_tree/python/range_tree.py deleted file mode 100644 index 3288ca9..0000000 --- a/12-range_tree/python/range_tree.py +++ /dev/null @@ -1,129 +0,0 @@ -#!/usr/bin/env python3 -import math - -class Node: - """Node in a binary tree `Tree`""" - - def __init__(self, key, value, left=None, right=None, parent=None): - self.key = key - self.value = value - self.parent = parent - self.left = left - self.right = right - if left is not None: left.parent = self - if right is not None: right.parent = self - -class Tree: - """A splay tree implementation""" - - def __init__(self, root=None): - self.root = root - - def rotate(self, node): - """ Rotate the given `node` up. - - Performs a single rotation of the edge between the given node - and its parent, choosing left or right rotation appropriately. - """ - if node.parent is not None: - if node.parent.left == node: - if node.right is not None: node.right.parent = node.parent - node.parent.left = node.right - node.right = node.parent - else: - if node.left is not None: node.left.parent = node.parent - node.parent.right = node.left - node.left = node.parent - if node.parent.parent is not None: - if node.parent.parent.left == node.parent: - node.parent.parent.left = node - else: - node.parent.parent.right = node - else: - self.root = node - node.parent.parent, node.parent = node, node.parent.parent - - def splay(self, node): - """Splay the given node""" - while node.parent is not None and node.parent.parent is not None: - if (node.parent.right == node and node.parent.parent.right == node.parent) or \ - (node.parent.left == node and node.parent.parent.left == node.parent): - self.rotate(node.parent) - self.rotate(node) - else: - self.rotate(node) - self.rotate(node) - if node.parent is not None: - self.rotate(node) - - def lookup(self, key): - """Look up the given key in the tree. - - Returns the node with the requested key or `None`. - """ - node, node_last = self.root, None - while node is not None: - node_last = node - if node.key == key: - break - if key < node.key: - node = node.left - else: - node = node.right - if node_last is not None: - self.splay(node_last) - return node - - def insert(self, key, value): - """Insert (key, value) into the tree. - - If the key is already present, do nothing. - """ - if self.root is None: - self.root = Node(key, value) - return - - node = self.root - while node.key != key: - if key < node.key: - if node.left is None: - node.left = Node(key, value, parent=node) - node = node.left - else: - if node.right is None: - node.right = Node(key, value, parent=node) - node = node.right - self.splay(node) - - def remove(self, key): - """Remove given key from the tree. - - It the key is not present, do nothing. - - The implementation first splays the element to be removed to - the root, and if it has both children, splays the largest element - in the left subtree and links it to the original right subtree. - """ - if self.lookup(key) is not None: - right = self.root.right - self.root = self.root.left - if self.root is None: - self.root, right = right, None - if self.root is not None: - self.root.parent = None - - if right is not None: - node = self.root - while node.right is not None: - node = node.right - self.splay(node) - self.root.right = right - right.parent = self.root - - def range_sum(self, left, right): - """Return the sum of elements with keys in range [left, right] - - Given a closed range [left, right], return the sum of values of elements - in the range, i.e., sum(value | (key, value) in tree, left <= key <= right). - """ - raise NotImplementedError() diff --git a/12-range_tree/python/range_tree_test.py b/12-range_tree/python/range_tree_test.py deleted file mode 100644 index bc866e1..0000000 --- a/12-range_tree/python/range_tree_test.py +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/env python3 -import sys - -from range_tree import Tree - -def test_tree(size, ascending): - sequence = [pow(7, i, size) for i in range(1, size)] - if ascending: sequence = sorted(sequence) - - tree = Tree() - for element in sequence: - tree.insert(element, element) - return tree - -def test_missing(size, ascending): - tree = test_tree(size, ascending) - - values = 0 - for _ in range(size): - values += tree.range_sum(-size, 0) - values += tree.range_sum(size, 2 * size) - assert values == 0, "Expected no values in an empty range" - -def test_suffixes(size, ascending): - tree = test_tree(size, ascending) - - for left in range(1, size): - values = tree.range_sum(left, size - 1) - expected = size * (size - 1) // 2 - left * (left - 1) // 2 - assert values == expected, "Expected {} for range [{}, {}], got {}".format(expected, left, size - 1, values) - -def test_updates(size, ascending): - tree = test_tree(size, ascending) - - for left in range(1, size): - tree.remove(left) - tree.insert(left + size - 1, left + size - 1) - values = tree.range_sum(left + 1, size + left) - expected = (size + left) * (size + left - 1) // 2 - (left + 1) * left // 2 - assert values == expected, "Expected {} for range [{}, {}], got {}".format(expected, left + 1, size + left, values) - -tests = [ - ("random_missing", lambda: test_missing(13, False)), - ("random_suffixes", lambda: test_suffixes(13, False)), - ("random_updates", lambda: test_updates(13, False)), - - ("path_missing", lambda: test_missing(13, True)), - ("path_suffixes", lambda: test_suffixes(13, True)), - ("path_updates", lambda: test_updates(13, True)), - - ("random_missing_big", lambda: test_missing(19997, False)), - ("random_suffixes_big", lambda: test_suffixes(19997, False)), - ("random_updates_big", lambda: test_updates(19997, False)), - - ("path_missing_big", lambda: test_missing(19997, True)), - ("path_suffixes_big", lambda: test_suffixes(19997, True)), - ("path_updates_big", lambda: test_updates(19997, True)), -] - -if __name__ == "__main__": - for required_test in sys.argv[1:] or [name for name, _ in tests]: - for name, test in tests: - if name == required_test: - print("Running test {}".format(name), file=sys.stderr) - test() - break - else: - raise ValueError("Unknown test {}".format(name)) diff --git a/12-range_tree/task.md b/12-range_tree/task.md deleted file mode 100644 index 6584a8c..0000000 --- a/12-range_tree/task.md +++ /dev/null @@ -1,24 +0,0 @@ -You are given an implementation of a Splay tree which associates every numeric -key with a numeric value. The Splay tree provides `lookup`, `insert`, and `remove` -operations. - -Your goal is to modify the Splay tree to support range queries in amortized -logarithmic time. The operation you need to implement takes a range of the -keys and it should return the sum of values of the elements in the given range. - -As usual, you should submit only the `range_tree.{h,py}` file. - -## Optional: Range updates (for extra 5 points) - -If you also implement an operation -``` -range_update(left, right, delta) -``` -which adds `delta` to the value of all elements with key in `[left, right]` range -and runs in amortized logarithmic time, you will get 5 points. - -Currently there are no automated tests for this method; therefore, if you -implement it, submit the solution to ReCodEx and write an email to your -teaching assistant. - -Source code templates can be found in [git](https://gitlab.kam.mff.cuni.cz/datovky/assignments/-/tree/master). -- GitLab