pspp-dev
[Top][All Lists]
Advanced

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

Re: casereader numbering


From: Ben Pfaff
Subject: Re: casereader numbering
Date: Tue, 22 Jul 2008 22:13:31 -0700
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux)

John Darrington <address@hidden> writes:

> I've started  rewriting EXAMINE to make it less crappy.
>
> One thing that would make it easier would be a function of the form
>
>  struct casereader * append_case_numbers (struct casereader *cr);
>
> which returns a new casereader which is identical to CR except that it
> has one extra column which contains the ordinal number of each case.

Here is a patch to try.  I haven't had a chance to test it, but I
think that it should work.  If it works for you I'll push it to
the repository.

commit 003c33f013e762fc162029f37c3f233fd29645d8
Author: Ben Pfaff <address@hidden>
Date:   Tue Jul 22 22:11:46 2008 -0700

    New function for adding a numbering column to a casereader.

diff --git a/src/data/casereader-translator.c b/src/data/casereader-translator.c
index b857b5b..229dac2 100644
--- a/src/data/casereader-translator.c
+++ b/src/data/casereader-translator.c
@@ -110,3 +110,58 @@ static const struct casereader_class 
casereader_translator_class =
     NULL,
     NULL,
   };
+
+struct casereader_arithmetic_sequence 
+  {
+    int value_ofs;
+    double first;
+    double increment;
+    casenumber n;
+  };
+
+static void cas_translate (struct ccase *input, struct ccase *output,
+                           void *aux);
+static bool cas_destroy (void *aux);
+
+/* Creates and returns a new casereader whose cases are produced
+   by reading from SUBREADER and appending an additional value,
+   which takes the value FIRST in the first case, FIRST +
+   INCREMENT in the second case, FIRST + INCREMENT * 2 in the
+   third case, and so on.
+
+   After this function is called, SUBREADER must not ever again
+   be referenced directly.  It will be destroyed automatically
+   when the translating casereader is destroyed. */
+struct casereader *
+casereader_create_arithmetic_sequence (struct casereader *subreader,
+                                       double first, double increment)
+{
+  /* This could be implemented with a great deal more efficiency
+     and generality.  However, this implementation is easy. */
+  struct casereader_arithmetic_sequence *cas = xmalloc (sizeof *cas);
+  cas->value_ofs = casereader_get_value_cnt (subreader);
+  cas->first = first;
+  cas->increment = increment;
+  cas->n = 0;
+  return casereader_create_translator (subreader, cas->value_ofs + 1,
+                                       cas_translate, cas_destroy, cas);
+}
+
+static void
+cas_translate (struct ccase *input, struct ccase *output, void *cas_)
+{
+  struct casereader_arithmetic_sequence *cas = cas_;
+  case_nullify (output);
+  case_move (output, input);
+  case_resize (output, cas->value_ofs + 1);
+  case_data_rw_idx (output, cas->value_ofs)->f
+    = cas->first + cas->increment * cas->n++;
+}
+
+static bool
+cas_destroy (void *cas_) 
+{
+  struct casereader_arithmetic_sequence *cas = cas_;
+  free (cas);
+  return true;
+}
diff --git a/src/data/casereader.h b/src/data/casereader.h
index 6d719c6..ba65cb1 100644
--- a/src/data/casereader.h
+++ b/src/data/casereader.h
@@ -112,4 +112,8 @@ casereader_create_translator (struct casereader *, size_t 
output_value_cnt,
                               bool (*destroy) (void *aux),
                               void *aux);
 
+struct casereader *
+casereader_create_arithmetic_sequence (struct casereader *,
+                                       double first, double increment);
+
 #endif /* data/casereader.h */

-- 
"Note that nobody reads every post in linux-kernel.   In fact, nobody who
 expects to have time left over to  actually do any real kernel work will
 read even half.  Except Alan Cox, but he's actually not human, but about
 a thousand gnomes working in under-ground caves in Swansea." --Linus




reply via email to

[Prev in Thread] Current Thread [Next in Thread]