2011年12月13日 星期二

[C++] RefPtr and PassRefPtr Basics

Link: http://www.webkit.org/coding/RefPtr.html

C++ 裡面已經有 smart pointer 的設計,為什麼 WebKit Open Source Project 還要自己寫一個 RefPtr? 答案可從 PassRefPtr 看出端倪,會如此設計,主要是解決下面常見的問題:
For example, for a function that took a smart pointer as a parameter and returned that same smart pointer as a return value, just passing the parameter and returning the value would increment and then decrement the reference count two to four times as the object moved from one smart pointer to another.
細節請看文章,使用方法一定要參考文末 guildline,因為很容易踩到 reference count = 0 的地雷。

2011年12月11日 星期日

Outliers - 10,000-Hour Rule

這是今天聽到的新梗,分享給大家。
Throughout the publication, Gladwell repeatedly mentions the "10,000-Hour Rule", claiming that the key to success in any field is, to a large extent, a matter of practicing a specific task for a total of around 10,000 hours.
簡單說就是:「要跨過專業頂尖人才的門檻,就是要有一萬個小時的專業技能。」以爬山來說,約略等於250岳(爬兩次百岳還不夠喔),也約略等於爬1200座中級山。有沒有那麼困難?

台灣山岳郊山訓練營「綜合複習、登山伙食介紹和夜行體驗、結業典禮」

所有黑白照片都是後製作品。
理由:
「如果景物顏色太多,
或是光線不好,
就改用黑白模式拍攝,
好讓照片的內容成為主角,
而不是雜亂的顏色」

我的主角是「大咖的手」。

這是細心的手

土地公土地婆都有保佑

前言:這天是台灣山岳郊山訓練營的「綜合複習、登山伙食介紹和夜行體驗、結業典禮」,我沒什麼事,剛好可以出來走走。原先預定路線是「石底觀音山→汐止大尖山」,但天雨路滑,教學不便,因此大咖更改路線。

日期:2011年12月11日(星期日)

天氣:大陰天偶爾細雨,無大風。

預定路線:台鐵汐科站→水源路康誥坑→勤進路大崎腳→光明寺→鹿窟事件碑汐碇路←→(深按頭山)→十三份→白雲古道白雲寺→汐科站。大部份路段屬於健行路段,沿途多遮雨處所,可供休息教學,全程不缺水。

實際路線:順撿深按頭山,其餘同預定路線。

紀錄:
  • 0830 台鐵汐科站北站出口。我跟汐止很有緣份(裝熟中),因為以前大學參加慈幼社服,常常去汐止樟樹社區。今年初去汐止看房子,今年底又去汐止爬山,你看看有多熟。
  • 0850 GO
  • 0907 白雲福德宮。那邊有很多土地公廟,算是此行的一大特色。
  • 0931 左側康誥坑溪有「汐止水道佈設記」碑石,祂是這麼說的:
    「本水道自大正十五年十二月興工,至昭和三年三月告竣,凡費金十五萬圓。諏卜本日舉通水式。我街居民七千餘,從茲得享潔淨用水,增進健康無疑也。古語曰:「造物雖無盡藏,然而源泉之涵濡殊屬有限。」我等當知體物節用,長保有餘,共慶無窮之福澤也。爰述數言以誌之。」
  • 0953 包包大搜查。大咖「講解」我的背包。今天我帶25L小包包,裡面有:換洗衣物、刁山小包包、備用眼鏡、Coleman爐頭、EPIgas 鈦鍋組、三罐小瓦斯罐、筷子、午餐、Thermos 800ml保溫瓶裝滿熱水、600ml 白開水、頭燈、高度計、指北針、折刀、簡單藥品、簡單行動糧、衛生紙、垃圾袋數個、護膝、毛巾、頭巾、帽子、雨衣、雨褲、羽絨衣外套。

    大咖還有「講解」他的背包,裝好多東西,嚇死我了。
  • 1053 GO
  • 1102 民間版的忠烈祠。
  • 1111 福德宮。
  • 1127 福德宮。這裡有遮風擋雨的空間,下面是廁所,水龍頭打開就有水源,大咖教官選定此處介紹登山伙食,大家一起用午餐,相當歡樂的行程。吃完午餐繼續泡茶聊是非。
  • 1320 GO
  • 1353 鹿窟事件碑。
    小小的鹿窟村落,散布在石碇與汐止、南港交界海拔六百多公尺的山巔,天氣好的時候可遠眺台北、基隆,對外交通極為不便。當年是台北縣石碇鄉鹿窟村,國民政府派軍隊掃蕩之後,特意改稱光明村至今,取其大放光明之意,昔日囚禁、刑求村民的「菜廟」,也已改建為光明寺。
    接下來的歷史,就留給凹陷曲折的不鏽鋼板細細述說吧,再說下去就尷尬了。接著大家往深按頭山繼續前進。拾木板階梯緩緩而上。
  • 1410 觀景平台。跨過欄杆繼續深入山林。山徑濕滑,沒穿雨鞋真不好走。
  • 1427 ▲深按頭山(H422M)。穿過層層桂竹林,終於摸到深按頭山土地調查局圖根點,此為南港區最高處,沒有展望。
  • 1442 三叉路,左往土庫岳,右往舊莊街,取右。接著經過十三分,因為十三分山不在計劃路線中,不取。途中經過許多桂花樹,香氣迷人,我家旁邊也有種桂花,香氣很熟悉,但沒想到是桂花香,難不成故鄉的桂花比較香?
  • 1604 白雲寺停車場,下方有廁所。大家小休一下。
  • 1612 白雲寺。白雲寺為閩南三合院的格局,右為雙護龍,左為單護龍,為石塊平砌式石頭厝。接下來就是踢柏油路下山。
  • 1645 有劇組借用民宅拍戲。
  • 1700 回到大馬路,離別總是特別感傷,在這裡有我們的回憶,在這裡我們得跟大咖教官說再見!我們下次一定會再見!再見了!

