Fork me on GitHub

2012/01/25


このエントリーをはてなブックマークに追加
やっぱり最初はhello worldって事で...

Rust

RustRust a safe, concurrent, practical language Rust is a curly-brace, block-structured expression langu...
Rust is a curly-brace, block-structured expression language. It visually resembles the C language family, but differs significantly in syntactic and semantic details. Its design is oriented toward concerns of “programming in the large”, that is, of creating and maintaining boundaries – both abstract and operational – that preserve large-system integrity, availability and concurrency.

http://www.rust-lang.org/
windowsのインストーラがダウンロード出来なかったのでgithubからダウンロードしてビルドした。
mozilla/rust - GitHub

a safe, concurrent, practical language

https://github.com/mozilla/rust
ビルドはmingw/msysで./configure && make install
LLVM上のコンパイラなのでビルドは結構リソースを食う。かなり食う。

ただ単にfizzbuzz出してもdanさんの真似になって面白く無かったのでメッセージボックス出してみた。
use std;
import std::io;

#[abi = "stdcall"]
native mod user32 {
  fn MessageBoxA(h: ctypes::c_uint,
      message: str::sbuf, title: str::sbuf, flag: ctypes::c_uint)
        -> ctypes::c_uint;
}

fn main() {
  str::as_buf("hello", { |message|
    str::as_buf("world", { |title|
      user32::MessageBoxA(0u, message, title, 0u);
    })
  })
}
abiとしてcdeclやstdcallが指定出切る。rustcコマンドはuser32に対してちゃんと-luser32というリンクオプションを足してくれるので、コマンドラインでガチャガチャやるイメージはあまりない。
なお、CARGO_ROOTという環境変数にc:/rust/binc:/rust/libでいう所のc:/rustを指定しておくと、ちゃんとリンカが判断してファイルを探してくれる。
vim編集中に簡単に実行出切る様に、quickrunにもプルリクエストを送っておいた。(マージされた)
#22: support mozilla rust. by mattn for thinca/vim-quickrun - Pull Request - GitHub
https://github.com/thinca/vim-quickrun/pull/22

環境が揃うユーザならば、vimでファイル開いて<leader>rとタイプすればメッセージボックスが出る様になります。
rust-win32
所感としては、str::as_bufがブロック式なので、rubyっぽく思えると同時に引数2つの場合めんどくさ過ぎる!と思った。言語というか、仕組み的にはGo言語が近いなーとか思ってたら、Dubheadさんから教えてもらったリンクに既に書いてあった。
Doc language FAQ - GitHub

Have you seen this Google language, Go? How does Rust compare?

面白そうなのでしばらく触ってみる。
Posted at 20:06 in ソフトウェア::lang::rust
Tagged as: rust
Bookmarks: add to hatena add to hatena | add to delicious.com | add to livedoor.clip add to livedoor.clip

2012/01/17


このエントリーをはてなブックマークに追加
trieなんたらが話題になってたのでなんとなく書いてみた。
ベンチとかはやってない。
404 Blog Not Found:Algorithm - 連想配列の実装としてのハッシュはオワコン?

そのデータ構造は、君の魂を差し出すに足るものかい? 連想配列( Associative array )がコレクション( Collection )、すなわち数多のデータ構造をまとめるデータ構造としての覇...

http://blog.livedoor.jp/dankogai/archives/51765855.html
#include <stdio.h>
#include <stdlib.h>

typedef struct _trie {
  char c;
  unsigned int n;
  struct _trie** next;
  void* value;
} trie;

trie*
trie_new() {
  trie* p = (trie*) malloc(sizeof(trie));
  p->c = 0;
  p->n = 0;
  p->next = NULL;
  return p;
}

void
trie_free(trie* p) {
  unsigned int i;
  for (i = 0; i < p->n; i++)
    trie_free(p->next[i]);
  if (p->n)
    free(p->next);
  free(p);
}

trie*
trie_put(trie* p, const char* key, const void* value) {
  if (*key == 0) {
    p->value = (void*) value;
    return p;
  }
  trie* next = NULL;
  int i;
  for (i = 0; i < p->n; i++)
    if (p->next[i]->c == *key) {
      next = p;
      break;
    }
  if (!next) {
    if (!(next = trie_new())) return NULL;
    trie** children = (trie**) realloc(p->next, p->n * sizeof(trie*));
    if (!children) return NULL;
    p->next = children;

    next->c = *key;
    p->next[p->n] = next;
    p->n++;
  }
  return trie_put(next, key+1, value);
}

trie*
trie_get(trie* p, const char* key) {
  if (p->c) {
    if (p->c != *key)
      return NULL;
    if (p->c == *key && *(key+1) == 0)
      return p;
    key++;
  }
  int i;
  trie* value = NULL;
  for (i = 0; i < p->n; i++) {
    if ((value = trie_get(p->next[i], key)))
      return value;
  }
  return NULL;
}

void
safe_puts(const char* key, const trie* p) {
  if (!p)
    printf("%s not found\n", key);
  else if (!p->value)
    printf("%s: null\n", key);
  else
    printf("%s%s\n", key, (char*) p->value);
}

