-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from illourr/start-setting-up-blog
Started setting up blog
- Loading branch information
Showing
23 changed files
with
349 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,95 @@ | ||
type NodeVal = number; | ||
class ListNode { | ||
val: NodeVal; | ||
next: ListNode | null; | ||
constructor(value: NodeVal) { | ||
this.val = value; | ||
class MyNode { | ||
val: number; | ||
next: MyNode | null; | ||
constructor(val: number) { | ||
this.val = val; | ||
this.next = null; | ||
} | ||
} | ||
|
||
class MyLinkedList { | ||
list: ListNode | null; | ||
head: MyNode | null; | ||
length: number; | ||
constructor() { | ||
this.list = null; | ||
} | ||
valueOf() { | ||
return this.list; | ||
this.head = null; | ||
this.length = 0; | ||
} | ||
|
||
get(index: number): number { | ||
let position = 0; | ||
let currentNode = this.list; | ||
while (position !== index && currentNode !== null) { | ||
currentNode = currentNode.next; | ||
position++; | ||
if (index < 0 || index >= this.length) { | ||
return -1; | ||
} | ||
if (position === index && currentNode !== null) { | ||
return currentNode.val; | ||
|
||
let current = this.head; | ||
for (let i = 0; i < index; i++) { | ||
current = current!.next; | ||
} | ||
return -1; | ||
|
||
return current!.val; | ||
} | ||
|
||
addAtHead(val: number): void { | ||
let next = this.list; | ||
const newNode = new ListNode(val); | ||
if (next !== null) { | ||
newNode.next = next; | ||
} | ||
this.list = newNode; | ||
const newNode = new MyNode(val); | ||
newNode.next = this.head; | ||
this.head = newNode; | ||
this.length++; | ||
} | ||
|
||
addAtTail(val: number): void { | ||
if (this.list == null) { | ||
const newNode = new MyNode(val); | ||
|
||
if (!this.head) { | ||
this.head = newNode; | ||
} else { | ||
let current = this.head; | ||
while (current.next) { | ||
current = current.next; | ||
} | ||
current.next = newNode; | ||
} | ||
|
||
this.length++; | ||
} | ||
|
||
addAtIndex(index: number, val: number): void { | ||
if (index < 0 || index > this.length) { | ||
return; | ||
} | ||
|
||
let currentNode = this.list; | ||
while (currentNode.next) { | ||
currentNode = currentNode.next; | ||
if (index === 0) { | ||
this.addAtHead(val); | ||
} else if (index === this.length) { | ||
this.addAtTail(val); | ||
} else { | ||
let current = this.head; | ||
for (let i = 0; i < index - 1; i++) { | ||
current = current!.next; | ||
} | ||
|
||
const newNode = new MyNode(val); | ||
newNode.next = current!.next; | ||
current!.next = newNode; | ||
|
||
this.length++; | ||
} | ||
currentNode.next = new ListNode(val); | ||
} | ||
|
||
addAtIndex(index: number, val: number): void {} | ||
deleteAtIndex(index: number): void { | ||
if (index < 0 || index >= this.length) { | ||
return; | ||
} | ||
|
||
deleteAtIndex(index: number): void {} | ||
} | ||
if (index === 0) { | ||
this.head = this.head!.next; | ||
} else { | ||
let current = this.head; | ||
for (let i = 0; i < index - 1; i++) { | ||
current = current!.next; | ||
} | ||
|
||
/** | ||
* Your MyLinkedList object will be instantiated and called as such: | ||
* var obj = new MyLinkedList() | ||
* var param_1 = obj.get(index) | ||
* obj.addAtHead(val) | ||
* obj.addAtTail(val) | ||
* obj.addAtIndex(index,val) | ||
* obj.deleteAtIndex(index) | ||
*/ | ||
|
||
const list = new MyLinkedList(); | ||
|
||
list.addAtHead(2); | ||
list.addAtTail(5); | ||
list.addAtHead(1); | ||
list.addAtTail(7); | ||
list.addAtTail(9); | ||
|
||
console.log(list.get(3)); | ||
current!.next = current!.next!.next; | ||
} | ||
|
||
this.length--; | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
86 changes: 86 additions & 0 deletions
86
learning/multithreaded-javascript/ch1-c-threads/happycoin-threads.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#include <inttypes.h> | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
#include <pthread.h> | ||
|
||
struct happy_result { | ||
size_t count; | ||
uint64_t * nums; | ||
}; | ||
|
||
uint64_t random64(uint32_t * seed) { | ||
uint64_t result; | ||
uint8_t * result8 = (uint8_t *)&result; | ||
for (size_t i = 0; i< sizeof(result); i++) { | ||
result8[i] = rand_r(seed); | ||
} | ||
return result; | ||
} | ||
|
||
uint64_t sum_digits_squared(uint64_t num) { | ||
uint64_t total = 0; | ||
while (num > 0) { | ||
uint64_t num_mod_base = num % 10; | ||
total += num_mod_base * num_mod_base; | ||
num = num / 10; | ||
} | ||
return total; | ||
} | ||
|
||
bool is_happy(uint64_t num) { | ||
while (num != 1 && num != 4) { | ||
num = sum_digits_squared(num); | ||
} | ||
return num == 1; | ||
} | ||
|
||
bool is_happycoin(uint64_t num) { | ||
return is_happy(num) && num % 10000 == 0; | ||
} | ||
|
||
void * get_happycoins(void * arg) { | ||
int attempts = *(int *)arg; | ||
int limit = attempts/10000; | ||
uint32_t seed = time(NULL); | ||
uint64_t * nums = malloc(limit * sizeof(uint64_t)); | ||
struct happy_result * result = malloc(sizeof(struct happy_result)); | ||
result->nums = nums; | ||
result->count = 0; | ||
for(int i = 1; i < attempts; i++) { | ||
if (result->count == limit) { | ||
break; | ||
} | ||
uint64_t random_num = random64(&seed); | ||
if (is_happycoin(random_num)) { | ||
result->nums[result->count++] = random_num; | ||
} | ||
} | ||
return (void *)result; | ||
} | ||
|
||
#define THREAD_COUNT 4 | ||
|
||
int main() { | ||
pthread_t thread [THREAD_COUNT]; | ||
|
||
int attempts = 10000000/THREAD_COUNT; | ||
int count = 0; | ||
for(int i = 0; i < THREAD_COUNT; i++) { | ||
pthread_create(&thread[i], NULL, get_happycoins, &attempts); | ||
} | ||
|
||
for(int j = 0; j < THREAD_COUNT; j++) { | ||
struct happy_result * result; | ||
pthread_join(thread[j], (void **)&result); | ||
count += result->count; | ||
for(int k = 0; k < result->count; k++) { | ||
printf("%" PRIu64 " ", result->nums[k]); | ||
} | ||
free(result->nums); | ||
free(result); | ||
} | ||
printf("\ncount %d\n", count); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
learning/multithreaded-javascript/ch2-shared-workers/blue.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<html> | ||
<head> | ||
<title>Shared Workers Blue</title> | ||
<script src="blue.js"></script> | ||
</head> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
console.log("blue.js"); | ||
|
||
const worker = new SharedWorker("shared-worker.js"); | ||
|
||
worker.port.onmessage = (event) => { | ||
console.log("EVENT", event.data); | ||
}; |
6 changes: 6 additions & 0 deletions
6
learning/multithreaded-javascript/ch2-shared-workers/red.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<html> | ||
<head> | ||
<title>Shared Workers Red</title> | ||
<script src="red.js"></script> | ||
</head> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
console.log("red.js"); | ||
|
||
const worker = new SharedWorker("shared-worker.js"); | ||
|
||
worker.port.onmessage = (event) => { | ||
console.log("EVENT", event.data); | ||
}; |
18 changes: 18 additions & 0 deletions
18
learning/multithreaded-javascript/ch2-shared-workers/shared-worker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const ID = Math.floor(Math.random() * 999999); | ||
console.log("shared-worker.js", ID); | ||
|
||
const ports = new Set(); | ||
|
||
self.onconnect = (event) => { | ||
const port = event.ports[0]; | ||
ports.add(port); | ||
console.log("CONN", ID, ports.size); | ||
|
||
port.onmessage = (event) => { | ||
console.log("MESSAGE", ID, event.data); | ||
|
||
for (let p of ports) { | ||
p.postMessage([ID, event.data]); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<html> | ||
<head> | ||
<title>Web Workers Hello World</title> | ||
<script src="main.js"></script> | ||
</head> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
console.log("hello from main.js"); | ||
|
||
const worker = new Worker("worker.js"); | ||
worker.onmessage = (msg) => { | ||
console.log("message received from worker:", msg.data); | ||
}; | ||
|
||
worker.postMessage("message sent to worker"); | ||
|
||
console.log("hello from end of main.js"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
console.log("hello from worker.js"); | ||
|
||
self.onmessage = (msg) => { | ||
console.log("message from main:", msg.data); | ||
postMessage("message sent from worker"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.