Archive for the ‘讀書心得’ tag
借書
今天去圖書館挖寶,又借了三本書:
- Reversing: Secrets of Reverse Engineering
- OOP Demystified
- COM+ Programming: A Practical Guide Using Visual C++ and ATL
本來還要借 Advanced .NET Remoting 的,可是應該是看不完,就先留給別人了…
很喜歡逛圖書館,在裡面搜刮的感覺很棒… 面對永遠都看不完的書,有時後會想:住在圖書館裡面好了 XD
BTW, 似乎應該要找一些文學類的來看阿 @@
.NET - Plugin
我之前在 .NET - Load Plugins 寫過如何實做簡單的 plugin interface,最近真正在用的時候才發現問題。
Assembly.Load* 雖然可以把 .dll 檔案動態載入,可是卻找不到任何方法可以 unload.. Assembly 在被載入之後是處於鎖定的狀態,如果這時候把檔案換掉的話,Assembly.Load 便無法再重新載入,會出現 Exception
這樣就沒辦法在後端偷偷換掉 backend 了 -_-
要實做動態的 load, unload 好像是要使用 System.AppDomain 來做(時際上是用 Remoting 的技術),看似簡單,可是我試到現在還沒成功過… 還變成好像 Assembly 被 Cache 起來的情況,怎麼 load, unload 都是用到舊的 Assembly… 天阿
Suffix Tree
Suffix Tree,在字串處理上有非常多的應用,尤其是字串的搜尋、定位等等,利用這個資料結構很多都可以在 O(n) 也就是 linear time 做完。
這是在這學期的 字串學(stringology) 學到的,作業就是實做 suffix tree,我用的是 Ukkonen 的演算法,可以用 on-line construction 的方法建構 suffix tree。
我是用 Nemerle 寫的(因為在練習),後來也有用 C# 改了另外的版本,但是還是 Nemerle 的版本比較完整。原本是用 System.Collections.Generic.Dictionary 來存每個 State 裡面的 transitions,可是非常的吃記憶體,用 mono –profile 可以很容易看出來一個 500kb 的檔案,在建構的時候光 Dictionary 就可以吃掉近 150mb 的記憶體;後來改用自己的 linked list 來做,節省了很多資源,2mb 的測試檔可以在 9 秒內建完樹並回報內部節點個數。
整個程式應該還有多可以改進的地方,但是目前的效率作為作業已經可以接受了,先放上來。在這裡。
Nemerle - Redefining symbols
在 nemerle 裡面,可以用
def a = 123;
這樣的格式來定義一個 immutable 的變數
然而,這樣的寫法也是 vaild 的
def a = 123;
def a = "123";
雖然看起來像是給 immutable 的 a assign 了兩次不同的值,實際上兩者有完全不同的意義,所以
def (x, y) = (1, 3);
def (x, y) = (y, x);
也都是被接受的語法。
.NET - Program Glue
C 的 stdio.h 有 popen/pclose 可以用,python 有 popen/popen2… ,perl 可以把程式當作檔案代號直接開啟執行,這些都可以做到程式間黏合、互動,那 .NET 要怎麼作呢?
可以用 System.Diagnostics.Process 來做到一樣的功能:
using System;
using System.Diagnostics;
public module T {
public Main (args: array[string]) : void
{
def proc = Process ();
def proc_info = ProcessStartInfo (args[0]);
proc_info.CreateNoWindow = true;
proc_info.UseShellExecute = false;
proc_info.RedirectStandardInput = true;
proc_info.RedirectStandardOutput = true;
proc_info.RedirectStandardError = true;
proc.StartInfo = proc_info;
proc.Start ();
while (true) {
proc.StandardInput.WriteLine (Console.ReadLine ());
Console.WriteLine (proc.StandardOutput.ReadLine ());
}
}
}
.NET - Load Plugins
寫 c/c++ 的時候,可以用 dlopen 來讀入 .so/.dll 檔當作 plugin 用,寫 perl 的時候用 require 就可以把另一個 perl module 讀到記憶體,elisp 也有類似的用法,.NET 則提供了 System.Reflection.Assembly 類別來處理這樣的事。
首先為了統一 Plugin 的介面,我們可以定義幾個 Plugin 專用的 interface:
// PluginInterfaces.dll
namespace Plugin.Interfaces
public interface IPlugin
Calculate (input: int) : int
然後 Plugin 的實做寫在另一個 Assembly:
// Plugins.dll
using Plugin.Interfaces
namespace Plugin.Test
public class Test1 : IPlugin
public Calculate (input: int) : int
input * 2
public class Test2 : IPlugin
public Calculate (input: int) : int
input + 3
然後主程式就可以把這些 plugins 動態的讀近來:
// Run.exe
using System
using System.Reflection
using Plugin.Interfaces
public module R
public Main (args: array[string]) : void
def p = Assembly.LoadFile (args[0]).CreateInstance (args[1]) :> IPlugin
Console.WriteLine (p)
Console.WriteLine (p.Calculate (100))
實際執行的結果:
$ ncc PluginInterfaces.n -out:PluginInterfaces.dll -target:library
$ ncc Plugins.n -ref:PluginInterfaces.dll -out:Plugins.dll -target:library
$ ncc Run.n -ref:PluginInterfaces.dll -out:Run.exe
$ ./Run.exe Plugins.dll Plugin.Test.Test1
Plugin.Test.Test1
200
$ ./Run.exe Plugins.dll Plugin.Test.Test2
Plugin.Test.Test2
103
嗯,大概就是這樣
TPOP
The Practice of Programming 中文名稱《程式設計專家手冊》,在圖書館閒逛的時候發現這本書,這本書談的是關於實務上的程式設計技巧,程式的風格、常用的資料結構、有效率的除錯、程式最佳化……等等。內容言之有物,的確都是一些非常實用的東西,難怪此書會被翻譯成這麼多語言的版本
中文版的翻譯還可以接受,雖然有些中文術語與現在所使用的不太相同,但是整體上來說並不會影響閱讀。
頭先設計範式
今天去搭車時經過天瓏書局,想起同學拜託幫忙看一下「UNIX System Programming」這本書,所以進去逛了一下,發現「頭先設計範式(Head First Design Patterns)」已經出中文版了。
我之前已經等不及先買了英文版回家,今天翻了一下中文版,讓我更加確定先買是對的,因為中文版的排版雖然也不差,風格樣式也跟原文吻合,可是整體的感覺就是差一點,也可能是先入為主吧,總之我看起來就覺得不一樣
最後,我要看的那本書標價台幣 1400 元… 真貴,已經超出我跟我同學的預算,所以我便兩手空空的去搭車了。