int
main(int argc, char* argv[]) {
  trie* p = trie_new();
  trie* v;

  trie_put(p, "foo""bar");
  trie_put(p, "bar""baz");

  v = trie_get(p, "baz");
  safe_puts("baz", v);

  v = trie_get(p, "foo");
  safe_puts("foo", v);

  v = trie_get(p, "bar");
  safe_puts("bar", v);

  trie_put(p, "うんこ""うんこっこー");
  v = trie_get(p, "うんこ");
  safe_puts("うんこ", v);

  v = trie_get(p, "404 blog");
  safe_puts("404 blog", v);

  trie_free(p);
  return 0;
}
baz not found
foo: bar
bar: baz
うんこ: うんこっこー
404 blog not found
Posted at 13:52 in ソフトウェア::lang::c
Tagged as: c
Bookmarks: add to hatena add to hatena | add to delicious.com | add to livedoor.clip add to livedoor.clip

2012/01/14


このエントリーをはてなブックマークに追加
といっても2ヶ月程前の話なんだけど。
mattn/lingr-radar-linux - GitHub

notify messages in lingr. should be work well for linux/windows

https://github.com/mattn/lingr-radar-linux
ujihisaさんに「ちゃんと動いた」ぽい様な事を報告してもらって、そう言えば紹介してなかったなーと思ったので。
要はオフィシャルが提供しているLingr Radar For MacのLinux版。pythonで書かれています。
Lingr Tools
http://radar.lingr.com/
動作にはyoshioriさんのpyLingrとpit、gntpが必要。pyLingrはpipなんかでは入らないので手作業が必要。
yoshiori/pyLingr - GitHub
https://github.com/yoshiori/pyLingr
リポジトリ内のpylingr.pylingr-radar.pyと同じディレクトリに置いて動かせばOK。
pitとgntpはpipなんかで入れる。
起動するとlingrのユーザとパスワードを求められるのでエディタで編集して保存、終了。参加しているルームの通知がGNTPプロトコルのGrowlに通知される。linuxだとgrowl-for-linuxで可能。

lingr-radar-for-linux
mattn/growl-for-linux @ GitHub

Introduction: Growl For Linux is Linux-compatible of Growl. Growl is a notification system for Mac O...

http://mattn.github.com/growl-for-linux/
ちなみにどれもwindowsで動作するのでGrowl For Windows入れてる人やgrowl-for-linuxをwindowsでビルド出来る人はwindowsでも実現可能。
よかったらどうぞ。
Posted at 23:33 in ソフトウェア::lang::python
Tagged as: lingr, linux
Bookmarks: add to hatena add to hatena | add to delicious.com | add to livedoor.clip add to livedoor.clip

2012/01/13


このエントリーをはてなブックマークに追加
追記: 良く見たらstrncpyの罠でもなんでもなく、バッファがクリアされてないのが原因だった。って事であとでpullreqでも送っとくかな。

まぁソート関数自身の問題じゃないので控えめに。

strncpyは必ず \0 で埋めてくれるとは限らない。
404 Blog Not Found:algorithm - bucketsort.[ch] - 汎用かつlibcの*sortより高速な

dankogai/c-bucketsort - GitHub

http://blog.livedoor.jp/dankogai/archives/51764911.html
dankogai/c-bucketsort - GitHub

bucketsort - bucket sort that can be used for general purpose

https://github.com/dankogai/c-bucketsort
strncpy(3)のmanページにはこう書いてある。

The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy.

The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.

なので...
# ./bucketsort README
とかするとNULストップしてないバッファで画面が賑やかになり、時にはけたたましいビープ音で心躍る。

サンプルプログラムの仕様が最終行の改行コードなしでも動く様にすべきなのかどうなのかが分からないので
diff --git a/main.c b/main.c
index 67e4e00..48391b4 100644
--- a/main.c
+++ b/main.c
@@ -74,6 +74,7 @@ int main(int argc, char **argv)
        if (!lines[lcur])
            EXIT("malloc failed");
        strncpy(lines[lcur], buf, slen - 1);    // do not copy \n
+       lines[lcur][slen - 1] = 0;
     }
     size_t i;
     // for (i = 0; i < lcur; i++) printf("%s\n", lines[i]);
こういうワークアラウンドでもOKなのかもしれないが(そういう点でfork/pullreqはやめた)、おそらくツールとして扱うなら改行コードがあった場合には削除...という動きが望ましいのでstrpbrk(3)やstrchr(3)と使ったり、自作strchrぽい処理を入れていくと他のsortと比較しておられる状況も少し変わったりするのかもしれませんね。確認してないけど。
sort(1)とはやってる事が違いすぎるのでそもそもな気もしなくない。どこを比較したんだろう...。

ちなみに全然オフトピだけど、GNU textutilsに入ってるsort(1)にはコア数によって動的にスレッドを生成してソートする処理が入ってるのでそういうの興味ある人はコード読むといいと思います。
Posted at 10:13 in ソフトウェア::lang::c
Tagged as: c
Bookmarks: add to hatena add to hatena | add to delicious.com | add to livedoor.clip add to livedoor.clip