Algorithm

共 6 篇文章。

memchr(3) rewrite

• Development

I have taken some time to rewrite memchr(3) to utilize similiar algorithm as my strlen(3) did. Two different formular, however, would have a roughly 1/3 difference.

Currently I have taken an approach that when memchr(3) is trying to match a pattern of 8-bit, then consider the string as 8-bit, which uses the first formular which is expected to have uniform O(n) time complexity by reducing unnecessary branches.

This approach, however, might be bogus since if you find 7-bit character within a 8-bit string, in that case, memchr(3) would perform poorly, if there are a lot of characters having their MSB as 1.

阅读全文… ( 本文约 114 字,阅读大致需要 1 分钟 )

如何计算不小于给定数的最小的2整数次方幂?

• Development

dengxf同学提到的一个问题,想了一下,想到的一个算法:

令xx为已知量,yy为目标量,kk为迭代轮数,

  1. 令 y=x−1y=x-1,k=0k=0
  2. 对于 yy 右移 2k2^k 位 >0> 0的情形,执行下列操作,直到此条件不符合为止:
    y与前述结果相与;kk累进11。
  3. yy累进11。

原理:第(2)步的循环将除最高位的所有0均置位。最后一步(3)修正这一值。

阅读全文… ( 本文约 199 字,阅读大致需要 1 分钟 )