diff --git a/js-bench/for_vs_map.js b/js-bench/for_vs_map.js new file mode 100644 index 0000000..ed41e0b --- /dev/null +++ b/js-bench/for_vs_map.js @@ -0,0 +1,34 @@ +const x = []; +for (let i = 0; i < 50_000_000; i++) { + x[i] = Math.random(); +} + +const for_test_list = [...x] +const for_timings = [] +for (let i = 0; i < 10; i++) { + const startTime = performance.now() + for (let i = 0; i < for_test_list.length; i++) { + for_test_list[i] * 2 + } + const timeElapsed = performance.now() - startTime + for_timings.push(timeElapsed) + console.log(`for#${i + 1} time: ${timeElapsed.toFixed(4)} ms`) +} +console.log(`Average 'for' time: ${(for_timings.reduce((a,b) => a+b)/for_timings.length).toFixed(4)} ms`) +print_xvals(for_test_list) + +const map_test_list = [...x] +const map_timings = [] +for (let i = 0; i < 10; i++) { + const startTime = performance.now() + map_test_list.map(val => val*2) + const timeElapsed = performance.now() - startTime + map_timings.push(timeElapsed) + console.log(`map#${i + 1} time: ${timeElapsed.toFixed(4)}`) +} +console.log(`Average 'map' time: ${(map_timings.reduce((a,b) => a+b)/for_timings.length).toFixed(4)} ms`) +print_xvals(map_test_list) + +function print_xvals(x) { + console.log(x[0], x[Math.floor(x.length / 2)], x[x.length - 1]) +} diff --git a/linked-list-odin/linked-list-odin b/linked-list-odin/linked-list-odin new file mode 100644 index 0000000..81efce5 Binary files /dev/null and b/linked-list-odin/linked-list-odin differ diff --git a/linked-list-odin/linked-list.odin b/linked-list-odin/linked-list.odin new file mode 100644 index 0000000..05425f1 --- /dev/null +++ b/linked-list-odin/linked-list.odin @@ -0,0 +1,44 @@ +package main + +import "core:fmt" +import ll "linked-list" + +main :: proc() { + + list := ll.init(f32) + list.data = 1 + fmt.println(list) + defer ll.deinit(list) + + + ll.append_value(list, 5) + fmt.println("append 5", ll.to_string(list)) + ll.append_value(list, 5) + fmt.println("append 5", ll.to_string(list)) + ll.remove(&list, 5) + fmt.println("remove 5", ll.to_string(list)) + ll.append_value(list, 5) + fmt.println("append 5", ll.to_string(list)) + ll.remove_all(&list, 5) + fmt.println("remove_all 5", ll.to_string(list)) + + ll.prepend_value(&list, 101) + fmt.println("prepend 101", ll.to_string(list)) + ll.remove(&list, 101) + fmt.println("remove 101", ll.to_string(list)) + + temp :: struct { + lol: i32, + yes: bool + } + struct_ll := ll.init(temp) + defer ll.deinit(struct_ll) + struct_ll.data = {lol=1} + fmt.println(ll.head(struct_ll)) + fmt.println(ll.tail(struct_ll)) + for i in 0..<100_000 { + ll.append_value(struct_ll, temp { lol = i32(i), yes = i % 2 == 0}) + } + fmt.println(ll.to_string(struct_ll)) + fmt.println(ll.tail(struct_ll)) +} diff --git a/linked-list-odin/linked-list/list.odin b/linked-list-odin/linked-list/list.odin new file mode 100644 index 0000000..b7955c4 --- /dev/null +++ b/linked-list-odin/linked-list/list.odin @@ -0,0 +1,125 @@ +package linked_list +import "core:fmt" + +node :: struct($Value: typeid) { + data: Value, + next: ^node(Value), +} + +to_string :: proc(head: ^node($T)) -> string { + curr := head + out: string + counter := 0 + out = fmt.tprintf("[%v]", curr.data) + for curr.next != nil{ + curr = curr.next + if counter >= 8 { + out = fmt.tprintf("%s -> ... -> [%v]", out, tail(curr.next)) + break + } + out = fmt.tprintf("%s -> [%v]", out, curr.data) + counter += 1 + } + + return out +} + +remove :: proc(head: ^^node($T), value: T) { + curr := head^ + pp := head + for curr != nil && curr.data != value { + pp = &curr.next + curr = curr.next + } + + if curr == nil do return + + pp^ = curr.next + free(curr) +} + +remove_all :: proc(head: ^^node($T), value: T) { + curr := head^ + pp := head + for { + for curr != nil && curr.data != value { + pp = &curr.next + curr = curr.next + } + if curr != nil { + pp^ = curr.next + free(curr) + curr = pp^ + } else { + return + } + } +} + +prepend_value :: proc(head: ^^node($T), value: T) -> ^node(T) { + n := prepend(head) + n.data = value + return n +} + +prepend :: proc(head: ^^node($T)) -> ^node(T) { + n := new(node(T)) + n.next = head^ + head^ = n + return n +} + +append_value :: proc(head: ^node($T), value: T) -> ^node(T) { + n := append(head) + n.data = value + return n +} + +append :: proc(head: ^node($T)) -> ^node(T) { + curr := head + for curr.next != nil { + curr = curr.next + } + curr.next = new(node(T)) + return curr.next +} + +head :: proc(h: ^node($T)) -> T { + return h.data +} + +tail :: proc(head: ^node($T)) -> T { + curr := head + for curr.next != nil { + curr = curr.next + } + return curr.data +} + +// counting starts from 0 +nth :: proc(head: ^node($T), n: u64) -> T { + curr := head + counter := 0 + for curr != nil { + if counter == n { + return curr.data + } + curr = curr.next + counter += 1 + } + return nil +} + +deinit :: proc(head: ^node($T)) { + curr := head + next: ^node(T) + for curr != nil { + next = curr.next + free(curr) + curr = next + } +} + +init :: proc($T: typeid) -> (head: ^node(T)) { + return new(node(T)) +}