shiro #3054(2007/09/17 19:33 GMT) [ Scheme ] Rating2/2=1.00
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
(use gauche.uvector) (use util.stream) ;; Eager版。nが大きいと破綻する (define (reverse-list bits) (list-ec (: n (expt 2 bits)) (reverse-bits n bits))) ;; Lazy版。 (define (reverse-stream bits) (stream-map (cut reverse-bits <> bits) (stream-iota (expt 2 bits)))) ;; 任意ビット長の整数の反転。速度を稼ぐためテーブル使用。 (define (reverse-bits word bits) (define rev8 '#u8(0 128 64 192 32 160 96 224 16 144 80 208 48 176 112 240 8 136 72 200 40 168 104 232 24 152 88 216 56 184 120 248 4 132 68 196 36 164 100 228 20 148 84 212 52 180 116 244 12 140 76 204 44 172 108 236 28 156 92 220 60 188 124 252 2 130 66 194 34 162 98 226 18 146 82 210 50 178 114 242 10 138 74 202 42 170 106 234 26 154 90 218 58 186 122 250 6 134 70 198 38 166 102 230 22 150 86 214 54 182 118 246 14 142 78 206 46 174 110 238 30 158 94 222 62 190 126 254 1 129 65 193 33 161 97 225 17 145 81 209 49 177 113 241 9 137 73 201 41 169 105 233 25 153 89 217 57 185 121 249 5 133 69 197 37 165 101 229 21 149 85 213 53 181 117 245 13 141 77 205 45 173 109 237 29 157 93 221 61 189 125 253 3 131 67 195 35 163 99 227 19 147 83 211 51 179 115 243 11 139 75 203 43 171 107 235 27 155 91 219 59 187 123 251 7 135 71 199 39 167 103 231 23 151 87 215 55 183 119 247 15 143 79 207 47 175 111 239 31 159 95 223 63 191 127 255)) (let loop ((bits bits) (word word) (revs '())) (if (<= bits 0) (ash (fold (lambda (dig r) (+ dig (ash r 8))) 0 (reverse revs)) bits) (loop (- bits 8) (ash word -8) (cons (u8vector-ref rev8 (logand word #xff)) revs)))))
Rating2/2=1.00-0+
[ reply ]
shiro
#3054()
[
Scheme
]
Rating2/2=1.00
今回は32bitまでとなってますが、n=32でeagerにリストを作るとメモリが大変なので、遅延ストリームを返すlazy版も作っときました。必要な分だけ計算できます。
gosh> (stream->list (stream-take (reverse-stream 32) 16))
(0 2147483648 1073741824 3221225472 536870912 2684354560 1610612736
3758096384 268435456 2415919104 1342177280 3489660928 805306368
2952790016 1879048192 4026531840)
32bit越えても大丈夫。仕様外だけど。
gosh> (stream->list (stream-take (reverse-stream 99) 10))
(0 316912650057057350374175801344 158456325028528675187087900672
475368975085586025561263702016 79228162514264337593543950336
396140812571321687967719751680 237684487542793012780631851008
554597137599850363154807652352 39614081257132168796771975168
356526731314189519170947776512)
Rating2/2=1.00-0+
[ reply ]