移除節點的程式碼:
void st_remove(struct st_node **root, struct st_node *del)
{
if (st_right(del)) {
struct st_node *least = st_first(st_right(del));
if (del == *root)
*root = least;
AAAA; //st_replace_right(del, least)
BBBB; //st_update(root, least)
return;
}
if (st_left(del)) {
struct st_node *most = st_last(st_left(del));
if (del == *root)
*root = most;
CCCC; //st_replace_left(del, most)
DDDD; //st_update(root, most)
return;
}
if (del == *root) {
*root = 0;
return;
}
/* empty node */
struct st_node *parent = st_parent(del);
if (st_left(parent) == del)
st_left(parent) = 0;
else
st_right(parent) = 0;
EEEE; //st_update(root, parent)
}
由小到大印出的程式碼:
static void __treeint_dump(struct st_node *n, int depth)
{
if (!n)
return;
__treeint_dump(FFFF, depth + 1); //st_left(n)
struct treeint *v = treeint_entry(n);
printf("%d\n", v->value);
__treeint_dump(GGGG, depth + 1); //st_right(n)
}
1.解釋
2.特別跟 AVL tree 和 red-black tree 相比
3.設計效能評比程式,探討上述程式碼和 red-black tree 效能落差
#include <stdint.h>
static inline uintptr_t align_up(uintptr_t sz, size_t alignment)
{
uintptr_t mask = alignment - 1;
if ((alignment & mask) == 0) { /* power of two? */
return MMMM; //sz&(~mask)
}
return (((sz + mask) / alignment) * alignment);
}
1.說明 Material_nonimplication
2.在 Linux 核心原始程式碼找出類似 align_up
的程式碼,並舉例說明其用法
/* Thread-callable quicksort. */
static void *qsort_thread(void *p)
{
struct qsort *qs, *qs2;
int i;
struct common *c;
qs = p;
c = qs->common;
again:
/* Wait for work to be allocated. */
verify(pthread_mutex_lock(&qs->mtx_st));
while (qs->st == ts_idle)
verify(HHHH);
verify(pthread_mutex_unlock(&qs->mtx_st));
if (qs->st == ts_term) {
return NULL;
}
assert(qs->st == ts_work);
qsort_algo(qs);
verify(pthread_mutex_lock(&c->mtx_al));
qs->st = ts_idle;
c->idlethreads++;
if (c->idlethreads == c->nthreads) {
for (i = 0; i < c->nthreads; i++) {
qs2 = &c->pool[i];
if (qs2 == qs)
continue;
verify(pthread_mutex_lock(&qs2->mtx_st));
qs2->st = ts_term;
verify(JJJJ);
verify(pthread_mutex_unlock(&qs2->mtx_st));
}
verify(pthread_mutex_unlock(&c->mtx_al));
return NULL;
}
verify(pthread_mutex_unlock(&c->mtx_al));
goto again;
}
回饋表單 https://docs.google.com/forms/d/e/1FAIpQLSchjBeTO5la96o1rMkUsYcTqPAAWdNqr1oHraqHkUiCHtraVw/viewform
題目 https://hackmd.io/@sysprog/linux2023-summer-quiz0
1.更新python版本
Jul 14, 2024Reference: 陳彥甫 Leetcode1234 Original code - Remove Linked List Elements I choose the Problem Remove Linked List Elements since linked-list is an ubiquitous topic in computer science so I wonder how it works in assembly. Also, I find some mistakes in his either c code or assembly. Hence, I would like to correct it and try to speedup the execution with optimizations. c++ code rewritten from 陳彥甫 ListNode* removeElements(ListNode* head, int val) { ListNode *cur = head, *prev = NULL; while (cur) { if (cur->val == val) {
Jan 13, 2023RV32M is a variation of the RISC-V instruction set architecture (ISA) that is designed for faster mathematical computation and provides balance between performance and code density. It is a extension of the RV32I base ISA and includes the following subsets:Multiplication operations are significant for many applications in the domain of digital signal processing, image processing, scientific computing, and many more. Division operations are needed for some scientific computing and special purpose operations like graphics rendering, etc. SPU32 ("Small Processing Unit 32"), a compact RISC-V processor implementing the RV32I instruction set, also includes some peripherals. This project is written in Verilog and is designed to be synthesizable using the yosys. YOSYS is an opensource framework for RTL synthesis, that is translate HDL into gate-level netlist implementation. We need the Verilog/SystemVerilog simulators such as Verilator, supporting RISC-V simulation. Verilator accepts Verilog or SystemVerilog, and compiles HDL code into a much faster optimized and optionally thread-partitioned model, which is in turn wrapped inside a C++/SystemC module. Setup environment
Jan 13, 2023蟻群演算法 ACO(Ant Colony Optimization)是模擬自然界中蟻群尋找食物的仿生演算法。在螞蟻移動的過程中,不斷嘗試最短路徑,並會在走過的路上留下特殊的費洛蒙,螞蟻之間可以感知這種物質濃度。如果路徑上的費洛蒙量較高,代表走過該路徑的螞蟻較多或是最新才走過,則後續的螞蟻有較大機率亦選擇此路徑。 在旅行商人的問題中,所有路徑都嘗試的方式為$\frac{n!}{2}$,即便使用平行運算進行暴力破解也需要花費許多時間(例如:最小的作業有17個城市需要3.5568743e+14次)。利用ACO演算法,由於每一次迭代就有m隻螞蟻依照規定走完所有的城市,並傳遞下訊息,可以優化下次選擇過程,讓我們在“猜測”路徑更有效率。 調整權重參數 $\alpha$、$\beta$ 和 $\rho$ 可以改變最終結果: $\alpha$ : 決定局部資訊影響性,提高可以讓收斂速度變快,但也可能不是最佳解 $\beta$ : 距離影響決策程度,提高提高會讓選擇變得隨機 $\rho$ : 訊息揮發速度,較小的話可以保留長時間訓系進行比較(但會慢一些)
Jan 9, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up