43.upstream.scm 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. ;;;;;; SRFI 43: Vector library -*- Scheme -*-
  2. ;;;
  3. ;;; $Id: vector-lib.scm,v 1.7 2009/03/29 09:46:03 sperber Exp $
  4. ;;;
  5. ;;; Taylor Campbell wrote this code; he places it in the public domain.
  6. ;;; Will Clinger [wdc] made some corrections, also in the public domain.
  7. ;;; Copyright (C) Taylor Campbell (2003). All rights reserved.
  8. ;;; Permission is hereby granted, free of charge, to any person obtaining a copy
  9. ;;; of this software and associated documentation files (the "Software"), to
  10. ;;; deal in the Software without restriction, including without limitation the
  11. ;;; rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  12. ;;; sell copies of the Software, and to permit persons to whom the Software is
  13. ;;; furnished to do so, subject to the following conditions:
  14. ;;; The above copyright notice and this permission notice shall be included in
  15. ;;; all copies or substantial portions of the Software.
  16. ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. ;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. ;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. ;;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. ;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. ;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. ;;; IN THE SOFTWARE.
  23. ;;; --------------------
  24. ;;; Exported procedure index
  25. ;;;
  26. ;;; * Constructors
  27. ;;; make-vector vector
  28. ;;; vector-unfold vector-unfold-right
  29. ;;; vector-copy vector-reverse-copy
  30. ;;; vector-append vector-concatenate
  31. ;;;
  32. ;;; * Predicates
  33. ;;; vector?
  34. ;;; vector-empty?
  35. ;;; vector=
  36. ;;;
  37. ;;; * Selectors
  38. ;;; vector-ref
  39. ;;; vector-length
  40. ;;;
  41. ;;; * Iteration
  42. ;;; vector-fold vector-fold-right
  43. ;;; vector-map vector-map!
  44. ;;; vector-for-each
  45. ;;; vector-count
  46. ;;;
  47. ;;; * Searching
  48. ;;; vector-index vector-skip
  49. ;;; vector-index-right vector-skip-right
  50. ;;; vector-binary-search
  51. ;;; vector-any vector-every
  52. ;;;
  53. ;;; * Mutators
  54. ;;; vector-set!
  55. ;;; vector-swap!
  56. ;;; vector-fill!
  57. ;;; vector-reverse!
  58. ;;; vector-copy! vector-reverse-copy!
  59. ;;; vector-reverse!
  60. ;;;
  61. ;;; * Conversion
  62. ;;; vector->list reverse-vector->list
  63. ;;; list->vector reverse-list->vector
  64. ;;; --------------------
  65. ;;; Commentary on efficiency of the code
  66. ;;; This code is somewhat tuned for efficiency. There are several
  67. ;;; internal routines that can be optimized greatly to greatly improve
  68. ;;; the performance of much of the library. These internal procedures
  69. ;;; are already carefully tuned for performance, and lambda-lifted by
  70. ;;; hand. Some other routines are lambda-lifted by hand, but only the
  71. ;;; loops are lambda-lifted, and only if some routine has two possible
  72. ;;; loops -- a fast path and an n-ary case --, whereas _all_ of the
  73. ;;; internal routines' loops are lambda-lifted so as to never cons a
  74. ;;; closure in their body (VECTOR-PARSE-START+END doesn't have a loop),
  75. ;;; even in Scheme systems that perform no loop optimization (which is
  76. ;;; most of them, unfortunately).
  77. ;;;
  78. ;;; Fast paths are provided for common cases in most of the loops in
  79. ;;; this library.
  80. ;;;
  81. ;;; All calls to primitive vector operations are protected by a prior
  82. ;;; type check; they can be safely converted to use unsafe equivalents
  83. ;;; of the operations, if available. Ideally, the compiler should be
  84. ;;; able to determine this, but the state of Scheme compilers today is
  85. ;;; not a happy one.
  86. ;;;
  87. ;;; Efficiency of the actual algorithms is a rather mundane point to
  88. ;;; mention; vector operations are rarely beyond being straightforward.
  89. ;;; --------------------
  90. ;;; Utilities
  91. ;;; SRFI 8, too trivial to put in the dependencies list.
  92. (define-syntax receive
  93. (syntax-rules ()
  94. ((receive ?formals ?producer ?body1 ?body2 ...)
  95. (call-with-values (lambda () ?producer)
  96. (lambda ?formals ?body1 ?body2 ...)))))
  97. ;;; Not the best LET*-OPTIONALS, but not the worst, either. Use Olin's
  98. ;;; if it's available to you.
  99. (define-syntax let*-optionals
  100. (syntax-rules ()
  101. ((let*-optionals (?x ...) ((?var ?default) ...) ?body1 ?body2 ...)
  102. (let ((args (?x ...)))
  103. (let*-optionals args ((?var ?default) ...) ?body1 ?body2 ...)))
  104. ((let*-optionals ?args ((?var ?default) ...) ?body1 ?body2 ...)
  105. (let*-optionals:aux ?args ?args ((?var ?default) ...)
  106. ?body1 ?body2 ...))))
  107. (define-syntax let*-optionals:aux
  108. (syntax-rules ()
  109. ((aux ?orig-args-var ?args-var () ?body1 ?body2 ...)
  110. (if (null? ?args-var)
  111. (let () ?body1 ?body2 ...)
  112. (error "too many arguments" (length ?orig-args-var)
  113. ?orig-args-var)))
  114. ((aux ?orig-args-var ?args-var
  115. ((?var ?default) ?more ...)
  116. ?body1 ?body2 ...)
  117. (if (null? ?args-var)
  118. (let* ((?var ?default) ?more ...) ?body1 ?body2 ...)
  119. (let ((?var (car ?args-var))
  120. (new-args (cdr ?args-var)))
  121. (let*-optionals:aux ?orig-args-var new-args
  122. (?more ...)
  123. ?body1 ?body2 ...))))))
  124. (define (nonneg-int? x)
  125. (and (integer? x)
  126. (not (negative? x))))
  127. (define (between? x y z)
  128. (and (< x y)
  129. (<= y z)))
  130. (define (unspecified-value) (if #f #f))
  131. ;++ This should be implemented more efficiently. It shouldn't cons a
  132. ;++ closure, and the cons cells used in the loops when using this could
  133. ;++ be reused.
  134. (define (vectors-ref vectors i)
  135. (map (lambda (v) (vector-ref v i)) vectors))
  136. ;;; --------------------
  137. ;;; Error checking
  138. ;;; Error signalling (not checking) is done in a way that tries to be
  139. ;;; as helpful to the person who gets the debugging prompt as possible.
  140. ;;; That said, error _checking_ tries to be as unredundant as possible.
  141. ;;; I don't use any sort of general condition mechanism; I use simply
  142. ;;; SRFI 23's ERROR, even in cases where it might be better to use such
  143. ;;; a general condition mechanism. Fix that when porting this to a
  144. ;;; Scheme implementation that has its own condition system.
  145. ;;; In argument checks, upon receiving an invalid argument, the checker
  146. ;;; procedure recursively calls itself, but in one of the arguments to
  147. ;;; itself is a call to ERROR; this mechanism is used in the hopes that
  148. ;;; the user may be thrown into a debugger prompt, proceed with another
  149. ;;; value, and let it be checked again.
  150. ;;; Type checking is pretty basic, but easily factored out and replaced
  151. ;;; with whatever your implementation's preferred type checking method
  152. ;;; is. I doubt there will be many other methods of index checking,
  153. ;;; though the index checkers might be better implemented natively.
  154. ;;; (CHECK-TYPE <type-predicate?> <value> <callee>) -> value
  155. ;;; Ensure that VALUE satisfies TYPE-PREDICATE?; if not, signal an
  156. ;;; error stating that VALUE did not satisfy TYPE-PREDICATE?, showing
  157. ;;; that this happened while calling CALLEE. Return VALUE if no
  158. ;;; error was signalled.
  159. (define (check-type pred? value callee)
  160. (if (pred? value)
  161. value
  162. ;; Recur: when (or if) the user gets a debugger prompt, he can
  163. ;; proceed where the call to ERROR was with the correct value.
  164. (check-type pred?
  165. (error "erroneous value"
  166. (list pred? value)
  167. `(while calling ,callee))
  168. callee)))
  169. ;;; (CHECK-INDEX <vector> <index> <callee>) -> index
  170. ;;; Ensure that INDEX is a valid index into VECTOR; if not, signal an
  171. ;;; error stating that it is not and that this happened in a call to
  172. ;;; CALLEE. Return INDEX when it is valid. (Note that this does NOT
  173. ;;; check that VECTOR is indeed a vector.)
  174. (define (check-index vec index callee)
  175. (let ((index (check-type integer? index callee)))
  176. (cond ((< index 0)
  177. (check-index vec
  178. (error "vector index too low"
  179. index
  180. `(into vector ,vec)
  181. `(while calling ,callee))
  182. callee))
  183. ((>= index (vector-length vec))
  184. (check-index vec
  185. (error "vector index too high"
  186. index
  187. `(into vector ,vec)
  188. `(while calling ,callee))
  189. callee))
  190. (else index))))
  191. ;;; (CHECK-INDICES <vector>
  192. ;;; <start> <start-name>
  193. ;;; <end> <end-name>
  194. ;;; <caller>) -> [start end]
  195. ;;; Ensure that START and END are valid bounds of a range within
  196. ;;; VECTOR; if not, signal an error stating that they are not, with
  197. ;;; the message being informative about what the argument names were
  198. ;;; called -- by using START-NAME & END-NAME --, and that it occurred
  199. ;;; while calling CALLEE. Also ensure that VEC is in fact a vector.
  200. ;;; Returns no useful value.
  201. (define (check-indices vec start start-name end end-name callee)
  202. (let ((lose (lambda things
  203. (apply error "vector range out of bounds"
  204. (append things
  205. `(vector was ,vec)
  206. `(,start-name was ,start)
  207. `(,end-name was ,end)
  208. `(while calling ,callee)))))
  209. (start (check-type integer? start callee))
  210. (end (check-type integer? end callee)))
  211. (cond ((> start end)
  212. ;; I'm not sure how well this will work. The intent is that
  213. ;; the programmer tells the debugger to proceed with both a
  214. ;; new START & a new END by returning multiple values
  215. ;; somewhere.
  216. (receive (new-start new-end)
  217. (lose `(,end-name < ,start-name))
  218. (check-indices vec
  219. new-start start-name
  220. new-end end-name
  221. callee)))
  222. ((< start 0)
  223. (check-indices vec
  224. (lose `(,start-name < 0))
  225. start-name
  226. end end-name
  227. callee))
  228. ((>= start (vector-length vec))
  229. (check-indices vec
  230. (lose `(,start-name > len)
  231. `(len was ,(vector-length vec)))
  232. start-name
  233. end end-name
  234. callee))
  235. ((> end (vector-length vec))
  236. (check-indices vec
  237. start start-name
  238. (lose `(,end-name > len)
  239. `(len was ,(vector-length vec)))
  240. end-name
  241. callee))
  242. (else
  243. (values start end)))))
  244. ;;; --------------------
  245. ;;; Internal routines
  246. ;;; These should all be integrated, native, or otherwise optimized --
  247. ;;; they're used a _lot_ --. All of the loops and LETs inside loops
  248. ;;; are lambda-lifted by hand, just so as not to cons closures in the
  249. ;;; loops. (If your compiler can do better than that if they're not
  250. ;;; lambda-lifted, then lambda-drop (?) them.)
  251. ;;; (VECTOR-PARSE-START+END <vector> <arguments>
  252. ;;; <start-name> <end-name>
  253. ;;; <callee>)
  254. ;;; -> [start end]
  255. ;;; Return two values, composing a valid range within VECTOR, as
  256. ;;; extracted from ARGUMENTS or defaulted from VECTOR -- 0 for START
  257. ;;; and the length of VECTOR for END --; START-NAME and END-NAME are
  258. ;;; purely for error checking.
  259. (define (vector-parse-start+end vec args start-name end-name callee)
  260. (let ((len (vector-length vec)))
  261. (cond ((null? args)
  262. (values 0 len))
  263. ((null? (cdr args))
  264. (check-indices vec
  265. (car args) start-name
  266. len end-name
  267. callee))
  268. ((null? (cddr args))
  269. (check-indices vec
  270. (car args) start-name
  271. (cadr args) end-name
  272. callee))
  273. (else
  274. (error "too many arguments"
  275. `(extra args were ,(cddr args))
  276. `(while calling ,callee))))))
  277. (define-syntax let-vector-start+end
  278. (syntax-rules ()
  279. ((let-vector-start+end ?callee ?vec ?args (?start ?end)
  280. ?body1 ?body2 ...)
  281. (let ((?vec (check-type vector? ?vec ?callee)))
  282. (receive (?start ?end)
  283. (vector-parse-start+end ?vec ?args '?start '?end
  284. ?callee)
  285. ?body1 ?body2 ...)))))
  286. ;;; (%SMALLEST-LENGTH <vector-list> <default-length> <callee>)
  287. ;;; -> exact, nonnegative integer
  288. ;;; Compute the smallest length of VECTOR-LIST. DEFAULT-LENGTH is
  289. ;;; the length that is returned if VECTOR-LIST is empty. Common use
  290. ;;; of this is in n-ary vector routines:
  291. ;;; (define (f vec . vectors)
  292. ;;; (let ((vec (check-type vector? vec f)))
  293. ;;; ...(%smallest-length vectors (vector-length vec) f)...))
  294. ;;; %SMALLEST-LENGTH takes care of the type checking -- which is what
  295. ;;; the CALLEE argument is for --; thus, the design is tuned for
  296. ;;; avoiding redundant type checks.
  297. (define %smallest-length
  298. (letrec ((loop (lambda (vector-list length callee)
  299. (if (null? vector-list)
  300. length
  301. (loop (cdr vector-list)
  302. (min (vector-length
  303. (check-type vector?
  304. (car vector-list)
  305. callee))
  306. length)
  307. callee)))))
  308. loop))
  309. ;;; (%VECTOR-COPY! <target> <tstart> <source> <sstart> <send>)
  310. ;;; Copy elements at locations SSTART to SEND from SOURCE to TARGET,
  311. ;;; starting at TSTART in TARGET.
  312. ;;;
  313. ;;; Optimize this! Probably with some combination of:
  314. ;;; - Force it to be integrated.
  315. ;;; - Let it use unsafe vector element dereferencing routines: bounds
  316. ;;; checking already happens outside of it. (Or use a compiler
  317. ;;; that figures this out, but Olin Shivers' PhD thesis seems to
  318. ;;; have been largely ignored in actual implementations...)
  319. ;;; - Implement it natively as a VM primitive: the VM can undoubtedly
  320. ;;; perform much faster than it can make Scheme perform, even with
  321. ;;; bounds checking.
  322. ;;; - Implement it in assembly: you _want_ the fine control that
  323. ;;; assembly can give you for this.
  324. ;;; I already lambda-lift it by hand, but you should be able to make it
  325. ;;; even better than that.
  326. (define %vector-copy!
  327. (letrec ((loop/l->r (lambda (target source send i j)
  328. (cond ((< i send)
  329. (vector-set! target j
  330. (vector-ref source i))
  331. (loop/l->r target source send
  332. (+ i 1) (+ j 1))))))
  333. (loop/r->l (lambda (target source sstart i j)
  334. (cond ((>= i sstart)
  335. (vector-set! target j
  336. (vector-ref source i))
  337. (loop/r->l target source sstart
  338. (- i 1) (- j 1)))))))
  339. (lambda (target tstart source sstart send)
  340. (if (> sstart tstart) ; Make sure we don't copy over
  341. ; ourselves.
  342. (loop/l->r target source send sstart tstart)
  343. (loop/r->l target source sstart (- send 1)
  344. (+ -1 tstart send (- sstart)))))))
  345. ;;; (%VECTOR-REVERSE-COPY! <target> <tstart> <source> <sstart> <send>)
  346. ;;; Copy elements from SSTART to SEND from SOURCE to TARGET, in the
  347. ;;; reverse order.
  348. (define %vector-reverse-copy!
  349. (letrec ((loop (lambda (target source sstart i j)
  350. (cond ((>= i sstart)
  351. (vector-set! target j (vector-ref source i))
  352. (loop target source sstart
  353. (- i 1)
  354. (+ j 1)))))))
  355. (lambda (target tstart source sstart send)
  356. (loop target source sstart
  357. (- send 1)
  358. tstart))))
  359. ;;; (%VECTOR-REVERSE! <vector>)
  360. (define %vector-reverse!
  361. (letrec ((loop (lambda (vec i j)
  362. (cond ((<= i j)
  363. (let ((v (vector-ref vec i)))
  364. (vector-set! vec i (vector-ref vec j))
  365. (vector-set! vec j v)
  366. (loop vec (+ i 1) (- j 1))))))))
  367. (lambda (vec start end)
  368. (loop vec start (- end 1)))))
  369. ;;; (%VECTOR-FOLD1 <kons> <knil> <vector>) -> knil'
  370. ;;; (KONS <index> <knil> <elt>) -> knil'
  371. (define %vector-fold1
  372. (letrec ((loop (lambda (kons knil len vec i)
  373. (if (= i len)
  374. knil
  375. (loop kons
  376. (kons i knil (vector-ref vec i))
  377. len vec (+ i 1))))))
  378. (lambda (kons knil len vec)
  379. (loop kons knil len vec 0))))
  380. ;;; (%VECTOR-FOLD2+ <kons> <knil> <vector> ...) -> knil'
  381. ;;; (KONS <index> <knil> <elt> ...) -> knil'
  382. (define %vector-fold2+
  383. (letrec ((loop (lambda (kons knil len vectors i)
  384. (if (= i len)
  385. knil
  386. (loop kons
  387. (apply kons i knil
  388. (vectors-ref vectors i))
  389. len vectors (+ i 1))))))
  390. (lambda (kons knil len vectors)
  391. (loop kons knil len vectors 0))))
  392. ;;; (%VECTOR-MAP! <f> <target> <length> <vector>) -> target
  393. ;;; (F <index> <elt>) -> elt'
  394. (define %vector-map1!
  395. (letrec ((loop (lambda (f target vec i)
  396. (if (zero? i)
  397. target
  398. (let ((j (- i 1)))
  399. (vector-set! target j
  400. (f j (vector-ref vec j)))
  401. (loop f target vec j))))))
  402. (lambda (f target vec len)
  403. (loop f target vec len))))
  404. ;;; (%VECTOR-MAP2+! <f> <target> <vectors> <len>) -> target
  405. ;;; (F <index> <elt> ...) -> elt'
  406. (define %vector-map2+!
  407. (letrec ((loop (lambda (f target vectors i)
  408. (if (zero? i)
  409. target
  410. (let ((j (- i 1)))
  411. (vector-set! target j
  412. (apply f j (vectors-ref vectors j)))
  413. (loop f target vectors j))))))
  414. (lambda (f target vectors len)
  415. (loop f target vectors len))))
  416. ;;;;;;;;;;;;;;;;;;;;;;;; ***** vector-lib ***** ;;;;;;;;;;;;;;;;;;;;;;;
  417. ;;; --------------------
  418. ;;; Constructors
  419. ;;; (MAKE-VECTOR <size> [<fill>]) -> vector
  420. ;;; [R5RS] Create a vector of length LENGTH. If FILL is present,
  421. ;;; initialize each slot in the vector with it; if not, the vector's
  422. ;;; initial contents are unspecified.
  423. (define make-vector make-vector)
  424. ;;; (VECTOR <elt> ...) -> vector
  425. ;;; [R5RS] Create a vector containing ELEMENT ..., in order.
  426. (define vector vector)
  427. ;;; This ought to be able to be implemented much more efficiently -- if
  428. ;;; we have the number of arguments available to us, we can create the
  429. ;;; vector without using LENGTH to determine the number of elements it
  430. ;;; should have.
  431. ;(define (vector . elements) (list->vector elements))
  432. ;;; (VECTOR-UNFOLD <f> <length> <initial-seed> ...) -> vector
  433. ;;; (F <index> <seed> ...) -> [elt seed' ...]
  434. ;;; The fundamental vector constructor. Creates a vector whose
  435. ;;; length is LENGTH and iterates across each index K between 0 and
  436. ;;; LENGTH, applying F at each iteration to the current index and the
  437. ;;; current seeds to receive N+1 values: first, the element to put in
  438. ;;; the Kth slot and then N new seeds for the next iteration.
  439. (define vector-unfold
  440. (letrec ((tabulate! ; Special zero-seed case.
  441. (lambda (f vec i len)
  442. (cond ((< i len)
  443. (vector-set! vec i (f i))
  444. (tabulate! f vec (+ i 1) len)))))
  445. (unfold1! ; Fast path for one seed.
  446. (lambda (f vec i len seed)
  447. (if (< i len)
  448. (receive (elt new-seed)
  449. (f i seed)
  450. (vector-set! vec i elt)
  451. (unfold1! f vec (+ i 1) len new-seed)))))
  452. (unfold2+! ; Slower variant for N seeds.
  453. (lambda (f vec i len seeds)
  454. (if (< i len)
  455. (receive (elt . new-seeds)
  456. (apply f i seeds)
  457. (vector-set! vec i elt)
  458. (unfold2+! f vec (+ i 1) len new-seeds))))))
  459. (lambda (f len . initial-seeds)
  460. (let ((f (check-type procedure? f vector-unfold))
  461. (len (check-type nonneg-int? len vector-unfold)))
  462. (let ((vec (make-vector len)))
  463. (cond ((null? initial-seeds)
  464. (tabulate! f vec 0 len))
  465. ((null? (cdr initial-seeds))
  466. (unfold1! f vec 0 len (car initial-seeds)))
  467. (else
  468. (unfold2+! f vec 0 len initial-seeds)))
  469. vec)))))
  470. ;;; (VECTOR-UNFOLD-RIGHT <f> <length> <initial-seed> ...) -> vector
  471. ;;; (F <seed> ...) -> [seed' ...]
  472. ;;; Like VECTOR-UNFOLD, but it generates elements from LENGTH to 0
  473. ;;; (still exclusive with LENGTH and inclusive with 0), not 0 to
  474. ;;; LENGTH as with VECTOR-UNFOLD.
  475. (define vector-unfold-right
  476. (letrec ((tabulate!
  477. (lambda (f vec i)
  478. (cond ((>= i 0)
  479. (vector-set! vec i (f i))
  480. (tabulate! f vec (- i 1))))))
  481. (unfold1!
  482. (lambda (f vec i seed)
  483. (if (>= i 0)
  484. (receive (elt new-seed)
  485. (f i seed)
  486. (vector-set! vec i elt)
  487. (unfold1! f vec (- i 1) new-seed)))))
  488. (unfold2+!
  489. (lambda (f vec i seeds)
  490. (if (>= i 0)
  491. (receive (elt . new-seeds)
  492. (apply f i seeds)
  493. (vector-set! vec i elt)
  494. (unfold2+! f vec (- i 1) new-seeds))))))
  495. (lambda (f len . initial-seeds)
  496. (let ((f (check-type procedure? f vector-unfold-right))
  497. (len (check-type nonneg-int? len vector-unfold-right)))
  498. (let ((vec (make-vector len))
  499. (i (- len 1)))
  500. (cond ((null? initial-seeds)
  501. (tabulate! f vec i))
  502. ((null? (cdr initial-seeds))
  503. (unfold1! f vec i (car initial-seeds)))
  504. (else
  505. (unfold2+! f vec i initial-seeds)))
  506. vec)))))
  507. ;;; (VECTOR-COPY <vector> [<start> <end> <fill>]) -> vector
  508. ;;; Create a newly allocated vector containing the elements from the
  509. ;;; range [START,END) in VECTOR. START defaults to 0; END defaults
  510. ;;; to the length of VECTOR. END may be greater than the length of
  511. ;;; VECTOR, in which case the vector is enlarged; if FILL is passed,
  512. ;;; the new locations from which there is no respective element in
  513. ;;; VECTOR are filled with FILL.
  514. (define (vector-copy vec . args)
  515. (let ((vec (check-type vector? vec vector-copy)))
  516. ;; We can't use LET-VECTOR-START+END, because we have one more
  517. ;; argument, and we want finer control, too.
  518. ;;
  519. ;; Olin's implementation of LET*-OPTIONALS would prove useful here:
  520. ;; the built-in argument-checks-as-you-go-along produces almost
  521. ;; _exactly_ the same code as VECTOR-COPY:PARSE-ARGS.
  522. (receive (start end fill)
  523. (vector-copy:parse-args vec args)
  524. (let ((new-vector (make-vector (- end start) fill)))
  525. (%vector-copy! new-vector 0
  526. vec start
  527. (if (> end (vector-length vec))
  528. (vector-length vec)
  529. end))
  530. new-vector))))
  531. ;;; Auxiliary for VECTOR-COPY.
  532. ;;; [wdc] Corrected to allow 0 <= start <= (vector-length vec).
  533. (define (vector-copy:parse-args vec args)
  534. (define (parse-args start end n fill)
  535. (let ((start (check-type nonneg-int? start vector-copy))
  536. (end (check-type nonneg-int? end vector-copy)))
  537. (cond ((and (<= 0 start end)
  538. (<= start n))
  539. (values start end fill))
  540. (else
  541. (error "illegal arguments"
  542. `(while calling ,vector-copy)
  543. `(start was ,start)
  544. `(end was ,end)
  545. `(vector was ,vec))))))
  546. (let ((n (vector-length vec)))
  547. (cond ((null? args)
  548. (parse-args 0 n n (unspecified-value)))
  549. ((null? (cdr args))
  550. (parse-args (car args) n n (unspecified-value)))
  551. ((null? (cddr args))
  552. (parse-args (car args) (cadr args) n (unspecified-value)))
  553. ((null? (cdddr args))
  554. (parse-args (car args) (cadr args) n (caddr args)))
  555. (else
  556. (error "too many arguments"
  557. vector-copy
  558. (cdddr args))))))
  559. ;;; (VECTOR-REVERSE-COPY <vector> [<start> <end>]) -> vector
  560. ;;; Create a newly allocated vector whose elements are the reversed
  561. ;;; sequence of elements between START and END in VECTOR. START's
  562. ;;; default is 0; END's default is the length of VECTOR.
  563. (define (vector-reverse-copy vec . maybe-start+end)
  564. (let-vector-start+end vector-reverse-copy vec maybe-start+end
  565. (start end)
  566. (let ((new (make-vector (- end start))))
  567. (%vector-reverse-copy! new 0 vec start end)
  568. new)))
  569. ;;; (VECTOR-APPEND <vector> ...) -> vector
  570. ;;; Append VECTOR ... into a newly allocated vector and return that
  571. ;;; new vector.
  572. (define (vector-append . vectors)
  573. (vector-concatenate:aux vectors vector-append))
  574. ;;; (VECTOR-CONCATENATE <vector-list>) -> vector
  575. ;;; Concatenate the vectors in VECTOR-LIST. This is equivalent to
  576. ;;; (apply vector-append VECTOR-LIST)
  577. ;;; but VECTOR-APPEND tends to be implemented in terms of
  578. ;;; VECTOR-CONCATENATE, and some Schemes bork when the list to apply
  579. ;;; a function to is too long.
  580. ;;;
  581. ;;; Actually, they're both implemented in terms of an internal routine.
  582. (define (vector-concatenate vector-list)
  583. (vector-concatenate:aux vector-list vector-concatenate))
  584. ;;; Auxiliary for VECTOR-APPEND and VECTOR-CONCATENATE
  585. (define vector-concatenate:aux
  586. (letrec ((compute-length
  587. (lambda (vectors len callee)
  588. (if (null? vectors)
  589. len
  590. (let ((vec (check-type vector? (car vectors)
  591. callee)))
  592. (compute-length (cdr vectors)
  593. (+ (vector-length vec) len)
  594. callee)))))
  595. (concatenate!
  596. (lambda (vectors target to)
  597. (if (null? vectors)
  598. target
  599. (let* ((vec1 (car vectors))
  600. (len (vector-length vec1)))
  601. (%vector-copy! target to vec1 0 len)
  602. (concatenate! (cdr vectors) target
  603. (+ to len)))))))
  604. (lambda (vectors callee)
  605. (cond ((null? vectors) ;+++
  606. (make-vector 0))
  607. ((null? (cdr vectors)) ;+++
  608. ;; Blech, we still have to allocate a new one.
  609. (let* ((vec (check-type vector? (car vectors) callee))
  610. (len (vector-length vec))
  611. (new (make-vector len)))
  612. (%vector-copy! new 0 vec 0 len)
  613. new))
  614. (else
  615. (let ((new-vector
  616. (make-vector (compute-length vectors 0 callee))))
  617. (concatenate! vectors new-vector 0)
  618. new-vector))))))
  619. ;;; --------------------
  620. ;;; Predicates
  621. ;;; (VECTOR? <value>) -> boolean
  622. ;;; [R5RS] Return #T if VALUE is a vector and #F if not.
  623. (define vector? vector?)
  624. ;;; (VECTOR-EMPTY? <vector>) -> boolean
  625. ;;; Return #T if VECTOR has zero elements in it, i.e. VECTOR's length
  626. ;;; is 0, and #F if not.
  627. (define (vector-empty? vec)
  628. (let ((vec (check-type vector? vec vector-empty?)))
  629. (zero? (vector-length vec))))
  630. ;;; (VECTOR= <elt=?> <vector> ...) -> boolean
  631. ;;; (ELT=? <value> <value>) -> boolean
  632. ;;; Determine vector equality generalized across element comparators.
  633. ;;; Vectors A and B are equal iff their lengths are the same and for
  634. ;;; each respective elements E_a and E_b (element=? E_a E_b) returns
  635. ;;; a true value. ELT=? is always applied to two arguments. Element
  636. ;;; comparison must be consistent wtih EQ?; that is, if (eq? E_a E_b)
  637. ;;; results in a true value, then (ELEMENT=? E_a E_b) must result in a
  638. ;;; true value. This may be exploited to avoid multiple unnecessary
  639. ;;; element comparisons. (This implementation does, but does not deal
  640. ;;; with the situation that ELEMENT=? is EQ? to avoid more unnecessary
  641. ;;; comparisons, but I believe this optimization is probably fairly
  642. ;;; insignificant.)
  643. ;;;
  644. ;;; If the number of vector arguments is zero or one, then #T is
  645. ;;; automatically returned. If there are N vector arguments,
  646. ;;; VECTOR_1 VECTOR_2 ... VECTOR_N, then VECTOR_1 & VECTOR_2 are
  647. ;;; compared; if they are equal, the vectors VECTOR_2 ... VECTOR_N
  648. ;;; are compared. The precise order in which ELT=? is applied is not
  649. ;;; specified.
  650. (define (vector= elt=? . vectors)
  651. (let ((elt=? (check-type procedure? elt=? vector=)))
  652. (cond ((null? vectors)
  653. #t)
  654. ((null? (cdr vectors))
  655. (check-type vector? (car vectors) vector=)
  656. #t)
  657. (else
  658. (let loop ((vecs vectors))
  659. (let ((vec1 (check-type vector? (car vecs) vector=))
  660. (vec2+ (cdr vecs)))
  661. (or (null? vec2+)
  662. (and (binary-vector= elt=? vec1 (car vec2+))
  663. (loop vec2+)))))))))
  664. (define (binary-vector= elt=? vector-a vector-b)
  665. (or (eq? vector-a vector-b) ;+++
  666. (let ((length-a (vector-length vector-a))
  667. (length-b (vector-length vector-b)))
  668. (letrec ((loop (lambda (i)
  669. (or (= i length-a)
  670. (and (< i length-b)
  671. (test (vector-ref vector-a i)
  672. (vector-ref vector-b i)
  673. i)))))
  674. (test (lambda (elt-a elt-b i)
  675. (and (or (eq? elt-a elt-b) ;+++
  676. (elt=? elt-a elt-b))
  677. (loop (+ i 1))))))
  678. (and (= length-a length-b)
  679. (loop 0))))))
  680. ;;; --------------------
  681. ;;; Selectors
  682. ;;; (VECTOR-REF <vector> <index>) -> value
  683. ;;; [R5RS] Return the value that the location in VECTOR at INDEX is
  684. ;;; mapped to in the store.
  685. (define vector-ref vector-ref)
  686. ;;; (VECTOR-LENGTH <vector>) -> exact, nonnegative integer
  687. ;;; [R5RS] Return the length of VECTOR.
  688. (define vector-length vector-length)
  689. ;;; --------------------
  690. ;;; Iteration
  691. ;;; (VECTOR-FOLD <kons> <initial-knil> <vector> ...) -> knil
  692. ;;; (KONS <knil> <elt> ...) -> knil' ; N vectors -> N+1 args
  693. ;;; The fundamental vector iterator. KONS is iterated over each
  694. ;;; index in all of the vectors in parallel, stopping at the end of
  695. ;;; the shortest; KONS is applied to an argument list of (list I
  696. ;;; STATE (vector-ref VEC I) ...), where STATE is the current state
  697. ;;; value -- the state value begins with KNIL and becomes whatever
  698. ;;; KONS returned at the respective iteration --, and I is the
  699. ;;; current index in the iteration. The iteration is strictly left-
  700. ;;; to-right.
  701. ;;; (vector-fold KONS KNIL (vector E_1 E_2 ... E_N))
  702. ;;; <=>
  703. ;;; (KONS (... (KONS (KONS KNIL E_1) E_2) ... E_N-1) E_N)
  704. (define (vector-fold kons knil vec . vectors)
  705. (let ((kons (check-type procedure? kons vector-fold))
  706. (vec (check-type vector? vec vector-fold)))
  707. (if (null? vectors)
  708. (%vector-fold1 kons knil (vector-length vec) vec)
  709. (%vector-fold2+ kons knil
  710. (%smallest-length vectors
  711. (vector-length vec)
  712. vector-fold)
  713. (cons vec vectors)))))
  714. ;;; (VECTOR-FOLD-RIGHT <kons> <initial-knil> <vector> ...) -> knil
  715. ;;; (KONS <knil> <elt> ...) -> knil' ; N vectors => N+1 args
  716. ;;; The fundamental vector recursor. Iterates in parallel across
  717. ;;; VECTOR ... right to left, applying KONS to the elements and the
  718. ;;; current state value; the state value becomes what KONS returns
  719. ;;; at each next iteration. KNIL is the initial state value.
  720. ;;; (vector-fold-right KONS KNIL (vector E_1 E_2 ... E_N))
  721. ;;; <=>
  722. ;;; (KONS (... (KONS (KONS KNIL E_N) E_N-1) ... E_2) E_1)
  723. ;;;
  724. ;;; Not implemented in terms of a more primitive operations that might
  725. ;;; called %VECTOR-FOLD-RIGHT due to the fact that it wouldn't be very
  726. ;;; useful elsewhere.
  727. (define vector-fold-right
  728. (letrec ((loop1 (lambda (kons knil vec i)
  729. (if (negative? i)
  730. knil
  731. (loop1 kons (kons i knil (vector-ref vec i))
  732. vec
  733. (- i 1)))))
  734. (loop2+ (lambda (kons knil vectors i)
  735. (if (negative? i)
  736. knil
  737. (loop2+ kons
  738. (apply kons i knil
  739. (vectors-ref vectors i))
  740. vectors
  741. (- i 1))))))
  742. (lambda (kons knil vec . vectors)
  743. (let ((kons (check-type procedure? kons vector-fold-right))
  744. (vec (check-type vector? vec vector-fold-right)))
  745. (if (null? vectors)
  746. (loop1 kons knil vec (- (vector-length vec) 1))
  747. (loop2+ kons knil (cons vec vectors)
  748. (- (%smallest-length vectors
  749. (vector-length vec)
  750. vector-fold-right)
  751. 1)))))))
  752. ;;; (VECTOR-MAP <f> <vector> ...) -> vector
  753. ;;; (F <elt> ...) -> value ; N vectors -> N args
  754. ;;; Constructs a new vector of the shortest length of the vector
  755. ;;; arguments. Each element at index I of the new vector is mapped
  756. ;;; from the old vectors by (F I (vector-ref VECTOR I) ...). The
  757. ;;; dynamic order of application of F is unspecified.
  758. (define (vector-map f vec . vectors)
  759. (let ((f (check-type procedure? f vector-map))
  760. (vec (check-type vector? vec vector-map)))
  761. (if (null? vectors)
  762. (let ((len (vector-length vec)))
  763. (%vector-map1! f (make-vector len) vec len))
  764. (let ((len (%smallest-length vectors
  765. (vector-length vec)
  766. vector-map)))
  767. (%vector-map2+! f (make-vector len) (cons vec vectors)
  768. len)))))
  769. ;;; (VECTOR-MAP! <f> <vector> ...) -> unspecified
  770. ;;; (F <elt> ...) -> element' ; N vectors -> N args
  771. ;;; Similar to VECTOR-MAP, but rather than mapping the new elements
  772. ;;; into a new vector, the new mapped elements are destructively
  773. ;;; inserted into the first vector. Again, the dynamic order of
  774. ;;; application of F is unspecified, so it is dangerous for F to
  775. ;;; manipulate the first VECTOR.
  776. (define (vector-map! f vec . vectors)
  777. (let ((f (check-type procedure? f vector-map!))
  778. (vec (check-type vector? vec vector-map!)))
  779. (if (null? vectors)
  780. (%vector-map1! f vec vec (vector-length vec))
  781. (%vector-map2+! f vec (cons vec vectors)
  782. (%smallest-length vectors
  783. (vector-length vec)
  784. vector-map!)))
  785. (unspecified-value)))
  786. ;;; (VECTOR-FOR-EACH <f> <vector> ...) -> unspecified
  787. ;;; (F <elt> ...) ; N vectors -> N args
  788. ;;; Simple vector iterator: applies F to each index in the range [0,
  789. ;;; LENGTH), where LENGTH is the length of the smallest vector
  790. ;;; argument passed, and the respective element at that index. In
  791. ;;; contrast with VECTOR-MAP, F is reliably applied to each
  792. ;;; subsequent elements, starting at index 0 from left to right, in
  793. ;;; the vectors.
  794. (define vector-for-each
  795. (letrec ((for-each1
  796. (lambda (f vec i len)
  797. (cond ((< i len)
  798. (f i (vector-ref vec i))
  799. (for-each1 f vec (+ i 1) len)))))
  800. (for-each2+
  801. (lambda (f vecs i len)
  802. (cond ((< i len)
  803. (apply f i (vectors-ref vecs i))
  804. (for-each2+ f vecs (+ i 1) len))))))
  805. (lambda (f vec . vectors)
  806. (let ((f (check-type procedure? f vector-for-each))
  807. (vec (check-type vector? vec vector-for-each)))
  808. (if (null? vectors)
  809. (for-each1 f vec 0 (vector-length vec))
  810. (for-each2+ f (cons vec vectors) 0
  811. (%smallest-length vectors
  812. (vector-length vec)
  813. vector-for-each)))))))
  814. ;;; (VECTOR-COUNT <predicate?> <vector> ...)
  815. ;;; -> exact, nonnegative integer
  816. ;;; (PREDICATE? <index> <value> ...) ; N vectors -> N+1 args
  817. ;;; PREDICATE? is applied element-wise to the elements of VECTOR ...,
  818. ;;; and a count is tallied of the number of elements for which a
  819. ;;; true value is produced by PREDICATE?. This count is returned.
  820. (define (vector-count pred? vec . vectors)
  821. (let ((pred? (check-type procedure? pred? vector-count))
  822. (vec (check-type vector? vec vector-count)))
  823. (if (null? vectors)
  824. (%vector-fold1 (lambda (index count elt)
  825. (if (pred? index elt)
  826. (+ count 1)
  827. count))
  828. 0
  829. (vector-length vec)
  830. vec)
  831. (%vector-fold2+ (lambda (index count . elts)
  832. (if (apply pred? index elts)
  833. (+ count 1)
  834. count))
  835. 0
  836. (%smallest-length vectors
  837. (vector-length vec)
  838. vector-count)
  839. (cons vec vectors)))))
  840. ;;; --------------------
  841. ;;; Searching
  842. ;;; (VECTOR-INDEX <predicate?> <vector> ...)
  843. ;;; -> exact, nonnegative integer or #F
  844. ;;; (PREDICATE? <elt> ...) -> boolean ; N vectors -> N args
  845. ;;; Search left-to-right across VECTOR ... in parallel, returning the
  846. ;;; index of the first set of values VALUE ... such that (PREDICATE?
  847. ;;; VALUE ...) returns a true value; if no such set of elements is
  848. ;;; reached, return #F.
  849. (define (vector-index pred? vec . vectors)
  850. (vector-index/skip pred? vec vectors vector-index))
  851. ;;; (VECTOR-SKIP <predicate?> <vector> ...)
  852. ;;; -> exact, nonnegative integer or #F
  853. ;;; (PREDICATE? <elt> ...) -> boolean ; N vectors -> N args
  854. ;;; (vector-index (lambda elts (not (apply PREDICATE? elts)))
  855. ;;; VECTOR ...)
  856. ;;; Like VECTOR-INDEX, but find the index of the first set of values
  857. ;;; that do _not_ satisfy PREDICATE?.
  858. (define (vector-skip pred? vec . vectors)
  859. (vector-index/skip (lambda elts (not (apply pred? elts)))
  860. vec vectors
  861. vector-skip))
  862. ;;; Auxiliary for VECTOR-INDEX & VECTOR-SKIP
  863. (define vector-index/skip
  864. (letrec ((loop1 (lambda (pred? vec len i)
  865. (cond ((= i len) #f)
  866. ((pred? (vector-ref vec i)) i)
  867. (else (loop1 pred? vec len (+ i 1))))))
  868. (loop2+ (lambda (pred? vectors len i)
  869. (cond ((= i len) #f)
  870. ((apply pred? (vectors-ref vectors i)) i)
  871. (else (loop2+ pred? vectors len
  872. (+ i 1)))))))
  873. (lambda (pred? vec vectors callee)
  874. (let ((pred? (check-type procedure? pred? callee))
  875. (vec (check-type vector? vec callee)))
  876. (if (null? vectors)
  877. (loop1 pred? vec (vector-length vec) 0)
  878. (loop2+ pred? (cons vec vectors)
  879. (%smallest-length vectors
  880. (vector-length vec)
  881. callee)
  882. 0))))))
  883. ;;; (VECTOR-INDEX-RIGHT <predicate?> <vector> ...)
  884. ;;; -> exact, nonnegative integer or #F
  885. ;;; (PREDICATE? <elt> ...) -> boolean ; N vectors -> N args
  886. ;;; Right-to-left variant of VECTOR-INDEX.
  887. (define (vector-index-right pred? vec . vectors)
  888. (vector-index/skip-right pred? vec vectors vector-index-right))
  889. ;;; (VECTOR-SKIP-RIGHT <predicate?> <vector> ...)
  890. ;;; -> exact, nonnegative integer or #F
  891. ;;; (PREDICATE? <elt> ...) -> boolean ; N vectors -> N args
  892. ;;; Right-to-left variant of VECTOR-SKIP.
  893. (define (vector-skip-right pred? vec . vectors)
  894. (vector-index/skip-right (lambda elts (not (apply pred? elts)))
  895. vec vectors
  896. vector-index-right))
  897. (define vector-index/skip-right
  898. (letrec ((loop1 (lambda (pred? vec i)
  899. (cond ((negative? i) #f)
  900. ((pred? (vector-ref vec i)) i)
  901. (else (loop1 pred? vec (- i 1))))))
  902. (loop2+ (lambda (pred? vectors i)
  903. (cond ((negative? i) #f)
  904. ((apply pred? (vectors-ref vectors i)) i)
  905. (else (loop2+ pred? vectors (- i 1)))))))
  906. (lambda (pred? vec vectors callee)
  907. (let ((pred? (check-type procedure? pred? callee))
  908. (vec (check-type vector? vec callee)))
  909. (if (null? vectors)
  910. (loop1 pred? vec (- (vector-length vec) 1))
  911. (loop2+ pred? (cons vec vectors)
  912. (- (%smallest-length vectors
  913. (vector-length vec)
  914. callee)
  915. 1)))))))
  916. ;;; (VECTOR-BINARY-SEARCH <vector> <value> <cmp> [<start> <end>])
  917. ;;; -> exact, nonnegative integer or #F
  918. ;;; (CMP <value1> <value2>) -> integer
  919. ;;; positive -> VALUE1 > VALUE2
  920. ;;; zero -> VALUE1 = VALUE2
  921. ;;; negative -> VALUE1 < VALUE2
  922. ;;; Perform a binary search through VECTOR for VALUE, comparing each
  923. ;;; element to VALUE with CMP.
  924. (define (vector-binary-search vec value cmp . maybe-start+end)
  925. (let ((cmp (check-type procedure? cmp vector-binary-search)))
  926. (let-vector-start+end vector-binary-search vec maybe-start+end
  927. (start end)
  928. (let loop ((start start) (end end) (j #f))
  929. (let ((i (quotient (+ start end) 2)))
  930. (if (or (= start end) (and j (= i j)))
  931. #f
  932. (let ((comparison
  933. (check-type integer?
  934. (cmp (vector-ref vec i) value)
  935. `(,cmp for ,vector-binary-search))))
  936. (cond ((zero? comparison) i)
  937. ((positive? comparison) (loop start i i))
  938. (else (loop i end i))))))))))
  939. ;;; (VECTOR-ANY <pred?> <vector> ...) -> value
  940. ;;; Apply PRED? to each parallel element in each VECTOR ...; if PRED?
  941. ;;; should ever return a true value, immediately stop and return that
  942. ;;; value; otherwise, when the shortest vector runs out, return #F.
  943. ;;; The iteration and order of application of PRED? across elements
  944. ;;; is of the vectors is strictly left-to-right.
  945. (define vector-any
  946. (letrec ((loop1 (lambda (pred? vec i len len-1)
  947. (and (not (= i len))
  948. (if (= i len-1)
  949. (pred? (vector-ref vec i))
  950. (or (pred? (vector-ref vec i))
  951. (loop1 pred? vec (+ i 1)
  952. len len-1))))))
  953. (loop2+ (lambda (pred? vectors i len len-1)
  954. (and (not (= i len))
  955. (if (= i len-1)
  956. (apply pred? (vectors-ref vectors i))
  957. (or (apply pred? (vectors-ref vectors i))
  958. (loop2+ pred? vectors (+ i 1)
  959. len len-1)))))))
  960. (lambda (pred? vec . vectors)
  961. (let ((pred? (check-type procedure? pred? vector-any))
  962. (vec (check-type vector? vec vector-any)))
  963. (if (null? vectors)
  964. (let ((len (vector-length vec)))
  965. (loop1 pred? vec 0 len (- len 1)))
  966. (let ((len (%smallest-length vectors
  967. (vector-length vec)
  968. vector-any)))
  969. (loop2+ pred? (cons vec vectors) 0 len (- len 1))))))))
  970. ;;; (VECTOR-EVERY <pred?> <vector> ...) -> value
  971. ;;; Apply PRED? to each parallel value in each VECTOR ...; if PRED?
  972. ;;; should ever return #F, immediately stop and return #F; otherwise,
  973. ;;; if PRED? should return a true value for each element, stopping at
  974. ;;; the end of the shortest vector, return the last value that PRED?
  975. ;;; returned. In the case that there is an empty vector, return #T.
  976. ;;; The iteration and order of application of PRED? across elements
  977. ;;; is of the vectors is strictly left-to-right.
  978. (define vector-every
  979. (letrec ((loop1 (lambda (pred? vec i len len-1)
  980. (or (= i len)
  981. (if (= i len-1)
  982. (pred? (vector-ref vec i))
  983. (and (pred? (vector-ref vec i))
  984. (loop1 pred? vec (+ i 1)
  985. len len-1))))))
  986. (loop2+ (lambda (pred? vectors i len len-1)
  987. (or (= i len)
  988. (if (= i len-1)
  989. (apply pred? (vectors-ref vectors i))
  990. (and (apply pred? (vectors-ref vectors i))
  991. (loop2+ pred? vectors (+ i 1)
  992. len len-1)))))))
  993. (lambda (pred? vec . vectors)
  994. (let ((pred? (check-type procedure? pred? vector-every))
  995. (vec (check-type vector? vec vector-every)))
  996. (if (null? vectors)
  997. (let ((len (vector-length vec)))
  998. (loop1 pred? vec 0 len (- len 1)))
  999. (let ((len (%smallest-length vectors
  1000. (vector-length vec)
  1001. vector-every)))
  1002. (loop2+ pred? (cons vec vectors) 0 len (- len 1))))))))
  1003. ;;; --------------------
  1004. ;;; Mutators
  1005. ;;; (VECTOR-SET! <vector> <index> <value>) -> unspecified
  1006. ;;; [R5RS] Assign the location at INDEX in VECTOR to VALUE.
  1007. (define vector-set! vector-set!)
  1008. ;;; (VECTOR-SWAP! <vector> <index1> <index2>) -> unspecified
  1009. ;;; Swap the values in the locations at INDEX1 and INDEX2.
  1010. (define (vector-swap! vec i j)
  1011. (let ((vec (check-type vector? vec vector-swap!)))
  1012. (let ((i (check-index vec i vector-swap!))
  1013. (j (check-index vec j vector-swap!)))
  1014. (let ((x (vector-ref vec i)))
  1015. (vector-set! vec i (vector-ref vec j))
  1016. (vector-set! vec j x)))))
  1017. ;;; (VECTOR-FILL! <vector> <value> [<start> <end>]) -> unspecified
  1018. ;;; [R5RS+] Fill the locations in VECTOR between START, whose default
  1019. ;;; is 0, and END, whose default is the length of VECTOR, with VALUE.
  1020. ;;;
  1021. ;;; This one can probably be made really fast natively.
  1022. (define vector-fill!
  1023. (let ((%vector-fill! vector-fill!)) ; Take the native one, under
  1024. ; the assumption that it's
  1025. ; faster, so we can use it if
  1026. ; there are no optional
  1027. ; arguments.
  1028. (lambda (vec value . maybe-start+end)
  1029. (if (null? maybe-start+end)
  1030. (%vector-fill! vec value) ;+++
  1031. (let-vector-start+end vector-fill! vec maybe-start+end
  1032. (start end)
  1033. (do ((i start (+ i 1)))
  1034. ((= i end))
  1035. (vector-set! vec i value)))))))
  1036. ;;; (VECTOR-COPY! <target> <tstart> <source> [<sstart> <send>])
  1037. ;;; -> unspecified
  1038. ;;; Copy the values in the locations in [SSTART,SEND) from SOURCE to
  1039. ;;; to TARGET, starting at TSTART in TARGET.
  1040. ;;; [wdc] Corrected to allow 0 <= sstart <= send <= (vector-length source).
  1041. (define (vector-copy! target tstart source . maybe-sstart+send)
  1042. (define (doit! sstart send source-length)
  1043. (let ((tstart (check-type nonneg-int? tstart vector-copy!))
  1044. (sstart (check-type nonneg-int? sstart vector-copy!))
  1045. (send (check-type nonneg-int? send vector-copy!)))
  1046. (cond ((and (<= 0 sstart send source-length)
  1047. (<= (+ tstart (- send sstart)) (vector-length target)))
  1048. (%vector-copy! target tstart source sstart send))
  1049. (else
  1050. (error "illegal arguments"
  1051. `(while calling ,vector-copy!)
  1052. `(target was ,target)
  1053. `(target-length was ,(vector-length target))
  1054. `(tstart was ,tstart)
  1055. `(source was ,source)
  1056. `(source-length was ,source-length)
  1057. `(sstart was ,sstart)
  1058. `(send was ,send))))))
  1059. (let ((n (vector-length source)))
  1060. (cond ((null? maybe-sstart+send)
  1061. (doit! 0 n n))
  1062. ((null? (cdr maybe-sstart+send))
  1063. (doit! (car maybe-sstart+send) n n))
  1064. ((null? (cddr maybe-sstart+send))
  1065. (doit! (car maybe-sstart+send) (cadr maybe-sstart+send) n))
  1066. (else
  1067. (error "too many arguments"
  1068. vector-copy!
  1069. (cddr maybe-sstart+send))))))
  1070. ;;; (VECTOR-REVERSE-COPY! <target> <tstart> <source> [<sstart> <send>])
  1071. ;;; [wdc] Corrected to allow 0 <= sstart <= send <= (vector-length source).
  1072. (define (vector-reverse-copy! target tstart source . maybe-sstart+send)
  1073. (define (doit! sstart send source-length)
  1074. (let ((tstart (check-type nonneg-int? tstart vector-reverse-copy!))
  1075. (sstart (check-type nonneg-int? sstart vector-reverse-copy!))
  1076. (send (check-type nonneg-int? send vector-reverse-copy!)))
  1077. (cond ((and (eq? target source)
  1078. (or (between? sstart tstart send)
  1079. (between? tstart sstart
  1080. (+ tstart (- send sstart)))))
  1081. (error "vector range for self-copying overlaps"
  1082. vector-reverse-copy!
  1083. `(vector was ,target)
  1084. `(tstart was ,tstart)
  1085. `(sstart was ,sstart)
  1086. `(send was ,send)))
  1087. ((and (<= 0 sstart send source-length)
  1088. (<= (+ tstart (- send sstart)) (vector-length target)))
  1089. (%vector-reverse-copy! target tstart source sstart send))
  1090. (else
  1091. (error "illegal arguments"
  1092. `(while calling ,vector-reverse-copy!)
  1093. `(target was ,target)
  1094. `(target-length was ,(vector-length target))
  1095. `(tstart was ,tstart)
  1096. `(source was ,source)
  1097. `(source-length was ,source-length)
  1098. `(sstart was ,sstart)
  1099. `(send was ,send))))))
  1100. (let ((n (vector-length source)))
  1101. (cond ((null? maybe-sstart+send)
  1102. (doit! 0 n n))
  1103. ((null? (cdr maybe-sstart+send))
  1104. (doit! (car maybe-sstart+send) n n))
  1105. ((null? (cddr maybe-sstart+send))
  1106. (doit! (car maybe-sstart+send) (cadr maybe-sstart+send) n))
  1107. (else
  1108. (error "too many arguments"
  1109. vector-reverse-copy!
  1110. (cddr maybe-sstart+send))))))
  1111. ;;; (VECTOR-REVERSE! <vector> [<start> <end>]) -> unspecified
  1112. ;;; Destructively reverse the contents of the sequence of locations
  1113. ;;; in VECTOR between START, whose default is 0, and END, whose
  1114. ;;; default is the length of VECTOR.
  1115. (define (vector-reverse! vec . start+end)
  1116. (let-vector-start+end vector-reverse! vec start+end
  1117. (start end)
  1118. (%vector-reverse! vec start end)))
  1119. ;;; --------------------
  1120. ;;; Conversion
  1121. ;;; (VECTOR->LIST <vector> [<start> <end>]) -> list
  1122. ;;; [R5RS+] Produce a list containing the elements in the locations
  1123. ;;; between START, whose default is 0, and END, whose default is the
  1124. ;;; length of VECTOR, from VECTOR.
  1125. (define vector->list
  1126. (let ((%vector->list vector->list))
  1127. (lambda (vec . maybe-start+end)
  1128. (if (null? maybe-start+end) ; Oughta use CASE-LAMBDA.
  1129. (%vector->list vec) ;+++
  1130. (let-vector-start+end vector->list vec maybe-start+end
  1131. (start end)
  1132. ;(unfold (lambda (i) ; No SRFI 1.
  1133. ; (< i start))
  1134. ; (lambda (i) (vector-ref vec i))
  1135. ; (lambda (i) (- i 1))
  1136. ; (- end 1))
  1137. (do ((i (- end 1) (- i 1))
  1138. (result '() (cons (vector-ref vec i) result)))
  1139. ((< i start) result)))))))
  1140. ;;; (REVERSE-VECTOR->LIST <vector> [<start> <end>]) -> list
  1141. ;;; Produce a list containing the elements in the locations between
  1142. ;;; START, whose default is 0, and END, whose default is the length
  1143. ;;; of VECTOR, from VECTOR, in reverse order.
  1144. (define (reverse-vector->list vec . maybe-start+end)
  1145. (let-vector-start+end reverse-vector->list vec maybe-start+end
  1146. (start end)
  1147. ;(unfold (lambda (i) (= i end)) ; No SRFI 1.
  1148. ; (lambda (i) (vector-ref vec i))
  1149. ; (lambda (i) (+ i 1))
  1150. ; start)
  1151. (do ((i start (+ i 1))
  1152. (result '() (cons (vector-ref vec i) result)))
  1153. ((= i end) result))))
  1154. ;;; (LIST->VECTOR <list> [<start> <end>]) -> vector
  1155. ;;; [R5RS+] Produce a vector containing the elements in LIST, which
  1156. ;;; must be a proper list, between START, whose default is 0, & END,
  1157. ;;; whose default is the length of LIST. It is suggested that if the
  1158. ;;; length of LIST is known in advance, the START and END arguments
  1159. ;;; be passed, so that LIST->VECTOR need not call LENGTH to determine
  1160. ;;; the the length.
  1161. ;;;
  1162. ;;; This implementation diverges on circular lists, unless LENGTH fails
  1163. ;;; and causes - to fail as well. Given a LENGTH* that computes the
  1164. ;;; length of a list's cycle, this wouldn't diverge, and would work
  1165. ;;; great for circular lists.
  1166. (define list->vector
  1167. (let ((%list->vector list->vector))
  1168. (lambda (lst . maybe-start+end)
  1169. ;; Checking the type of a proper list is expensive, so we do it
  1170. ;; amortizedly, or let %LIST->VECTOR or LIST-TAIL do it.
  1171. (if (null? maybe-start+end) ; Oughta use CASE-LAMBDA.
  1172. (%list->vector lst) ;+++
  1173. ;; We can't use LET-VECTOR-START+END, because we're using the
  1174. ;; bounds of a _list_, not a vector.
  1175. (let*-optionals maybe-start+end
  1176. ((start 0)
  1177. (end (length lst))) ; Ugh -- LENGTH
  1178. (let ((start (check-type nonneg-int? start list->vector))
  1179. (end (check-type nonneg-int? end list->vector)))
  1180. ((lambda (f)
  1181. (vector-unfold f (- end start) (list-tail lst start)))
  1182. (lambda (index l)
  1183. (cond ((null? l)
  1184. (error "list was too short"
  1185. `(list was ,lst)
  1186. `(attempted end was ,end)
  1187. `(while calling ,list->vector)))
  1188. ((pair? l)
  1189. (values (car l) (cdr l)))
  1190. (else
  1191. ;; Make this look as much like what CHECK-TYPE
  1192. ;; would report as possible.
  1193. (error "erroneous value"
  1194. ;; We want SRFI 1's PROPER-LIST?, but it
  1195. ;; would be a waste to link all of SRFI
  1196. ;; 1 to this module for only the single
  1197. ;; function PROPER-LIST?.
  1198. (list list? lst)
  1199. `(while calling
  1200. ,list->vector))))))))))))
  1201. ;;; (REVERSE-LIST->VECTOR <list> [<start> <end>]) -> vector
  1202. ;;; Produce a vector containing the elements in LIST, which must be a
  1203. ;;; proper list, between START, whose default is 0, and END, whose
  1204. ;;; default is the length of LIST, in reverse order. It is suggested
  1205. ;;; that if the length of LIST is known in advance, the START and END
  1206. ;;; arguments be passed, so that REVERSE-LIST->VECTOR need not call
  1207. ;;; LENGTH to determine the the length.
  1208. ;;;
  1209. ;;; This also diverges on circular lists unless, again, LENGTH returns
  1210. ;;; something that makes - bork.
  1211. (define (reverse-list->vector lst . maybe-start+end)
  1212. (let*-optionals maybe-start+end
  1213. ((start 0)
  1214. (end (length lst))) ; Ugh -- LENGTH
  1215. (let ((start (check-type nonneg-int? start reverse-list->vector))
  1216. (end (check-type nonneg-int? end reverse-list->vector)))
  1217. ((lambda (f)
  1218. (vector-unfold-right f (- end start) (list-tail lst start)))
  1219. (lambda (index l)
  1220. (cond ((null? l)
  1221. (error "list too short"
  1222. `(list was ,lst)
  1223. `(attempted end was ,end)
  1224. `(while calling ,reverse-list->vector)))
  1225. ((pair? l)
  1226. (values (car l) (cdr l)))
  1227. (else
  1228. (error "erroneous value"
  1229. (list list? lst)
  1230. `(while calling ,reverse-list->vector)))))))))