From 8e7bbea20da30bd391fd5323bb60f5e14cca4811 Mon Sep 17 00:00:00 2001
From: Boris Stefanovic <owldev@bluewin.ch>
Date: Tue, 3 May 2022 16:31:58 +0200
Subject: [PATCH] save

---
 ex4/ex4.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 100 insertions(+)

diff --git a/ex4/ex4.c b/ex4/ex4.c
index 68c42f8..7d153f8 100644
--- a/ex4/ex4.c
+++ b/ex4/ex4.c
@@ -1,6 +1,106 @@
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 
+#define QUAD 4
+#define SIDE 16
+
+
+
+typedef struct node {
+	int value;
+	struct node* children[QUAD];
+} node_t;
+
+
+node_t* node_create(const int value) {
+	node_t *node = malloc(sizeof(node_t));
+	node->value = value;
+	for (int i = 0; i < QUAD; ++i) node->children[i] = NULL;
+	return node;
+}
+
+
+bool is_leaf(node_t* node) {
+	return node->children[0] = NULL;
+}
+
+
+void mat_to_qtr(
+		const int** a,
+		const int x1, const int x2,
+		const int y1, const int y2,
+		node_t* node
+) {
+	if (x2-x1 != y2-y1) printf("PROBLEM!\n");
+	const int size = x2 - x1;
+	switch (size) {
+		case 1:
+			node->value = a[y1][x1];
+			return;
+		default:
+			for (int i = 0; i < QUAD; ++i)
+				node->children[i] = node_create(0);
+			mat_to_qtr(a,
+						x1, x1 + (x2 - x1) / 2,
+						y1, y1 + (y2 - y1) / 2,
+						node->children[0]);
+			mat_to_qtr(a,
+						x1 + (x2 - x1) / 2, x2,
+						y1, y1 + (y2 - y1) / 2,
+						node->children[1]);
+			mat_to_qtr(a,
+						x1, x1 + (x2 - x1) / 2,
+						y1 + (y2 - y1) / 2, y2,
+						node->children[2]);
+			mat_to_qtr(a,
+						x1 + (x2 - x1) / 2, x2,
+						y1, y1 + (y2 - y1) / 2,
+						node->children[3]);
+	}
+}
+
+
+node_t* qtr_copy(node_t* node) {
+	if (node == NULL) return NULL;
+	node_t *dest = malloc(sizeof(node_t));
+	dest->value = node->value;
+	for (int i = 0; i < QUAD; ++i) {
+		dest->children[i] = qtr_copy(node->children[i]);
+	}
+	return dest;
+}
+
+
+
+const int initlist[] = {
+0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 
+0,  3,  3,  3,  3,  0,  0,  7,  7,  7,  7,  0,  0, 11, 11, 11,
+0,  3,  0,  0,  0,  0,  0,  7,  0,  0,  0,  0,  0, 11,  0,  0,
+0,  3,  3,  3,  0,  0,  0,  7,  7,  7,  0,  0,  0, 11, 11, 11,
+0,  3,  0,  0,  0,  0,  0,  7,  0,  0,  0,  0,  0, 11,  0,  0,
+0,  3,  0,  0,  0,  0,  0,  7,  7,  7,  7,  0,  0, 11, 11, 11,
+0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+0,  3,  3,  3,  3,  0,  0,  7,  7,  7,  7,  0,  0, 11, 11, 11,
+0,  3,  0,  0,  0,  0,  0,  7,  0,  0,  0,  0,  0, 11,  0,  0,
+0,  3,  3,  3,  0,  0,  0,  7,  7,  7,  0,  0,  0, 11, 11, 11,
+0,  3,  0,  0,  0,  0,  0,  7,  0,  0,  0,  0,  0, 11,  0,  0,
+0,  3,  0,  0,  0,  0,  0,  7,  7,  7,  7,  0,  0, 11, 11, 11,
+0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+0,  3,  3,  3,  3,  0,  0,  7,  7,  7,  7,  0,  0, 11, 11, 11
+};
+
+
 int main() {
+	int a[16][16];
+	for (int y = 0; y < 16; ++y)
+		for (int x = 0; x < 16; ++x)
+			a[y][x] = initlist[16 * y + x];
+	node_t *root = malloc(sizeof(node_t));
+	mat_to_qtr(a, 0, 16, 0, 16, root);
+	node_t *copy = qtr_copy(root);
+	return EXIT_SUCCESS;
 }
-- 
GitLab