vlist.scm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. ;;; -*- mode: scheme; coding: utf-8; -*-
  2. ;;;
  3. ;;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
  4. ;;;
  5. ;;; This library is free software; you can redistribute it and/or
  6. ;;; modify it under the terms of the GNU Lesser General Public
  7. ;;; License as published by the Free Software Foundation; either
  8. ;;; version 3 of the License, or (at your option) any later version.
  9. ;;;
  10. ;;; This library is distributed in the hope that it will be useful,
  11. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;; Lesser General Public License for more details.
  14. ;;;
  15. ;;; You should have received a copy of the GNU Lesser General Public
  16. ;;; License along with this library; if not, write to the Free Software
  17. ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (ice-9 vlist)
  19. #:use-module (srfi srfi-1)
  20. #:use-module (srfi srfi-9)
  21. #:use-module (srfi srfi-9 gnu)
  22. #:use-module (srfi srfi-26)
  23. #:export (vlist? vlist-cons vlist-head vlist-tail vlist-null?
  24. vlist-null list->vlist vlist-ref vlist-drop vlist-take
  25. vlist-length vlist-fold vlist-fold-right vlist-map
  26. vlist-unfold vlist-unfold-right vlist-append
  27. vlist-reverse vlist-filter vlist-delete vlist->list
  28. vlist-for-each
  29. block-growth-factor
  30. vhash? vhash-cons vhash-consq vhash-consv
  31. vhash-assoc vhash-assq vhash-assv
  32. vhash-delete vhash-delq vhash-delv
  33. vhash-fold vhash-fold-right
  34. vhash-fold* vhash-foldq* vhash-foldv*
  35. alist->vhash))
  36. ;;; Author: Ludovic Courtès <ludo@gnu.org>
  37. ;;;
  38. ;;; Commentary:
  39. ;;;
  40. ;;; This module provides an implementations of vlists, a functional list-like
  41. ;;; data structure described by Phil Bagwell in "Fast Functional Lists,
  42. ;;; Hash-Lists, Dequeues and Variable-Length Arrays", EPFL Technical Report,
  43. ;;; 2002.
  44. ;;;
  45. ;;; The idea is to store vlist elements in increasingly large contiguous blocks
  46. ;;; (implemented as vectors here). These blocks are linked to one another using
  47. ;;; a pointer to the next block (called `block-base' here) and an offset within
  48. ;;; that block (`block-offset' here). The size of these blocks form a geometric
  49. ;;; series with ratio `block-growth-factor'.
  50. ;;;
  51. ;;; In the best case (e.g., using a vlist returned by `list->vlist'),
  52. ;;; elements from the first half of an N-element vlist are accessed in O(1)
  53. ;;; (assuming `block-growth-factor' is 2), and `vlist-length' takes only
  54. ;;; O(ln(N)). Furthermore, the data structure improves data locality since
  55. ;;; vlist elements are adjacent, which plays well with caches.
  56. ;;;
  57. ;;; Code:
  58. ;;;
  59. ;;; VList Blocks and Block Descriptors.
  60. ;;;
  61. (define block-growth-factor
  62. (let ((f (make-fluid)))
  63. (fluid-set! f 2)
  64. f))
  65. (define-syntax-rule (define-inline (name formals ...) body ...)
  66. ;; Work around the lack of an inliner.
  67. (define-syntax name
  68. (syntax-rules ()
  69. ((_ formals ...)
  70. (begin body ...)))))
  71. (define-inline (make-block base offset size hash-tab?)
  72. ;; Return a block (and block descriptor) of SIZE elements pointing to BASE
  73. ;; at OFFSET. If HASH-TAB? is true, a "hash table" is also added.
  74. ;; Note: We use `next-free' instead of `last-used' as suggested by Bagwell.
  75. ;; XXX: We could improve locality here by having a single vector but currently
  76. ;; the extra arithmetic outweighs the benefits (!).
  77. (vector (make-vector size)
  78. base offset size 0
  79. (and hash-tab? (make-vector size #f))))
  80. (define-syntax-rule (define-block-accessor name index)
  81. (define-inline (name block)
  82. (vector-ref block index)))
  83. (define-block-accessor block-content 0)
  84. (define-block-accessor block-base 1)
  85. (define-block-accessor block-offset 2)
  86. (define-block-accessor block-size 3)
  87. (define-block-accessor block-next-free 4)
  88. (define-block-accessor block-hash-table 5)
  89. (define-inline (increment-block-next-free! block)
  90. (vector-set! block 4
  91. (+ (block-next-free block) 1)))
  92. (define-inline (block-append! block value)
  93. ;; This is not thread-safe. To fix it, see Section 2.8 of the paper.
  94. (let ((offset (block-next-free block)))
  95. (increment-block-next-free! block)
  96. (vector-set! (block-content block) offset value)
  97. #t))
  98. (define-inline (block-ref block offset)
  99. (vector-ref (block-content block) offset))
  100. (define-inline (block-ref* block offset)
  101. (let ((v (block-ref block offset)))
  102. (if (block-hash-table block)
  103. (car v) ;; hide the vhash link
  104. v)))
  105. (define-inline (block-hash-table-ref block offset)
  106. (vector-ref (block-hash-table block) offset))
  107. (define-inline (block-hash-table-set! block offset value)
  108. (vector-set! (block-hash-table block) offset value))
  109. (define block-null
  110. ;; The null block.
  111. (make-block #f 0 0 #f))
  112. ;;;
  113. ;;; VLists.
  114. ;;;
  115. (define-record-type <vlist>
  116. ;; A vlist is just a base+offset pair pointing to a block.
  117. ;; XXX: Allocating a <vlist> record in addition to the block at each
  118. ;; `vlist-cons' call is inefficient. However, Bagwell's hack to avoid it
  119. ;; (Section 2.2) would require GC_ALL_INTERIOR_POINTERS, which would be a
  120. ;; performance hit for everyone.
  121. (make-vlist base offset)
  122. vlist?
  123. (base vlist-base)
  124. (offset vlist-offset))
  125. (set-record-type-printer! <vlist>
  126. (lambda (vl port)
  127. (cond ((vlist-null? vl)
  128. (format port "#<vlist ()>"))
  129. ((block-hash-table (vlist-base vl))
  130. (format port "#<vhash ~x ~a pairs>"
  131. (object-address vl)
  132. (vhash-fold (lambda (k v r)
  133. (+ 1 r))
  134. 0
  135. vl)))
  136. (else
  137. (format port "#<vlist ~a>"
  138. (vlist->list vl))))))
  139. (define vlist-null
  140. ;; The empty vlist.
  141. (make-vlist block-null 0))
  142. (define-inline (block-cons item vlist hash-tab?)
  143. (let loop ((base (vlist-base vlist))
  144. (offset (+ 1 (vlist-offset vlist))))
  145. (if (and (< offset (block-size base))
  146. (= offset (block-next-free base))
  147. (block-append! base item))
  148. (make-vlist base offset)
  149. (let ((size (cond ((eq? base block-null) 1)
  150. ((< offset (block-size base))
  151. ;; new vlist head
  152. 1)
  153. (else
  154. (* (fluid-ref block-growth-factor)
  155. (block-size base))))))
  156. ;; Prepend a new block pointing to BASE.
  157. (loop (make-block base (- offset 1) size hash-tab?)
  158. 0)))))
  159. (define (vlist-cons item vlist)
  160. "Return a new vlist with @var{item} as its head and @var{vlist} as its
  161. tail."
  162. ;; Note: Calling `vlist-cons' on a vhash will not do the right thing: it
  163. ;; doesn't box ITEM so that it can have the hidden "next" link used by
  164. ;; vhash items, and it passes `#f' as the HASH-TAB? argument to
  165. ;; `block-cons'. However, inserting all the checks here has an important
  166. ;; performance penalty, hence this choice.
  167. (block-cons item vlist #f))
  168. (define (vlist-head vlist)
  169. "Return the head of @var{vlist}."
  170. (let ((base (vlist-base vlist))
  171. (offset (vlist-offset vlist)))
  172. (block-ref* base offset)))
  173. (define (vlist-tail vlist)
  174. "Return the tail of @var{vlist}."
  175. (let ((base (vlist-base vlist))
  176. (offset (vlist-offset vlist)))
  177. (if (> offset 0)
  178. (make-vlist base (- offset 1))
  179. (make-vlist (block-base base)
  180. (block-offset base)))))
  181. (define (vlist-null? vlist)
  182. "Return true if @var{vlist} is empty."
  183. (let ((base (vlist-base vlist)))
  184. (and (not (block-base base))
  185. (= 0 (block-size base)))))
  186. ;;;
  187. ;;; VList Utilities.
  188. ;;;
  189. (define (list->vlist lst)
  190. "Return a new vlist whose contents correspond to @var{lst}."
  191. (vlist-reverse (fold vlist-cons vlist-null lst)))
  192. (define (vlist-fold proc init vlist)
  193. "Fold over @var{vlist}, calling @var{proc} for each element."
  194. ;; FIXME: Handle multiple lists.
  195. (let loop ((base (vlist-base vlist))
  196. (offset (vlist-offset vlist))
  197. (result init))
  198. (if (eq? base block-null)
  199. result
  200. (let* ((next (- offset 1))
  201. (done? (< next 0)))
  202. (loop (if done? (block-base base) base)
  203. (if done? (block-offset base) next)
  204. (proc (block-ref* base offset) result))))))
  205. (define (vlist-fold-right proc init vlist)
  206. "Fold over @var{vlist}, calling @var{proc} for each element, starting from
  207. the last element."
  208. (define len (vlist-length vlist))
  209. (let loop ((index (1- len))
  210. (result init))
  211. (if (< index 0)
  212. result
  213. (loop (1- index)
  214. (proc (vlist-ref vlist index) result)))))
  215. (define (vlist-reverse vlist)
  216. "Return a new @var{vlist} whose content are those of @var{vlist} in reverse
  217. order."
  218. (vlist-fold vlist-cons vlist-null vlist))
  219. (define (vlist-map proc vlist)
  220. "Map @var{proc} over the elements of @var{vlist} and return a new vlist."
  221. (vlist-fold (lambda (item result)
  222. (vlist-cons (proc item) result))
  223. vlist-null
  224. (vlist-reverse vlist)))
  225. (define (vlist->list vlist)
  226. "Return a new list whose contents match those of @var{vlist}."
  227. (vlist-fold-right cons '() vlist))
  228. (define (vlist-ref vlist index)
  229. "Return the element at index @var{index} in @var{vlist}."
  230. (let loop ((index index)
  231. (base (vlist-base vlist))
  232. (offset (vlist-offset vlist)))
  233. (if (<= index offset)
  234. (block-ref* base (- offset index))
  235. (loop (- index offset 1)
  236. (block-base base)
  237. (block-offset base)))))
  238. (define (vlist-drop vlist count)
  239. "Return a new vlist that does not contain the @var{count} first elements of
  240. @var{vlist}."
  241. (let loop ((count count)
  242. (base (vlist-base vlist))
  243. (offset (vlist-offset vlist)))
  244. (if (<= count offset)
  245. (make-vlist base (- offset count))
  246. (loop (- count offset 1)
  247. (block-base base)
  248. (block-offset base)))))
  249. (define (vlist-take vlist count)
  250. "Return a new vlist that contains only the @var{count} first elements of
  251. @var{vlist}."
  252. (let loop ((count count)
  253. (vlist vlist)
  254. (result vlist-null))
  255. (if (= 0 count)
  256. (vlist-reverse result)
  257. (loop (- count 1)
  258. (vlist-tail vlist)
  259. (vlist-cons (vlist-head vlist) result)))))
  260. (define (vlist-filter pred vlist)
  261. "Return a new vlist containing all the elements from @var{vlist} that
  262. satisfy @var{pred}."
  263. (vlist-fold-right (lambda (e v)
  264. (if (pred e)
  265. (vlist-cons e v)
  266. v))
  267. vlist-null
  268. vlist))
  269. (define* (vlist-delete x vlist #:optional (equal? equal?))
  270. "Return a new vlist corresponding to @var{vlist} without the elements
  271. @var{equal?} to @var{x}."
  272. (vlist-filter (lambda (e)
  273. (not (equal? e x)))
  274. vlist))
  275. (define (vlist-length vlist)
  276. "Return the length of @var{vlist}."
  277. (let loop ((base (vlist-base vlist))
  278. (len (vlist-offset vlist)))
  279. (if (eq? base block-null)
  280. len
  281. (loop (block-base base)
  282. (+ len 1 (block-offset base))))))
  283. (define* (vlist-unfold p f g seed
  284. #:optional (tail-gen (lambda (x) vlist-null)))
  285. "Return a new vlist. See the description of SRFI-1 `unfold' for details."
  286. (let uf ((seed seed))
  287. (if (p seed)
  288. (tail-gen seed)
  289. (vlist-cons (f seed)
  290. (uf (g seed))))))
  291. (define* (vlist-unfold-right p f g seed #:optional (tail vlist-null))
  292. "Return a new vlist. See the description of SRFI-1 `unfold-right' for
  293. details."
  294. (let uf ((seed seed) (lis tail))
  295. (if (p seed)
  296. lis
  297. (uf (g seed) (vlist-cons (f seed) lis)))))
  298. (define (vlist-append . vlists)
  299. "Append the given lists."
  300. (if (null? vlists)
  301. vlist-null
  302. (fold-right (lambda (vlist result)
  303. (vlist-fold-right (lambda (e v)
  304. (vlist-cons e v))
  305. result
  306. vlist))
  307. vlist-null
  308. vlists)))
  309. (define (vlist-for-each proc vlist)
  310. "Call @var{proc} on each element of @var{vlist}. The result is unspecified."
  311. (vlist-fold (lambda (item x)
  312. (proc item))
  313. (if #f #f)
  314. vlist))
  315. ;;;
  316. ;;; Hash Lists, aka. `VHash'.
  317. ;;;
  318. ;; Assume keys K1 and K2, H = hash(K1) = hash(K2), and two values V1 and V2
  319. ;; associated with K1 and K2, respectively. The resulting layout is a
  320. ;; follows:
  321. ;;
  322. ;; ,--------------------.
  323. ;; | ,-> (K1 . V1) ---. |
  324. ;; | | | |
  325. ;; | | (K2 . V2) <--' |
  326. ;; | | |
  327. ;; +-|------------------+
  328. ;; | | |
  329. ;; | | |
  330. ;; | `-- O <---------------H
  331. ;; | |
  332. ;; `--------------------'
  333. ;;
  334. ;; The bottom part is the "hash table" part of the vhash, as returned by
  335. ;; `block-hash-table'; the other half is the data part. O is the offset of
  336. ;; the first value associated with a key that hashes to H in the data part.
  337. ;; The (K1 . V1) pair has a "hidden" link to the (K2 . V2) pair; hiding the
  338. ;; link is handled by `block-ref'.
  339. ;; This API potentially requires users to repeat which hash function and which
  340. ;; equality predicate to use. This can lead to unpredictable results if they
  341. ;; are used in consistenly, e.g., between `vhash-cons' and `vhash-assoc', which
  342. ;; is undesirable, as argued in http://savannah.gnu.org/bugs/?22159 . OTOH, two
  343. ;; arguments can be made in favor of this API:
  344. ;;
  345. ;; - It's consistent with how alists are handled in SRFI-1.
  346. ;;
  347. ;; - In practice, users will probably consistenly use either the `q', the `v',
  348. ;; or the plain variant (`vlist-cons' and `vlist-assoc' without any optional
  349. ;; argument), i.e., they will rarely explicitly pass a hash function or
  350. ;; equality predicate.
  351. (define (vhash? obj)
  352. "Return true if @var{obj} is a hash list."
  353. (and (vlist? obj)
  354. (let ((base (vlist-base obj)))
  355. (and base
  356. (vector? (block-hash-table base))))))
  357. (define* (vhash-cons key value vhash #:optional (hash hash))
  358. "Return a new hash list based on @var{vhash} where @var{key} is associated
  359. with @var{value}. Use @var{hash} to compute @var{key}'s hash."
  360. (let* ((key+value (cons key value))
  361. (entry (cons key+value #f))
  362. (vlist (block-cons entry vhash #t))
  363. (base (vlist-base vlist))
  364. (khash (hash key (block-size base))))
  365. (let ((o (block-hash-table-ref base khash)))
  366. (if o (set-cdr! entry o)))
  367. (block-hash-table-set! base khash
  368. (vlist-offset vlist))
  369. vlist))
  370. (define vhash-consq (cut vhash-cons <> <> <> hashq))
  371. (define vhash-consv (cut vhash-cons <> <> <> hashv))
  372. (define-inline (%vhash-fold* proc init key vhash equal? hash)
  373. ;; Fold over all the values associated with KEY in VHASH.
  374. (define khash
  375. (let ((size (block-size (vlist-base vhash))))
  376. (and (> size 0) (hash key size))))
  377. (let loop ((base (vlist-base vhash))
  378. (khash khash)
  379. (offset (and khash
  380. (block-hash-table-ref (vlist-base vhash)
  381. khash)))
  382. (max-offset (vlist-offset vhash))
  383. (result init))
  384. (let ((answer (and offset (block-ref base offset))))
  385. (cond ((and (pair? answer)
  386. (<= offset max-offset)
  387. (let ((answer-key (caar answer)))
  388. (equal? key answer-key)))
  389. (let ((result (proc (cdar answer) result))
  390. (next-offset (cdr answer)))
  391. (loop base khash next-offset max-offset result)))
  392. ((and (pair? answer) (cdr answer))
  393. =>
  394. (lambda (next-offset)
  395. (loop base khash next-offset max-offset result)))
  396. (else
  397. (let ((next-base (block-base base)))
  398. (if (and next-base (> (block-size next-base) 0))
  399. (let* ((khash (hash key (block-size next-base)))
  400. (offset (block-hash-table-ref next-base khash)))
  401. (loop next-base khash offset (block-offset base)
  402. result))
  403. result)))))))
  404. (define* (vhash-fold* proc init key vhash
  405. #:optional (equal? equal?) (hash hash))
  406. "Fold over all the values associated with @var{key} in @var{vhash}, with each
  407. call to @var{proc} having the form @code{(proc value result)}, where
  408. @var{result} is the result of the previous call to @var{proc} and @var{init} the
  409. value of @var{result} for the first call to @var{proc}."
  410. (%vhash-fold* proc init key vhash equal? hash))
  411. (define (vhash-foldq* proc init key vhash)
  412. "Same as @code{vhash-fold*}, but using @code{hashq} and @code{eq?}."
  413. (%vhash-fold* proc init key vhash eq? hashq))
  414. (define (vhash-foldv* proc init key vhash)
  415. "Same as @code{vhash-fold*}, but using @code{hashv} and @code{eqv?}."
  416. (%vhash-fold* proc init key vhash eqv? hashv))
  417. (define-inline (%vhash-assoc key vhash equal? hash)
  418. ;; A specialization of `vhash-fold*' that stops when the first value
  419. ;; associated with KEY is found or when the end-of-list is reached. Inline to
  420. ;; make sure `vhash-assq' gets to use the `eq?' instruction instead of calling
  421. ;; the `eq?' subr.
  422. (define khash
  423. (let ((size (block-size (vlist-base vhash))))
  424. (and (> size 0) (hash key size))))
  425. (let loop ((base (vlist-base vhash))
  426. (khash khash)
  427. (offset (and khash
  428. (block-hash-table-ref (vlist-base vhash)
  429. khash)))
  430. (max-offset (vlist-offset vhash)))
  431. (let ((answer (and offset (block-ref base offset))))
  432. (cond ((and (pair? answer)
  433. (<= offset max-offset)
  434. (let ((answer-key (caar answer)))
  435. (equal? key answer-key)))
  436. (car answer))
  437. ((and (pair? answer) (cdr answer))
  438. =>
  439. (lambda (next-offset)
  440. (loop base khash next-offset max-offset)))
  441. (else
  442. (let ((next-base (block-base base)))
  443. (and next-base
  444. (> (block-size next-base) 0)
  445. (let* ((khash (hash key (block-size next-base)))
  446. (offset (block-hash-table-ref next-base khash)))
  447. (loop next-base khash offset
  448. (block-offset base))))))))))
  449. (define* (vhash-assoc key vhash #:optional (equal? equal?) (hash hash))
  450. "Return the first key/value pair from @var{vhash} whose key is equal to
  451. @var{key} according to the @var{equal?} equality predicate."
  452. (%vhash-assoc key vhash equal? hash))
  453. (define (vhash-assq key vhash)
  454. "Return the first key/value pair from @var{vhash} whose key is @code{eq?} to
  455. @var{key}."
  456. (%vhash-assoc key vhash eq? hashq))
  457. (define (vhash-assv key vhash)
  458. "Return the first key/value pair from @var{vhash} whose key is @code{eqv?} to
  459. @var{key}."
  460. (%vhash-assoc key vhash eqv? hashv))
  461. (define* (vhash-delete key vhash #:optional (equal? equal?) (hash hash))
  462. "Remove all associations from @var{vhash} with @var{key}, comparing keys
  463. with @var{equal?}."
  464. (if (vhash-assoc key vhash equal? hash)
  465. (vlist-fold (lambda (k+v result)
  466. (let ((k (car k+v))
  467. (v (cdr k+v)))
  468. (if (equal? k key)
  469. result
  470. (vhash-cons k v result hash))))
  471. vlist-null
  472. vhash)
  473. vhash))
  474. (define vhash-delq (cut vhash-delete <> <> eq? hashq))
  475. (define vhash-delv (cut vhash-delete <> <> eqv? hashv))
  476. (define (vhash-fold proc seed vhash)
  477. "Fold over the key/pair elements of @var{vhash}. For each pair call
  478. @var{proc} as @code{(@var{proc} key value result)}."
  479. (vlist-fold (lambda (key+value result)
  480. (proc (car key+value) (cdr key+value)
  481. result))
  482. seed
  483. vhash))
  484. (define (vhash-fold-right proc seed vhash)
  485. "Fold over the key/pair elements of @var{vhash}, starting from the 0th
  486. element. For each pair call @var{proc} as @code{(@var{proc} key value
  487. result)}."
  488. (vlist-fold-right (lambda (key+value result)
  489. (proc (car key+value) (cdr key+value)
  490. result))
  491. seed
  492. vhash))
  493. (define* (alist->vhash alist #:optional (hash hash))
  494. "Return the vhash corresponding to @var{alist}, an association list."
  495. (fold-right (lambda (pair result)
  496. (vhash-cons (car pair) (cdr pair) result hash))
  497. vlist-null
  498. alist))
  499. ;;; vlist.scm ends here