unif.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570
  1. /* Copyright (C) 1995,1996,1997,1998, 2000, 2002 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. /*
  42. This file has code for arrays in lots of variants (double, integer,
  43. unsigned etc. ). It suffers from hugely repetitive code because
  44. there is similar (but different) code for every variant included. (urg.)
  45. --hwn
  46. */
  47. #include <stdio.h>
  48. #include "libguile/_scm.h"
  49. #include "libguile/chars.h"
  50. #include "libguile/eval.h"
  51. #include "libguile/fports.h"
  52. #include "libguile/smob.h"
  53. #include "libguile/strop.h"
  54. #include "libguile/feature.h"
  55. #include "libguile/root.h"
  56. #include "libguile/strings.h"
  57. #include "libguile/vectors.h"
  58. #include "libguile/validate.h"
  59. #include "libguile/unif.h"
  60. #include "libguile/ramap.h"
  61. #ifdef HAVE_UNISTD_H
  62. #include <unistd.h>
  63. #endif
  64. /* The set of uniform scm_vector types is:
  65. * Vector of: Called:
  66. * unsigned char string
  67. * char byvect
  68. * boolean bvect
  69. * signed long ivect
  70. * unsigned long uvect
  71. * float fvect
  72. * double dvect
  73. * complex double cvect
  74. * short svect
  75. * long long llvect
  76. */
  77. long scm_tc16_array;
  78. /* return the size of an element in a uniform array or 0 if type not
  79. found. */
  80. scm_sizet
  81. scm_uniform_element_size (SCM obj)
  82. {
  83. scm_sizet result;
  84. switch (SCM_TYP7 (obj))
  85. {
  86. case scm_tc7_bvect:
  87. case scm_tc7_uvect:
  88. case scm_tc7_ivect:
  89. result = sizeof (long);
  90. break;
  91. case scm_tc7_byvect:
  92. result = sizeof (char);
  93. break;
  94. case scm_tc7_svect:
  95. result = sizeof (short);
  96. break;
  97. #ifdef HAVE_LONG_LONGS
  98. case scm_tc7_llvect:
  99. result = sizeof (long_long);
  100. break;
  101. #endif
  102. case scm_tc7_fvect:
  103. result = sizeof (float);
  104. break;
  105. case scm_tc7_dvect:
  106. result = sizeof (double);
  107. break;
  108. case scm_tc7_cvect:
  109. result = 2 * sizeof (double);
  110. break;
  111. default:
  112. result = 0;
  113. }
  114. return result;
  115. }
  116. /* Silly function used not to modify the semantics of the silly
  117. * prototype system in order to be backward compatible.
  118. */
  119. static int
  120. singp (SCM obj)
  121. {
  122. if (!SCM_SLOPPY_REALP (obj))
  123. return 0;
  124. else
  125. {
  126. double x = SCM_REAL_VALUE (obj);
  127. float fx = x;
  128. return (- SCM_FLTMAX < x) && (x < SCM_FLTMAX) && (fx == x);
  129. }
  130. }
  131. SCM
  132. scm_make_uve (long k, SCM prot)
  133. {
  134. SCM v;
  135. long i, type;
  136. if (SCM_EQ_P (prot, SCM_BOOL_T))
  137. {
  138. i = sizeof (long) * ((k + SCM_LONG_BIT - 1) / SCM_LONG_BIT);
  139. type = scm_tc7_bvect;
  140. }
  141. else if (SCM_CHARP (prot) && (SCM_CHAR (prot) == '\0'))
  142. {
  143. i = sizeof (char) * k;
  144. type = scm_tc7_byvect;
  145. }
  146. else if (SCM_CHARP (prot))
  147. {
  148. i = sizeof (char) * k;
  149. type = scm_tc7_string;
  150. }
  151. else if (SCM_INUMP (prot))
  152. {
  153. i = sizeof (long) * k;
  154. if (SCM_INUM (prot) > 0)
  155. type = scm_tc7_uvect;
  156. else
  157. type = scm_tc7_ivect;
  158. }
  159. else if (SCM_SYMBOLP (prot) && (1 == SCM_LENGTH (prot)))
  160. {
  161. char s;
  162. s = SCM_CHARS (prot)[0];
  163. if (s == 's')
  164. {
  165. i = sizeof (short) * k;
  166. type = scm_tc7_svect;
  167. }
  168. #ifdef HAVE_LONG_LONGS
  169. else if (s == 'l')
  170. {
  171. i = sizeof (long_long) * k;
  172. type = scm_tc7_llvect;
  173. }
  174. #endif
  175. else
  176. {
  177. return scm_make_vector (SCM_MAKINUM (k), SCM_UNDEFINED);
  178. }
  179. }
  180. else
  181. if (SCM_IMP (prot) || !SCM_INEXACTP (prot))
  182. /* Huge non-unif vectors are NOT supported. */
  183. /* no special scm_vector */
  184. return scm_make_vector (SCM_MAKINUM (k), SCM_UNDEFINED);
  185. else if (singp (prot))
  186. {
  187. i = sizeof (float) * k;
  188. type = scm_tc7_fvect;
  189. }
  190. else if (SCM_COMPLEXP (prot))
  191. {
  192. i = 2 * sizeof (double) * k;
  193. type = scm_tc7_cvect;
  194. }
  195. else
  196. {
  197. i = sizeof (double) * k;
  198. type = scm_tc7_dvect;
  199. }
  200. SCM_NEWCELL (v);
  201. SCM_DEFER_INTS;
  202. SCM_SETCHARS (v, (char *) scm_must_malloc (i ? i : 1, "vector"));
  203. SCM_SETLENGTH (v, (k < SCM_LENGTH_MAX ? k : SCM_LENGTH_MAX), type);
  204. SCM_ALLOW_INTS;
  205. return v;
  206. }
  207. SCM_DEFINE (scm_uniform_vector_length, "uniform-vector-length", 1, 0, 0,
  208. (SCM v),
  209. "Returns the number of elements in @var{uve}.")
  210. #define FUNC_NAME s_scm_uniform_vector_length
  211. {
  212. SCM_ASRTGO (SCM_NIMP (v), badarg1);
  213. switch SCM_TYP7
  214. (v)
  215. {
  216. default:
  217. badarg1:SCM_WTA(1,v);
  218. case scm_tc7_bvect:
  219. case scm_tc7_string:
  220. case scm_tc7_byvect:
  221. case scm_tc7_uvect:
  222. case scm_tc7_ivect:
  223. case scm_tc7_fvect:
  224. case scm_tc7_dvect:
  225. case scm_tc7_cvect:
  226. case scm_tc7_vector:
  227. case scm_tc7_wvect:
  228. case scm_tc7_svect:
  229. #ifdef HAVE_LONG_LONGS
  230. case scm_tc7_llvect:
  231. #endif
  232. return SCM_MAKINUM (SCM_LENGTH (v));
  233. }
  234. }
  235. #undef FUNC_NAME
  236. SCM_DEFINE (scm_array_p, "array?", 1, 1, 0,
  237. (SCM v, SCM prot),
  238. "Returns @code{#t} if the @var{obj} is an array, and @code{#f} if not.\n\n"
  239. "The @var{prototype} argument is used with uniform arrays and is described\n"
  240. "elsewhere.")
  241. #define FUNC_NAME s_scm_array_p
  242. {
  243. int nprot;
  244. int enclosed;
  245. nprot = SCM_UNBNDP (prot);
  246. enclosed = 0;
  247. if (SCM_IMP (v))
  248. return SCM_BOOL_F;
  249. while (SCM_TYP7 (v) == scm_tc7_smob)
  250. {
  251. if (!SCM_ARRAYP (v))
  252. return SCM_BOOL_F;
  253. if (nprot)
  254. return SCM_BOOL_T;
  255. if (enclosed++)
  256. return SCM_BOOL_F;
  257. v = SCM_ARRAY_V (v);
  258. }
  259. if (nprot)
  260. return SCM_BOOL(nprot);
  261. else
  262. {
  263. int protp = 0;
  264. switch (SCM_TYP7 (v))
  265. {
  266. case scm_tc7_bvect:
  267. protp = (SCM_EQ_P (prot, SCM_BOOL_T));
  268. case scm_tc7_string:
  269. protp = SCM_CHARP(prot) && (SCM_CHAR (prot) != '\0');
  270. case scm_tc7_byvect:
  271. protp = SCM_EQ_P (prot, SCM_MAKE_CHAR ('\0'));
  272. case scm_tc7_uvect:
  273. protp = SCM_INUMP(prot) && SCM_INUM(prot)>0;
  274. case scm_tc7_ivect:
  275. protp = SCM_INUMP(prot) && SCM_INUM(prot)<=0;
  276. case scm_tc7_svect:
  277. protp = SCM_SYMBOLP (prot)
  278. && (1 == SCM_LENGTH (prot))
  279. && ('s' == SCM_CHARS (prot)[0]);
  280. #ifdef HAVE_LONG_LONGS
  281. case scm_tc7_llvect:
  282. protp = SCM_SYMBOLP (prot)
  283. && (1 == SCM_LENGTH (prot))
  284. && ('s' == SCM_CHARS (prot)[0]);
  285. #endif
  286. case scm_tc7_fvect:
  287. protp = singp (prot);
  288. case scm_tc7_dvect:
  289. protp = SCM_REALP(prot);
  290. case scm_tc7_cvect:
  291. protp = SCM_COMPLEXP(prot);
  292. case scm_tc7_vector:
  293. case scm_tc7_wvect:
  294. protp = SCM_NULLP(prot);
  295. default:
  296. /* no default */
  297. ;
  298. }
  299. return SCM_BOOL(protp);
  300. }
  301. }
  302. #undef FUNC_NAME
  303. SCM_DEFINE (scm_array_rank, "array-rank", 1, 0, 0,
  304. (SCM ra),
  305. "Returns the number of dimensions of @var{obj}. If @var{obj} is not an\n"
  306. "array, @code{0} is returned.")
  307. #define FUNC_NAME s_scm_array_rank
  308. {
  309. if (SCM_IMP (ra))
  310. return SCM_INUM0;
  311. switch (SCM_TYP7 (ra))
  312. {
  313. default:
  314. return SCM_INUM0;
  315. case scm_tc7_string:
  316. case scm_tc7_vector:
  317. case scm_tc7_wvect:
  318. case scm_tc7_byvect:
  319. case scm_tc7_uvect:
  320. case scm_tc7_ivect:
  321. case scm_tc7_fvect:
  322. case scm_tc7_cvect:
  323. case scm_tc7_dvect:
  324. #ifdef HAVE_LONG_LONGS
  325. case scm_tc7_llvect:
  326. #endif
  327. case scm_tc7_svect:
  328. return SCM_MAKINUM (1L);
  329. case scm_tc7_smob:
  330. if (SCM_ARRAYP (ra))
  331. return SCM_MAKINUM (SCM_ARRAY_NDIM (ra));
  332. return SCM_INUM0;
  333. }
  334. }
  335. #undef FUNC_NAME
  336. SCM_DEFINE (scm_array_dimensions, "array-dimensions", 1, 0, 0,
  337. (SCM ra),
  338. "@code{Array-dimensions} is similar to @code{array-shape} but replaces\n"
  339. "elements with a @code{0} minimum with one greater than the maximum. So:\n"
  340. "@example\n"
  341. "(array-dimensions (make-array 'foo '(-1 3) 5)) @result{} ((-1 3) 5)\n"
  342. "@end example")
  343. #define FUNC_NAME s_scm_array_dimensions
  344. {
  345. SCM res = SCM_EOL;
  346. scm_sizet k;
  347. scm_array_dim *s;
  348. if (SCM_IMP (ra))
  349. return SCM_BOOL_F;
  350. switch (SCM_TYP7 (ra))
  351. {
  352. default:
  353. return SCM_BOOL_F;
  354. case scm_tc7_string:
  355. case scm_tc7_vector:
  356. case scm_tc7_wvect:
  357. case scm_tc7_bvect:
  358. case scm_tc7_byvect:
  359. case scm_tc7_uvect:
  360. case scm_tc7_ivect:
  361. case scm_tc7_fvect:
  362. case scm_tc7_cvect:
  363. case scm_tc7_dvect:
  364. case scm_tc7_svect:
  365. #ifdef HAVE_LONG_LONGS
  366. case scm_tc7_llvect:
  367. #endif
  368. return scm_cons (SCM_MAKINUM (SCM_LENGTH (ra)), SCM_EOL);
  369. case scm_tc7_smob:
  370. if (!SCM_ARRAYP (ra))
  371. return SCM_BOOL_F;
  372. k = SCM_ARRAY_NDIM (ra);
  373. s = SCM_ARRAY_DIMS (ra);
  374. while (k--)
  375. res = scm_cons (s[k].lbnd
  376. ? scm_cons2 (SCM_MAKINUM (s[k].lbnd),
  377. SCM_MAKINUM (s[k].ubnd),
  378. SCM_EOL)
  379. : SCM_MAKINUM (1 + s[k].ubnd),
  380. res);
  381. return res;
  382. }
  383. }
  384. #undef FUNC_NAME
  385. SCM_DEFINE (scm_shared_array_root, "shared-array-root", 1, 0, 0,
  386. (SCM ra),
  387. "Return the root vector of a shared array.")
  388. #define FUNC_NAME s_scm_shared_array_root
  389. {
  390. SCM_ASSERT (SCM_ARRAYP (ra), ra, SCM_ARG1, FUNC_NAME);
  391. return SCM_ARRAY_V (ra);
  392. }
  393. #undef FUNC_NAME
  394. SCM_DEFINE (scm_shared_array_offset, "shared-array-offset", 1, 0, 0,
  395. (SCM ra),
  396. "Return the root vector index of the first element in the array.")
  397. #define FUNC_NAME s_scm_shared_array_offset
  398. {
  399. SCM_ASSERT (SCM_ARRAYP (ra), ra, SCM_ARG1, FUNC_NAME);
  400. return SCM_MAKINUM (SCM_ARRAY_BASE (ra));
  401. }
  402. #undef FUNC_NAME
  403. SCM_DEFINE (scm_shared_array_increments, "shared-array-increments", 1, 0, 0,
  404. (SCM ra),
  405. "For each dimension, return the distance between elements in the root vector.")
  406. #define FUNC_NAME s_scm_shared_array_increments
  407. {
  408. SCM res = SCM_EOL;
  409. scm_sizet k;
  410. scm_array_dim *s;
  411. SCM_ASSERT (SCM_ARRAYP (ra), ra, SCM_ARG1, FUNC_NAME);
  412. k = SCM_ARRAY_NDIM (ra);
  413. s = SCM_ARRAY_DIMS (ra);
  414. while (k--)
  415. res = scm_cons (SCM_MAKINUM (s[k].inc), res);
  416. return res;
  417. }
  418. #undef FUNC_NAME
  419. static char s_bad_ind[] = "Bad scm_array index";
  420. long
  421. scm_aind (SCM ra, SCM args, const char *what)
  422. {
  423. SCM ind;
  424. register long j;
  425. register scm_sizet pos = SCM_ARRAY_BASE (ra);
  426. register scm_sizet k = SCM_ARRAY_NDIM (ra);
  427. scm_array_dim *s = SCM_ARRAY_DIMS (ra);
  428. if (SCM_INUMP (args))
  429. {
  430. SCM_ASSERT (1 == k, scm_makfrom0str (what), SCM_WNA, NULL);
  431. return pos + (SCM_INUM (args) - s->lbnd) * (s->inc);
  432. }
  433. while (k && SCM_NIMP (args))
  434. {
  435. ind = SCM_CAR (args);
  436. args = SCM_CDR (args);
  437. SCM_ASSERT (SCM_INUMP (ind), ind, s_bad_ind, what);
  438. j = SCM_INUM (ind);
  439. SCM_ASSERT (j >= (s->lbnd) && j <= (s->ubnd), ind, SCM_OUTOFRANGE, what);
  440. pos += (j - s->lbnd) * (s->inc);
  441. k--;
  442. s++;
  443. }
  444. SCM_ASSERT (0 == k && SCM_NULLP (args), scm_makfrom0str (what), SCM_WNA,
  445. NULL);
  446. return pos;
  447. }
  448. SCM
  449. scm_make_ra (int ndim)
  450. {
  451. SCM ra;
  452. SCM_NEWCELL (ra);
  453. SCM_DEFER_INTS;
  454. SCM_NEWSMOB(ra, ((long) ndim << 17) + scm_tc16_array,
  455. scm_must_malloc ((long) (sizeof (scm_array) + ndim * sizeof (scm_array_dim)),
  456. "array"));
  457. SCM_ARRAY_V (ra) = scm_nullvect;
  458. SCM_ALLOW_INTS;
  459. return ra;
  460. }
  461. static char s_bad_spec[] = "Bad scm_array dimension";
  462. /* Increments will still need to be set. */
  463. SCM
  464. scm_shap2ra (SCM args, const char *what)
  465. {
  466. scm_array_dim *s;
  467. SCM ra, spec, sp;
  468. int ndim = scm_ilength (args);
  469. SCM_ASSERT (0 <= ndim, args, s_bad_spec, what);
  470. ra = scm_make_ra (ndim);
  471. SCM_ARRAY_BASE (ra) = 0;
  472. s = SCM_ARRAY_DIMS (ra);
  473. for (; SCM_NIMP (args); s++, args = SCM_CDR (args))
  474. {
  475. spec = SCM_CAR (args);
  476. if (SCM_IMP (spec))
  477. {
  478. SCM_ASSERT (SCM_INUMP (spec) && SCM_INUM (spec) >= 0, spec,
  479. s_bad_spec, what);
  480. s->lbnd = 0;
  481. s->ubnd = SCM_INUM (spec) - 1;
  482. s->inc = 1;
  483. }
  484. else
  485. {
  486. SCM_ASSERT (SCM_CONSP (spec) && SCM_INUMP (SCM_CAR (spec)), spec,
  487. s_bad_spec, what);
  488. s->lbnd = SCM_INUM (SCM_CAR (spec));
  489. sp = SCM_CDR (spec);
  490. SCM_ASSERT (SCM_CONSP (sp)
  491. && SCM_INUMP (SCM_CAR (sp)) && SCM_NULLP (SCM_CDR (sp)),
  492. spec, s_bad_spec, what);
  493. s->ubnd = SCM_INUM (SCM_CAR (sp));
  494. s->inc = 1;
  495. }
  496. }
  497. return ra;
  498. }
  499. SCM_DEFINE (scm_dimensions_to_uniform_array, "dimensions->uniform-array", 2, 1, 0,
  500. (SCM dims, SCM prot, SCM fill),
  501. "@deffnx primitive make-uniform-vector length prototype [fill]\n"
  502. "Creates and returns a uniform array or vector of type corresponding to\n"
  503. "@var{prototype} with dimensions @var{dims} or length @var{length}. If\n"
  504. "@var{fill} is supplied, it's used to fill the array, otherwise \n"
  505. "@var{prototype} is used.")
  506. #define FUNC_NAME s_scm_dimensions_to_uniform_array
  507. {
  508. scm_sizet k, vlen = 1;
  509. long rlen = 1;
  510. scm_array_dim *s;
  511. SCM ra;
  512. if (SCM_INUMP (dims))
  513. {
  514. if (SCM_INUM (dims) < SCM_LENGTH_MAX)
  515. {
  516. SCM answer = scm_make_uve (SCM_INUM (dims), prot);
  517. if (!SCM_UNBNDP (fill))
  518. scm_array_fill_x (answer, fill);
  519. else if (SCM_SYMBOLP (prot))
  520. scm_array_fill_x (answer, SCM_MAKINUM (0));
  521. else
  522. scm_array_fill_x (answer, prot);
  523. return answer;
  524. }
  525. else
  526. dims = scm_cons (dims, SCM_EOL);
  527. }
  528. SCM_ASSERT (SCM_NULLP (dims) || SCM_CONSP (dims),
  529. dims, SCM_ARG1, FUNC_NAME);
  530. ra = scm_shap2ra (dims, FUNC_NAME);
  531. SCM_SETOR_CAR (ra, SCM_ARRAY_CONTIGUOUS);
  532. s = SCM_ARRAY_DIMS (ra);
  533. k = SCM_ARRAY_NDIM (ra);
  534. while (k--)
  535. {
  536. s[k].inc = (rlen > 0 ? rlen : 0);
  537. rlen = (s[k].ubnd - s[k].lbnd + 1) * s[k].inc;
  538. vlen *= (s[k].ubnd - s[k].lbnd + 1);
  539. }
  540. if (rlen < SCM_LENGTH_MAX)
  541. SCM_ARRAY_V (ra) = scm_make_uve ((rlen > 0 ? rlen : 0L), prot);
  542. else
  543. {
  544. scm_sizet bit;
  545. switch (SCM_TYP7 (scm_make_uve (0L, prot)))
  546. {
  547. default:
  548. bit = SCM_LONG_BIT;
  549. break;
  550. case scm_tc7_bvect:
  551. bit = 1;
  552. break;
  553. case scm_tc7_string:
  554. bit = SCM_CHAR_BIT;
  555. break;
  556. case scm_tc7_fvect:
  557. bit = sizeof (float) * SCM_CHAR_BIT / sizeof (char);
  558. break;
  559. case scm_tc7_dvect:
  560. bit = sizeof (double) * SCM_CHAR_BIT / sizeof (char);
  561. break;
  562. case scm_tc7_cvect:
  563. bit = 2 * sizeof (double) * SCM_CHAR_BIT / sizeof (char);
  564. break;
  565. }
  566. SCM_ARRAY_BASE (ra) = (SCM_LONG_BIT + bit - 1) / bit;
  567. rlen += SCM_ARRAY_BASE (ra);
  568. SCM_ARRAY_V (ra) = scm_make_uve (rlen, prot);
  569. *((long *) SCM_VELTS (SCM_ARRAY_V (ra))) = rlen;
  570. }
  571. if (!SCM_UNBNDP (fill))
  572. {
  573. scm_array_fill_x (ra, fill);
  574. }
  575. else if (SCM_SYMBOLP (prot))
  576. scm_array_fill_x (ra, SCM_MAKINUM (0));
  577. else
  578. scm_array_fill_x (ra, prot);
  579. if (1 == SCM_ARRAY_NDIM (ra) && 0 == SCM_ARRAY_BASE (ra))
  580. if (s->ubnd < s->lbnd || (0 == s->lbnd && 1 == s->inc))
  581. return SCM_ARRAY_V (ra);
  582. return ra;
  583. }
  584. #undef FUNC_NAME
  585. void
  586. scm_ra_set_contp (SCM ra)
  587. {
  588. scm_sizet k = SCM_ARRAY_NDIM (ra);
  589. if (k)
  590. {
  591. long inc = SCM_ARRAY_DIMS (ra)[k - 1].inc;
  592. while (k--)
  593. {
  594. if (inc != SCM_ARRAY_DIMS (ra)[k].inc)
  595. {
  596. SCM_SETAND_CAR (ra, ~SCM_ARRAY_CONTIGUOUS);
  597. return;
  598. }
  599. inc *= (SCM_ARRAY_DIMS (ra)[k].ubnd
  600. - SCM_ARRAY_DIMS (ra)[k].lbnd + 1);
  601. }
  602. }
  603. SCM_SETOR_CAR (ra, SCM_ARRAY_CONTIGUOUS);
  604. }
  605. SCM_DEFINE (scm_make_shared_array, "make-shared-array", 2, 0, 1,
  606. (SCM oldra, SCM mapfunc, SCM dims),
  607. "@code{make-shared-array} can be used to create shared subarrays of other\n"
  608. "arrays. The @var{mapper} is a function that translates coordinates in\n"
  609. "the new array into coordinates in the old array. A @var{mapper} must be\n"
  610. "linear, and its range must stay within the bounds of the old array, but\n"
  611. "it can be otherwise arbitrary. A simple example:\n"
  612. "@example\n"
  613. "(define fred (make-array #f 8 8))\n"
  614. "(define freds-diagonal\n"
  615. " (make-shared-array fred (lambda (i) (list i i)) 8))\n"
  616. "(array-set! freds-diagonal 'foo 3)\n"
  617. "(array-ref fred 3 3) @result{} foo\n"
  618. "(define freds-center\n"
  619. " (make-shared-array fred (lambda (i j) (list (+ 3 i) (+ 3 j))) 2 2))\n"
  620. "(array-ref freds-center 0 0) @result{} foo\n"
  621. "@end example")
  622. #define FUNC_NAME s_scm_make_shared_array
  623. {
  624. SCM ra;
  625. SCM inds, indptr;
  626. SCM imap;
  627. scm_sizet i, k;
  628. long old_min, new_min, old_max, new_max;
  629. scm_array_dim *s;
  630. SCM_VALIDATE_ARRAY (1,oldra);
  631. SCM_VALIDATE_PROC (2,mapfunc);
  632. ra = scm_shap2ra (dims, FUNC_NAME);
  633. if (SCM_ARRAYP (oldra))
  634. {
  635. SCM_ARRAY_V (ra) = SCM_ARRAY_V (oldra);
  636. old_min = old_max = SCM_ARRAY_BASE (oldra);
  637. s = SCM_ARRAY_DIMS (oldra);
  638. k = SCM_ARRAY_NDIM (oldra);
  639. while (k--)
  640. {
  641. if (s[k].inc > 0)
  642. old_max += (s[k].ubnd - s[k].lbnd) * s[k].inc;
  643. else
  644. old_min += (s[k].ubnd - s[k].lbnd) * s[k].inc;
  645. }
  646. }
  647. else
  648. {
  649. SCM_ARRAY_V (ra) = oldra;
  650. old_min = 0;
  651. old_max = (long) SCM_LENGTH (oldra) - 1;
  652. }
  653. inds = SCM_EOL;
  654. s = SCM_ARRAY_DIMS (ra);
  655. for (k = 0; k < SCM_ARRAY_NDIM (ra); k++)
  656. {
  657. inds = scm_cons (SCM_MAKINUM (s[k].lbnd), inds);
  658. if (s[k].ubnd < s[k].lbnd)
  659. {
  660. if (1 == SCM_ARRAY_NDIM (ra))
  661. ra = scm_make_uve (0L, scm_array_prototype (ra));
  662. else
  663. SCM_ARRAY_V (ra) = scm_make_uve (0L, scm_array_prototype (ra));
  664. return ra;
  665. }
  666. }
  667. imap = scm_apply (mapfunc, scm_reverse (inds), SCM_EOL);
  668. if (SCM_ARRAYP (oldra))
  669. i = (scm_sizet) scm_aind (oldra, imap, FUNC_NAME);
  670. else
  671. {
  672. if (SCM_NINUMP (imap))
  673. {
  674. SCM_ASSERT (1 == scm_ilength (imap) && SCM_INUMP (SCM_CAR (imap)),
  675. imap, s_bad_ind, FUNC_NAME);
  676. imap = SCM_CAR (imap);
  677. }
  678. i = SCM_INUM (imap);
  679. }
  680. SCM_ARRAY_BASE (ra) = new_min = new_max = i;
  681. indptr = inds;
  682. k = SCM_ARRAY_NDIM (ra);
  683. while (k--)
  684. {
  685. if (s[k].ubnd > s[k].lbnd)
  686. {
  687. SCM_SETCAR (indptr, SCM_MAKINUM (SCM_INUM (SCM_CAR (indptr)) + 1));
  688. imap = scm_apply (mapfunc, scm_reverse (inds), SCM_EOL);
  689. if (SCM_ARRAYP (oldra))
  690. s[k].inc = scm_aind (oldra, imap, FUNC_NAME) - i;
  691. else
  692. {
  693. if (SCM_NINUMP (imap))
  694. {
  695. SCM_ASSERT (1 == scm_ilength (imap) && SCM_INUMP (SCM_CAR (imap)),
  696. imap, s_bad_ind, FUNC_NAME);
  697. imap = SCM_CAR (imap);
  698. }
  699. s[k].inc = (long) SCM_INUM (imap) - i;
  700. }
  701. i += s[k].inc;
  702. if (s[k].inc > 0)
  703. new_max += (s[k].ubnd - s[k].lbnd) * s[k].inc;
  704. else
  705. new_min += (s[k].ubnd - s[k].lbnd) * s[k].inc;
  706. }
  707. else
  708. s[k].inc = new_max - new_min + 1; /* contiguous by default */
  709. indptr = SCM_CDR (indptr);
  710. }
  711. SCM_ASSERT (old_min <= new_min && old_max >= new_max, SCM_UNDEFINED,
  712. "mapping out of range", FUNC_NAME);
  713. if (1 == SCM_ARRAY_NDIM (ra) && 0 == SCM_ARRAY_BASE (ra))
  714. {
  715. if (1 == s->inc && 0 == s->lbnd
  716. && SCM_LENGTH (SCM_ARRAY_V (ra)) == 1 + s->ubnd)
  717. return SCM_ARRAY_V (ra);
  718. if (s->ubnd < s->lbnd)
  719. return scm_make_uve (0L, scm_array_prototype (ra));
  720. }
  721. scm_ra_set_contp (ra);
  722. return ra;
  723. }
  724. #undef FUNC_NAME
  725. /* args are RA . DIMS */
  726. SCM_DEFINE (scm_transpose_array, "transpose-array", 1, 0, 1,
  727. (SCM ra, SCM args),
  728. "Returns an array sharing contents with @var{array}, but with dimensions\n"
  729. "arranged in a different order. There must be one @var{dim} argument for\n"
  730. "each dimension of @var{array}. @var{dim0}, @var{dim1}, @dots{} should\n"
  731. "be integers between 0 and the rank of the array to be returned. Each\n"
  732. "integer in that range must appear at least once in the argument list.\n\n"
  733. "The values of @var{dim0}, @var{dim1}, @dots{} correspond to dimensions\n"
  734. "in the array to be returned, their positions in the argument list to\n"
  735. "dimensions of @var{array}. Several @var{dim}s may have the same value,\n"
  736. "in which case the returned array will have smaller rank than\n"
  737. "@var{array}.\n\n"
  738. "examples:\n"
  739. "@example\n"
  740. "(transpose-array '#2((a b) (c d)) 1 0) @result{} #2((a c) (b d))\n"
  741. "(transpose-array '#2((a b) (c d)) 0 0) @result{} #1(a d)\n"
  742. "(transpose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 1 0) @result{}\n"
  743. " #2((a 4) (b 5) (c 6))\n"
  744. "@end example")
  745. #define FUNC_NAME s_scm_transpose_array
  746. {
  747. SCM res, vargs, *ve = &vargs;
  748. scm_array_dim *s, *r;
  749. int ndim, i, k;
  750. SCM_ASSERT (SCM_NIMP (ra), ra, SCM_ARG1, FUNC_NAME);
  751. switch (SCM_TYP7 (ra))
  752. {
  753. default:
  754. badarg:SCM_WTA (1,ra);
  755. case scm_tc7_bvect:
  756. case scm_tc7_string:
  757. case scm_tc7_byvect:
  758. case scm_tc7_uvect:
  759. case scm_tc7_ivect:
  760. case scm_tc7_fvect:
  761. case scm_tc7_dvect:
  762. case scm_tc7_cvect:
  763. case scm_tc7_svect:
  764. #ifdef HAVE_LONG_LONGS
  765. case scm_tc7_llvect:
  766. #endif
  767. SCM_ASSERT (!SCM_NULLP (args) && SCM_NULLP (SCM_CDR (args)),
  768. scm_makfrom0str (FUNC_NAME), SCM_WNA, NULL);
  769. SCM_ASSERT (SCM_INUMP (SCM_CAR (args)), SCM_CAR (args), SCM_ARG2,
  770. FUNC_NAME);
  771. SCM_ASSERT (SCM_EQ_P (SCM_INUM0, SCM_CAR (args)), SCM_CAR (args), SCM_OUTOFRANGE,
  772. FUNC_NAME);
  773. return ra;
  774. case scm_tc7_smob:
  775. SCM_ASRTGO (SCM_ARRAYP (ra), badarg);
  776. vargs = scm_vector (args);
  777. SCM_ASSERT (SCM_LENGTH (vargs) == SCM_ARRAY_NDIM (ra),
  778. scm_makfrom0str (FUNC_NAME), SCM_WNA, NULL);
  779. ve = SCM_VELTS (vargs);
  780. ndim = 0;
  781. for (k = 0; k < SCM_ARRAY_NDIM (ra); k++)
  782. {
  783. SCM_ASSERT (SCM_INUMP (ve[k]), ve[k], (SCM_ARG2 + k),
  784. FUNC_NAME);
  785. i = SCM_INUM (ve[k]);
  786. SCM_ASSERT (i >= 0 && i < SCM_ARRAY_NDIM (ra), ve[k],
  787. SCM_OUTOFRANGE, FUNC_NAME);
  788. if (ndim < i)
  789. ndim = i;
  790. }
  791. ndim++;
  792. res = scm_make_ra (ndim);
  793. SCM_ARRAY_V (res) = SCM_ARRAY_V (ra);
  794. SCM_ARRAY_BASE (res) = SCM_ARRAY_BASE (ra);
  795. for (k = ndim; k--;)
  796. {
  797. SCM_ARRAY_DIMS (res)[k].lbnd = 0;
  798. SCM_ARRAY_DIMS (res)[k].ubnd = -1;
  799. }
  800. for (k = SCM_ARRAY_NDIM (ra); k--;)
  801. {
  802. i = SCM_INUM (ve[k]);
  803. s = &(SCM_ARRAY_DIMS (ra)[k]);
  804. r = &(SCM_ARRAY_DIMS (res)[i]);
  805. if (r->ubnd < r->lbnd)
  806. {
  807. r->lbnd = s->lbnd;
  808. r->ubnd = s->ubnd;
  809. r->inc = s->inc;
  810. ndim--;
  811. }
  812. else
  813. {
  814. if (r->ubnd > s->ubnd)
  815. r->ubnd = s->ubnd;
  816. if (r->lbnd < s->lbnd)
  817. {
  818. SCM_ARRAY_BASE (res) += (s->lbnd - r->lbnd) * r->inc;
  819. r->lbnd = s->lbnd;
  820. }
  821. r->inc += s->inc;
  822. }
  823. }
  824. SCM_ASSERT (ndim <= 0, args, "bad argument list", FUNC_NAME);
  825. scm_ra_set_contp (res);
  826. return res;
  827. }
  828. }
  829. #undef FUNC_NAME
  830. /* args are RA . AXES */
  831. SCM_DEFINE (scm_enclose_array, "enclose-array", 1, 0, 1,
  832. (SCM ra, SCM axes),
  833. "@var{dim0}, @var{dim1} @dots{} should be nonnegative integers less than\n"
  834. "the rank of @var{array}. @var{enclose-array} returns an array\n"
  835. "resembling an array of shared arrays. The dimensions of each shared\n"
  836. "array are the same as the @var{dim}th dimensions of the original array,\n"
  837. "the dimensions of the outer array are the same as those of the original\n"
  838. "array that did not match a @var{dim}.\n\n"
  839. "An enclosed array is not a general Scheme array. Its elements may not\n"
  840. "be set using @code{array-set!}. Two references to the same element of\n"
  841. "an enclosed array will be @code{equal?} but will not in general be\n"
  842. "@code{eq?}. The value returned by @var{array-prototype} when given an\n"
  843. "enclosed array is unspecified.\n\n"
  844. "examples:\n"
  845. "@example\n"
  846. "(enclose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1) @result{}\n"
  847. " #<enclosed-array (#1(a d) #1(b e) #1(c f)) (#1(1 4) #1(2 5) #1(3 6))>\n\n"
  848. "(enclose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 0) @result{}\n"
  849. " #<enclosed-array #2((a 1) (d 4)) #2((b 2) (e 5)) #2((c 3) (f 6))>\n"
  850. "@end example")
  851. #define FUNC_NAME s_scm_enclose_array
  852. {
  853. SCM axv, res, ra_inr;
  854. scm_array_dim vdim, *s = &vdim;
  855. int ndim, j, k, ninr, noutr;
  856. if (SCM_NULLP (axes))
  857. axes = scm_cons ((SCM_ARRAYP (ra) ? SCM_MAKINUM (SCM_ARRAY_NDIM (ra) - 1) : SCM_INUM0), SCM_EOL);
  858. ninr = scm_ilength (axes);
  859. SCM_ASSERT (0 <= ninr, scm_makfrom0str (FUNC_NAME), SCM_WNA, NULL);
  860. ra_inr = scm_make_ra (ninr);
  861. SCM_ASRTGO (SCM_NIMP (ra), badarg1);
  862. switch SCM_TYP7
  863. (ra)
  864. {
  865. default:
  866. badarg1:SCM_WTA (1,ra);
  867. case scm_tc7_string:
  868. case scm_tc7_bvect:
  869. case scm_tc7_byvect:
  870. case scm_tc7_uvect:
  871. case scm_tc7_ivect:
  872. case scm_tc7_fvect:
  873. case scm_tc7_dvect:
  874. case scm_tc7_cvect:
  875. case scm_tc7_vector:
  876. case scm_tc7_wvect:
  877. case scm_tc7_svect:
  878. #ifdef HAVE_LONG_LONGS
  879. case scm_tc7_llvect:
  880. #endif
  881. s->lbnd = 0;
  882. s->ubnd = SCM_LENGTH (ra) - 1;
  883. s->inc = 1;
  884. SCM_ARRAY_V (ra_inr) = ra;
  885. SCM_ARRAY_BASE (ra_inr) = 0;
  886. ndim = 1;
  887. break;
  888. case scm_tc7_smob:
  889. SCM_ASRTGO (SCM_ARRAYP (ra), badarg1);
  890. s = SCM_ARRAY_DIMS (ra);
  891. SCM_ARRAY_V (ra_inr) = SCM_ARRAY_V (ra);
  892. SCM_ARRAY_BASE (ra_inr) = SCM_ARRAY_BASE (ra);
  893. ndim = SCM_ARRAY_NDIM (ra);
  894. break;
  895. }
  896. noutr = ndim - ninr;
  897. axv = scm_make_string (SCM_MAKINUM (ndim), SCM_MAKE_CHAR (0));
  898. SCM_ASSERT (0 <= noutr, scm_makfrom0str (FUNC_NAME), SCM_WNA, NULL);
  899. res = scm_make_ra (noutr);
  900. SCM_ARRAY_BASE (res) = SCM_ARRAY_BASE (ra_inr);
  901. SCM_ARRAY_V (res) = ra_inr;
  902. for (k = 0; k < ninr; k++, axes = SCM_CDR (axes))
  903. {
  904. SCM_ASSERT (SCM_INUMP (SCM_CAR (axes)), SCM_CAR (axes), "bad axis", FUNC_NAME);
  905. j = SCM_INUM (SCM_CAR (axes));
  906. SCM_ARRAY_DIMS (ra_inr)[k].lbnd = s[j].lbnd;
  907. SCM_ARRAY_DIMS (ra_inr)[k].ubnd = s[j].ubnd;
  908. SCM_ARRAY_DIMS (ra_inr)[k].inc = s[j].inc;
  909. SCM_CHARS (axv)[j] = 1;
  910. }
  911. for (j = 0, k = 0; k < noutr; k++, j++)
  912. {
  913. while (SCM_CHARS (axv)[j])
  914. j++;
  915. SCM_ARRAY_DIMS (res)[k].lbnd = s[j].lbnd;
  916. SCM_ARRAY_DIMS (res)[k].ubnd = s[j].ubnd;
  917. SCM_ARRAY_DIMS (res)[k].inc = s[j].inc;
  918. }
  919. scm_ra_set_contp (ra_inr);
  920. scm_ra_set_contp (res);
  921. return res;
  922. }
  923. #undef FUNC_NAME
  924. SCM_DEFINE (scm_array_in_bounds_p, "array-in-bounds?", 1, 0, 1,
  925. (SCM v, SCM args),
  926. "Returns @code{#t} if its arguments would be acceptable to array-ref.")
  927. #define FUNC_NAME s_scm_array_in_bounds_p
  928. {
  929. SCM ind = SCM_EOL;
  930. long pos = 0;
  931. register scm_sizet k;
  932. register long j;
  933. scm_array_dim *s;
  934. SCM_ASRTGO (SCM_NIMP (v), badarg1);
  935. if (SCM_NIMP (args))
  936. {
  937. ind = SCM_CAR (args);
  938. args = SCM_CDR (args);
  939. SCM_ASSERT (SCM_INUMP (ind), ind, SCM_ARG2, FUNC_NAME);
  940. pos = SCM_INUM (ind);
  941. }
  942. tail:
  943. switch SCM_TYP7
  944. (v)
  945. {
  946. default:
  947. badarg1:SCM_WTA (1,v);
  948. wna: scm_wrong_num_args (scm_makfrom0str (FUNC_NAME));
  949. case scm_tc7_smob:
  950. k = SCM_ARRAY_NDIM (v);
  951. s = SCM_ARRAY_DIMS (v);
  952. pos = SCM_ARRAY_BASE (v);
  953. if (!k)
  954. {
  955. SCM_ASRTGO (SCM_NULLP (ind), wna);
  956. ind = SCM_INUM0;
  957. }
  958. else
  959. while (!0)
  960. {
  961. j = SCM_INUM (ind);
  962. if (!(j >= (s->lbnd) && j <= (s->ubnd)))
  963. {
  964. SCM_ASRTGO (--k == scm_ilength (args), wna);
  965. return SCM_BOOL_F;
  966. }
  967. pos += (j - s->lbnd) * (s->inc);
  968. if (!(--k && SCM_NIMP (args)))
  969. break;
  970. ind = SCM_CAR (args);
  971. args = SCM_CDR (args);
  972. s++;
  973. SCM_ASSERT (SCM_INUMP (ind), ind, s_bad_ind, FUNC_NAME);
  974. }
  975. SCM_ASRTGO (0 == k, wna);
  976. v = SCM_ARRAY_V (v);
  977. goto tail;
  978. case scm_tc7_bvect:
  979. case scm_tc7_string:
  980. case scm_tc7_byvect:
  981. case scm_tc7_uvect:
  982. case scm_tc7_ivect:
  983. case scm_tc7_fvect:
  984. case scm_tc7_dvect:
  985. case scm_tc7_cvect:
  986. case scm_tc7_svect:
  987. #ifdef HAVE_LONG_LONGS
  988. case scm_tc7_llvect:
  989. #endif
  990. case scm_tc7_vector:
  991. case scm_tc7_wvect:
  992. SCM_ASRTGO (SCM_NULLP (args) && SCM_INUMP (ind), wna);
  993. return SCM_BOOL(pos >= 0 && pos < SCM_LENGTH (v));
  994. }
  995. }
  996. #undef FUNC_NAME
  997. SCM_REGISTER_PROC(s_array_ref, "array-ref", 1, 0, 1, scm_uniform_vector_ref);
  998. SCM_DEFINE (scm_uniform_vector_ref, "uniform-vector-ref", 2, 0, 0,
  999. (SCM v, SCM args),
  1000. "Returns the element at the @code{(index1, index2)} element in @var{array}.")
  1001. #define FUNC_NAME s_scm_uniform_vector_ref
  1002. {
  1003. long pos;
  1004. if (SCM_IMP (v))
  1005. {
  1006. SCM_ASRTGO (SCM_NULLP (args), badarg);
  1007. return v;
  1008. }
  1009. else if (SCM_ARRAYP (v))
  1010. {
  1011. pos = scm_aind (v, args, FUNC_NAME);
  1012. v = SCM_ARRAY_V (v);
  1013. }
  1014. else
  1015. {
  1016. if (SCM_NIMP (args))
  1017. {
  1018. SCM_ASSERT (SCM_CONSP (args) && SCM_INUMP (SCM_CAR (args)), args, SCM_ARG2, FUNC_NAME);
  1019. pos = SCM_INUM (SCM_CAR (args));
  1020. SCM_ASRTGO (SCM_NULLP (SCM_CDR (args)), wna);
  1021. }
  1022. else
  1023. {
  1024. SCM_VALIDATE_INUM (2,args);
  1025. pos = SCM_INUM (args);
  1026. }
  1027. SCM_ASRTGO (pos >= 0 && pos < SCM_LENGTH (v), outrng);
  1028. }
  1029. switch SCM_TYP7
  1030. (v)
  1031. {
  1032. default:
  1033. if (SCM_NULLP (args))
  1034. return v;
  1035. badarg:
  1036. SCM_WTA (1,v);
  1037. abort ();
  1038. outrng:
  1039. scm_out_of_range (FUNC_NAME, SCM_MAKINUM (pos));
  1040. wna:
  1041. scm_wrong_num_args (SCM_FUNC_NAME);
  1042. case scm_tc7_smob:
  1043. { /* enclosed */
  1044. int k = SCM_ARRAY_NDIM (v);
  1045. SCM res = scm_make_ra (k);
  1046. SCM_ARRAY_V (res) = SCM_ARRAY_V (v);
  1047. SCM_ARRAY_BASE (res) = pos;
  1048. while (k--)
  1049. {
  1050. SCM_ARRAY_DIMS (res)[k].lbnd = SCM_ARRAY_DIMS (v)[k].lbnd;
  1051. SCM_ARRAY_DIMS (res)[k].ubnd = SCM_ARRAY_DIMS (v)[k].ubnd;
  1052. SCM_ARRAY_DIMS (res)[k].inc = SCM_ARRAY_DIMS (v)[k].inc;
  1053. }
  1054. return res;
  1055. }
  1056. case scm_tc7_bvect:
  1057. if (SCM_BITVEC_REF (v, pos))
  1058. return SCM_BOOL_T;
  1059. else
  1060. return SCM_BOOL_F;
  1061. case scm_tc7_string:
  1062. return SCM_MAKE_CHAR (SCM_UCHARS (v)[pos]);
  1063. case scm_tc7_byvect:
  1064. return SCM_MAKINUM (((char *)SCM_CHARS (v))[pos]);
  1065. case scm_tc7_uvect:
  1066. return scm_ulong2num (((unsigned long *) SCM_VELTS (v))[pos]);
  1067. case scm_tc7_ivect:
  1068. return scm_long2num(((signed long *) SCM_VELTS (v))[pos]);
  1069. case scm_tc7_svect:
  1070. return SCM_MAKINUM (((short *) SCM_CELL_WORD_1 (v))[pos]);
  1071. #ifdef HAVE_LONG_LONGS
  1072. case scm_tc7_llvect:
  1073. return scm_long_long2num (((long_long *) SCM_CELL_WORD_1 (v))[pos]);
  1074. #endif
  1075. case scm_tc7_fvect:
  1076. return scm_make_real (((float *) SCM_CELL_WORD_1 (v))[pos]);
  1077. case scm_tc7_dvect:
  1078. return scm_make_real (((double *) SCM_CELL_WORD_1 (v))[pos]);
  1079. case scm_tc7_cvect:
  1080. return scm_make_complex (((double *) SCM_CELL_WORD_1 (v))[2 * pos],
  1081. ((double *) SCM_CELL_WORD_1 (v))[2 * pos + 1]);
  1082. case scm_tc7_vector:
  1083. case scm_tc7_wvect:
  1084. return SCM_VELTS (v)[pos];
  1085. }
  1086. }
  1087. #undef FUNC_NAME
  1088. /* Internal version of scm_uniform_vector_ref for uves that does no error checking and
  1089. tries to recycle conses. (Make *sure* you want them recycled.) */
  1090. SCM
  1091. scm_cvref (SCM v, scm_sizet pos, SCM last)
  1092. {
  1093. switch SCM_TYP7 (v)
  1094. {
  1095. default:
  1096. scm_wta (v, (char *) SCM_ARG1, "PROGRAMMING ERROR: scm_cvref");
  1097. case scm_tc7_bvect:
  1098. if (SCM_BITVEC_REF(v,pos))
  1099. return SCM_BOOL_T;
  1100. else
  1101. return SCM_BOOL_F;
  1102. case scm_tc7_string:
  1103. return SCM_MAKE_CHAR (SCM_UCHARS (v)[pos]);
  1104. case scm_tc7_byvect:
  1105. return SCM_MAKINUM (((char *)SCM_CHARS (v))[pos]);
  1106. case scm_tc7_uvect:
  1107. return scm_ulong2num(((unsigned long *) SCM_VELTS (v))[pos]);
  1108. case scm_tc7_ivect:
  1109. return scm_long2num(((signed long *) SCM_VELTS (v))[pos]);
  1110. case scm_tc7_svect:
  1111. return SCM_MAKINUM (((short *) SCM_CELL_WORD_1 (v))[pos]);
  1112. #ifdef HAVE_LONG_LONGS
  1113. case scm_tc7_llvect:
  1114. return scm_long_long2num (((long_long *) SCM_CELL_WORD_1 (v))[pos]);
  1115. #endif
  1116. case scm_tc7_fvect:
  1117. if (SCM_NIMP (last) && !SCM_EQ_P (last, scm_flo0) && SCM_SLOPPY_REALP (last))
  1118. {
  1119. SCM_REAL_VALUE (last) = ((float *) SCM_CELL_WORD_1 (v))[pos];
  1120. return last;
  1121. }
  1122. return scm_make_real (((float *) SCM_CELL_WORD_1 (v))[pos]);
  1123. case scm_tc7_dvect:
  1124. if (SCM_NIMP (last) && !SCM_EQ_P (last, scm_flo0) && SCM_SLOPPY_REALP (last))
  1125. {
  1126. SCM_REAL_VALUE (last) = ((double *) SCM_CELL_WORD_1 (v))[pos];
  1127. return last;
  1128. }
  1129. return scm_make_real (((double *) SCM_CELL_WORD_1 (v))[pos]);
  1130. case scm_tc7_cvect:
  1131. if (SCM_NIMP (last) && SCM_SLOPPY_COMPLEXP (last))
  1132. {
  1133. SCM_COMPLEX_REAL (last) = ((double *) SCM_CELL_WORD_1 (v))[2 * pos];
  1134. SCM_COMPLEX_IMAG (last) = ((double *) SCM_CELL_WORD_1 (v))[2 * pos + 1];
  1135. return last;
  1136. }
  1137. return scm_make_complex (((double *) SCM_CELL_WORD_1 (v))[2 * pos],
  1138. ((double *) SCM_CELL_WORD_1 (v))[2 * pos + 1]);
  1139. case scm_tc7_vector:
  1140. case scm_tc7_wvect:
  1141. return SCM_VELTS (v)[pos];
  1142. case scm_tc7_smob:
  1143. { /* enclosed scm_array */
  1144. int k = SCM_ARRAY_NDIM (v);
  1145. SCM res = scm_make_ra (k);
  1146. SCM_ARRAY_V (res) = SCM_ARRAY_V (v);
  1147. SCM_ARRAY_BASE (res) = pos;
  1148. while (k--)
  1149. {
  1150. SCM_ARRAY_DIMS (res)[k].ubnd = SCM_ARRAY_DIMS (v)[k].ubnd;
  1151. SCM_ARRAY_DIMS (res)[k].lbnd = SCM_ARRAY_DIMS (v)[k].lbnd;
  1152. SCM_ARRAY_DIMS (res)[k].inc = SCM_ARRAY_DIMS (v)[k].inc;
  1153. }
  1154. return res;
  1155. }
  1156. }
  1157. }
  1158. SCM_REGISTER_PROC(s_uniform_array_set1_x, "uniform-array-set1!", 3, 0, 0, scm_array_set_x);
  1159. /* Note that args may be a list or an immediate object, depending which
  1160. PROC is used (and it's called from C too). */
  1161. SCM_DEFINE (scm_array_set_x, "array-set!", 2, 0, 1,
  1162. (SCM v, SCM obj, SCM args),
  1163. "Sets the element at the @code{(index1, index2)} element in @var{array} to\n"
  1164. "@var{new-value}. The value returned by array-set! is unspecified.")
  1165. #define FUNC_NAME s_scm_array_set_x
  1166. {
  1167. long pos = 0;
  1168. SCM_ASRTGO (SCM_NIMP (v), badarg1);
  1169. if (SCM_ARRAYP (v))
  1170. {
  1171. pos = scm_aind (v, args, FUNC_NAME);
  1172. v = SCM_ARRAY_V (v);
  1173. }
  1174. else
  1175. {
  1176. if (SCM_NIMP (args))
  1177. {
  1178. SCM_ASSERT (SCM_CONSP(args) && SCM_INUMP (SCM_CAR (args)), args,
  1179. SCM_ARG3, FUNC_NAME);
  1180. SCM_ASRTGO (SCM_NULLP (SCM_CDR (args)), wna);
  1181. pos = SCM_INUM (SCM_CAR (args));
  1182. }
  1183. else
  1184. {
  1185. SCM_VALIDATE_INUM_COPY (3,args,pos);
  1186. }
  1187. SCM_ASRTGO (pos >= 0 && pos < SCM_LENGTH (v), outrng);
  1188. }
  1189. switch (SCM_TYP7 (v))
  1190. {
  1191. default: badarg1:
  1192. SCM_WTA (1,v);
  1193. abort ();
  1194. outrng:
  1195. scm_out_of_range (FUNC_NAME, SCM_MAKINUM (pos));
  1196. wna:
  1197. scm_wrong_num_args (SCM_FUNC_NAME);
  1198. case scm_tc7_smob: /* enclosed */
  1199. goto badarg1;
  1200. case scm_tc7_bvect:
  1201. if (SCM_FALSEP (obj))
  1202. SCM_BITVEC_CLR(v,pos);
  1203. else if (SCM_EQ_P (obj, SCM_BOOL_T))
  1204. SCM_BITVEC_SET(v,pos);
  1205. else
  1206. badobj:SCM_WTA (2,obj);
  1207. break;
  1208. case scm_tc7_string:
  1209. SCM_ASRTGO (SCM_CHARP (obj), badobj);
  1210. SCM_UCHARS (v)[pos] = SCM_CHAR (obj);
  1211. break;
  1212. case scm_tc7_byvect:
  1213. if (SCM_CHARP (obj))
  1214. obj = SCM_MAKINUM ((char) SCM_CHAR (obj));
  1215. SCM_ASRTGO (SCM_INUMP (obj), badobj);
  1216. ((char *)SCM_CHARS (v))[pos] = SCM_INUM (obj);
  1217. break;
  1218. case scm_tc7_uvect:
  1219. SCM_VELTS(v)[pos] = SCM_PACK (scm_num2ulong(obj, (char *)SCM_ARG2, FUNC_NAME));
  1220. break;
  1221. case scm_tc7_ivect:
  1222. SCM_VELTS(v)[pos] = SCM_PACK (scm_num2long(obj, (char *)SCM_ARG2, FUNC_NAME));
  1223. break;
  1224. case scm_tc7_svect:
  1225. SCM_ASRTGO (SCM_INUMP (obj), badobj);
  1226. ((short *) SCM_CELL_WORD_1 (v))[pos] = SCM_INUM (obj);
  1227. break;
  1228. #ifdef HAVE_LONG_LONGS
  1229. case scm_tc7_llvect:
  1230. ((long_long *) SCM_CELL_WORD_1 (v))[pos] = scm_num2long_long (obj, (char *)SCM_ARG2, FUNC_NAME);
  1231. break;
  1232. #endif
  1233. case scm_tc7_fvect:
  1234. ((float *) SCM_CELL_WORD_1 (v))[pos] = (float) scm_num2dbl (obj, FUNC_NAME);
  1235. break;
  1236. case scm_tc7_dvect:
  1237. ((double *) SCM_CELL_WORD_1 (v))[pos] = scm_num2dbl (obj, FUNC_NAME);
  1238. break;
  1239. case scm_tc7_cvect:
  1240. SCM_ASRTGO (SCM_INEXACTP (obj), badobj);
  1241. if (SCM_REALP (obj)) {
  1242. ((double *) SCM_CELL_WORD_1 (v))[2 * pos] = SCM_REAL_VALUE (obj);
  1243. ((double *) SCM_CELL_WORD_1 (v))[2 * pos + 1] = 0.0;
  1244. } else {
  1245. ((double *) SCM_CELL_WORD_1 (v))[2 * pos] = SCM_COMPLEX_REAL (obj);
  1246. ((double *) SCM_CELL_WORD_1 (v))[2 * pos + 1] = SCM_COMPLEX_IMAG (obj);
  1247. }
  1248. break;
  1249. case scm_tc7_vector:
  1250. case scm_tc7_wvect:
  1251. SCM_VELTS (v)[pos] = obj;
  1252. break;
  1253. }
  1254. return SCM_UNSPECIFIED;
  1255. }
  1256. #undef FUNC_NAME
  1257. /* attempts to unroll an array into a one-dimensional array.
  1258. returns the unrolled array or #f if it can't be done. */
  1259. /* if strict is not SCM_UNDEFINED, return #f if returned array
  1260. wouldn't have contiguous elements. */
  1261. SCM_DEFINE (scm_array_contents, "array-contents", 1, 1, 0,
  1262. (SCM ra, SCM strict),
  1263. "@deffnx primitive array-contents array strict\n"
  1264. "If @var{array} may be @dfn{unrolled} into a one dimensional shared array\n"
  1265. "without changing their order (last subscript changing fastest), then\n"
  1266. "@code{array-contents} returns that shared array, otherwise it returns\n"
  1267. "@code{#f}. All arrays made by @var{make-array} and\n"
  1268. "@var{make-uniform-array} may be unrolled, some arrays made by\n"
  1269. "@var{make-shared-array} may not be.\n\n"
  1270. "If the optional argument @var{strict} is provided, a shared array will\n"
  1271. "be returned only if its elements are stored internally contiguous in\n"
  1272. "memory.")
  1273. #define FUNC_NAME s_scm_array_contents
  1274. {
  1275. SCM sra;
  1276. if (SCM_IMP (ra))
  1277. return SCM_BOOL_F;
  1278. switch SCM_TYP7 (ra)
  1279. {
  1280. default:
  1281. return SCM_BOOL_F;
  1282. case scm_tc7_vector:
  1283. case scm_tc7_wvect:
  1284. case scm_tc7_string:
  1285. case scm_tc7_bvect:
  1286. case scm_tc7_byvect:
  1287. case scm_tc7_uvect:
  1288. case scm_tc7_ivect:
  1289. case scm_tc7_fvect:
  1290. case scm_tc7_dvect:
  1291. case scm_tc7_cvect:
  1292. case scm_tc7_svect:
  1293. #ifdef HAVE_LONG_LONGS
  1294. case scm_tc7_llvect:
  1295. #endif
  1296. return ra;
  1297. case scm_tc7_smob:
  1298. {
  1299. scm_sizet k, ndim = SCM_ARRAY_NDIM (ra), len = 1;
  1300. if (!SCM_ARRAYP (ra) || !SCM_ARRAY_CONTP (ra))
  1301. return SCM_BOOL_F;
  1302. for (k = 0; k < ndim; k++)
  1303. len *= SCM_ARRAY_DIMS (ra)[k].ubnd - SCM_ARRAY_DIMS (ra)[k].lbnd + 1;
  1304. if (!SCM_UNBNDP (strict))
  1305. {
  1306. if (ndim && (1 != SCM_ARRAY_DIMS (ra)[ndim - 1].inc))
  1307. return SCM_BOOL_F;
  1308. if (scm_tc7_bvect == SCM_TYP7 (SCM_ARRAY_V (ra)))
  1309. {
  1310. if (len != SCM_LENGTH (SCM_ARRAY_V (ra)) ||
  1311. SCM_ARRAY_BASE (ra) % SCM_LONG_BIT ||
  1312. len % SCM_LONG_BIT)
  1313. return SCM_BOOL_F;
  1314. }
  1315. }
  1316. if ((len == SCM_LENGTH (SCM_ARRAY_V (ra))) && 0 == SCM_ARRAY_BASE (ra) && SCM_ARRAY_DIMS (ra)->inc)
  1317. return SCM_ARRAY_V (ra);
  1318. sra = scm_make_ra (1);
  1319. SCM_ARRAY_DIMS (sra)->lbnd = 0;
  1320. SCM_ARRAY_DIMS (sra)->ubnd = len - 1;
  1321. SCM_ARRAY_V (sra) = SCM_ARRAY_V (ra);
  1322. SCM_ARRAY_BASE (sra) = SCM_ARRAY_BASE (ra);
  1323. SCM_ARRAY_DIMS (sra)->inc = (ndim ? SCM_ARRAY_DIMS (ra)[ndim - 1].inc : 1);
  1324. return sra;
  1325. }
  1326. }
  1327. }
  1328. #undef FUNC_NAME
  1329. SCM
  1330. scm_ra2contig (SCM ra, int copy)
  1331. {
  1332. SCM ret;
  1333. long inc = 1;
  1334. scm_sizet k, len = 1;
  1335. for (k = SCM_ARRAY_NDIM (ra); k--;)
  1336. len *= SCM_ARRAY_DIMS (ra)[k].ubnd - SCM_ARRAY_DIMS (ra)[k].lbnd + 1;
  1337. k = SCM_ARRAY_NDIM (ra);
  1338. if (SCM_ARRAY_CONTP (ra) && ((0 == k) || (1 == SCM_ARRAY_DIMS (ra)[k - 1].inc)))
  1339. {
  1340. if (scm_tc7_bvect != SCM_TYP7 (ra))
  1341. return ra;
  1342. if ((len == SCM_LENGTH (SCM_ARRAY_V (ra)) &&
  1343. 0 == SCM_ARRAY_BASE (ra) % SCM_LONG_BIT &&
  1344. 0 == len % SCM_LONG_BIT))
  1345. return ra;
  1346. }
  1347. ret = scm_make_ra (k);
  1348. SCM_ARRAY_BASE (ret) = 0;
  1349. while (k--)
  1350. {
  1351. SCM_ARRAY_DIMS (ret)[k].lbnd = SCM_ARRAY_DIMS (ra)[k].lbnd;
  1352. SCM_ARRAY_DIMS (ret)[k].ubnd = SCM_ARRAY_DIMS (ra)[k].ubnd;
  1353. SCM_ARRAY_DIMS (ret)[k].inc = inc;
  1354. inc *= SCM_ARRAY_DIMS (ra)[k].ubnd - SCM_ARRAY_DIMS (ra)[k].lbnd + 1;
  1355. }
  1356. SCM_ARRAY_V (ret) = scm_make_uve ((inc - 1), scm_array_prototype (ra));
  1357. if (copy)
  1358. scm_array_copy_x (ra, ret);
  1359. return ret;
  1360. }
  1361. SCM_DEFINE (scm_uniform_array_read_x, "uniform-array-read!", 1, 3, 0,
  1362. (SCM ra, SCM port_or_fd, SCM start, SCM end),
  1363. "@deffnx primitive uniform-vector-read! uve [port-or-fdes] [start] [end]\n"
  1364. "Attempts to read all elements of @var{ura}, in lexicographic order, as\n"
  1365. "binary objects from @var{port-or-fdes}.\n"
  1366. "If an end of file is encountered during\n"
  1367. "uniform-array-read! the objects up to that point only are put into @var{ura}\n"
  1368. "(starting at the beginning) and the remainder of the array is\n"
  1369. "unchanged.\n\n"
  1370. "The optional arguments @var{start} and @var{end} allow\n"
  1371. "a specified region of a vector (or linearized array) to be read,\n"
  1372. "leaving the remainder of the vector unchanged.\n\n"
  1373. "@code{uniform-array-read!} returns the number of objects read.\n"
  1374. "@var{port-or-fdes} may be omitted, in which case it defaults to the value\n"
  1375. "returned by @code{(current-input-port)}.")
  1376. #define FUNC_NAME s_scm_uniform_array_read_x
  1377. {
  1378. SCM cra = SCM_UNDEFINED, v = ra;
  1379. long sz, vlen, ans;
  1380. long cstart = 0;
  1381. long cend;
  1382. long offset = 0;
  1383. SCM_ASRTGO (SCM_NIMP (v), badarg1);
  1384. if (SCM_UNBNDP (port_or_fd))
  1385. port_or_fd = scm_cur_inp;
  1386. else
  1387. SCM_ASSERT (SCM_INUMP (port_or_fd)
  1388. || (SCM_OPINPORTP (port_or_fd)),
  1389. port_or_fd, SCM_ARG2, FUNC_NAME);
  1390. vlen = SCM_LENGTH (v);
  1391. loop:
  1392. switch SCM_TYP7 (v)
  1393. {
  1394. default:
  1395. badarg1:SCM_WTA (SCM_ARG1,v);
  1396. case scm_tc7_smob:
  1397. SCM_ASRTGO (SCM_ARRAYP (v), badarg1);
  1398. cra = scm_ra2contig (ra, 0);
  1399. cstart += SCM_ARRAY_BASE (cra);
  1400. vlen = SCM_ARRAY_DIMS (cra)->inc *
  1401. (SCM_ARRAY_DIMS (cra)->ubnd - SCM_ARRAY_DIMS (cra)->lbnd + 1);
  1402. v = SCM_ARRAY_V (cra);
  1403. goto loop;
  1404. case scm_tc7_string:
  1405. case scm_tc7_byvect:
  1406. sz = sizeof (char);
  1407. break;
  1408. case scm_tc7_bvect:
  1409. vlen = (vlen + SCM_LONG_BIT - 1) / SCM_LONG_BIT;
  1410. cstart /= SCM_LONG_BIT;
  1411. case scm_tc7_uvect:
  1412. case scm_tc7_ivect:
  1413. sz = sizeof (long);
  1414. break;
  1415. case scm_tc7_svect:
  1416. sz = sizeof (short);
  1417. break;
  1418. #ifdef HAVE_LONG_LONGS
  1419. case scm_tc7_llvect:
  1420. sz = sizeof (long_long);
  1421. break;
  1422. #endif
  1423. case scm_tc7_fvect:
  1424. sz = sizeof (float);
  1425. break;
  1426. case scm_tc7_dvect:
  1427. sz = sizeof (double);
  1428. break;
  1429. case scm_tc7_cvect:
  1430. sz = 2 * sizeof (double);
  1431. break;
  1432. }
  1433. cend = vlen;
  1434. if (!SCM_UNBNDP (start))
  1435. {
  1436. offset =
  1437. SCM_NUM2LONG (3, start);
  1438. if (offset < 0 || offset >= cend)
  1439. scm_out_of_range (FUNC_NAME, start);
  1440. if (!SCM_UNBNDP (end))
  1441. {
  1442. long tend =
  1443. SCM_NUM2LONG (4, end);
  1444. if (tend <= offset || tend > cend)
  1445. scm_out_of_range (FUNC_NAME, end);
  1446. cend = tend;
  1447. }
  1448. }
  1449. if (SCM_NIMP (port_or_fd))
  1450. {
  1451. scm_port *pt = SCM_PTAB_ENTRY (port_or_fd);
  1452. int remaining = (cend - offset) * sz;
  1453. char *dest = SCM_CHARS (v) + (cstart + offset) * sz;
  1454. if (pt->rw_active == SCM_PORT_WRITE)
  1455. scm_flush (port_or_fd);
  1456. ans = cend - offset;
  1457. while (remaining > 0)
  1458. {
  1459. if (pt->read_pos < pt->read_end)
  1460. {
  1461. int to_copy = min (pt->read_end - pt->read_pos,
  1462. remaining);
  1463. memcpy (dest, pt->read_pos, to_copy);
  1464. pt->read_pos += to_copy;
  1465. remaining -= to_copy;
  1466. dest += to_copy;
  1467. }
  1468. else
  1469. {
  1470. if (scm_fill_input (port_or_fd) == EOF)
  1471. {
  1472. if (remaining % sz != 0)
  1473. {
  1474. SCM_MISC_ERROR ("unexpected EOF", SCM_EOL);
  1475. }
  1476. ans -= remaining / sz;
  1477. break;
  1478. }
  1479. }
  1480. }
  1481. if (pt->rw_random)
  1482. pt->rw_active = SCM_PORT_READ;
  1483. }
  1484. else /* file descriptor. */
  1485. {
  1486. SCM_SYSCALL (ans = read (SCM_INUM (port_or_fd),
  1487. SCM_CHARS (v) + (cstart + offset) * sz,
  1488. (scm_sizet) (sz * (cend - offset))));
  1489. if (ans == -1)
  1490. SCM_SYSERROR;
  1491. }
  1492. if (SCM_TYP7 (v) == scm_tc7_bvect)
  1493. ans *= SCM_LONG_BIT;
  1494. if (!SCM_EQ_P (v, ra) && !SCM_EQ_P (cra, ra))
  1495. scm_array_copy_x (cra, ra);
  1496. return SCM_MAKINUM (ans);
  1497. }
  1498. #undef FUNC_NAME
  1499. SCM_DEFINE (scm_uniform_array_write, "uniform-array-write", 1, 3, 0,
  1500. (SCM v, SCM port_or_fd, SCM start, SCM end),
  1501. "@deffnx primitive uniform-vector-write uve [port-or-fdes] [start] [end]\n"
  1502. "Writes all elements of @var{ura} as binary objects to\n"
  1503. "@var{port-or-fdes}.\n\n"
  1504. "The optional arguments @var{start}\n"
  1505. "and @var{end} allow\n"
  1506. "a specified region of a vector (or linearized array) to be written.\n\n"
  1507. "The number of objects actually written is returned. \n"
  1508. "@var{port-or-fdes} may be\n"
  1509. "omitted, in which case it defaults to the value returned by\n"
  1510. "@code{(current-output-port)}.")
  1511. #define FUNC_NAME s_scm_uniform_array_write
  1512. {
  1513. long sz, vlen, ans;
  1514. long offset = 0;
  1515. long cstart = 0;
  1516. long cend;
  1517. port_or_fd = SCM_COERCE_OUTPORT (port_or_fd);
  1518. SCM_ASRTGO (SCM_NIMP (v), badarg1);
  1519. if (SCM_UNBNDP (port_or_fd))
  1520. port_or_fd = scm_cur_outp;
  1521. else
  1522. SCM_ASSERT (SCM_INUMP (port_or_fd)
  1523. || (SCM_OPOUTPORTP (port_or_fd)),
  1524. port_or_fd, SCM_ARG2, FUNC_NAME);
  1525. vlen = SCM_LENGTH (v);
  1526. loop:
  1527. switch SCM_TYP7 (v)
  1528. {
  1529. default:
  1530. badarg1:SCM_WTA (1, v);
  1531. case scm_tc7_smob:
  1532. SCM_ASRTGO (SCM_ARRAYP (v), badarg1);
  1533. v = scm_ra2contig (v, 1);
  1534. cstart = SCM_ARRAY_BASE (v);
  1535. vlen = SCM_ARRAY_DIMS (v)->inc
  1536. * (SCM_ARRAY_DIMS (v)->ubnd - SCM_ARRAY_DIMS (v)->lbnd + 1);
  1537. v = SCM_ARRAY_V (v);
  1538. goto loop;
  1539. case scm_tc7_string:
  1540. case scm_tc7_byvect:
  1541. sz = sizeof (char);
  1542. break;
  1543. case scm_tc7_bvect:
  1544. vlen = (vlen + SCM_LONG_BIT - 1) / SCM_LONG_BIT;
  1545. cstart /= SCM_LONG_BIT;
  1546. case scm_tc7_uvect:
  1547. case scm_tc7_ivect:
  1548. sz = sizeof (long);
  1549. break;
  1550. case scm_tc7_svect:
  1551. sz = sizeof (short);
  1552. break;
  1553. #ifdef HAVE_LONG_LONGS
  1554. case scm_tc7_llvect:
  1555. sz = sizeof (long_long);
  1556. break;
  1557. #endif
  1558. case scm_tc7_fvect:
  1559. sz = sizeof (float);
  1560. break;
  1561. case scm_tc7_dvect:
  1562. sz = sizeof (double);
  1563. break;
  1564. case scm_tc7_cvect:
  1565. sz = 2 * sizeof (double);
  1566. break;
  1567. }
  1568. cend = vlen;
  1569. if (!SCM_UNBNDP (start))
  1570. {
  1571. offset =
  1572. SCM_NUM2LONG (3, start);
  1573. if (offset < 0 || offset >= cend)
  1574. scm_out_of_range (FUNC_NAME, start);
  1575. if (!SCM_UNBNDP (end))
  1576. {
  1577. long tend =
  1578. SCM_NUM2LONG (4, end);
  1579. if (tend <= offset || tend > cend)
  1580. scm_out_of_range (FUNC_NAME, end);
  1581. cend = tend;
  1582. }
  1583. }
  1584. if (SCM_NIMP (port_or_fd))
  1585. {
  1586. char *source = SCM_CHARS (v) + (cstart + offset) * sz;
  1587. ans = cend - offset;
  1588. scm_lfwrite (source, ans * sz, port_or_fd);
  1589. }
  1590. else /* file descriptor. */
  1591. {
  1592. SCM_SYSCALL (ans = write (SCM_INUM (port_or_fd),
  1593. SCM_CHARS (v) + (cstart + offset) * sz,
  1594. (scm_sizet) (sz * (cend - offset))));
  1595. if (ans == -1)
  1596. SCM_SYSERROR;
  1597. }
  1598. if (SCM_TYP7 (v) == scm_tc7_bvect)
  1599. ans *= SCM_LONG_BIT;
  1600. return SCM_MAKINUM (ans);
  1601. }
  1602. #undef FUNC_NAME
  1603. static char cnt_tab[16] =
  1604. {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
  1605. SCM_DEFINE (scm_bit_count, "bit-count", 2, 0, 0,
  1606. (SCM b, SCM bitvector),
  1607. "Returns the number of occurrences of the boolean B in BITVECTOR.")
  1608. #define FUNC_NAME s_scm_bit_count
  1609. {
  1610. SCM_VALIDATE_BOOL (1, b);
  1611. SCM_ASSERT (!SCM_IMP (bitvector) && SCM_TYP7 (bitvector) == scm_tc7_bvect,
  1612. bitvector, 2, FUNC_NAME);
  1613. if (SCM_LENGTH (bitvector) == 0) {
  1614. return SCM_INUM0;
  1615. } else {
  1616. unsigned long int count = 0;
  1617. unsigned long int i = (SCM_LENGTH (bitvector) - 1) / SCM_LONG_BIT;
  1618. unsigned long int w = SCM_UNPACK (SCM_VELTS (bitvector)[i]);
  1619. if (SCM_FALSEP (b)) {
  1620. w = ~w;
  1621. };
  1622. w <<= SCM_LONG_BIT - 1 - ((SCM_LENGTH (bitvector) - 1) % SCM_LONG_BIT);
  1623. while (1) {
  1624. while (w) {
  1625. count += cnt_tab[w & 0x0f];
  1626. w >>= 4;
  1627. }
  1628. if (i == 0) {
  1629. return SCM_MAKINUM (count);
  1630. } else {
  1631. --i;
  1632. w = SCM_UNPACK (SCM_VELTS (bitvector)[i]);
  1633. if (SCM_FALSEP (b)) {
  1634. w = ~w;
  1635. }
  1636. }
  1637. }
  1638. }
  1639. }
  1640. #undef FUNC_NAME
  1641. SCM_DEFINE (scm_bit_position, "bit-position", 3, 0, 0,
  1642. (SCM item, SCM v, SCM k),
  1643. "Returns the minimum index of an occurrence of @var{bool} in @var{bv}\n"
  1644. "which is at least @var{k}. If no @var{bool} occurs within the specified\n"
  1645. "range @code{#f} is returned.")
  1646. #define FUNC_NAME s_scm_bit_position
  1647. {
  1648. long i, lenw, xbits, pos;
  1649. register unsigned long w;
  1650. SCM_VALIDATE_NIM (2,v);
  1651. SCM_VALIDATE_INUM_COPY (3,k,pos);
  1652. SCM_ASSERT ((pos <= SCM_LENGTH (v)) && (pos >= 0),
  1653. k, SCM_OUTOFRANGE, FUNC_NAME);
  1654. if (pos == SCM_LENGTH (v))
  1655. return SCM_BOOL_F;
  1656. switch SCM_TYP7 (v)
  1657. {
  1658. default:
  1659. SCM_WTA (2,v);
  1660. case scm_tc7_bvect:
  1661. if (0 == SCM_LENGTH (v))
  1662. return SCM_MAKINUM (-1L);
  1663. lenw = (SCM_LENGTH (v) - 1) / SCM_LONG_BIT; /* watch for part words */
  1664. i = pos / SCM_LONG_BIT;
  1665. w = SCM_UNPACK (SCM_VELTS (v)[i]);
  1666. if (SCM_FALSEP (item))
  1667. w = ~w;
  1668. xbits = (pos % SCM_LONG_BIT);
  1669. pos -= xbits;
  1670. w = ((w >> xbits) << xbits);
  1671. xbits = SCM_LONG_BIT - 1 - (SCM_LENGTH (v) - 1) % SCM_LONG_BIT;
  1672. while (!0)
  1673. {
  1674. if (w && (i == lenw))
  1675. w = ((w << xbits) >> xbits);
  1676. if (w)
  1677. while (w)
  1678. switch (w & 0x0f)
  1679. {
  1680. default:
  1681. return SCM_MAKINUM (pos);
  1682. case 2:
  1683. case 6:
  1684. case 10:
  1685. case 14:
  1686. return SCM_MAKINUM (pos + 1);
  1687. case 4:
  1688. case 12:
  1689. return SCM_MAKINUM (pos + 2);
  1690. case 8:
  1691. return SCM_MAKINUM (pos + 3);
  1692. case 0:
  1693. pos += 4;
  1694. w >>= 4;
  1695. }
  1696. if (++i > lenw)
  1697. break;
  1698. pos += SCM_LONG_BIT;
  1699. w = SCM_UNPACK (SCM_VELTS (v)[i]);
  1700. if (SCM_FALSEP (item))
  1701. w = ~w;
  1702. }
  1703. return SCM_BOOL_F;
  1704. }
  1705. }
  1706. #undef FUNC_NAME
  1707. SCM_DEFINE (scm_bit_set_star_x, "bit-set*!", 3, 0, 0,
  1708. (SCM v, SCM kv, SCM obj),
  1709. "If uve is a bit-vector @var{bv} and uve must be of the same length. If\n"
  1710. "@var{bool} is @code{#t}, uve is OR'ed into @var{bv}; If @var{bool} is @code{#f}, the\n"
  1711. "inversion of uve is AND'ed into @var{bv}.\n\n"
  1712. "If uve is a unsigned integer vector all the elements of uve must be\n"
  1713. "between 0 and the @code{LENGTH} of @var{bv}. The bits of @var{bv}\n"
  1714. "corresponding to the indexes in uve are set to @var{bool}.\n\n"
  1715. "The return value is unspecified.")
  1716. #define FUNC_NAME s_scm_bit_set_star_x
  1717. {
  1718. register long i, k, vlen;
  1719. SCM_ASRTGO (SCM_NIMP (v), badarg1);
  1720. SCM_ASRTGO (SCM_NIMP (kv), badarg2);
  1721. switch SCM_TYP7 (kv)
  1722. {
  1723. default:
  1724. badarg2:SCM_WTA (2,kv);
  1725. case scm_tc7_uvect:
  1726. switch SCM_TYP7 (v)
  1727. {
  1728. default:
  1729. badarg1: SCM_WTA (1,v);
  1730. case scm_tc7_bvect:
  1731. vlen = SCM_LENGTH (v);
  1732. if (SCM_FALSEP (obj))
  1733. for (i = SCM_LENGTH (kv); i;)
  1734. {
  1735. k = SCM_UNPACK (SCM_VELTS (kv)[--i]);
  1736. SCM_ASSERT ((k < vlen), SCM_MAKINUM (k), SCM_OUTOFRANGE, FUNC_NAME);
  1737. SCM_BITVEC_CLR(v,k);
  1738. }
  1739. else if (SCM_EQ_P (obj, SCM_BOOL_T))
  1740. for (i = SCM_LENGTH (kv); i;)
  1741. {
  1742. k = SCM_UNPACK (SCM_VELTS (kv)[--i]);
  1743. SCM_ASSERT ((k < vlen), SCM_MAKINUM (k), SCM_OUTOFRANGE, FUNC_NAME);
  1744. SCM_BITVEC_SET(v,k);
  1745. }
  1746. else
  1747. badarg3:SCM_WTA (3,obj);
  1748. }
  1749. break;
  1750. case scm_tc7_bvect:
  1751. SCM_ASRTGO (SCM_TYP7 (v) == scm_tc7_bvect && SCM_LENGTH (v) == SCM_LENGTH (kv), badarg1);
  1752. if (SCM_FALSEP (obj))
  1753. for (k = (SCM_LENGTH (v) + SCM_LONG_BIT - 1) / SCM_LONG_BIT; k--;)
  1754. SCM_UNPACK (SCM_VELTS (v)[k]) &= ~ SCM_UNPACK(SCM_VELTS (kv)[k]);
  1755. else if (SCM_EQ_P (obj, SCM_BOOL_T))
  1756. for (k = (SCM_LENGTH (v) + SCM_LONG_BIT - 1) / SCM_LONG_BIT; k--;)
  1757. SCM_UNPACK (SCM_VELTS (v)[k]) |= SCM_UNPACK (SCM_VELTS (kv)[k]);
  1758. else
  1759. goto badarg3;
  1760. break;
  1761. }
  1762. return SCM_UNSPECIFIED;
  1763. }
  1764. #undef FUNC_NAME
  1765. SCM_DEFINE (scm_bit_count_star, "bit-count*", 3, 0, 0,
  1766. (SCM v, SCM kv, SCM obj),
  1767. "Returns\n"
  1768. "@example\n"
  1769. "(bit-count (bit-set*! (if bool bv (bit-invert! bv)) uve #t) #t).\n"
  1770. "@end example\n"
  1771. "@var{bv} is not modified.")
  1772. #define FUNC_NAME s_scm_bit_count_star
  1773. {
  1774. register long i, vlen, count = 0;
  1775. register unsigned long k;
  1776. int fObj = 0;
  1777. SCM_ASRTGO (SCM_NIMP (v), badarg1);
  1778. SCM_ASRTGO (SCM_NIMP (kv), badarg2);
  1779. switch SCM_TYP7 (kv)
  1780. {
  1781. default:
  1782. badarg2:
  1783. SCM_WTA (2,kv);
  1784. case scm_tc7_uvect:
  1785. switch SCM_TYP7
  1786. (v)
  1787. {
  1788. default:
  1789. badarg1:
  1790. SCM_WTA (1,v);
  1791. case scm_tc7_bvect:
  1792. vlen = SCM_LENGTH (v);
  1793. if (SCM_FALSEP (obj))
  1794. for (i = SCM_LENGTH (kv); i;)
  1795. {
  1796. k = SCM_UNPACK (SCM_VELTS (kv)[--i]);
  1797. SCM_ASSERT ((k < vlen), SCM_MAKINUM (k), SCM_OUTOFRANGE, FUNC_NAME);
  1798. if (!SCM_BITVEC_REF(v,k))
  1799. count++;
  1800. }
  1801. else if (SCM_EQ_P (obj, SCM_BOOL_T))
  1802. for (i = SCM_LENGTH (kv); i;)
  1803. {
  1804. k = SCM_UNPACK (SCM_VELTS (kv)[--i]);
  1805. SCM_ASSERT ((k < vlen), SCM_MAKINUM (k), SCM_OUTOFRANGE, FUNC_NAME);
  1806. if (SCM_BITVEC_REF (v,k))
  1807. count++;
  1808. }
  1809. else
  1810. badarg3:SCM_WTA (3,obj);
  1811. }
  1812. break;
  1813. case scm_tc7_bvect:
  1814. SCM_ASRTGO (SCM_TYP7 (v) == scm_tc7_bvect && SCM_LENGTH (v) == SCM_LENGTH (kv), badarg1);
  1815. if (0 == SCM_LENGTH (v))
  1816. return SCM_INUM0;
  1817. SCM_ASRTGO (SCM_BOOLP (obj), badarg3);
  1818. fObj = SCM_EQ_P (obj, SCM_BOOL_T);
  1819. i = (SCM_LENGTH (v) - 1) / SCM_LONG_BIT;
  1820. k = SCM_UNPACK (SCM_VELTS (kv)[i]) & (fObj ? SCM_UNPACK (SCM_VELTS (v)[i]) : ~ SCM_UNPACK (SCM_VELTS (v)[i]));
  1821. k <<= SCM_LONG_BIT - 1 - ((SCM_LENGTH (v) - 1) % SCM_LONG_BIT);
  1822. while (1)
  1823. {
  1824. for (; k; k >>= 4)
  1825. count += cnt_tab[k & 0x0f];
  1826. if (0 == i--)
  1827. return SCM_MAKINUM (count);
  1828. /* urg. repetitive (see above.) */
  1829. k = SCM_UNPACK (SCM_VELTS (kv)[i]) & (fObj ? SCM_UNPACK(SCM_VELTS (v)[i]) : ~SCM_UNPACK (SCM_VELTS (v)[i]));
  1830. }
  1831. }
  1832. return SCM_MAKINUM (count);
  1833. }
  1834. #undef FUNC_NAME
  1835. SCM_DEFINE (scm_bit_invert_x, "bit-invert!", 1, 0, 0,
  1836. (SCM v),
  1837. "Modifies @var{bv} by replacing each element with its negation.")
  1838. #define FUNC_NAME s_scm_bit_invert_x
  1839. {
  1840. register long k;
  1841. SCM_ASRTGO (SCM_NIMP (v), badarg1);
  1842. k = SCM_LENGTH (v);
  1843. switch SCM_TYP7
  1844. (v)
  1845. {
  1846. case scm_tc7_bvect:
  1847. for (k = (k + SCM_LONG_BIT - 1) / SCM_LONG_BIT; k--;)
  1848. SCM_UNPACK (SCM_VELTS (v)[k]) = ~SCM_UNPACK(SCM_VELTS (v)[k]);
  1849. break;
  1850. default:
  1851. badarg1:SCM_WTA (1,v);
  1852. }
  1853. return SCM_UNSPECIFIED;
  1854. }
  1855. #undef FUNC_NAME
  1856. SCM
  1857. scm_istr2bve (char *str, long len)
  1858. {
  1859. SCM v = scm_make_uve (len, SCM_BOOL_T);
  1860. long *data = (long *) SCM_VELTS (v);
  1861. register unsigned long mask;
  1862. register long k;
  1863. register long j;
  1864. for (k = 0; k < (len + SCM_LONG_BIT - 1) / SCM_LONG_BIT; k++)
  1865. {
  1866. data[k] = 0L;
  1867. j = len - k * SCM_LONG_BIT;
  1868. if (j > SCM_LONG_BIT)
  1869. j = SCM_LONG_BIT;
  1870. for (mask = 1L; j--; mask <<= 1)
  1871. switch (*str++)
  1872. {
  1873. case '0':
  1874. break;
  1875. case '1':
  1876. data[k] |= mask;
  1877. break;
  1878. default:
  1879. return SCM_BOOL_F;
  1880. }
  1881. }
  1882. return v;
  1883. }
  1884. static SCM
  1885. ra2l (SCM ra,scm_sizet base,scm_sizet k)
  1886. {
  1887. register SCM res = SCM_EOL;
  1888. register long inc = SCM_ARRAY_DIMS (ra)[k].inc;
  1889. register scm_sizet i;
  1890. if (SCM_ARRAY_DIMS (ra)[k].ubnd < SCM_ARRAY_DIMS (ra)[k].lbnd)
  1891. return SCM_EOL;
  1892. i = base + (1 + SCM_ARRAY_DIMS (ra)[k].ubnd - SCM_ARRAY_DIMS (ra)[k].lbnd) * inc;
  1893. if (k < SCM_ARRAY_NDIM (ra) - 1)
  1894. {
  1895. do
  1896. {
  1897. i -= inc;
  1898. res = scm_cons (ra2l (ra, i, k + 1), res);
  1899. }
  1900. while (i != base);
  1901. }
  1902. else
  1903. do
  1904. {
  1905. i -= inc;
  1906. res = scm_cons (scm_uniform_vector_ref (SCM_ARRAY_V (ra), SCM_MAKINUM (i)), res);
  1907. }
  1908. while (i != base);
  1909. return res;
  1910. }
  1911. SCM_DEFINE (scm_array_to_list, "array->list", 1, 0, 0,
  1912. (SCM v),
  1913. "Returns a list consisting of all the elements, in order, of @var{array}.")
  1914. #define FUNC_NAME s_scm_array_to_list
  1915. {
  1916. SCM res = SCM_EOL;
  1917. register long k;
  1918. SCM_ASRTGO (SCM_NIMP (v), badarg1);
  1919. switch SCM_TYP7
  1920. (v)
  1921. {
  1922. default:
  1923. badarg1:SCM_WTA (1,v);
  1924. case scm_tc7_smob:
  1925. SCM_ASRTGO (SCM_ARRAYP (v), badarg1);
  1926. return ra2l (v, SCM_ARRAY_BASE (v), 0);
  1927. case scm_tc7_vector:
  1928. case scm_tc7_wvect:
  1929. return scm_vector_to_list (v);
  1930. case scm_tc7_string:
  1931. return scm_string_to_list (v);
  1932. case scm_tc7_bvect:
  1933. {
  1934. long *data = (long *) SCM_VELTS (v);
  1935. register unsigned long mask;
  1936. for (k = (SCM_LENGTH (v) - 1) / SCM_LONG_BIT; k > 0; k--)
  1937. for (mask = 1UL << (SCM_LONG_BIT - 1); mask; mask >>= 1)
  1938. res = scm_cons (SCM_BOOL(((long *) data)[k] & mask), res);
  1939. for (mask = 1L << ((SCM_LENGTH (v) % SCM_LONG_BIT) - 1); mask; mask >>= 1)
  1940. res = scm_cons (SCM_BOOL(((long *) data)[k] & mask), res);
  1941. return res;
  1942. }
  1943. case scm_tc7_uvect: {
  1944. long *data = (long *)SCM_VELTS(v);
  1945. for (k = SCM_LENGTH(v) - 1; k >= 0; k--)
  1946. res = scm_cons(scm_ulong2num(data[k]), res);
  1947. return res;
  1948. }
  1949. case scm_tc7_ivect: {
  1950. long *data = (long *)SCM_VELTS(v);
  1951. for (k = SCM_LENGTH(v) - 1; k >= 0; k--)
  1952. res = scm_cons(scm_long2num(data[k]), res);
  1953. return res;
  1954. }
  1955. case scm_tc7_svect: {
  1956. short *data;
  1957. data = (short *)SCM_VELTS(v);
  1958. for (k = SCM_LENGTH(v) - 1; k >= 0; k--)
  1959. res = scm_cons(SCM_MAKINUM (data[k]), res);
  1960. return res;
  1961. }
  1962. #ifdef HAVE_LONG_LONGS
  1963. case scm_tc7_llvect: {
  1964. long_long *data;
  1965. data = (long_long *)SCM_VELTS(v);
  1966. for (k = SCM_LENGTH(v) - 1; k >= 0; k--)
  1967. res = scm_cons(scm_long_long2num(data[k]), res);
  1968. return res;
  1969. }
  1970. #endif
  1971. case scm_tc7_fvect:
  1972. {
  1973. float *data = (float *) SCM_VELTS (v);
  1974. for (k = SCM_LENGTH (v) - 1; k >= 0; k--)
  1975. res = scm_cons (scm_make_real (data[k]), res);
  1976. return res;
  1977. }
  1978. case scm_tc7_dvect:
  1979. {
  1980. double *data = (double *) SCM_VELTS (v);
  1981. for (k = SCM_LENGTH (v) - 1; k >= 0; k--)
  1982. res = scm_cons (scm_make_real (data[k]), res);
  1983. return res;
  1984. }
  1985. case scm_tc7_cvect:
  1986. {
  1987. double (*data)[2] = (double (*)[2]) SCM_VELTS (v);
  1988. for (k = SCM_LENGTH (v) - 1; k >= 0; k--)
  1989. res = scm_cons (scm_make_complex (data[k][0], data[k][1]), res);
  1990. return res;
  1991. }
  1992. }
  1993. }
  1994. #undef FUNC_NAME
  1995. static char s_bad_ralst[] = "Bad scm_array contents list";
  1996. static int l2ra(SCM lst, SCM ra, scm_sizet base, scm_sizet k);
  1997. SCM_DEFINE (scm_list_to_uniform_array, "list->uniform-array", 3, 0, 0,
  1998. (SCM ndim, SCM prot, SCM lst),
  1999. "@deffnx procedure list->uniform-vector prot lst\n"
  2000. "Returns a uniform array of the type indicated by prototype @var{prot}\n"
  2001. "with elements the same as those of @var{lst}. Elements must be of the\n"
  2002. "appropriate type, no coercions are done.")
  2003. #define FUNC_NAME s_scm_list_to_uniform_array
  2004. {
  2005. SCM shp = SCM_EOL;
  2006. SCM row = lst;
  2007. SCM ra;
  2008. scm_sizet k;
  2009. long n;
  2010. SCM_VALIDATE_INUM_COPY (1,ndim,k);
  2011. while (k--)
  2012. {
  2013. n = scm_ilength (row);
  2014. SCM_ASSERT (n >= 0, lst, SCM_ARG3, FUNC_NAME);
  2015. shp = scm_cons (SCM_MAKINUM (n), shp);
  2016. if (SCM_NIMP (row))
  2017. row = SCM_CAR (row);
  2018. }
  2019. ra = scm_dimensions_to_uniform_array (scm_reverse (shp), prot,
  2020. SCM_UNDEFINED);
  2021. if (SCM_NULLP (shp))
  2022. {
  2023. SCM_ASRTGO (1 == scm_ilength (lst), badlst);
  2024. scm_array_set_x (ra, SCM_CAR (lst), SCM_EOL);
  2025. return ra;
  2026. }
  2027. if (!SCM_ARRAYP (ra))
  2028. {
  2029. for (k = 0; k < SCM_LENGTH (ra); k++, lst = SCM_CDR (lst))
  2030. scm_array_set_x (ra, SCM_CAR (lst), SCM_MAKINUM (k));
  2031. return ra;
  2032. }
  2033. if (l2ra (lst, ra, SCM_ARRAY_BASE (ra), 0))
  2034. return ra;
  2035. else
  2036. badlst:scm_wta (lst, s_bad_ralst, FUNC_NAME);
  2037. return SCM_BOOL_F;
  2038. }
  2039. #undef FUNC_NAME
  2040. static int
  2041. l2ra (SCM lst, SCM ra, scm_sizet base, scm_sizet k)
  2042. {
  2043. register long inc = SCM_ARRAY_DIMS (ra)[k].inc;
  2044. register long n = (1 + SCM_ARRAY_DIMS (ra)[k].ubnd - SCM_ARRAY_DIMS (ra)[k].lbnd);
  2045. int ok = 1;
  2046. if (n <= 0)
  2047. return (SCM_NULLP (lst));
  2048. if (k < SCM_ARRAY_NDIM (ra) - 1)
  2049. {
  2050. while (n--)
  2051. {
  2052. if (SCM_IMP (lst) || SCM_NCONSP (lst))
  2053. return 0;
  2054. ok = ok && l2ra (SCM_CAR (lst), ra, base, k + 1);
  2055. base += inc;
  2056. lst = SCM_CDR (lst);
  2057. }
  2058. if (SCM_NNULLP (lst))
  2059. return 0;
  2060. }
  2061. else
  2062. {
  2063. while (n--)
  2064. {
  2065. if (SCM_IMP (lst) || SCM_NCONSP (lst))
  2066. return 0;
  2067. scm_array_set_x (SCM_ARRAY_V (ra), SCM_CAR (lst), SCM_MAKINUM (base));
  2068. base += inc;
  2069. lst = SCM_CDR (lst);
  2070. }
  2071. if (SCM_NNULLP (lst))
  2072. return 0;
  2073. }
  2074. return ok;
  2075. }
  2076. static void
  2077. rapr1 (SCM ra,scm_sizet j,scm_sizet k,SCM port,scm_print_state *pstate)
  2078. {
  2079. long inc = 1;
  2080. long n = SCM_LENGTH (ra);
  2081. int enclosed = 0;
  2082. tail:
  2083. switch SCM_TYP7 (ra)
  2084. {
  2085. case scm_tc7_smob:
  2086. if (enclosed++)
  2087. {
  2088. SCM_ARRAY_BASE (ra) = j;
  2089. if (n-- > 0)
  2090. scm_iprin1 (ra, port, pstate);
  2091. for (j += inc; n-- > 0; j += inc)
  2092. {
  2093. scm_putc (' ', port);
  2094. SCM_ARRAY_BASE (ra) = j;
  2095. scm_iprin1 (ra, port, pstate);
  2096. }
  2097. break;
  2098. }
  2099. if (k + 1 < SCM_ARRAY_NDIM (ra))
  2100. {
  2101. long i;
  2102. inc = SCM_ARRAY_DIMS (ra)[k].inc;
  2103. for (i = SCM_ARRAY_DIMS (ra)[k].lbnd; i < SCM_ARRAY_DIMS (ra)[k].ubnd; i++)
  2104. {
  2105. scm_putc ('(', port);
  2106. rapr1 (ra, j, k + 1, port, pstate);
  2107. scm_puts (") ", port);
  2108. j += inc;
  2109. }
  2110. if (i == SCM_ARRAY_DIMS (ra)[k].ubnd)
  2111. { /* could be zero size. */
  2112. scm_putc ('(', port);
  2113. rapr1 (ra, j, k + 1, port, pstate);
  2114. scm_putc (')', port);
  2115. }
  2116. break;
  2117. }
  2118. if SCM_ARRAY_NDIM
  2119. (ra)
  2120. { /* Could be zero-dimensional */
  2121. inc = SCM_ARRAY_DIMS (ra)[k].inc;
  2122. n = (SCM_ARRAY_DIMS (ra)[k].ubnd - SCM_ARRAY_DIMS (ra)[k].lbnd + 1);
  2123. }
  2124. else
  2125. n = 1;
  2126. ra = SCM_ARRAY_V (ra);
  2127. goto tail;
  2128. default:
  2129. /* scm_tc7_bvect and scm_tc7_llvect only? */
  2130. if (n-- > 0)
  2131. scm_iprin1 (scm_uniform_vector_ref (ra, SCM_MAKINUM (j)), port, pstate);
  2132. for (j += inc; n-- > 0; j += inc)
  2133. {
  2134. scm_putc (' ', port);
  2135. scm_iprin1 (scm_cvref (ra, j, SCM_UNDEFINED), port, pstate);
  2136. }
  2137. break;
  2138. case scm_tc7_string:
  2139. if (n-- > 0)
  2140. scm_iprin1 (SCM_MAKE_CHAR (SCM_UCHARS (ra)[j]), port, pstate);
  2141. if (SCM_WRITINGP (pstate))
  2142. for (j += inc; n-- > 0; j += inc)
  2143. {
  2144. scm_putc (' ', port);
  2145. scm_iprin1 (SCM_MAKE_CHAR (SCM_UCHARS (ra)[j]), port, pstate);
  2146. }
  2147. else
  2148. for (j += inc; n-- > 0; j += inc)
  2149. scm_putc (SCM_CHARS (ra)[j], port);
  2150. break;
  2151. case scm_tc7_byvect:
  2152. if (n-- > 0)
  2153. scm_intprint (((char *) SCM_CELL_WORD_1 (ra))[j], 10, port);
  2154. for (j += inc; n-- > 0; j += inc)
  2155. {
  2156. scm_putc (' ', port);
  2157. scm_intprint (((char *)SCM_CELL_WORD_1 (ra))[j], 10, port);
  2158. }
  2159. break;
  2160. case scm_tc7_uvect:
  2161. {
  2162. char str[11];
  2163. if (n-- > 0)
  2164. {
  2165. /* intprint can't handle >= 2^31. */
  2166. sprintf (str, "%lu", ((unsigned long *) SCM_VELTS (ra))[j]);
  2167. scm_puts (str, port);
  2168. }
  2169. for (j += inc; n-- > 0; j += inc)
  2170. {
  2171. scm_putc (' ', port);
  2172. sprintf (str, "%lu", ((unsigned long *) SCM_VELTS (ra))[j]);
  2173. scm_puts (str, port);
  2174. }
  2175. }
  2176. case scm_tc7_ivect:
  2177. if (n-- > 0)
  2178. scm_intprint (((signed long *) SCM_VELTS (ra))[j], 10, port);
  2179. for (j += inc; n-- > 0; j += inc)
  2180. {
  2181. scm_putc (' ', port);
  2182. scm_intprint (((signed long *) SCM_VELTS (ra))[j], 10, port);
  2183. }
  2184. break;
  2185. case scm_tc7_svect:
  2186. if (n-- > 0)
  2187. scm_intprint (((short *) SCM_CELL_WORD_1 (ra))[j], 10, port);
  2188. for (j += inc; n-- > 0; j += inc)
  2189. {
  2190. scm_putc (' ', port);
  2191. scm_intprint (((short *) SCM_CELL_WORD_1 (ra))[j], 10, port);
  2192. }
  2193. break;
  2194. case scm_tc7_fvect:
  2195. if (n-- > 0)
  2196. {
  2197. SCM z = scm_make_real (1.0);
  2198. SCM_REAL_VALUE (z) = ((float *) SCM_VELTS (ra))[j];
  2199. scm_print_real (z, port, pstate);
  2200. for (j += inc; n-- > 0; j += inc)
  2201. {
  2202. scm_putc (' ', port);
  2203. SCM_REAL_VALUE (z) = ((float *) SCM_VELTS (ra))[j];
  2204. scm_print_real (z, port, pstate);
  2205. }
  2206. }
  2207. break;
  2208. case scm_tc7_dvect:
  2209. if (n-- > 0)
  2210. {
  2211. SCM z = scm_make_real (1.0 / 3.0);
  2212. SCM_REAL_VALUE (z) = ((double *) SCM_VELTS (ra))[j];
  2213. scm_print_real (z, port, pstate);
  2214. for (j += inc; n-- > 0; j += inc)
  2215. {
  2216. scm_putc (' ', port);
  2217. SCM_REAL_VALUE (z) = ((double *) SCM_VELTS (ra))[j];
  2218. scm_print_real (z, port, pstate);
  2219. }
  2220. }
  2221. break;
  2222. case scm_tc7_cvect:
  2223. if (n-- > 0)
  2224. {
  2225. SCM cz = scm_make_complex (0.0, 1.0), z = scm_make_real (1.0 / 3.0);
  2226. SCM_REAL_VALUE (z) =
  2227. SCM_COMPLEX_REAL (cz) = ((double *) SCM_VELTS (ra))[2 * j];
  2228. SCM_COMPLEX_IMAG (cz) = ((double *) SCM_VELTS (ra))[2 * j + 1];
  2229. scm_print_complex ((0.0 == SCM_COMPLEX_IMAG (cz) ? z : cz),
  2230. port, pstate);
  2231. for (j += inc; n-- > 0; j += inc)
  2232. {
  2233. scm_putc (' ', port);
  2234. SCM_REAL_VALUE (z)
  2235. = SCM_COMPLEX_REAL (cz) = ((double *) SCM_VELTS (ra))[2 * j];
  2236. SCM_COMPLEX_IMAG (cz) = ((double *) SCM_VELTS (ra))[2 * j + 1];
  2237. scm_print_complex ((0.0 == SCM_COMPLEX_IMAG (cz) ? z : cz),
  2238. port, pstate);
  2239. }
  2240. }
  2241. break;
  2242. }
  2243. }
  2244. int
  2245. scm_raprin1 (SCM exp, SCM port, scm_print_state *pstate)
  2246. {
  2247. SCM v = exp;
  2248. scm_sizet base = 0;
  2249. scm_putc ('#', port);
  2250. tail:
  2251. switch SCM_TYP7 (v)
  2252. {
  2253. case scm_tc7_smob:
  2254. {
  2255. long ndim = SCM_ARRAY_NDIM (v);
  2256. base = SCM_ARRAY_BASE (v);
  2257. v = SCM_ARRAY_V (v);
  2258. if (SCM_ARRAYP (v))
  2259. {
  2260. scm_puts ("<enclosed-array ", port);
  2261. rapr1 (exp, base, 0, port, pstate);
  2262. scm_putc ('>', port);
  2263. return 1;
  2264. }
  2265. else
  2266. {
  2267. scm_intprint (ndim, 10, port);
  2268. goto tail;
  2269. }
  2270. }
  2271. case scm_tc7_bvect:
  2272. if (SCM_EQ_P (exp, v))
  2273. { /* a uve, not an scm_array */
  2274. register long i, j, w;
  2275. scm_putc ('*', port);
  2276. for (i = 0; i < (SCM_LENGTH (exp)) / SCM_LONG_BIT; i++)
  2277. {
  2278. scm_bits_t w = SCM_UNPACK (SCM_VELTS (exp)[i]);
  2279. for (j = SCM_LONG_BIT; j; j--)
  2280. {
  2281. scm_putc (w & 1 ? '1' : '0', port);
  2282. w >>= 1;
  2283. }
  2284. }
  2285. j = SCM_LENGTH (exp) % SCM_LONG_BIT;
  2286. if (j)
  2287. {
  2288. w = SCM_UNPACK (SCM_VELTS (exp)[SCM_LENGTH (exp) / SCM_LONG_BIT]);
  2289. for (; j; j--)
  2290. {
  2291. scm_putc (w & 1 ? '1' : '0', port);
  2292. w >>= 1;
  2293. }
  2294. }
  2295. return 1;
  2296. }
  2297. else
  2298. scm_putc ('b', port);
  2299. break;
  2300. case scm_tc7_string:
  2301. scm_putc ('a', port);
  2302. break;
  2303. case scm_tc7_byvect:
  2304. scm_putc ('y', port);
  2305. break;
  2306. case scm_tc7_uvect:
  2307. scm_putc ('u', port);
  2308. break;
  2309. case scm_tc7_ivect:
  2310. scm_putc ('e', port);
  2311. break;
  2312. case scm_tc7_svect:
  2313. scm_putc ('h', port);
  2314. break;
  2315. #ifdef HAVE_LONG_LONGS
  2316. case scm_tc7_llvect:
  2317. scm_putc ('l', port);
  2318. break;
  2319. #endif
  2320. case scm_tc7_fvect:
  2321. scm_putc ('s', port);
  2322. break;
  2323. case scm_tc7_dvect:
  2324. scm_putc ('i', port);
  2325. break;
  2326. case scm_tc7_cvect:
  2327. scm_putc ('c', port);
  2328. break;
  2329. }
  2330. scm_putc ('(', port);
  2331. rapr1 (exp, base, 0, port, pstate);
  2332. scm_putc (')', port);
  2333. return 1;
  2334. }
  2335. SCM_DEFINE (scm_array_prototype, "array-prototype", 1, 0, 0,
  2336. (SCM ra),
  2337. "Returns an object that would produce an array of the same type as\n"
  2338. "@var{array}, if used as the @var{prototype} for\n"
  2339. "@code{make-uniform-array}.")
  2340. #define FUNC_NAME s_scm_array_prototype
  2341. {
  2342. int enclosed = 0;
  2343. SCM_ASRTGO (SCM_NIMP (ra), badarg);
  2344. loop:
  2345. switch SCM_TYP7
  2346. (ra)
  2347. {
  2348. default:
  2349. badarg:SCM_WTA (1,ra);
  2350. case scm_tc7_smob:
  2351. SCM_ASRTGO (SCM_ARRAYP (ra), badarg);
  2352. if (enclosed++)
  2353. return SCM_UNSPECIFIED;
  2354. ra = SCM_ARRAY_V (ra);
  2355. goto loop;
  2356. case scm_tc7_vector:
  2357. case scm_tc7_wvect:
  2358. return SCM_EOL;
  2359. case scm_tc7_bvect:
  2360. return SCM_BOOL_T;
  2361. case scm_tc7_string:
  2362. return SCM_MAKE_CHAR ('a');
  2363. case scm_tc7_byvect:
  2364. return SCM_MAKE_CHAR ('\0');
  2365. case scm_tc7_uvect:
  2366. return SCM_MAKINUM (1L);
  2367. case scm_tc7_ivect:
  2368. return SCM_MAKINUM (-1L);
  2369. case scm_tc7_svect:
  2370. return SCM_CDR (scm_intern ("s", 1));
  2371. #ifdef HAVE_LONG_LONGS
  2372. case scm_tc7_llvect:
  2373. return SCM_CDR (scm_intern ("l", 1));
  2374. #endif
  2375. case scm_tc7_fvect:
  2376. return scm_make_real (1.0);
  2377. case scm_tc7_dvect:
  2378. return scm_make_real (1.0 / 3.0);
  2379. case scm_tc7_cvect:
  2380. return scm_make_complex (0.0, 1.0);
  2381. }
  2382. }
  2383. #undef FUNC_NAME
  2384. static SCM
  2385. markra (SCM ptr)
  2386. {
  2387. return SCM_ARRAY_V (ptr);
  2388. }
  2389. static scm_sizet
  2390. freera (SCM ptr)
  2391. {
  2392. scm_must_free (SCM_CHARS (ptr));
  2393. return sizeof (scm_array) + SCM_ARRAY_NDIM (ptr) * sizeof (scm_array_dim);
  2394. }
  2395. void
  2396. scm_init_unif ()
  2397. {
  2398. scm_tc16_array = scm_make_smob_type_mfpe ("array", 0,
  2399. markra,
  2400. freera,
  2401. scm_raprin1,
  2402. scm_array_equal_p);
  2403. scm_add_feature ("array");
  2404. #include "libguile/unif.x"
  2405. }
  2406. /*
  2407. Local Variables:
  2408. c-file-style: "gnu"
  2409. End:
  2410. */