• R/O
  • SSH
  • HTTPS

creole: Commit


Commit MetaInfo

Revision3 (tree)
Time2010-04-19 08:07:48
Authorphjgt

Log Message

デコードtrieの読み込みをポータブルに

Change Summary

Incremental Difference

--- util.lisp (revision 2)
+++ util.lisp (revision 3)
@@ -1,5 +1,7 @@
11 (in-package :creole)
22
3+(declaim (inline read-little-endian-byte to-signed))
4+
35 (defmacro a.if (exp then else)
46 `(let ((it ,exp))
57 (if it ,then ,else)))
@@ -9,10 +11,6 @@
911 #+SBCL (declare (sb-ext:muffle-conditions sb-ext:compiler-note))
1012 ,@body))
1113
12-(defun read-seq (in size)
13- (let ((buf (make-array size :element-type (stream-element-type in))))
14- (read-sequence buf in) buf))
15-
1614 (defmacro ensure-function (function-desginator)
1715 `(etypecase ,function-desginator
1816 (function)
@@ -26,3 +24,21 @@
2624 (defmacro defconst-onceonly (name value &optional doc)
2725 `(defconstant ,name (if (boundp ',name) (symbol-value ',name) ,value)
2826 ,@(when doc (list doc))))
27+
28+(defun to-signed (num byte-size)
29+ (if (< num (ash #x80 (* (1- byte-size) 8)))
30+ num
31+ (- num (ash #x100 (* (1- byte-size) 8)))))
32+
33+(defun read-little-endian-byte (stream byte-size)
34+ (let ((byte 0))
35+ (dotimes (i byte-size (to-signed byte byte-size))
36+ (setf (ldb (byte 8 (* i 8)) byte)
37+ (the octet (read-byte stream))))))
38+
39+(defun read-little-endian-bytes (in byte-size count)
40+ (declare ((integer 1 4) byte-size)
41+ (fixnum count))
42+ (let ((buf (make-array count :element-type `(signed-byte ,(* byte-size 8)))))
43+ (dotimes (i count buf)
44+ (setf (aref buf i) (read-little-endian-byte in byte-size)))))
\ No newline at end of file
--- trie.lisp (revision 2)
+++ trie.lisp (revision 3)
@@ -4,17 +4,16 @@
44
55 (defstruct trie
66 (base #() :type (simple-array (or character fixnum)) :read-only t)
7- (chck #() :type (simple-array (unsigned-byte 16)) :read-only t))
7+ (chck #() :type (simple-array (signed-byte 16)) :read-only t))
88
99 (defun load-trie (path)
10- (let ((size (with-open-file (in path) (/ (file-length in) 6))))
11- (make-trie
12- :base (with-open-file (in path :element-type '(signed-byte 32))
13- (map 'vector (lambda (a) (if (minusp a) (code-char (1- (- a))) a))
14- (read-seq in size)))
15- :chck (with-open-file (in path :element-type '(unsigned-byte 16))
16- (file-position in (* size 2))
17- (read-seq in size)))))
10+ (with-open-file (in path :element-type 'octet)
11+ (let ((size (/ (file-length in) 6)))
12+ (make-trie
13+ :base (map 'vector (lambda (a) (if (minusp a) (code-char (1- (- a))) a))
14+ (read-little-endian-bytes in 4 size))
15+ :chck (progn (file-position in (* size 4))
16+ (read-little-endian-bytes in 2 size))))))
1817
1918 (defun next-index (node code)
2019 (+ node code))
Show on old repository browser