Make `SCM_LONG_BIT' usable in preprocessor conditionals.
[bpt/guile.git] / libguile / array-map.c
index 747747a..d4da152 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1996,1998,2000,2001,2004,2005, 2006, 2008, 2009 Free Software Foundation, Inc.
+/* Copyright (C) 1996,1998,2000,2001,2004,2005, 2006, 2008, 2009, 2010, 2012 Free Software Foundation, Inc.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
@@ -168,13 +168,16 @@ scm_ra_matchp (SCM ra0, SCM ras)
      SCM lra;           list of source arrays.
      const char *what;  caller, for error reporting. */
 int 
-scm_ramapc (int (*cproc)(), SCM data, SCM ra0, SCM lra, const char *what)
+scm_ramapc (void *cproc_ptr, SCM data, SCM ra0, SCM lra, const char *what)
 {
   SCM z;
   SCM vra0, ra1, vra1;
   SCM lvra, *plvra;
   long *vinds;
   int k, kmax;
+  int (*cproc) ();
+
+  cproc = cproc_ptr;
   switch (scm_ra_matchp (ra0, lra))
     {
     default:
@@ -317,8 +320,8 @@ scm_ramapc (int (*cproc)(), SCM data, SCM ra0, SCM lra, const char *what)
 
 SCM_DEFINE (scm_array_fill_x, "array-fill!", 2, 0, 0,
            (SCM ra, SCM fill),
-           "Store @var{fill} in every element of @var{array}.  The value returned\n"
-           "is unspecified.")
+           "Store @var{fill} in every element of array @var{ra}.  The value\n"
+           "returned is unspecified.")
 #define FUNC_NAME s_scm_array_fill_x
 {
   scm_ramapc (scm_array_fill_int, fill, ra, SCM_EOL, FUNC_NAME);
@@ -371,9 +374,9 @@ SCM_REGISTER_PROC(s_array_copy_in_order_x, "array-copy-in-order!", 2, 0, 0, scm_
 SCM_DEFINE (scm_array_copy_x, "array-copy!", 2, 0, 0,
            (SCM src, SCM dst),
            "@deffnx {Scheme Procedure} array-copy-in-order! src dst\n"
-           "Copy every element from vector or array @var{source} to the\n"
-           "corresponding element of @var{destination}.  @var{destination} must have\n"
-           "the same rank as @var{source}, and be at least as large in each\n"
+           "Copy every element from vector or array @var{src} to the\n"
+           "corresponding element of @var{dst}.  @var{dst} must have the\n"
+           "same rank as @var{src}, and be at least as large in each\n"
            "dimension.  The order is unspecified.")
 #define FUNC_NAME s_scm_array_copy_x
 {
@@ -645,11 +648,7 @@ ramap (SCM ra0, SCM proc, SCM ras)
       unsigned long k, i1 = SCM_I_ARRAY_BASE (ra1);
       long inc1 = SCM_I_ARRAY_DIMS (ra1)->inc;
       ra1 = SCM_I_ARRAY_V (ra1);
-      ras = SCM_CDR (ras);
-      if (scm_is_null(ras))
-       ras = scm_nullvect;
-      else
-       ras = scm_vector (ras);
+      ras = scm_vector (SCM_CDR (ras));
       
       for (; i <= n; i++, i1 += inc1)
        {
@@ -671,12 +670,13 @@ SCM_SYMBOL (sym_b, "b");
 SCM_DEFINE (scm_array_map_x, "array-map!", 2, 0, 1,
            (SCM ra0, SCM proc, SCM lra),
            "@deffnx {Scheme Procedure} array-map-in-order! ra0 proc . lra\n"
-           "@var{array1}, @dots{} must have the same number of dimensions as\n"
-           "@var{array0} and have a range for each index which includes the range\n"
-           "for the corresponding index in @var{array0}.  @var{proc} is applied to\n"
-           "each tuple of elements of @var{array1} @dots{} and the result is stored\n"
-           "as the corresponding element in @var{array0}.  The value returned is\n"
-           "unspecified.  The order of application is unspecified.")
+           "@var{array1}, @dots{} must have the same number of dimensions\n"
+           "as @var{ra0} and have a range for each index which includes the\n"
+           "range for the corresponding index in @var{ra0}.  @var{proc} is\n"
+           "applied to each tuple of elements of @var{array1}, @dots{} and\n"
+           "the result is stored as the corresponding element in @var{ra0}.\n"
+           "The value returned is unspecified.  The order of application is\n"
+           "unspecified.")
 #define FUNC_NAME s_scm_array_map_x
 {
   SCM_VALIDATE_PROC (2, proc);
@@ -706,11 +706,8 @@ rafe (SCM ra0, SCM proc, SCM ras)
       unsigned long k, i1 = SCM_I_ARRAY_BASE (ra1);
       long inc1 = SCM_I_ARRAY_DIMS (ra1)->inc;
       ra1 = SCM_I_ARRAY_V (ra1);
-      ras = SCM_CDR (ras);
-      if (scm_is_null(ras))
-       ras = scm_nullvect;
-      else
-       ras = scm_vector (ras);
+      ras = scm_vector (SCM_CDR (ras));
+
       for (; i <= n; i++, i0 += inc0, i1 += inc1)
        {
          args = SCM_EOL;
@@ -726,7 +723,7 @@ rafe (SCM ra0, SCM proc, SCM ras)
 
 SCM_DEFINE (scm_array_for_each, "array-for-each", 2, 0, 1,
            (SCM proc, SCM ra0, SCM lra),
-           "Apply @var{proc} to each tuple of elements of @var{array0} @dots{}\n"
+           "Apply @var{proc} to each tuple of elements of @var{ra0} @dots{}\n"
            "in row-major order.  The value returned is unspecified.")
 #define FUNC_NAME s_scm_array_for_each
 {
@@ -739,7 +736,7 @@ SCM_DEFINE (scm_array_for_each, "array-for-each", 2, 0, 1,
 
 SCM_DEFINE (scm_array_index_map_x, "array-index-map!", 2, 0, 0,
            (SCM ra, SCM proc),
-           "Apply @var{proc} to the indices of each element of @var{array} in\n"
+           "Apply @var{proc} to the indices of each element of @var{ra} in\n"
            "turn, storing the result in the corresponding element.  The value\n"
            "returned and the order of application are unspecified.\n\n"
            "One can implement @var{array-indexes} as\n"
@@ -819,123 +816,63 @@ SCM_DEFINE (scm_array_index_map_x, "array-index-map!", 2, 0, 0,
 
 
 static int
-raeql_1 (SCM ra0, SCM as_equal, SCM ra1)
+array_compare (scm_t_array_handle *hx, scm_t_array_handle *hy,
+               size_t dim, unsigned long posx, unsigned long posy)
 {
-  unsigned long i0 = 0, i1 = 0;
-  long inc0 = 1, inc1 = 1;
-  unsigned long n;
-  ra1 = SCM_CAR (ra1);
-  if (SCM_I_ARRAYP(ra0))
-    {
-      n = SCM_I_ARRAY_DIMS (ra0)->ubnd - SCM_I_ARRAY_DIMS (ra0)->lbnd + 1;
-      i0 = SCM_I_ARRAY_BASE (ra0);
-      inc0 = SCM_I_ARRAY_DIMS (ra0)->inc;
-      ra0 = SCM_I_ARRAY_V (ra0);
-    }
+  if (dim == scm_array_handle_rank (hx))
+    return scm_is_true (scm_equal_p (scm_array_handle_ref (hx, posx),
+                                     scm_array_handle_ref (hy, posy)));
   else
-    n = scm_c_generalized_vector_length (ra0);
-
-  if (SCM_I_ARRAYP (ra1))
     {
-      i1 = SCM_I_ARRAY_BASE (ra1);
-      inc1 = SCM_I_ARRAY_DIMS (ra1)->inc;
-      ra1 = SCM_I_ARRAY_V (ra1);
-    }
+      long incx, incy;
+      size_t i;
 
-  if (scm_is_generalized_vector (ra0))
-    {
-      for (; n--; i0 += inc0, i1 += inc1)
-       {
-         if (scm_is_false (as_equal))
-           {
-             if (scm_is_false (scm_array_equal_p (GVREF (ra0, i0), GVREF (ra1, i1))))
-               return 0;
-           }
-         else if (scm_is_false (scm_equal_p (GVREF (ra0, i0), GVREF (ra1, i1))))
-           return 0;
-       }
+      if (hx->dims[dim].lbnd != hy->dims[dim].lbnd
+          || hx->dims[dim].ubnd != hy->dims[dim].ubnd)
+        return 0;
+
+      i = hx->dims[dim].ubnd - hx->dims[dim].lbnd + 1;
+      
+      incx = hx->dims[dim].inc;
+      incy = hy->dims[dim].inc;
+      posx += (i - 1) * incx;
+      posy += (i - 1) * incy;
+
+      for (; i > 0; i--, posx -= incx, posy -= incy)
+        if (!array_compare (hx, hy, dim + 1, posx, posy))
+          return 0;
       return 1;
     }
-  else
-    return 0;
 }
 
-
-
-static int
-raeql (SCM ra0, SCM as_equal, SCM ra1)
+SCM
+scm_array_equal_p (SCM x, SCM y)
 {
-  SCM v0 = ra0, v1 = ra1;
-  scm_t_array_dim dim0, dim1;
-  scm_t_array_dim *s0 = &dim0, *s1 = &dim1;
-  unsigned long bas0 = 0, bas1 = 0;
-  int k, unroll = 1, vlen = 1, ndim = 1;
-  if (SCM_I_ARRAYP (ra0))
-    {
-      ndim = SCM_I_ARRAY_NDIM (ra0);
-      s0 = SCM_I_ARRAY_DIMS (ra0);
-      bas0 = SCM_I_ARRAY_BASE (ra0);
-      v0 = SCM_I_ARRAY_V (ra0);
-    }
-  else
-    {
-      s0->inc = 1;
-      s0->lbnd = 0;
-      s0->ubnd = scm_c_generalized_vector_length (v0) - 1;
-      unroll = 0;
-    }
-  if (SCM_I_ARRAYP (ra1))
-    {
-      if (ndim != SCM_I_ARRAY_NDIM (ra1))
-       return 0;
-      s1 = SCM_I_ARRAY_DIMS (ra1);
-      bas1 = SCM_I_ARRAY_BASE (ra1);
-      v1 = SCM_I_ARRAY_V (ra1);
-    }
-  else
-    {
-      /*
-       Huh ? Schizophrenic return type. --hwn
-      */
-      if (1 != ndim)
-       return 0;
-      s1->inc = 1;
-      s1->lbnd = 0;
-      s1->ubnd = scm_c_generalized_vector_length (v1) - 1;
-      unroll = 0;
-    }
-  if (SCM_TYP7 (v0) != SCM_TYP7 (v1))
-    return 0;
-  for (k = ndim; k--;)
-    {
-      if (s0[k].lbnd != s1[k].lbnd || s0[k].ubnd != s1[k].ubnd)
-       return 0;
-      if (unroll)
-       {
-         unroll = (s0[k].inc == s1[k].inc);
-         vlen *= s0[k].ubnd - s1[k].lbnd + 1;
-       }
-    }
-  if (unroll && bas0 == bas1 && scm_is_eq (v0, v1))
-    return 1;
-  return scm_ramapc (raeql_1, as_equal, ra0, scm_cons (ra1, SCM_EOL), "");
-}
+  scm_t_array_handle hx, hy;
+  SCM res;  
+  
+  scm_array_get_handle (x, &hx);
+  scm_array_get_handle (y, &hy);
+  
+  res = scm_from_bool (hx.ndims == hy.ndims
+                       && hx.element_type == hy.element_type);
 
+  if (scm_is_true (res))
+    res = scm_from_bool (array_compare (&hx, &hy, 0, 0, 0));
 
-SCM
-scm_raequal (SCM ra0, SCM ra1)
-{
-  return scm_from_bool(raeql (ra0, SCM_BOOL_T, ra1));
+  scm_array_handle_release (&hy);
+  scm_array_handle_release (&hx);
+
+  return res;
 }
 
+static SCM scm_i_array_equal_p (SCM, SCM, SCM);
 SCM_DEFINE (scm_i_array_equal_p, "array-equal?", 0, 2, 1,
             (SCM ra0, SCM ra1, SCM rest),
            "Return @code{#t} iff all arguments are arrays with the same\n"
            "shape, the same type, and have corresponding elements which are\n"
            "either @code{equal?}  or @code{array-equal?}.  This function\n"
-           "differs from @code{equal?} in that a one dimensional shared\n"
-           "array may be @var{array-equal?} but not @var{equal?} to a\n"
-           "vector or uniform vector.")
+           "differs from @code{equal?} in that all arguments must be arrays.")
 #define FUNC_NAME s_scm_i_array_equal_p
 {
   if (SCM_UNBNDP (ra0) || SCM_UNBNDP (ra1))
@@ -953,19 +890,9 @@ SCM_DEFINE (scm_i_array_equal_p, "array-equal?", 0, 2, 1,
 #undef FUNC_NAME
 
 
-SCM
-scm_array_equal_p (SCM ra0, SCM ra1)
-{
-  if (SCM_I_ARRAYP (ra0) || SCM_I_ARRAYP (ra1))
-    return scm_from_bool(raeql (ra0, SCM_BOOL_F, ra1));
-  return scm_equal_p (ra0, ra1);
-}
-
-
 void
 scm_init_array_map (void)
 {
-  scm_smobs[SCM_TC2SMOBNUM (scm_i_tc16_array)].equalp = scm_raequal;
 #include "libguile/array-map.x"
   scm_add_feature (s_scm_array_for_each);
 }