srfi-43.scm 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. ;;; srfi-43.scm -- SRFI 43 Vector library
  2. ;; Copyright (C) 2014 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 3 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;; Author: Mark H Weaver <mhw@netris.org>
  18. (define-module (srfi srfi-43)
  19. #:use-module (srfi srfi-1)
  20. #:use-module (srfi srfi-8)
  21. #:re-export (make-vector vector vector? vector-ref vector-set!
  22. vector-length)
  23. #:replace (vector-copy vector-fill! list->vector vector->list)
  24. #:export (vector-empty? vector= vector-unfold vector-unfold-right
  25. vector-reverse-copy
  26. vector-append vector-concatenate
  27. vector-fold vector-fold-right
  28. vector-map vector-map!
  29. vector-for-each vector-count
  30. vector-index vector-index-right
  31. vector-skip vector-skip-right
  32. vector-binary-search
  33. vector-any vector-every
  34. vector-swap! vector-reverse!
  35. vector-copy! vector-reverse-copy!
  36. reverse-vector->list
  37. reverse-list->vector))
  38. (cond-expand-provide (current-module) '(srfi-43))
  39. (define (error-from who msg . args)
  40. (apply error
  41. (string-append (symbol->string who) ": " msg)
  42. args))
  43. (define-syntax-rule (assert-nonneg-exact-integer k who)
  44. (unless (and (exact-integer? k)
  45. (not (negative? k)))
  46. (error-from who "expected non-negative exact integer, got" k)))
  47. (define-syntax-rule (assert-procedure f who)
  48. (unless (procedure? f)
  49. (error-from who "expected procedure, got" f)))
  50. (define-syntax-rule (assert-vector v who)
  51. (unless (vector? v)
  52. (error-from who "expected vector, got" v)))
  53. (define-syntax-rule (assert-valid-index i len who)
  54. (unless (and (exact-integer? i)
  55. (<= 0 i len))
  56. (error-from who "invalid index" i)))
  57. (define-syntax-rule (assert-valid-start start len who)
  58. (unless (and (exact-integer? start)
  59. (<= 0 start len))
  60. (error-from who "invalid start index" start)))
  61. (define-syntax-rule (assert-valid-range start end len who)
  62. (unless (and (exact-integer? start)
  63. (exact-integer? end)
  64. (<= 0 start end len))
  65. (error-from who "invalid index range" start end)))
  66. (define-syntax-rule (assert-vectors vs who)
  67. (let loop ((vs vs))
  68. (unless (null? vs)
  69. (assert-vector (car vs) who)
  70. (loop (cdr vs)))))
  71. ;; Return the length of the shortest vector in VS.
  72. ;; VS must have at least one element.
  73. (define (min-length vs)
  74. (let loop ((vs (cdr vs))
  75. (result (vector-length (car vs))))
  76. (if (null? vs)
  77. result
  78. (loop (cdr vs) (min result (vector-length (car vs)))))))
  79. ;; Return a list of the Ith elements of the vectors in VS.
  80. (define (vectors-ref vs i)
  81. (let loop ((vs vs) (xs '()))
  82. (if (null? vs)
  83. (reverse! xs)
  84. (loop (cdr vs) (cons (vector-ref (car vs) i)
  85. xs)))))
  86. (define vector-unfold
  87. (case-lambda
  88. "(vector-unfold f length initial-seed ...) -> vector
  89. The fundamental vector constructor. Create a vector whose length is
  90. LENGTH and iterates across each index k from 0 up to LENGTH - 1,
  91. applying F at each iteration to the current index and current seeds, in
  92. that order, to receive n + 1 values: the element to put in the kth slot
  93. of the new vector, and n new seeds for the next iteration. It is an
  94. error for the number of seeds to vary between iterations."
  95. ((f len)
  96. (assert-procedure f 'vector-unfold)
  97. (assert-nonneg-exact-integer len 'vector-unfold)
  98. (let ((v (make-vector len)))
  99. (let loop ((i 0))
  100. (unless (= i len)
  101. (vector-set! v i (f i))
  102. (loop (+ i 1))))
  103. v))
  104. ((f len seed)
  105. (assert-procedure f 'vector-unfold)
  106. (assert-nonneg-exact-integer len 'vector-unfold)
  107. (let ((v (make-vector len)))
  108. (let loop ((i 0) (seed seed))
  109. (unless (= i len)
  110. (receive (x seed) (f i seed)
  111. (vector-set! v i x)
  112. (loop (+ i 1) seed))))
  113. v))
  114. ((f len seed1 seed2)
  115. (assert-procedure f 'vector-unfold)
  116. (assert-nonneg-exact-integer len 'vector-unfold)
  117. (let ((v (make-vector len)))
  118. (let loop ((i 0) (seed1 seed1) (seed2 seed2))
  119. (unless (= i len)
  120. (receive (x seed1 seed2) (f i seed1 seed2)
  121. (vector-set! v i x)
  122. (loop (+ i 1) seed1 seed2))))
  123. v))
  124. ((f len . seeds)
  125. (assert-procedure f 'vector-unfold)
  126. (assert-nonneg-exact-integer len 'vector-unfold)
  127. (let ((v (make-vector len)))
  128. (let loop ((i 0) (seeds seeds))
  129. (unless (= i len)
  130. (receive (x . seeds) (apply f i seeds)
  131. (vector-set! v i x)
  132. (loop (+ i 1) seeds))))
  133. v))))
  134. (define vector-unfold-right
  135. (case-lambda
  136. "(vector-unfold-right f length initial-seed ...) -> vector
  137. The fundamental vector constructor. Create a vector whose length is
  138. LENGTH and iterates across each index k from LENGTH - 1 down to 0,
  139. applying F at each iteration to the current index and current seeds, in
  140. that order, to receive n + 1 values: the element to put in the kth slot
  141. of the new vector, and n new seeds for the next iteration. It is an
  142. error for the number of seeds to vary between iterations."
  143. ((f len)
  144. (assert-procedure f 'vector-unfold-right)
  145. (assert-nonneg-exact-integer len 'vector-unfold-right)
  146. (let ((v (make-vector len)))
  147. (let loop ((i (- len 1)))
  148. (unless (negative? i)
  149. (vector-set! v i (f i))
  150. (loop (- i 1))))
  151. v))
  152. ((f len seed)
  153. (assert-procedure f 'vector-unfold-right)
  154. (assert-nonneg-exact-integer len 'vector-unfold-right)
  155. (let ((v (make-vector len)))
  156. (let loop ((i (- len 1)) (seed seed))
  157. (unless (negative? i)
  158. (receive (x seed) (f i seed)
  159. (vector-set! v i x)
  160. (loop (- i 1) seed))))
  161. v))
  162. ((f len seed1 seed2)
  163. (assert-procedure f 'vector-unfold-right)
  164. (assert-nonneg-exact-integer len 'vector-unfold-right)
  165. (let ((v (make-vector len)))
  166. (let loop ((i (- len 1)) (seed1 seed1) (seed2 seed2))
  167. (unless (negative? i)
  168. (receive (x seed1 seed2) (f i seed1 seed2)
  169. (vector-set! v i x)
  170. (loop (- i 1) seed1 seed2))))
  171. v))
  172. ((f len . seeds)
  173. (assert-procedure f 'vector-unfold-right)
  174. (assert-nonneg-exact-integer len 'vector-unfold-right)
  175. (let ((v (make-vector len)))
  176. (let loop ((i (- len 1)) (seeds seeds))
  177. (unless (negative? i)
  178. (receive (x . seeds) (apply f i seeds)
  179. (vector-set! v i x)
  180. (loop (- i 1) seeds))))
  181. v))))
  182. (define guile-vector-copy (@ (guile) vector-copy))
  183. ;; TODO: Enhance Guile core 'vector-copy' to do this.
  184. (define vector-copy
  185. (case-lambda*
  186. "(vector-copy vec [start [end [fill]]]) -> vector
  187. Allocate a new vector whose length is END - START and fills it with
  188. elements from vec, taking elements from vec starting at index START
  189. and stopping at index END. START defaults to 0 and END defaults to
  190. the value of (vector-length VEC). If END extends beyond the length of
  191. VEC, the slots in the new vector that obviously cannot be filled by
  192. elements from VEC are filled with FILL, whose default value is
  193. unspecified."
  194. ((v) (guile-vector-copy v))
  195. ((v start)
  196. (assert-vector v 'vector-copy)
  197. (let ((len (vector-length v)))
  198. (assert-valid-start start len 'vector-copy)
  199. (let ((result (make-vector (- len start))))
  200. (vector-move-left! v start len result 0)
  201. result)))
  202. ((v start end #:optional (fill *unspecified*))
  203. (assert-vector v 'vector-copy)
  204. (let ((len (vector-length v)))
  205. (unless (and (exact-integer? start)
  206. (exact-integer? end)
  207. (<= 0 start end))
  208. (error-from 'vector-copy "invalid index range" start end))
  209. (let ((result (make-vector (- end start) fill)))
  210. (vector-move-left! v start (min end len) result 0)
  211. result)))))
  212. (define vector-reverse-copy
  213. (let ()
  214. (define (%vector-reverse-copy vec start end)
  215. (let* ((len (- end start))
  216. (result (make-vector len)))
  217. (let loop ((i 0) (j (- end 1)))
  218. (unless (= i len)
  219. (vector-set! result i (vector-ref vec j))
  220. (loop (+ i 1) (- j 1))))
  221. result))
  222. (case-lambda
  223. "(vector-reverse-copy vec [start [end]]) -> vector
  224. Allocate a new vector whose length is END - START and fills it with
  225. elements from vec, taking elements from vec in reverse order starting
  226. at index START and stopping at index END. START defaults to 0 and END
  227. defaults to the value of (vector-length VEC)."
  228. ((vec)
  229. (assert-vector vec 'vector-reverse-copy)
  230. (%vector-reverse-copy vec 0 (vector-length vec)))
  231. ((vec start)
  232. (assert-vector vec 'vector-reverse-copy)
  233. (let ((len (vector-length vec)))
  234. (assert-valid-start start len 'vector-reverse-copy)
  235. (%vector-reverse-copy vec start len)))
  236. ((vec start end)
  237. (assert-vector vec 'vector-reverse-copy)
  238. (let ((len (vector-length vec)))
  239. (assert-valid-range start end len 'vector-reverse-copy)
  240. (%vector-reverse-copy vec start end))))))
  241. (define (%vector-concatenate vs)
  242. (let* ((result-len (let loop ((vs vs) (len 0))
  243. (if (null? vs)
  244. len
  245. (loop (cdr vs) (+ len (vector-length (car vs)))))))
  246. (result (make-vector result-len)))
  247. (let loop ((vs vs) (pos 0))
  248. (unless (null? vs)
  249. (let* ((v (car vs))
  250. (len (vector-length v)))
  251. (vector-move-left! v 0 len result pos)
  252. (loop (cdr vs) (+ pos len)))))
  253. result))
  254. (define vector-append
  255. (case-lambda
  256. "(vector-append vec ...) -> vector
  257. Return a newly allocated vector that contains all elements in order
  258. from the subsequent locations in VEC ..."
  259. (() (vector))
  260. ((v)
  261. (assert-vector v 'vector-append)
  262. (guile-vector-copy v))
  263. ((v1 v2)
  264. (assert-vector v1 'vector-append)
  265. (assert-vector v2 'vector-append)
  266. (let ((len1 (vector-length v1))
  267. (len2 (vector-length v2)))
  268. (let ((result (make-vector (+ len1 len2))))
  269. (vector-move-left! v1 0 len1 result 0)
  270. (vector-move-left! v2 0 len2 result len1)
  271. result)))
  272. (vs
  273. (assert-vectors vs 'vector-append)
  274. (%vector-concatenate vs))))
  275. (define (vector-concatenate vs)
  276. "(vector-concatenate list-of-vectors) -> vector
  277. Append each vector in LIST-OF-VECTORS. Equivalent to:
  278. (apply vector-append LIST-OF-VECTORS)"
  279. (assert-vectors vs 'vector-concatenate)
  280. (%vector-concatenate vs))
  281. (define (vector-empty? vec)
  282. "(vector-empty? vec) -> boolean
  283. Return true if VEC is empty, i.e. its length is 0, and false if not."
  284. (assert-vector vec 'vector-empty?)
  285. (zero? (vector-length vec)))
  286. (define vector=
  287. (let ()
  288. (define (all-of-length? len vs)
  289. (or (null? vs)
  290. (and (= len (vector-length (car vs)))
  291. (all-of-length? len (cdr vs)))))
  292. (define (=up-to? i elt=? v1 v2)
  293. (or (negative? i)
  294. (let ((x1 (vector-ref v1 i))
  295. (x2 (vector-ref v2 i)))
  296. (and (or (eq? x1 x2) (elt=? x1 x2))
  297. (=up-to? (- i 1) elt=? v1 v2)))))
  298. (case-lambda
  299. "(vector= elt=? vec ...) -> boolean
  300. Return true if the vectors VEC ... have equal lengths and equal
  301. elements according to ELT=?. ELT=? is always applied to two
  302. arguments. Element comparison must be consistent with eq?, in the
  303. following sense: if (eq? a b) returns true, then (elt=? a b) must also
  304. return true. The order in which comparisons are performed is
  305. unspecified."
  306. ((elt=?)
  307. (assert-procedure elt=? 'vector=)
  308. #t)
  309. ((elt=? v)
  310. (assert-procedure elt=? 'vector=)
  311. (assert-vector v 'vector=)
  312. #t)
  313. ((elt=? v1 v2)
  314. (assert-procedure elt=? 'vector=)
  315. (assert-vector v1 'vector=)
  316. (assert-vector v2 'vector=)
  317. (let ((len (vector-length v1)))
  318. (and (= len (vector-length v2))
  319. (=up-to? (- len 1) elt=? v1 v2))))
  320. ((elt=? v1 . vs)
  321. (assert-procedure elt=? 'vector=)
  322. (assert-vector v1 'vector=)
  323. (assert-vectors vs 'vector=)
  324. (let ((len (vector-length v1)))
  325. (and (all-of-length? len vs)
  326. (let loop ((vs vs))
  327. (or (null? vs)
  328. (and (=up-to? (- len 1) elt=? v1 (car vs))
  329. (loop (cdr vs)))))))))))
  330. (define vector-fold
  331. (case-lambda
  332. "(vector-fold kons knil vec1 vec2 ...) -> value
  333. The fundamental vector iterator. KONS is iterated over each index in
  334. all of the vectors, stopping at the end of the shortest; KONS is
  335. applied as (KONS i state (vector-ref VEC1 i) (vector-ref VEC2 i) ...)
  336. where STATE is the current state value, and I is the current index.
  337. The current state value begins with KNIL, and becomes whatever KONS
  338. returned at the respective iteration. The iteration is strictly
  339. left-to-right."
  340. ((kcons knil v)
  341. (assert-procedure kcons 'vector-fold)
  342. (assert-vector v 'vector-fold)
  343. (let ((len (vector-length v)))
  344. (let loop ((i 0) (state knil))
  345. (if (= i len)
  346. state
  347. (loop (+ i 1) (kcons i state (vector-ref v i)))))))
  348. ((kcons knil v1 v2)
  349. (assert-procedure kcons 'vector-fold)
  350. (assert-vector v1 'vector-fold)
  351. (assert-vector v2 'vector-fold)
  352. (let ((len (min (vector-length v1) (vector-length v2))))
  353. (let loop ((i 0) (state knil))
  354. (if (= i len)
  355. state
  356. (loop (+ i 1)
  357. (kcons i state (vector-ref v1 i) (vector-ref v2 i)))))))
  358. ((kcons knil . vs)
  359. (assert-procedure kcons 'vector-fold)
  360. (assert-vectors vs 'vector-fold)
  361. (let ((len (min-length vs)))
  362. (let loop ((i 0) (state knil))
  363. (if (= i len)
  364. state
  365. (loop (+ i 1) (apply kcons i state (vectors-ref vs i)))))))))
  366. (define vector-fold-right
  367. (case-lambda
  368. "(vector-fold-right kons knil vec1 vec2 ...) -> value
  369. The fundamental vector iterator. KONS is iterated over each index in
  370. all of the vectors, starting at the end of the shortest; KONS is
  371. applied as (KONS i state (vector-ref VEC1 i) (vector-ref VEC2 i) ...)
  372. where STATE is the current state value, and I is the current index.
  373. The current state value begins with KNIL, and becomes whatever KONS
  374. returned at the respective iteration. The iteration is strictly
  375. right-to-left."
  376. ((kcons knil v)
  377. (assert-procedure kcons 'vector-fold-right)
  378. (assert-vector v 'vector-fold-right)
  379. (let ((len (vector-length v)))
  380. (let loop ((i (- len 1)) (state knil))
  381. (if (negative? i)
  382. state
  383. (loop (- i 1) (kcons i state (vector-ref v i)))))))
  384. ((kcons knil v1 v2)
  385. (assert-procedure kcons 'vector-fold-right)
  386. (assert-vector v1 'vector-fold-right)
  387. (assert-vector v2 'vector-fold-right)
  388. (let ((len (min (vector-length v1) (vector-length v2))))
  389. (let loop ((i (- len 1)) (state knil))
  390. (if (negative? i)
  391. state
  392. (loop (- i 1)
  393. (kcons i state (vector-ref v1 i) (vector-ref v2 i)))))))
  394. ((kcons knil . vs)
  395. (assert-procedure kcons 'vector-fold-right)
  396. (assert-vectors vs 'vector-fold-right)
  397. (let ((len (min-length vs)))
  398. (let loop ((i (- len 1)) (state knil))
  399. (if (negative? i)
  400. state
  401. (loop (- i 1) (apply kcons i state (vectors-ref vs i)))))))))
  402. (define vector-map
  403. (case-lambda
  404. "(vector-map f vec2 vec2 ...) -> vector
  405. Return a new vector of the shortest size of the vector arguments.
  406. Each element at index i of the new vector is mapped from the old
  407. vectors by (F i (vector-ref VEC1 i) (vector-ref VEC2 i) ...). The
  408. dynamic order of application of F is unspecified."
  409. ((f v)
  410. (assert-procedure f 'vector-map)
  411. (assert-vector v 'vector-map)
  412. (let* ((len (vector-length v))
  413. (result (make-vector len)))
  414. (let loop ((i 0))
  415. (unless (= i len)
  416. (vector-set! result i (f i (vector-ref v i)))
  417. (loop (+ i 1))))
  418. result))
  419. ((f v1 v2)
  420. (assert-procedure f 'vector-map)
  421. (assert-vector v1 'vector-map)
  422. (assert-vector v2 'vector-map)
  423. (let* ((len (min (vector-length v1) (vector-length v2)))
  424. (result (make-vector len)))
  425. (let loop ((i 0))
  426. (unless (= i len)
  427. (vector-set! result i (f i (vector-ref v1 i) (vector-ref v2 i)))
  428. (loop (+ i 1))))
  429. result))
  430. ((f . vs)
  431. (assert-procedure f 'vector-map)
  432. (assert-vectors vs 'vector-map)
  433. (let* ((len (min-length vs))
  434. (result (make-vector len)))
  435. (let loop ((i 0))
  436. (unless (= i len)
  437. (vector-set! result i (apply f i (vectors-ref vs i)))
  438. (loop (+ i 1))))
  439. result))))
  440. (define vector-map!
  441. (case-lambda
  442. "(vector-map! f vec2 vec2 ...) -> unspecified
  443. Similar to vector-map, but rather than mapping the new elements into a
  444. new vector, the new mapped elements are destructively inserted into
  445. VEC1. The dynamic order of application of F is unspecified."
  446. ((f v)
  447. (assert-procedure f 'vector-map!)
  448. (assert-vector v 'vector-map!)
  449. (let ((len (vector-length v)))
  450. (let loop ((i 0))
  451. (unless (= i len)
  452. (vector-set! v i (f i (vector-ref v i)))
  453. (loop (+ i 1))))))
  454. ((f v1 v2)
  455. (assert-procedure f 'vector-map!)
  456. (assert-vector v1 'vector-map!)
  457. (assert-vector v2 'vector-map!)
  458. (let ((len (min (vector-length v1) (vector-length v2))))
  459. (let loop ((i 0))
  460. (unless (= i len)
  461. (vector-set! v1 i (f i (vector-ref v1 i) (vector-ref v2 i)))
  462. (loop (+ i 1))))))
  463. ((f . vs)
  464. (assert-procedure f 'vector-map!)
  465. (assert-vectors vs 'vector-map!)
  466. (let ((len (min-length vs))
  467. (v1 (car vs)))
  468. (let loop ((i 0))
  469. (unless (= i len)
  470. (vector-set! v1 i (apply f i (vectors-ref vs i)))
  471. (loop (+ i 1))))))))
  472. (define vector-for-each
  473. (case-lambda
  474. "(vector-for-each f vec1 vec2 ...) -> unspecified
  475. Call (F i VEC1[i] VEC2[i] ...) for each index i less than the length
  476. of the shortest vector passed. The iteration is strictly
  477. left-to-right."
  478. ((f v)
  479. (assert-procedure f 'vector-for-each)
  480. (assert-vector v 'vector-for-each)
  481. (let ((len (vector-length v)))
  482. (let loop ((i 0))
  483. (unless (= i len)
  484. (f i (vector-ref v i))
  485. (loop (+ i 1))))))
  486. ((f v1 v2)
  487. (assert-procedure f 'vector-for-each)
  488. (assert-vector v1 'vector-for-each)
  489. (assert-vector v2 'vector-for-each)
  490. (let ((len (min (vector-length v1)
  491. (vector-length v2))))
  492. (let loop ((i 0))
  493. (unless (= i len)
  494. (f i (vector-ref v1 i) (vector-ref v2 i))
  495. (loop (+ i 1))))))
  496. ((f . vs)
  497. (assert-procedure f 'vector-for-each)
  498. (assert-vectors vs 'vector-for-each)
  499. (let ((len (min-length vs)))
  500. (let loop ((i 0))
  501. (unless (= i len)
  502. (apply f i (vectors-ref vs i))
  503. (loop (+ i 1))))))))
  504. (define vector-count
  505. (case-lambda
  506. "(vector-count pred? vec1 vec2 ...) -> exact nonnegative integer
  507. Count the number of indices i for which (PRED? VEC1[i] VEC2[i] ...)
  508. returns true, where i is less than the length of the shortest vector
  509. passed."
  510. ((pred? v)
  511. (assert-procedure pred? 'vector-count)
  512. (assert-vector v 'vector-count)
  513. (let ((len (vector-length v)))
  514. (let loop ((i 0) (count 0))
  515. (cond ((= i len) count)
  516. ((pred? i (vector-ref v i))
  517. (loop (+ i 1) (+ count 1)))
  518. (else
  519. (loop (+ i 1) count))))))
  520. ((pred? v1 v2)
  521. (assert-procedure pred? 'vector-count)
  522. (assert-vector v1 'vector-count)
  523. (assert-vector v2 'vector-count)
  524. (let ((len (min (vector-length v1)
  525. (vector-length v2))))
  526. (let loop ((i 0) (count 0))
  527. (cond ((= i len) count)
  528. ((pred? i (vector-ref v1 i) (vector-ref v2 i))
  529. (loop (+ i 1) (+ count 1)))
  530. (else
  531. (loop (+ i 1) count))))))
  532. ((pred? . vs)
  533. (assert-procedure pred? 'vector-count)
  534. (assert-vectors vs 'vector-count)
  535. (let ((len (min-length vs)))
  536. (let loop ((i 0) (count 0))
  537. (cond ((= i len) count)
  538. ((apply pred? i (vectors-ref vs i))
  539. (loop (+ i 1) (+ count 1)))
  540. (else
  541. (loop (+ i 1) count))))))))
  542. (define vector-index
  543. (case-lambda
  544. "(vector-index pred? vec1 vec2 ...) -> exact nonnegative integer or #f
  545. Find and return the index of the first elements in VEC1 VEC2 ... that
  546. satisfy PRED?. If no matching element is found by the end of the
  547. shortest vector, return #f."
  548. ((pred? v)
  549. (assert-procedure pred? 'vector-index)
  550. (assert-vector v 'vector-index)
  551. (let ((len (vector-length v)))
  552. (let loop ((i 0))
  553. (and (< i len)
  554. (if (pred? (vector-ref v i))
  555. i
  556. (loop (+ i 1)))))))
  557. ((pred? v1 v2)
  558. (assert-procedure pred? 'vector-index)
  559. (assert-vector v1 'vector-index)
  560. (assert-vector v2 'vector-index)
  561. (let ((len (min (vector-length v1)
  562. (vector-length v2))))
  563. (let loop ((i 0))
  564. (and (< i len)
  565. (if (pred? (vector-ref v1 i)
  566. (vector-ref v2 i))
  567. i
  568. (loop (+ i 1)))))))
  569. ((pred? . vs)
  570. (assert-procedure pred? 'vector-index)
  571. (assert-vectors vs 'vector-index)
  572. (let ((len (min-length vs)))
  573. (let loop ((i 0))
  574. (and (< i len)
  575. (if (apply pred? (vectors-ref vs i))
  576. i
  577. (loop (+ i 1)))))))))
  578. (define vector-index-right
  579. (case-lambda
  580. "(vector-index-right pred? vec1 vec2 ...) -> exact nonnegative integer or #f
  581. Find and return the index of the last elements in VEC1 VEC2 ... that
  582. satisfy PRED?, searching from right-to-left. If no matching element
  583. is found before the end of the shortest vector, return #f."
  584. ((pred? v)
  585. (assert-procedure pred? 'vector-index-right)
  586. (assert-vector v 'vector-index-right)
  587. (let ((len (vector-length v)))
  588. (let loop ((i (- len 1)))
  589. (and (>= i 0)
  590. (if (pred? (vector-ref v i))
  591. i
  592. (loop (- i 1)))))))
  593. ((pred? v1 v2)
  594. (assert-procedure pred? 'vector-index-right)
  595. (assert-vector v1 'vector-index-right)
  596. (assert-vector v2 'vector-index-right)
  597. (let ((len (min (vector-length v1)
  598. (vector-length v2))))
  599. (let loop ((i (- len 1)))
  600. (and (>= i 0)
  601. (if (pred? (vector-ref v1 i)
  602. (vector-ref v2 i))
  603. i
  604. (loop (- i 1)))))))
  605. ((pred? . vs)
  606. (assert-procedure pred? 'vector-index-right)
  607. (assert-vectors vs 'vector-index-right)
  608. (let ((len (min-length vs)))
  609. (let loop ((i (- len 1)))
  610. (and (>= i 0)
  611. (if (apply pred? (vectors-ref vs i))
  612. i
  613. (loop (- i 1)))))))))
  614. (define vector-skip
  615. (case-lambda
  616. "(vector-skip pred? vec1 vec2 ...) -> exact nonnegative integer or #f
  617. Find and return the index of the first elements in VEC1 VEC2 ... that
  618. do not satisfy PRED?. If no matching element is found by the end of
  619. the shortest vector, return #f."
  620. ((pred? v)
  621. (assert-procedure pred? 'vector-skip)
  622. (assert-vector v 'vector-skip)
  623. (let ((len (vector-length v)))
  624. (let loop ((i 0))
  625. (and (< i len)
  626. (if (pred? (vector-ref v i))
  627. (loop (+ i 1))
  628. i)))))
  629. ((pred? v1 v2)
  630. (assert-procedure pred? 'vector-skip)
  631. (assert-vector v1 'vector-skip)
  632. (assert-vector v2 'vector-skip)
  633. (let ((len (min (vector-length v1)
  634. (vector-length v2))))
  635. (let loop ((i 0))
  636. (and (< i len)
  637. (if (pred? (vector-ref v1 i)
  638. (vector-ref v2 i))
  639. (loop (+ i 1))
  640. i)))))
  641. ((pred? . vs)
  642. (assert-procedure pred? 'vector-skip)
  643. (assert-vectors vs 'vector-skip)
  644. (let ((len (min-length vs)))
  645. (let loop ((i 0))
  646. (and (< i len)
  647. (if (apply pred? (vectors-ref vs i))
  648. (loop (+ i 1))
  649. i)))))))
  650. (define vector-skip-right
  651. (case-lambda
  652. "(vector-skip-right pred? vec1 vec2 ...) -> exact nonnegative integer or #f
  653. Find and return the index of the last elements in VEC1 VEC2 ... that
  654. do not satisfy PRED?, searching from right-to-left. If no matching
  655. element is found before the end of the shortest vector, return #f."
  656. ((pred? v)
  657. (assert-procedure pred? 'vector-skip-right)
  658. (assert-vector v 'vector-skip-right)
  659. (let ((len (vector-length v)))
  660. (let loop ((i (- len 1)))
  661. (and (not (negative? i))
  662. (if (pred? (vector-ref v i))
  663. (loop (- i 1))
  664. i)))))
  665. ((pred? v1 v2)
  666. (assert-procedure pred? 'vector-skip-right)
  667. (assert-vector v1 'vector-skip-right)
  668. (assert-vector v2 'vector-skip-right)
  669. (let ((len (min (vector-length v1)
  670. (vector-length v2))))
  671. (let loop ((i (- len 1)))
  672. (and (not (negative? i))
  673. (if (pred? (vector-ref v1 i)
  674. (vector-ref v2 i))
  675. (loop (- i 1))
  676. i)))))
  677. ((pred? . vs)
  678. (assert-procedure pred? 'vector-skip-right)
  679. (assert-vectors vs 'vector-skip-right)
  680. (let ((len (min-length vs)))
  681. (let loop ((i (- len 1)))
  682. (and (not (negative? i))
  683. (if (apply pred? (vectors-ref vs i))
  684. (loop (- i 1))
  685. i)))))))
  686. (define vector-binary-search
  687. (let ()
  688. (define (%vector-binary-search vec value cmp start end)
  689. (let loop ((lo start) (hi end))
  690. (and (< lo hi)
  691. (let* ((i (quotient (+ lo hi) 2))
  692. (x (vector-ref vec i))
  693. (c (cmp x value)))
  694. (cond ((zero? c) i)
  695. ((positive? c) (loop lo i))
  696. ((negative? c) (loop (+ i 1) hi)))))))
  697. (case-lambda
  698. "(vector-binary-search vec value cmp [start [end]]) -> exact nonnegative integer or #f
  699. Find and return an index of VEC between START and END whose value is
  700. VALUE using a binary search. If no matching element is found, return
  701. #f. The default START is 0 and the default END is the length of VEC.
  702. CMP must be a procedure of two arguments such that (CMP A B) returns
  703. a negative integer if A < B, a positive integer if A > B, or zero if
  704. A = B. The elements of VEC must be sorted in non-decreasing order
  705. according to CMP."
  706. ((vec value cmp)
  707. (assert-vector vec 'vector-binary-search)
  708. (assert-procedure cmp 'vector-binary-search)
  709. (%vector-binary-search vec value cmp 0 (vector-length vec)))
  710. ((vec value cmp start)
  711. (assert-vector vec 'vector-binary-search)
  712. (let ((len (vector-length vec)))
  713. (assert-valid-start start len 'vector-binary-search)
  714. (%vector-binary-search vec value cmp start len)))
  715. ((vec value cmp start end)
  716. (assert-vector vec 'vector-binary-search)
  717. (let ((len (vector-length vec)))
  718. (assert-valid-range start end len 'vector-binary-search)
  719. (%vector-binary-search vec value cmp start end))))))
  720. (define vector-any
  721. (case-lambda
  722. "(vector-any pred? vec1 vec2 ...) -> value or #f
  723. Find the first parallel set of elements from VEC1 VEC2 ... for which
  724. PRED? returns a true value. If such a parallel set of elements
  725. exists, vector-any returns the value that PRED? returned for that set
  726. of elements. The iteration is strictly left-to-right."
  727. ((pred? v)
  728. (assert-procedure pred? 'vector-any)
  729. (assert-vector v 'vector-any)
  730. (let ((len (vector-length v)))
  731. (let loop ((i 0))
  732. (and (< i len)
  733. (or (pred? (vector-ref v i))
  734. (loop (+ i 1)))))))
  735. ((pred? v1 v2)
  736. (assert-procedure pred? 'vector-any)
  737. (assert-vector v1 'vector-any)
  738. (assert-vector v2 'vector-any)
  739. (let ((len (min (vector-length v1)
  740. (vector-length v2))))
  741. (let loop ((i 0))
  742. (and (< i len)
  743. (or (pred? (vector-ref v1 i)
  744. (vector-ref v2 i))
  745. (loop (+ i 1)))))))
  746. ((pred? . vs)
  747. (assert-procedure pred? 'vector-any)
  748. (assert-vectors vs 'vector-any)
  749. (let ((len (min-length vs)))
  750. (let loop ((i 0))
  751. (and (< i len)
  752. (or (apply pred? (vectors-ref vs i))
  753. (loop (+ i 1)))))))))
  754. (define vector-every
  755. (case-lambda
  756. "(vector-every pred? vec1 vec2 ...) -> value or #f
  757. If, for every index i less than the length of the shortest vector
  758. argument, the set of elements VEC1[i] VEC2[i] ... satisfies PRED?,
  759. vector-every returns the value that PRED? returned for the last set of
  760. elements, at the last index of the shortest vector. The iteration is
  761. strictly left-to-right."
  762. ((pred? v)
  763. (assert-procedure pred? 'vector-every)
  764. (assert-vector v 'vector-every)
  765. (let ((len (vector-length v)))
  766. (or (zero? len)
  767. (let loop ((i 0))
  768. (let ((val (pred? (vector-ref v i)))
  769. (next-i (+ i 1)))
  770. (if (or (not val) (= next-i len))
  771. val
  772. (loop next-i)))))))
  773. ((pred? v1 v2)
  774. (assert-procedure pred? 'vector-every)
  775. (assert-vector v1 'vector-every)
  776. (assert-vector v2 'vector-every)
  777. (let ((len (min (vector-length v1)
  778. (vector-length v2))))
  779. (or (zero? len)
  780. (let loop ((i 0))
  781. (let ((val (pred? (vector-ref v1 i)
  782. (vector-ref v2 i)))
  783. (next-i (+ i 1)))
  784. (if (or (not val) (= next-i len))
  785. val
  786. (loop next-i)))))))
  787. ((pred? . vs)
  788. (assert-procedure pred? 'vector-every)
  789. (assert-vectors vs 'vector-every)
  790. (let ((len (min-length vs)))
  791. (or (zero? len)
  792. (let loop ((i 0))
  793. (let ((val (apply pred? (vectors-ref vs i)))
  794. (next-i (+ i 1)))
  795. (if (or (not val) (= next-i len))
  796. val
  797. (loop next-i)))))))))
  798. (define (vector-swap! vec i j)
  799. "(vector-swap! vec i j) -> unspecified
  800. Swap the values of the locations in VEC at I and J."
  801. (assert-vector vec 'vector-swap!)
  802. (let ((len (vector-length vec)))
  803. (assert-valid-index i len 'vector-swap!)
  804. (assert-valid-index j len 'vector-swap!)
  805. (let ((tmp (vector-ref vec i)))
  806. (vector-set! vec i (vector-ref vec j))
  807. (vector-set! vec j tmp))))
  808. ;; TODO: Enhance Guile core 'vector-fill!' to do this.
  809. (define vector-fill!
  810. (let ()
  811. (define guile-vector-fill!
  812. (@ (guile) vector-fill!))
  813. (define (%vector-fill! vec fill start end)
  814. (let loop ((i start))
  815. (when (< i end)
  816. (vector-set! vec i fill)
  817. (loop (+ i 1)))))
  818. (case-lambda
  819. "(vector-fill! vec fill [start [end]]) -> unspecified
  820. Assign the value of every location in VEC between START and END to
  821. FILL. START defaults to 0 and END defaults to the length of VEC."
  822. ((vec fill)
  823. (guile-vector-fill! vec fill))
  824. ((vec fill start)
  825. (assert-vector vec 'vector-fill!)
  826. (let ((len (vector-length vec)))
  827. (assert-valid-start start len 'vector-fill!)
  828. (%vector-fill! vec fill start len)))
  829. ((vec fill start end)
  830. (assert-vector vec 'vector-fill!)
  831. (let ((len (vector-length vec)))
  832. (assert-valid-range start end len 'vector-fill!)
  833. (%vector-fill! vec fill start end))))))
  834. (define (%vector-reverse! vec start end)
  835. (let loop ((i start) (j (- end 1)))
  836. (when (< i j)
  837. (let ((tmp (vector-ref vec i)))
  838. (vector-set! vec i (vector-ref vec j))
  839. (vector-set! vec j tmp)
  840. (loop (+ i 1) (- j 1))))))
  841. (define vector-reverse!
  842. (case-lambda
  843. "(vector-reverse! vec [start [end]]) -> unspecified
  844. Destructively reverse the contents of VEC between START and END.
  845. START defaults to 0 and END defaults to the length of VEC."
  846. ((vec)
  847. (assert-vector vec 'vector-reverse!)
  848. (%vector-reverse! vec 0 (vector-length vec)))
  849. ((vec start)
  850. (assert-vector vec 'vector-reverse!)
  851. (let ((len (vector-length vec)))
  852. (assert-valid-start start len 'vector-reverse!)
  853. (%vector-reverse! vec start len)))
  854. ((vec start end)
  855. (assert-vector vec 'vector-reverse!)
  856. (let ((len (vector-length vec)))
  857. (assert-valid-range start end len 'vector-reverse!)
  858. (%vector-reverse! vec start end)))))
  859. (define-syntax-rule (define-vector-copier! copy! docstring inner-proc)
  860. (define copy!
  861. (let ((%copy! inner-proc))
  862. (case-lambda
  863. docstring
  864. ((target tstart source)
  865. (assert-vector target 'copy!)
  866. (assert-vector source 'copy!)
  867. (let ((tlen (vector-length target))
  868. (slen (vector-length source)))
  869. (assert-valid-start tstart tlen 'copy!)
  870. (unless (>= tlen (+ tstart slen))
  871. (error-from 'copy! "would write past end of target"))
  872. (%copy! target tstart source 0 slen)))
  873. ((target tstart source sstart)
  874. (assert-vector target 'copy!)
  875. (assert-vector source 'copy!)
  876. (let ((tlen (vector-length target))
  877. (slen (vector-length source)))
  878. (assert-valid-start tstart tlen 'copy!)
  879. (assert-valid-start sstart slen 'copy!)
  880. (unless (>= tlen (+ tstart (- slen sstart)))
  881. (error-from 'copy! "would write past end of target"))
  882. (%copy! target tstart source sstart slen)))
  883. ((target tstart source sstart send)
  884. (assert-vector target 'copy!)
  885. (assert-vector source 'copy!)
  886. (let ((tlen (vector-length target))
  887. (slen (vector-length source)))
  888. (assert-valid-start tstart tlen 'copy!)
  889. (assert-valid-range sstart send slen 'copy!)
  890. (unless (>= tlen (+ tstart (- send sstart)))
  891. (error-from 'copy! "would write past end of target"))
  892. (%copy! target tstart source sstart send)))))))
  893. (define-vector-copier! vector-copy!
  894. "(vector-copy! target tstart source [sstart [send]]) -> unspecified
  895. Copy a block of elements from SOURCE to TARGET, both of which must be
  896. vectors, starting in TARGET at TSTART and starting in SOURCE at
  897. SSTART, ending when SEND - SSTART elements have been copied. It is an
  898. error for TARGET to have a length less than TSTART + (SEND - SSTART).
  899. SSTART defaults to 0 and SEND defaults to the length of SOURCE."
  900. (lambda (target tstart source sstart send)
  901. (if (< tstart sstart)
  902. (vector-move-left! source sstart send target tstart)
  903. (vector-move-right! source sstart send target tstart))))
  904. (define-vector-copier! vector-reverse-copy!
  905. "(vector-reverse-copy! target tstart source [sstart [send]]) -> unspecified
  906. Like vector-copy!, but copy the elements in the reverse order. It is
  907. an error if TARGET and SOURCE are identical vectors and the TARGET and
  908. SOURCE ranges overlap; however, if TSTART = SSTART,
  909. vector-reverse-copy! behaves as (vector-reverse! TARGET TSTART SEND)
  910. would."
  911. (lambda (target tstart source sstart send)
  912. (if (and (eq? target source) (= tstart sstart))
  913. (%vector-reverse! target sstart send)
  914. (let loop ((i tstart) (j (- send 1)))
  915. (when (>= j sstart)
  916. (vector-set! target i (vector-ref source j))
  917. (loop (+ i 1) (- j 1)))))))
  918. (define vector->list
  919. (let ()
  920. (define (%vector->list vec start end)
  921. (let loop ((i (- end 1))
  922. (result '()))
  923. (if (< i start)
  924. result
  925. (loop (- i 1) (cons (vector-ref vec i) result)))))
  926. (case-lambda
  927. "(vector->list vec [start [end]]) -> proper-list
  928. Return a newly allocated list containing the elements in VEC between
  929. START and END. START defaults to 0 and END defaults to the length of
  930. VEC."
  931. ((vec)
  932. (assert-vector vec 'vector->list)
  933. (%vector->list vec 0 (vector-length vec)))
  934. ((vec start)
  935. (assert-vector vec 'vector->list)
  936. (let ((len (vector-length vec)))
  937. (assert-valid-start start len 'vector->list)
  938. (%vector->list vec start len)))
  939. ((vec start end)
  940. (assert-vector vec 'vector->list)
  941. (let ((len (vector-length vec)))
  942. (assert-valid-range start end len 'vector->list)
  943. (%vector->list vec start end))))))
  944. (define reverse-vector->list
  945. (let ()
  946. (define (%reverse-vector->list vec start end)
  947. (let loop ((i start)
  948. (result '()))
  949. (if (>= i end)
  950. result
  951. (loop (+ i 1) (cons (vector-ref vec i) result)))))
  952. (case-lambda
  953. "(reverse-vector->list vec [start [end]]) -> proper-list
  954. Return a newly allocated list containing the elements in VEC between
  955. START and END in reverse order. START defaults to 0 and END defaults
  956. to the length of VEC."
  957. ((vec)
  958. (assert-vector vec 'reverse-vector->list)
  959. (%reverse-vector->list vec 0 (vector-length vec)))
  960. ((vec start)
  961. (assert-vector vec 'reverse-vector->list)
  962. (let ((len (vector-length vec)))
  963. (assert-valid-start start len 'reverse-vector->list)
  964. (%reverse-vector->list vec start len)))
  965. ((vec start end)
  966. (assert-vector vec 'reverse-vector->list)
  967. (let ((len (vector-length vec)))
  968. (assert-valid-range start end len 'reverse-vector->list)
  969. (%reverse-vector->list vec start end))))))
  970. ;; TODO: change to use 'case-lambda' and improve error checking.
  971. (define* (list->vector lst #:optional (start 0) (end (length lst)))
  972. "(list->vector proper-list [start [end]]) -> vector
  973. Return a newly allocated vector of the elements from PROPER-LIST with
  974. indices between START and END. START defaults to 0 and END defaults
  975. to the length of PROPER-LIST."
  976. (let* ((len (- end start))
  977. (result (make-vector len)))
  978. (let loop ((i 0) (lst (drop lst start)))
  979. (if (= i len)
  980. result
  981. (begin (vector-set! result i (car lst))
  982. (loop (+ i 1) (cdr lst)))))))
  983. ;; TODO: change to use 'case-lambda' and improve error checking.
  984. (define* (reverse-list->vector lst #:optional (start 0) (end (length lst)))
  985. "(reverse-list->vector proper-list [start [end]]) -> vector
  986. Return a newly allocated vector of the elements from PROPER-LIST with
  987. indices between START and END, in reverse order. START defaults to 0
  988. and END defaults to the length of PROPER-LIST."
  989. (let* ((len (- end start))
  990. (result (make-vector len)))
  991. (let loop ((i (- len 1)) (lst (drop lst start)))
  992. (if (negative? i)
  993. result
  994. (begin (vector-set! result i (car lst))
  995. (loop (- i 1) (cdr lst)))))))