# Notes for Radish
## WSL
### Command
```
# 回主目錄 (home folder)
$ cd
neo@bala:/mnt/c/Users/neowong$ cd
neo@bala:~$
# 印出目前目錄
$ pwd
neo@bala:~$ pwd
/home/neo
# 到 My Document
neo@bala:~$ cd /mnt/c/Users/neo/Documents/
neo@bala:/mnt/c/Users/neo/Documents$
```
### Access WSL Linux file from Windows
```
in 檔案總管 : \\WSL$
```
## unordered_map sample
```C=
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <unordered_map>
#include <set>
#include <algorithm>
#include <cmath>
#include <cwctype>
#include <locale>
using namespace std;
//#define DEBUG
struct xxx {
int data;
struct xxx* parent;
};
int main(int argc, const char * argv[] ) {
unordered_map<int, xxx*> in;
struct xxx *a = new xxx;
struct xxx *b = new xxx;
struct xxx *c = NULL;
// Add 0, and it's map to xxx
a->data = 0;
a->parent = NULL; // means a is a root
in[a->data] = a;
// b's parent is a, and add 0
b->data = 2;
b->parent = a;
in[b->data] = b;
// Search 2, and print out
c = in[2];
cout << c->data << endl; // should be 2
// get its parent
c = c->parent;
cout << c->data << endl; // should be 0
// loop map
// will be
// 2:2
// 0:0
for (auto& it: in) {
cout << it.first << ":" << it.second->data << endl;
}
return 0;
}
```
# Link list
### Link list in C
```C=
#include <stdio.h>
#include <stdlib.h>
// define a node struct
struct node {
int data;
struct node *next;
};
typedef struct node node_t;
// new a node with data assigned
node_t * new_node(int d) {
node_t *t;
t=(node_t*)malloc(sizeof(node_t));
if(t==NULL)
return NULL;
t->data = d;
t->next = NULL;
return t;
}
// add node into end of link list
void add_node(node_t* h, node_t* n) {
node_t * c = h;
while (c->next != NULL) {
c = c->next;
}
/* now we can add a new variable */
c->next = n;
}
// start from head loop list
void print_list(node_t * n) {
node_t * c = n;
while (c != NULL) {
printf("%d\n", c->data);
c = c->next;
}
}
// find node - sequence search
node_t * find_node(node_t *h, int d) {
node_t * c = h;
while (c != NULL) {
printf( "[%d]", c->data);
if(c->data == d)
return c;
c = c->next;
}
return NULL;
}
int main( int argc, char** argv){
node_t *head = NULL;
node_t *n = NULL;
// new a head
head = new_node(0);
if(head!=NULL)
printf( "Head data : %d\n", head->data);
// add two node
n = new_node(1);
add_node(head,n);
n = new_node(2);
add_node(head,n);
// print list
print_list(head);
// find
n = find_node(head,2);
if(n!=NULL)
printf( "2 found\n");
return 0;
}
```
### Link list in C w/ binary search
```C=
#include <stdio.h>
#include <stdlib.h>
// define a node struct
typedef struct node {
int data;
struct node *next;
} node_t;
node_t * new_node(int d) {
node_t *t;
t=(node_t*)malloc(sizeof(node_t));
if(t==NULL)
return NULL;
t->data = d;
t->next = NULL;
return t;
}
// Function to insert a node into the sorted linked list
void add_node_sorted(node_t** h, node_t*n ) {
if (*h == NULL || n->data < (*h)->data) {
n->next = *h;
*h = n;
} else {
node_t * c = *h;
while (c->next != NULL && c->next->data < n->data) {
c = c->next;
}
n->next = c->next;
c->next = n;
}
}
void add_node(node_t* h, node_t* n) {
node_t * c = h;
while (c->next != NULL) {
c = c->next;
}
/* now we can add a new variable */
c->next = n;
}
void print_list(node_t * n) {
node_t * c = n;
while (c != NULL) {
printf("%d\n", c->data);
c = c->next;
}
}
node_t * find_node(node_t *h, int d) {
node_t * c = h;
while (c != NULL) {
printf( "[%d]", c->data);
if(c->data == d)
return c;
c = c->next;
}
return NULL;
}
// Function for faster search in the sorted linked list
node_t * binarySearch(node_t *h, int data) {
node_t *low = h;
node_t *high = NULL;
while (low != high) {
node_t *mid = low;
while (mid->next != high) {
mid = mid->next;
}
int midValue = mid->data;
if (midValue == data) {
return mid;
} else if (midValue < data) {
low = mid->next;
} else {
high = mid;
}
}
return NULL; // Element not found
}
int main( int argc, char** argv){
node_t *head = NULL;
node_t *n = NULL;
add_node_sorted( &head, new_node(5) );
add_node_sorted( &head, new_node(2) );
add_node_sorted( &head, new_node(6) );
add_node_sorted( &head, new_node(0) );
add_node_sorted( &head, new_node(8) );
print_list(head);
n = binarySearch(head,6);
if(n==NULL)
printf( "no found\n");
else
printf("found: %d\n", n->data);
return 0;
}
```
# tips-n-tricks
### Use scanf read unknow number of input
```C=
int scanf_command( int *op1, int *op2)
{
char str[20];
while (scanf("%s", str)==1)
{
if(strcmp(str,"union")==0) {
if(scanf("%d",op1)!=1) continue;
if(scanf("%d",op2)!=1) continue;
return 0; //union
} else if(strcmp(str,"find")==0) {
if(scanf("%d",op1)!=1) continue;
return 1; //find
} else if(strcmp(str,"same")==0) {
if(scanf("%d",op1)!=1) continue;
if(scanf("%d",op2)!=1) continue;
return 2; //same
}
}
return -1;
}
int main()
{
int op1, op2, cmd;
while(1){
cmd = scanf_command( &op1, &op2);
if(cmd==1)
printf( "%d: %d\n", cmd, op1 );
else
printf( "%d: %d,%d\n", cmd, op1, op2 );
}
return 0;
}
```
### Read stdin sample
<pre>
</pre>
### Read file sample
<pre>
</pre>
### Use vscode with WSL
<pre>
$ cd <workfolder>
$ code .
</pre>
# Linux shell command sample
### Find string in a text file
```BASH=
$ grep -e \ 11\ -e 7763 -e 4958 input_02.txt
union 7763 4958
union 8738 4958
union 11 7763
union 7909 4958
find 7763
same 7763 7133
union 4958 7070
```
# git cheat book
### 檢視提交的歷史記錄
```
$ git log
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
Date: Mon Mar 17 21:52:11 2008 -0700
```
### 顯示每筆提交所做的修改內容
```
$ git log -p
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
Date: Mon Mar 17 21:52:11 2008 -0700
changed the version number
diff --git a/Rakefile b/Rakefile
index a874b73..8f94139 100644
--- a/Rakefile
+++ b/Rakefile
@@ -5,7 +5,7 @@ require 'rake/gempackagetask'
spec = Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = "simplegit"
- s.version = "0.1.0"
+ s.version = "0.1.1"
```
### 取得一個 Git 倉儲 - 在現有資料夾中初始化倉儲
```
$ git init
$ git add <file>
$ git commit -m "init balabala"
```
### 取得一個 Git 倉儲 - 克隆現有的倉儲
```
$ git clone https://github.com/bala/bala.git
```
### 檢查你的檔案狀態
```
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
```
### 比對尚未預存的修改
```
$ git diff
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 8ebb991..643e24f 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -65,7 +65,8 @@ branch directly, things can get messy.
Please include a nice description of your changes when you submit your PR;
if we have to read the whole diff to figure out why you're contributing
in the first place, you're less likely to get feedback and have your change
-merged in.
+merged in. Also, split your changes into comprehensive chunks if your patch is
```
### 提交你的修改
```
$ git commit -m "balabala"
```
### Reference link
* https://mycollegenotebook.medium.com/c%E8%AA%9E%E8%A8%80%E7%AD%86%E8%A8%98-%E6%8C%87%E6%A8%99-pointers-d28edcdd6283
* [Git介紹](https://git-scm.com/book/zh-tw/v2)
### RISC-V
* https://wen00072.github.io/blog/2015/12/10/about-inline-asm/
* https://msyksphinz-self.github.io/riscv-isadoc/html/index.html