pspp-dev
[Top][All Lists]
Advanced

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

[patch 2/7] Add insert_range, insert_element functions.


From: blp
Subject: [patch 2/7] Add insert_range, insert_element functions.
Date: Sat, 02 Jun 2007 15:36:49 -0700
User-agent: quilt/0.45-1

diff --git a/src/libpspp/array.c b/src/libpspp/array.c
index 2e011b6..56afb1a 100644
--- a/src/libpspp/array.c
+++ b/src/libpspp/array.c
@@ -360,6 +360,31 @@ remove_element (void *array, size_t count, size_t size,
   remove_range (array, count, size, idx, 1);
 }
 
+/* Makes room for N elements starting at IDX in ARRAY, which
+   initially consists of COUNT elements of SIZE bytes each, by
+   shifting elements IDX...COUNT (exclusive) to the right by N
+   positions. */
+void
+insert_range (void *array_, size_t count, size_t size,
+              size_t idx, size_t n) 
+{
+  char *array = array_;
+
+  assert (idx <= count);
+  memmove (array + (idx + n) * size, array + idx * size, (count - idx) * size);
+}
+
+/* Makes room for a new element at IDX in ARRAY, which initially
+   consists of COUNT elements of SIZE bytes each, by shifting
+   elements IDX...COUNT (exclusive) to the right by one
+   positions. */
+void
+insert_element (void *array, size_t count, size_t size,
+                size_t idx) 
+{
+  insert_range (array, count, size, idx, 1);
+}
+
 /* Moves an element in ARRAY, which consists of COUNT elements of
    SIZE bytes each, from OLD_IDX to NEW_IDX, shifting around
    other elements as needed.  Runs in O(abs(OLD_IDX - NEW_IDX))
diff --git a/src/libpspp/array.h b/src/libpspp/array.h
index 38d38bb..a867ade 100644
--- a/src/libpspp/array.h
+++ b/src/libpspp/array.h
@@ -108,6 +108,20 @@ void remove_range (void *array, size_t count, size_t size,
 void remove_element (void *array, size_t count, size_t size,
                      size_t idx);
 
+/* Makes room for N elements starting at IDX in ARRAY, which
+   initially consists of COUNT elements of SIZE bytes each, by
+   shifting elements IDX...COUNT (exclusive) to the right by N
+   positions. */
+void insert_range (void *array, size_t count, size_t size,
+                   size_t idx, size_t n);
+
+/* Makes room for a new element at IDX in ARRAY, which initially
+   consists of COUNT elements of SIZE bytes each, by shifting
+   elements IDX...COUNT (exclusive) to the right by one
+   positions. */
+void insert_element (void *array, size_t count, size_t size,
+                     size_t idx);
+
 /* Moves an element in ARRAY, which consists of COUNT elements of
    SIZE bytes each, from OLD_IDX to NEW_IDX, shifting around
    other elements as needed.  Runs in O(abs(OLD_IDX - NEW_IDX))
-- 
1.4.4.3

--





reply via email to

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