pspp-cvs
[Top][All Lists]
Advanced

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

[Pspp-cvs] pspp/src/data ChangeLog automake.mk case-orderi...


From: Ben Pfaff
Subject: [Pspp-cvs] pspp/src/data ChangeLog automake.mk case-orderi...
Date: Thu, 07 Jun 2007 05:38:56 +0000

CVSROOT:        /cvsroot/pspp
Module name:    pspp
Changes by:     Ben Pfaff <blp> 07/06/07 05:38:56

Modified files:
        src/data       : ChangeLog automake.mk 
Added files:
        src/data       : case-ordering.c case-ordering.h 

Log message:
        Add interface to lexicographical ordering of cases.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/pspp/src/data/ChangeLog?cvsroot=pspp&r1=1.125&r2=1.126
http://cvs.savannah.gnu.org/viewcvs/pspp/src/data/automake.mk?cvsroot=pspp&r1=1.21&r2=1.22
http://cvs.savannah.gnu.org/viewcvs/pspp/src/data/case-ordering.c?cvsroot=pspp&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/pspp/src/data/case-ordering.h?cvsroot=pspp&rev=1.1

Patches:
Index: ChangeLog
===================================================================
RCS file: /cvsroot/pspp/pspp/src/data/ChangeLog,v
retrieving revision 1.125
retrieving revision 1.126
diff -u -b -r1.125 -r1.126
--- ChangeLog   7 Jun 2007 05:36:24 -0000       1.125
+++ ChangeLog   7 Jun 2007 05:38:56 -0000       1.126
@@ -1,5 +1,15 @@
 2007-06-06  Ben Pfaff  <address@hidden>
 
+       Add interface to lexicographical ordering of cases.
+
+       * automake.mk: Add new files.
+
+       * case-ordering.c: New file.
+
+       * case-ordering.h: New file.
+
+2007-06-06  Ben Pfaff  <address@hidden>
+
        Add casereaders and casewriters, the basis of the new data processing
        implementation.  A casereader is a uniform interface to reading cases
        from a data source; a casewriter is a uniform interface to writing

Index: automake.mk
===================================================================
RCS file: /cvsroot/pspp/pspp/src/data/automake.mk,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -b -r1.21 -r1.22
--- automake.mk 7 Jun 2007 05:36:24 -0000       1.21
+++ automake.mk 7 Jun 2007 05:38:56 -0000       1.22
@@ -8,6 +8,8 @@
        src/data/any-writer.h \
        src/data/calendar.c \
        src/data/calendar.h \
+       src/data/case-ordering.c \
+       src/data/case-ordering.h \
        src/data/case-sink.c \
        src/data/case-sink.h \
        src/data/case-source.c \