總覺得時間過的很快很快。

2011年12月10日 星期六

稍微進行一個閱讀「黑心房仲的告白」的動作

來看看最近有什麼興奮的活水。
下半年房市不旺,但小宅脫穎而出,2房產品銷售更勝套房,業者統計近2個月小宅產品銷售率,2房產品11月銷售率都逾2成,台中市更達24%。業者說,除房屋總價較低,因現代人孩子生得少,2房產品適合現代小家庭需求,是「進可攻、退可守」的產品(2011.12.9)(蘋果日報)

題材隨時都有,但房市投資到底要注意什麼事呢?
不要賠錢。不要賠錢。不要賠錢。
這本書教大家「不要賠錢」,簡單說就是要冷靜,先有自己的一套房市投機哲學,再來面對眼花撩亂的銷售技巧。相信我,有錢能使鬼推磨,仲介為了賺錢,必然使出渾身解數,這就是仲介的原罪,千萬不要罵仲介黑心,因為這是仲介的工作。

若有這樣的心理準備,我們的心將會很平靜,外面風風雨雨又奈我何?這是一筆大生意,倘若不幸賠錢,誰來拯救我們?

我以前的習慣:看屋前要做足功課,看屋時保持清醒,事後檢討,仔細思考惡魔細節。有一陣子沒關心房價,最近得開始熟悉區域行情,多算勝,少算不勝,而況於無算乎?就算沒有買房的打算,也可唬爛房仲自備五百萬,想找一千萬的房子。

看。又。不。用。錢。
唬。爛。也。不。用。錢。
謝謝大家收看廢文,我們下次見!

註:央行100年11月30日再次強調:「本行於98年10月即積極採取道德勸說,籲請銀行注意不動產相關貸款之風險控管,並於99年6月採行針對性審慎措施,訂定「中央銀行對金融機構辦理特定地區購屋貸款業務規定」,對已有一戶以上房屋之抵押擔保放款,限制其貸款條件,以及99年12月新增對金融機構辦理土地抵押貸款相關規定等,其目的即在促進金融穩定,也在防止泡沫。」央行應該也挺擔心的。

2011年12月9日 星期五

New Old Thing (1982 - 2008)

My Personal Notes and Solutions

Here, I give solutions and notes of some books. When an exercise is not completely solved, I would not mark square, $\Box$, in the end of it. Surely, any comments or suggestions are welcomed.

Education
  • M.S., Department of Mathematics College of Science, National Taiwan University, Sep. 2004 – June 2006. Here is my thesis [PS]. It is talking about subgroups and modular invariant of 2-dimensional special linear groups.
  • B.S., Department of Computer Science and Information Engineering, National Taiwan University, Sep. 2000 – June 2004

Award
Please see my album: https://plus.google.com/u/0/photos/108503938719747417956/albums/5441354475986595585


Hope for 2012
  • Guide de haute montagne.
  • Speculator in the real-estate market.
  • Speculator in the stock market.

