pspp-dev
[Top][All Lists]
Advanced

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

[patch 1/5] Add move_range function.


From: blp
Subject: [patch 1/5] Add move_range function.
Date: Sun, 03 Jun 2007 15:47:12 -0700
User-agent: quilt/0.45-1

Index: merge/src/libpspp/array.h
===================================================================
--- merge.orig/src/libpspp/array.h      2007-05-31 22:06:31.000000000 -0700
+++ merge/src/libpspp/array.h   2007-05-31 22:36:10.000000000 -0700
@@ -115,6 +115,12 @@
 void move_element (void *array, size_t count, size_t size,
                    size_t old_idx, size_t new_idx);
 
+/* Moves N elements in ARRAY starting at OLD_IDX, which consists
+   of COUNT elements of SIZE bytes each, so that they now start
+   at NEW_IDX, shifting around other elements as needed. */
+void move_range (void *array, size_t count, size_t size,
+                 size_t old_idx, size_t new_idx, size_t n);
+
 /* Removes elements equal to ELEMENT from ARRAY, which consists
    of COUNT elements of SIZE bytes each.  Returns the number of
    remaining elements.  AUX is passed to COMPARE as auxiliary
Index: merge/src/libpspp/array.c
===================================================================
--- merge.orig/src/libpspp/array.c      2007-05-31 22:09:03.000000000 -0700
+++ merge/src/libpspp/array.c   2007-05-31 22:36:10.000000000 -0700
@@ -390,6 +390,36 @@
     }
 }
 
+/* Moves N elements in ARRAY starting at OLD_IDX, which consists
+   of COUNT elements of SIZE bytes each, so that they now start
+   at NEW_IDX, shifting around other elements as needed. */
+void
+move_range (void *array_, size_t count, size_t size,
+            size_t old_idx, size_t new_idx, size_t n)
+{
+  assert (array_ != NULL || count == 0);
+  assert (n <= count);
+  assert (old_idx + n <= count);
+  assert (new_idx + n <= count);
+  
+  if (old_idx != new_idx && n > 0) 
+    {
+      char *array = array_;
+      char *range = xmalloc (size * n);
+      char *new = array + new_idx * size;
+      char *old = array + old_idx * size;
+
+      memcpy (range, old, size * n);
+      if (new < old)
+        memmove (new + size * n, new, (old_idx - new_idx) * size);
+      else
+        memmove (old, old + size * n, (new_idx - old_idx) * size);
+      memcpy (new, range, size * n);
+
+      free (range);
+    }
+}
+
 /* A predicate and its auxiliary data. */
 struct pred_aux 
   {

--





reply via email to

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