跳到主要內容

huffman coding 實作

Github link

huffman 實作我使用了幾個 data structure
1. map
2. min heap
3. tree

主要分3個階段完成huffman coding 所需要的 data structure,對於之後的encode 跟decode是很重要的
Phase 1 : read file and record the frequency of each character (unordered_map)
Phase 2 : using map to construct min heap (min heap)
Phase 3 : using min-heap to create huffman coding tree (tree)


在講 encode 前,先說明huffman的概念

Concept :

wiki link

1. 無失真壓縮
2. 常出現的字,用最少的bit來描述
3. 應用在檔案壓縮

encode algorithm :


1. construct huffman coding tree
    if there are more than 2 in min heap, we dequeue two item.
    add these two frequency and create a new node as virtual item.
    make these two item be new node's left/right child. then push back to min-heap
    go repeat until there are no more than 2
illustrated :
https://www.google.com/search?q=huffman+coding&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiQ4tKOlO7iAhXIw7wKHfESDUAQ_AUIECgB&biw=1024&bih=462#imgrc=QMXyN8ycoMLfrM:

2. replace test character to huffman code

decode algorithm :


1. using huffman tree, scan all the bit from encoded file, and traverse the tree.
    when we get '0' go left child, '1' go right child, until reach leaf node, and then we know the original character.

encode 需注意的地方:

為了達到壓縮,我們要確保,寫黨時候一定是以 binary的方式寫入。
[how]
https://stackoverflow.com/questions/11249859/how-to-save-a-byte-type-char-array-data-to-a-file-in-c

git  操作筆記


如果想對已經push 的 同一筆commit 改寫,怎麼做 ?

git commit --amend 
git push -f // 強制覆寫 (危險!)


留言

這個網誌中的熱門文章

編譯入門 - gcc toolchain

程式編譯的過程是很複雜的,初學者在學習寫程式的時候,大部份是透過IDE來編譯程式的,所以將內部的編譯流程都隱藏了起來。其實過程是很複雜的,我打算先以觀察gcc 編譯程式的的過程以及中間的產物來開始學習。 以下資料是透過閱讀 「 程式設計師的自我修養 - 連結、載入、程式庫 」並在自己的電腦上驗證結果。 先透過一個最簡單的入門程式開始學起, Hello World !! 接下來我們使用 gcc 來編譯它巴 $gcc hello.c $./a.out Hello World! 上面看似簡單的編譯指令,其實中間包括了4個階段 前置處理 (Preprocessing) 編譯 (Compilation) 組譯 (Assembly) 連結 (Linking) 圖示 : 下面我會使用 gcc 一步一步的編譯 Hello World 這隻程式,將結果呈獻出來。

藉由select & poll來學習 Linux device driver programming

最近因為想學習撰寫一隻 Linux device driver,所以先藉由 Linux Device Driver Programming 驅動程式設計 ,這本書裡的select & poll 的範例程式開始學習。但是因為這本書當時再寫的時候,kernel版本大致分為 2.4 和 2.6,版本比較舊,有些函式已有變動,所以有做一些修改。(我的Kernel版本為4.2版) 驅動程式筆記與程式碼講解 函式加上 static ,讓命名空間限制在檔案內。不過在這裡就算不加static也不會影響kernel整體的符號表。這是只緊限於kernel module的時候,也就是動態載入的驅動程式來說。如果是希望其他module能夠呼叫的話,就必須要使用EXPORT_SYMBOL來明確匯出函式。 驅動程式內部能夠使用到 printf(),因為kernel space沒有直接對應的console (鍵盤,畫面)。但是還是有可代用的function printk()。它輸出的資料會跑到kernel buffer內。kernel buffer 可以用 dmesg 指令查閱,不過空間才只有 128KB(default),而且是環狀的形式(所以心資料會蓋過最舊的資料)。因此不能一直把資料保留在裏面,可以用syslogd 或是 klogd 之類的程式把資料寫到 syslog 裡(var/log/message),不過這種方法還是可能會漏掉訊息。 下面是訊息等級,在 kernel 4.2 版本,預設等級是 log level。但是在 kernel 2.6 版本預設等級是 4。 驅動程式的進入點並不是 main() ,因為驅動程式與一般應用程式是不同的,而且必須準備多個,其中至少要兩個進入點 insmod 與 modprobe 呼叫的初始化函式 rmmod 呼叫的結束函式 對於這隻driver來說就是 devone_init()與devone_exit()。 其他進入點還包含 系統呼叫 中斷服務程序 計時器程序 驅動程式碼

安裝QT可能會發生的問題,跟解決辦法

參考的官方文件 https://wiki.qt.io/Install_Qt_5_on_Ubuntu 安裝完後,執行QT creator,可能會發生的問題是 garygu@garygu-UX330UA:~/Qt/Tools/QtCreator/bin$ ./qtcreator qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb. Aborted (core dumped) 看起來是在run的過程中有缺少shared library,以下網站也有提到。 https://stackoverflow.com/questions/17106315/failed-to-load-platform-plugin-xcb-while-launching-qt5-app-on-linux-without 所以透過ldd 可以知道有哪些shared library是有缺少的。從not found的library下手安裝就行了。 garygu@garygu-UX330UA:~/Qt/Tools/QtCreator/lib/Qt/plugins/platforms$ ldd libqxcb.so  linux-vdso.so.1 (0x00007fff229e2000) libQt5XcbQpa.so.5 => /home/garygu/Qt/Tools/QtCreator/lib/Qt/plugins/platforms/./../../lib/libQt5XcbQpa.so.5 (0x00007ffb4854...