2011年12月8日 星期四

Isolation Level

http://www.postgresql.org/docs/9.1/static/transaction-iso.html 講的不錯。Isolation Level 大原則是共通的,只是每一家做的不太一樣。In PostgreSQL, phantom reads are not possible in the implementation of Repeatable Read.

以 Microsoft SQL Server 2005 來說,她多一種 isolation level: snapshot. (PostgreSQL 也有 snapshot isolation.) The main reason for its adoption is that it allows better performance than serializability. 不難想像 serializable 採用 pessimistic concurrency control,而 snapshot 採用 optimistic concurrency control.

Parameterized Test in JUnit 4.10

最近準備練習使用 OpenRules 這套 business decision management system,因此同事找題目讓大家練習:How are ABO alleles inherited by our children? 答案請參考 http://www.bloodbook.com/inherited.html。

Q: 如何用程式表達答案呢?

A: 根據前輩的教誨,我們要對 interface 寫程式,不要對 class 寫程式。因此這個問題的 interface 如下:
package main;

import java.util.Set;

public interface BloodTypeCalculator {
    Set<BloodType> calculate(BloodType father, BloodType mother);
    
    Set<BloodType> calculate(BloodType grandfather,    
                             BloodType grandmother,
                             BloodType maternalGrandfather, 
                             BloodType maternalGrandmother);
}
BloodType 是簡單的 Java enum type: O, A, B, AB. BloodTypeCalculator 提供兩個 methods:
  1. 根據父母的血型計算小孩可能的血型
  2. 根據父母的父母的血型計算小孩可能的血型

好,那要怎麼測 BloodTypeCalculator interface? 這個問題非常難回答,我的想法是:
直接寫 test class,讓 ctor 可以接收 BloodTypeCalculator interface pointer (in the sense of C++). 這樣就可以了。
但這樣不足以成事,如果沒有適當設計 test class,JUnit 不支援吃參數的 ctor。解決辦法就是 parameterized test. 不囉唆,直接貼程式碼:
package test;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import junit.framework.Assert;

import main.BloodType;
import main.BloodTypeCalculator;
import main.NativeBloodTypeCalculator;
import main.OpenRulesBloodTypeCalculator;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(value = Parameterized.class)
public class BloodTypeCalculatorTest {

    public BloodTypeCalculatorTest(BloodTypeCalculator c) {
        this.c = c;
    }
    
    @Parameters
    public static Collection<Object[]> parameters() {
        Object[][] result = new Object[][] { 
            { new NativeBloodTypeCalculator() }, 
            { new OpenRulesBloodTypeCalculator() }
        };
        return Arrays.asList(result);
    }
    
    @Test
    public void calculate_OAndA_returnsOAndA() throws Exception {
        Set<BloodType> expected = new HashSet<BloodType>();
        expected.add(BloodType.O);
        expected.add(BloodType.A);
        Set<BloodType> actual = c.calculate(BloodType.O, BloodType.A);
        Assert.assertEquals(expected, actual);
        
        // It's OK when we exchange father's blood type and mother's one.
        actual = c.calculate(BloodType.A, BloodType.O);
        Assert.assertEquals(expected, actual);
    }
    
    private BloodTypeCalculator c;
}
這邊寫法都很固定。首先要讓 JUnit 知道這是 parameterized test,因此前面要加 annotation @RunWith(value = Parameterized.class).

接著寫 public static Collection<Object[]> parameters() method,前面一定要加 @Parameters annotation,此 method 一定要 static,return value type 一定要 Collection<Object[]>, Collection<Object[][]> 等等之類的,Object 的 dimension 就是 number of parameters of ctor. 至於 method name,自己喜歡就好了。

parameter() method 內容請直接模仿上面寫法,保證不會錯。

當 JUnit 準備執行此 test class,她會先看 parameter() method,讀到 2 筆參數,就為這個 test class 生成 2 個 test class objects. 接著再執行內部的 test methods.

因為 OpenRulesBloodTypeCalculator class 沒有 real implementation,所以全部 failure.


PS:
  • JUnit 中,error 和 failure 代表不同的意義
  • Test method 的 naming rule: 分三段命名。
    • Method name: The name of the method you are testing.
    • State under test: The conditions used to produce the expected behavior.
    • Expected behavior: What you expect the tested method to do under the specified conditions.
    參考書籍:Roy Osherove, The Art of Unit Testing: With Examples in .Net