Bill Allombert on Tue, 27 Sep 2005 15:17:42 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

gp_readvec_file


Hello PARI-dev,

Here a patch to read files containing several PARI objects and put
them in a vector. For example if your file contain
1
[1,2,3]
I
2.333
you will get [1,[1,2,3],I,2.333].

(To use: install("gp_readvec_file","s"); )

I wrote it because I though this was faster than reading the same file
with [] and , added. Apparently it is only 38% faster, but it is 
certainly more convenient to use.

When you read large files, it is a good idea to turn "simplify" off
(\y). "simplify" can waste quite a lot of time/memory.

Cheers,
Bill.

Index: pari/src/language/es.c
===================================================================
--- pari.orig/src/language/es.c	2005-09-24 15:32:00.000000000 +0200
+++ pari/src/language/es.c	2005-09-24 16:54:46.000000000 +0200
@@ -247,6 +247,42 @@
   popinfile(); return x;
 }
 
+GEN
+gp_readvec_stream(FILE *fi)
+{
+  pari_sp ltop=avma;
+  Buffer *b = new_buffer();
+  long i=1, n=16;
+  GEN z=cgetg(n+1,t_VEC);
+  for(;;)
+  {
+    if (!gp_read_stream_buf(fi, b)) break;
+    if (!*(b->buf)) continue;
+    if (i>n)
+    {
+      if (DEBUGLEVEL)
+        fprintferr("gp_readvec_stream: reaching %ld entries\n",n);
+      n<<=1;
+      z=vec_lengthen(z,n);
+    }
+    gel(z,i++) = readseq(b->buf);
+  }
+  if (DEBUGLEVEL)
+    fprintferr("gp_readvec_stream: found %ld entries\n",i-1);
+  delete_buffer(b);
+  setlg(z,i);
+  return gerepilecopy(ltop,z);
+}
+
+GEN
+gp_readvec_file(char *s)
+{
+  GEN x;
+  switchin(s);
+  x=gp_readvec_stream(infile);
+  popinfile(); return x;
+}
+
 /* Read from file (up to '\n' or EOF) and copy at s0 (points in b->buf) */
 char *
 file_input(char **s0, int junk, input_method *IM, filtre_t *F)