Index: case-ordering.c
===================================================================
RCS file: case-ordering.c
diff -N case-ordering.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ case-ordering.c     7 Jun 2007 05:38:56 -0000       1.1
@@ -0,0 +1,168 @@
+/* PSPP - computes sample statistics.
+   Copyright (C) 2007 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA. */
+
+#include <config.h>
+
+#include <data/case-ordering.h>
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <data/dictionary.h>
+#include <data/variable.h>
+
+#include "xalloc.h"
+
+/* One key used for sorting. */
+struct sort_key
+  {
+    struct variable *var;       /* Variable. */
+    enum sort_direction dir;    /* Sort direction. */
+  };
+
+/* A set of criteria for ordering cases. */
+struct case_ordering 
+  {
+    size_t value_cnt;           /* Number of `union value's per case. */
+
+    /* Sort keys. */
+    struct sort_key *keys;
+    size_t key_cnt;
+  };
+
+struct case_ordering *
+case_ordering_create (const struct dictionary *dict) 
+{
+  struct case_ordering *co = xmalloc (sizeof *co);
+  co->value_cnt = dict_get_next_value_idx (dict);
+  co->keys = NULL;
+  co->key_cnt = 0;
+  return co;
+}
+
+struct case_ordering *
+case_ordering_clone (const struct case_ordering *orig) 
+{
+  struct case_ordering *co = xmalloc (sizeof *co);
+  co->value_cnt = orig->value_cnt;
+  co->keys = xmemdup (orig->keys, orig->key_cnt * sizeof *orig->keys);
+  co->key_cnt = orig->key_cnt;
+  return co;
+}
+
+void
+case_ordering_destroy (struct case_ordering *co) 
+{
+  if (co != NULL) 
+    {
+      free (co->keys);
+      free (co);
+    }
+}
+
+size_t
+case_ordering_get_value_cnt (const struct case_ordering *co) 
+{
+  return co->value_cnt;
+}
+
+int
+case_ordering_compare_cases (const struct ccase *a, const struct ccase *b,
+                             const struct case_ordering *co) 
+{
+  size_t i;
+  
+  for (i = 0; i < co->key_cnt; i++) 
+    {
+      const struct sort_key *key = &co->keys[i];
+      int width = var_get_width (key->var);
+      int cmp;
+      
+      if (width == 0) 
+        {
+          double af = case_num (a, key->var);
+          double bf = case_num (b, key->var);
+          if (af == bf)
+            continue;
+          cmp = af > bf ? 1 : -1;
+        }
+      else 
+        {
+          const char *as = case_str (a, key->var);
+          const char *bs = case_str (b, key->var);
+          cmp = memcmp (as, bs, width);
+          if (cmp == 0)
+            continue;
+        }
+
+      return key->dir == SRT_ASCEND ? cmp : -cmp;
+    }
+  return 0;
+}
+
+bool
+case_ordering_add_var (struct case_ordering *co,
+                       struct variable *var, enum sort_direction dir) 
+{
+  struct sort_key *key;
+  size_t i;
+
+  for (i = 0; i < co->key_cnt; i++)
+    if (var_get_case_index (co->keys[i].var) == var_get_case_index (var))
+      return false;
+
+  co->keys = xnrealloc (co->keys, co->key_cnt + 1, sizeof *co->keys);
+  key = &co->keys[co->key_cnt++];
+  key->var = var;
+  key->dir = dir;
+  return true;
+}
+
+size_t
+case_ordering_get_var_cnt (const struct case_ordering *co) 
+{
+  return co->key_cnt;
+}
+
+struct variable *
+case_ordering_get_var (const struct case_ordering *co, size_t idx) 
+{
+  assert (idx < co->key_cnt);
+  return co->keys[idx].var;
+}
+
+enum sort_direction
+case_ordering_get_direction (const struct case_ordering *co, size_t idx) 
+{
+  assert (idx < co->key_cnt);
+  return co->keys[idx].dir;
+}
+
+void
+case_ordering_get_vars (const struct case_ordering *co,
+                        struct variable ***vars, size_t *var_cnt) 
+{
+  size_t i;
+  
+  *var_cnt = co->key_cnt;
+  *vars = xnmalloc (*var_cnt, sizeof **vars);
+  for (i = 0; i < co->key_cnt; i++)
+    (*vars)[i] = co->keys[i].var;
+}
+

Index: case-ordering.h
===================================================================
RCS file: case-ordering.h
diff -N case-ordering.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ case-ordering.h     7 Jun 2007 05:38:56 -0000       1.1
@@ -0,0 +1,51 @@
+/* PSPP - computes sample statistics.
+   Copyright (C) 2007 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA. */
+
+#ifndef DATA_CASE_ORDERING_H
+#define DATA_CASE_ORDERING_H 1
+
+#include <stddef.h>
+#include <data/case.h>
+
+struct dictionary;
+
+/* Sort direction. */
+enum sort_direction
+  {
+    SRT_ASCEND,                        /* A, B, C, ..., X, Y, Z. */
+    SRT_DESCEND                        /* Z, Y, X, ..., C, B, A. */
+  };
+
+struct case_ordering *case_ordering_create (const struct dictionary *);
+struct case_ordering *case_ordering_clone (const struct case_ordering *);
+void case_ordering_destroy (struct case_ordering *);
+
+size_t case_ordering_get_value_cnt (const struct case_ordering *);
+int case_ordering_compare_cases (const struct ccase *, const struct ccase *,
+                                 const struct case_ordering *);
+
+bool case_ordering_add_var (struct case_ordering *,
+                            struct variable *, enum sort_direction);
+size_t case_ordering_get_var_cnt (const struct case_ordering *);
+struct variable *case_ordering_get_var (const struct case_ordering *, size_t);
+enum sort_direction case_ordering_get_direction (const struct case_ordering *,
+                                                 size_t);
+void case_ordering_get_vars (const struct case_ordering *,
+                             struct variable ***, size_t *);
+
+#endif /* data/case-ordering.h */




reply via email to

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