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 by



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 by



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 by