aboutsummaryrefslogtreecommitdiff
path: root/entries/adirar111
diff options
context:
space:
mode:
authorfunemy2022-10-25 07:14:52 +0000
committerfunemy2022-10-25 07:14:52 +0000
commit998ef837b43203319397f191036a97a5adc42220 (patch)
tree920558bd60d282c663fd2da1cd4144cac1849a1d /entries/adirar111
parentfdac6c60e115297a58f5b81da0c4b7f18ac758f2 (diff)
parentce7544a6db594f7d3dfad0d7dc65d01515e57ad6 (diff)
Merge branch 'main' of github.com:braxtonhall/fib
Diffstat (limited to 'entries/adirar111')
-rw-r--r--entries/adirar111/c-filesystem/cache/01
-rw-r--r--entries/adirar111/c-filesystem/cache/11
-rw-r--r--entries/adirar111/c-filesystem/cache/101
-rw-r--r--entries/adirar111/c-filesystem/cache/111
-rw-r--r--entries/adirar111/c-filesystem/cache/121
-rw-r--r--entries/adirar111/c-filesystem/cache/131
-rw-r--r--entries/adirar111/c-filesystem/cache/21
-rw-r--r--entries/adirar111/c-filesystem/cache/31
-rw-r--r--entries/adirar111/c-filesystem/cache/41
-rw-r--r--entries/adirar111/c-filesystem/cache/51
-rw-r--r--entries/adirar111/c-filesystem/cache/61
-rw-r--r--entries/adirar111/c-filesystem/cache/71
-rw-r--r--entries/adirar111/c-filesystem/cache/81
-rw-r--r--entries/adirar111/c-filesystem/cache/91
-rw-r--r--entries/adirar111/c-filesystem/fib-fs.c134
-rw-r--r--entries/adirar111/wasm/fib.wasmbin0 -> 144 bytes
-rw-r--r--entries/adirar111/wasm/fib.wat27
-rw-r--r--entries/adirar111/wasm/index.html21
-rw-r--r--entries/adirar111/wasm/index.js23
-rw-r--r--entries/adirar111/wasm/style.css27
20 files changed, 246 insertions, 0 deletions
diff --git a/entries/adirar111/c-filesystem/cache/0 b/entries/adirar111/c-filesystem/cache/0
new file mode 100644
index 0000000..573541a
--- /dev/null
+++ b/entries/adirar111/c-filesystem/cache/0
@@ -0,0 +1 @@
+0
diff --git a/entries/adirar111/c-filesystem/cache/1 b/entries/adirar111/c-filesystem/cache/1
new file mode 100644
index 0000000..56a6051
--- /dev/null
+++ b/entries/adirar111/c-filesystem/cache/1
@@ -0,0 +1 @@
+1 \ No newline at end of file
diff --git a/entries/adirar111/c-filesystem/cache/10 b/entries/adirar111/c-filesystem/cache/10
new file mode 100644
index 0000000..7c6ba0f
--- /dev/null
+++ b/entries/adirar111/c-filesystem/cache/10
@@ -0,0 +1 @@
+55 \ No newline at end of file
diff --git a/entries/adirar111/c-filesystem/cache/11 b/entries/adirar111/c-filesystem/cache/11
new file mode 100644
index 0000000..8643cf6
--- /dev/null
+++ b/entries/adirar111/c-filesystem/cache/11
@@ -0,0 +1 @@
+89
diff --git a/entries/adirar111/c-filesystem/cache/12 b/entries/adirar111/c-filesystem/cache/12
new file mode 100644
index 0000000..70e1a64
--- /dev/null
+++ b/entries/adirar111/c-filesystem/cache/12
@@ -0,0 +1 @@
+144 \ No newline at end of file
diff --git a/entries/adirar111/c-filesystem/cache/13 b/entries/adirar111/c-filesystem/cache/13
new file mode 100644
index 0000000..f937f7e
--- /dev/null
+++ b/entries/adirar111/c-filesystem/cache/13
@@ -0,0 +1 @@
+233 \ No newline at end of file
diff --git a/entries/adirar111/c-filesystem/cache/2 b/entries/adirar111/c-filesystem/cache/2
new file mode 100644
index 0000000..56a6051
--- /dev/null
+++ b/entries/adirar111/c-filesystem/cache/2
@@ -0,0 +1 @@
+1 \ No newline at end of file
diff --git a/entries/adirar111/c-filesystem/cache/3 b/entries/adirar111/c-filesystem/cache/3
new file mode 100644
index 0000000..d8263ee
--- /dev/null
+++ b/entries/adirar111/c-filesystem/cache/3
@@ -0,0 +1 @@
+2 \ No newline at end of file
diff --git a/entries/adirar111/c-filesystem/cache/4 b/entries/adirar111/c-filesystem/cache/4
new file mode 100644
index 0000000..00750ed
--- /dev/null
+++ b/entries/adirar111/c-filesystem/cache/4
@@ -0,0 +1 @@
+3
diff --git a/entries/adirar111/c-filesystem/cache/5 b/entries/adirar111/c-filesystem/cache/5
new file mode 100644
index 0000000..7813681
--- /dev/null
+++ b/entries/adirar111/c-filesystem/cache/5
@@ -0,0 +1 @@
+5 \ No newline at end of file
diff --git a/entries/adirar111/c-filesystem/cache/6 b/entries/adirar111/c-filesystem/cache/6
new file mode 100644
index 0000000..301160a
--- /dev/null
+++ b/entries/adirar111/c-filesystem/cache/6
@@ -0,0 +1 @@
+8 \ No newline at end of file
diff --git a/entries/adirar111/c-filesystem/cache/7 b/entries/adirar111/c-filesystem/cache/7
new file mode 100644
index 0000000..ca7bf83
--- /dev/null
+++ b/entries/adirar111/c-filesystem/cache/7
@@ -0,0 +1 @@
+13 \ No newline at end of file
diff --git a/entries/adirar111/c-filesystem/cache/8 b/entries/adirar111/c-filesystem/cache/8
new file mode 100644
index 0000000..b5045cc
--- /dev/null
+++ b/entries/adirar111/c-filesystem/cache/8
@@ -0,0 +1 @@
+21 \ No newline at end of file
diff --git a/entries/adirar111/c-filesystem/cache/9 b/entries/adirar111/c-filesystem/cache/9
new file mode 100644
index 0000000..3e932fe
--- /dev/null
+++ b/entries/adirar111/c-filesystem/cache/9
@@ -0,0 +1 @@
+34 \ No newline at end of file
diff --git a/entries/adirar111/c-filesystem/fib-fs.c b/entries/adirar111/c-filesystem/fib-fs.c
new file mode 100644
index 0000000..73126b6
--- /dev/null
+++ b/entries/adirar111/c-filesystem/fib-fs.c
@@ -0,0 +1,134 @@
+/* fib, but the cache is a directory of files
+ * cache/
+ * 0 -> 0
+ * 1 -> 1
+ * 2 -> 1
+ * 3 -> 2
+ * ...
+ * unix filesystem required
+*/
+#include <dirent.h>
+#include <string.h>
+#include <stdio.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+static const char CACHE_DIR[] = "cache";
+static const int BUF_SIZE = 30;
+static const int PATH_SIZE = 30;
+static const int N_STR_SIZE = 30;
+static const int LOF_INITIAL_SIZE = 1;
+
+long read_from_cache(char* path) {
+ char buf[BUF_SIZE];
+ int fd = open(path, O_RDONLY);
+
+ if (fd == -1) {
+ close(fd);
+ return 0;
+ }
+
+ if (!read(fd, buf, BUF_SIZE)) {
+ close(fd);
+ return 0;
+ }
+
+ close(fd);
+ return strtol(buf, NULL, 10);
+}
+
+long write_to_cache(char* path, long result) {
+ char buf[BUF_SIZE];
+ int fd = open(path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
+ sprintf(buf, "%ld", result);
+
+ if (fd == -1) {
+ close(fd);
+ return 0;
+ }
+
+ if (!write(fd, buf, strlen(buf))) {
+ close(fd);
+ return 0;
+ }
+
+ close(fd);
+ return 1;
+}
+
+long init_cache() {
+ char path_to_0[PATH_SIZE];
+ char path_to_1[PATH_SIZE];
+ sprintf(path_to_0, "%s/%s", CACHE_DIR, "0");
+ sprintf(path_to_1, "%s/%s", CACHE_DIR, "1");
+
+ if (!write_to_cache(path_to_0, 0)) {
+ return 0;
+ }
+
+ if (!write_to_cache(path_to_1, 1)) {
+ return 0;
+ }
+
+ return 1;
+}
+
+long get_list_of_files(long** list_of_files, long curr_size) {
+ mkdir("cache", 0777);
+ struct dirent *d;
+ DIR* cache_dir = opendir(CACHE_DIR);
+
+ if (!cache_dir) {
+ closedir(cache_dir);
+ return 0;
+ }
+
+ init_cache();
+
+ while ((d = readdir(cache_dir)) != NULL) {
+ if (d->d_type == DT_REG) {
+ *((*list_of_files) + curr_size - 1) = strtol(d->d_name, NULL, 10);
+ curr_size++;
+ *list_of_files = realloc(*list_of_files, curr_size * sizeof(long));
+ }
+ }
+
+ closedir(cache_dir);
+ return curr_size - 1;
+}
+
+
+int is_in_cache(long* list_of_files, long size_of_lof, long n) {
+ for (int i = 0; i < size_of_lof; i++) {
+ if (list_of_files[i] == n) return 1;
+ }
+ return 0;
+}
+
+long fib(long n) {
+ char n_str[N_STR_SIZE];
+ char path[PATH_SIZE];
+ long* list_of_files = malloc(sizeof(long));
+ long size_of_lof = get_list_of_files(&list_of_files, LOF_INITIAL_SIZE);
+
+ sprintf(n_str, "%ld", n);
+ sprintf(path, "%s/%s", CACHE_DIR, n_str);
+
+ if (is_in_cache(list_of_files, size_of_lof, n)) {
+ free(list_of_files);
+ return read_from_cache(path);
+ }
+
+ free(list_of_files);
+ long to_cache = fib(n-1) + fib(n-2);
+ write_to_cache(path, to_cache);
+ return to_cache;
+}
+
+
+int main() {
+ printf("FIB RESULT: %ld\n", fib(13));
+}
diff --git a/entries/adirar111/wasm/fib.wasm b/entries/adirar111/wasm/fib.wasm
new file mode 100644
index 0000000..6a93250
--- /dev/null
+++ b/entries/adirar111/wasm/fib.wasm
Binary files differ
diff --git a/entries/adirar111/wasm/fib.wat b/entries/adirar111/wasm/fib.wat
new file mode 100644
index 0000000..6ec9b69
--- /dev/null
+++ b/entries/adirar111/wasm/fib.wat
@@ -0,0 +1,27 @@
+;; iterative fib(n) in web assembly text format (wat)
+;; this gets compiled to the fib.wasm binary
+;; and gets fetched via a data URL in index.js
+(module
+ (export "fib" (func $fib))
+ (func $fib (param $n i32) (result i32)
+ (local $last i32)
+ (local $sum i32)
+ (local $i i32)
+ (local $tmp i32)
+ (if (i32.lt_s (local.get $n) (i32.const 2))
+ (return (local.get $n))
+ )
+ (local.set $last (i32.const 0))
+ (local.set $sum (i32.const 1))
+ (local.set $i (i32.const 2))
+ (local.set $n (i32.add (local.get $n) (i32.const 1)))
+ (loop $loop
+ (local.set $tmp (local.get $sum))
+ (local.set $sum (i32.add (local.get $sum) (local.get $last)))
+ (local.set $last (local.get $tmp))
+ (local.set $i (i32.add (local.get $i) (i32.const 1)))
+ (br_if $loop (i32.lt_s (local.get $i) (local.get $n)))
+ )
+ (return (local.get $sum))
+ )
+)
diff --git a/entries/adirar111/wasm/index.html b/entries/adirar111/wasm/index.html
new file mode 100644
index 0000000..8357a55
--- /dev/null
+++ b/entries/adirar111/wasm/index.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>wasm fib</title>
+ <link
+ href="https://fonts.googleapis.com/css2?family=Source+Code+Pro:wght@600;700&display=swap"
+ rel="stylesheet"
+ />
+ <link rel="stylesheet" href="style.css" />
+ </head>
+ <body>
+ <p>go ahead, type a number</p>
+ <div id="input-area">
+ <input id="input" type="text" autocomplete="off" />
+ <button id="compute">compute!</button>
+ </div>
+ <p>fib.wasm says:</p>
+ <span id="result"></span>
+ <script src="index.js"></script>
+ </body>
+</html>
diff --git a/entries/adirar111/wasm/index.js b/entries/adirar111/wasm/index.js
new file mode 100644
index 0000000..667ff82
--- /dev/null
+++ b/entries/adirar111/wasm/index.js
@@ -0,0 +1,23 @@
+const wasmBinaryBase64 = "AGFzbQEAAAABBgFgAX8BfwMCAQAHBwEDZmliAAAKRwFFAQR/IABBAkgEQCAADwtBACEBQQEhAkECIQMgAEEBaiEAA0AgAiEEIAIgAWohAiAEIQEgA0EBaiEDIAMgAEgNAAsgAg8LACgEbmFtZQEGAQADZmliAhkBAAUAAW4BBGxhc3QCA3N1bQMBaQQDdG1w"
+const wasmBinaryDataURL = `data:application/wasm;base64,${wasmBinaryBase64}`;
+
+WebAssembly.instantiateStreaming(fetch(wasmBinaryDataURL)).then(
+ (wasmObj) => {
+ const {fib: fibWasm} = wasmObj.instance.exports
+ const fib = (event) => {
+ event.preventDefault()
+ const input = document.getElementById("input")
+ const result = document.getElementById("result")
+ const n = parseInt(input.value)
+ if (isNaN(n)) {
+ return result.textContent = "that wasn't very wasm of you"
+ }
+ if (n < 0) {
+ return result.textContent = "why are you being so negative"
+ }
+ result.textContent = fibWasm(n)
+ }
+ const button = document.getElementById("compute")
+ button.onclick = fib
+ }
+);
diff --git a/entries/adirar111/wasm/style.css b/entries/adirar111/wasm/style.css
new file mode 100644
index 0000000..8ab403f
--- /dev/null
+++ b/entries/adirar111/wasm/style.css
@@ -0,0 +1,27 @@
+html {
+ background: #5a46ee;
+ color: #fff;
+ font-family: "Source Code Pro"
+}
+
+body {
+ display: flex;
+ flex-direction: column;
+ justify-content: center;
+ align-items: center;
+}
+
+#input-area {
+ display: flex;
+ flex-direction: row;
+ justify-content: space-between;
+}
+
+#input {
+ height: 40px;
+ font-size: 30px
+}
+
+#result {
+ font-size: 50px
+}