home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / apl / 1250 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  4.8 KB

  1. Path: sparky!uunet!noc.near.net!hri.com!spool.mu.edu!think.com!rpi!utcsri!geac!jtsv16!itcyyz!yrloc!intern
  2. From: pow@ipsa@ipsaint.ipsa.reuter.COM (Remote Addressee:)
  3. Newsgroups: comp.lang.apl
  4. Subject: Keyed Files for J
  5. Message-ID: <1992Dec20.001540.14080@yrloc.ipsa.reuter.COM>
  6. Date: 20 Dec 92 17:46:58 GMT
  7. Sender: intern@yrloc.ipsa.reuter.COM (Intern via QUADRAM)
  8. Reply-To: pow@ipsa@ipsaint.ipsa.reuter.COM (Remote Addressee:)
  9. Organization: Reuters Information Services (Canada)
  10. Lines: 141
  11.  
  12.  
  13. -----------Message forwarded from IPSA Mailbox-------------
  14.  
  15.  
  16. no. 6843765 filed 23.39.19  sat 19 dec 1992
  17. from pow@ipsa
  18. to   uclapl
  19. subj Keyed Files for J
  20.  
  21. @transferred from ipsa  no. 5455051 filed 23.39.07  sat 19 dec 1992
  22.  
  23. Below is a script for some J verbs to store values in a file by key. I'd
  24. be delighted to receive improvements and suggestions.
  25. /Mike Powell
  26. ---------------------
  27. NB. KEYED FILES.                  Mike Powell, Dec 1992.
  28.  
  29. NB. J version 6.
  30. NB. Tested on NeXT.
  31. NB. All values are stored in 5!:5 format. Values retrieved from the file
  32. NB. are recreated with the Do verb, ". .
  33. NB. There's no checking done. Ill-formed arguments, references to
  34. NB. non-existent files or keys etc, all generate errors.
  35.  
  36. NB. Create <'filename'
  37. NB.   Creates a file and initializes its directory. Initially,
  38. NB.   0-511     contain a two row integer matrix describing where to find
  39. NB.             the list of keys and table of locations. Columns are
  40. NB.             offset, size and slot capacity.
  41. NB.   512-1535  The list of keys.
  42. NB.             This is a list of boxed nouns. Initially empty.
  43. NB.   1536-2559 The table of locations.
  44. NB.             A two row integer matrix describing where to find the
  45. NB.    objects associated with the elements of the list of keys.
  46. NB.   Later, should either the list of keys or table of locations exceed
  47. NB.   the slot capacity, they will be moved to a different part of the
  48. NB.   file and given a greater capacity.
  49. NB.   Returns the size of the file.
  50.  
  51. NB. key Get <'filename'
  52. NB.   Returns the value associated with key. Produces an error if the key
  53. NB.   does not exist.
  54.  
  55. NB. (key;value)Put<'filename'
  56. NB.   Returns three integers - the offset,size and reserved capacity used
  57. NB.   for this value.
  58.  
  59. NB.   If the key is a new one, its value is padded and appended to the end.
  60. NB.   Padding is about 25%, rounded up to a 512B boundary.
  61. NB.   If the key already exists and the value fits in the existing slot,
  62. NB.   the new value replaces the old.
  63. NB.   If the key already exists and the size of the new value exceeds the
  64. NB.   space reserved, the value is padded and appended to the end.
  65. NB.   Makes no attempt to reuse wasted space within the file.
  66.  
  67. NB.   A key is a noun. For example, the following are all usable as distinct
  68. NB.   keys:
  69. NB.     'abcd
  70. NB.  <'abcd'
  71. NB.  1 4$'abcd'
  72. NB.  i.4 10
  73. NB.  6.7j8
  74.  
  75. NB. Dir<'filename'
  76. NB.   Returns the list of keys.
  77. NB.   For example, to test if a key is already present;
  78. NB.   (<'abc')e. Dir<'filename'
  79.  
  80. NB. (<'sourcefile')Copy <'destination file'
  81. NB.   Copies the contents of one file to another. Eliminates wasted space.
  82. NB.   Returns the before and after file sizes.
  83.  
  84. rep =. 5!:5
  85. wr =. 1!:2
  86. ap =. 1!:3
  87. sz =. 1!:4
  88. iw =. 1!:12
  89. ev=. ". 1!:11
  90.  
  91. m=.  < 'a=.rep<''a'' [ a=.i.0'
  92. m=.m,< 'b=.rep<''b'' [ b=.0 3$0'
  93. m=.m,< 'c=.rep<''c'' [ c=.2 3$512,($a),1024,1536,($b),1024'
  94. m=.m,< 'c=.(512{.c),(1024{.a),1024{.b'
  95. m=.m,< '$c [ c wr y.'
  96. Create =. m : ''
  97.  
  98. m=.  < 'a=.ev y.,<0 512'
  99. m=.m,< 'b=.ev y.,<0 1{0{a'
  100. m=.m,< 'c=.ev y.,<0 1{1{a'
  101. m=.m,< 'ev y.,<,0 1{(b i. <x.){c'
  102. Get =. '' : m
  103.  
  104. m=.  < 'a=.0{y.'
  105. m=.m,< 'y.=.>1{y.'
  106. m=.m,< 'b=.(sz a),$y.'
  107. m=.m,< 'y.=.(512*>.($y.)%400){.y.'
  108. m=.m,< 'a=.y. ap a'
  109. m=.m,< 'b,$y.'
  110.  
  111. n=.  < 'a=.(2{x.)>:$>1{y.'
  112. n=.n,< '$.=.((-.a)#2),(4#a)#3 4 5 6'
  113. n=.n,< 'write y.'
  114. n=.n,< 'a=.0{y.'
  115. n=.n,< 'y.=.>1{y.'
  116. n=.n,< 'a=. y. iw a,<(0{x.),$y.'
  117. n=.n,< '(0{x.),($y.),2{x.'
  118. write =. m : n
  119.  
  120. m=.  < 'a=.0{x.'
  121. m=.m,< 'x.=.rep<''x.'' [ x.=.>1{x.'
  122. m=.m,< 'b=.ev y.,<0 512'
  123. m=.m,< 'c=.ev y.,<0 1{0{b'
  124. m=.m,< 'd=.ev y.,<0 1{1{b'
  125. m=.m,< 'e=.c i. a'
  126. m=.m,< '$.=.((e>:#c)#7 8 9 10 11),(e<#c)#12 13 14 15'
  127.  
  128. m=.m,< 'f=.write y.,<x.'
  129. m=.m,< 'a=.(,0{b) write y.,<rep <''c'' [ c=.c,a'
  130. m=.m,< 'b=.(,1{b) write y.,<rep <''d'' [ d=.d,f'
  131. m=.m,< 'a=.512{.rep<''a'' [ a=.2 3$a,b'
  132. m=.m,< 'f [ a iw y.,<0 512'
  133.  
  134. m=.m,< 'f=.(,e{d) write y.,<x.'
  135. m=.m,< 'a=.(,1{b) write y.,<rep<''a'' [ a=.(e{.d),f,(e+1)}.d'
  136. m=.m,< 'a=.512{.rep<''a'' [ a=.2 3$(0{b),a'
  137. m=.m,< 'f [ a iw y.,<0 512'
  138. Put=. '' : m
  139.  
  140. Dir =. 'ev y.,<0 1{0{ev y.,<0 512' : ''
  141.  
  142. m=.  < 'a=.Dir x.'
  143. m=.m,< '$.=.(($a)$2),3 [ b=.0'
  144. m=.m,< 'b=.b+1 [ ((b{a),<(>b{a)Get x.)Put y.'
  145. m=.m,< '(sz x.),sz y.'
  146. Copy =. '' : m
  147.  
  148. -----------------------------------------------------------
  149. This posting is forwarded from an internal Reuters mailbox.
  150. No statement or opinion contained herein should be taken as
  151. being Reuters policy, or even as being approved by Reuters,
  152. in any way.
  153.