changeset 11877:d11919389e09 release-3-0-x

fix sorting of non-POD objects
author Jaroslav Hajek <highegg@gmail.com>
date Sun, 12 Oct 2008 10:40:30 +0200
parents b1122c225f6c
children 2415586d0f5a
files liboctave/ChangeLog liboctave/oct-sort.cc
diffstat 2 files changed, 27 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/ChangeLog
+++ b/liboctave/ChangeLog
@@ -1,3 +1,11 @@
+2008-10-09  Jaroslav Hajek <highegg@gmail.com>
+
+	* oct-sort.cc (octave_sort<T>::merge_getmem,
+	octave_sort<T>::merge_freemem): Replace malloc -> new [], free ->
+	delete [].
+	(octave_sort<T>::merge_lo, octave_sort<T>::merge_hi): Replace
+	std::memcpy and std::memmove with std::copy.
+
 2008-10-12  Jaroslav Hajek <highegg@gmail.com>
 
 	* CSparse.cc (ComplexMatrix::expm): Improve inverse preconditioning
--- a/liboctave/oct-sort.cc
+++ b/liboctave/oct-sort.cc
@@ -1,5 +1,6 @@
 /*
 Copyright (C) 2003, 2004, 2005, 2006, 2007 David Bateman
+Copyright (C) 2008 Jaroslav Hajek
 
 This file is part of Octave.
 
@@ -27,6 +28,11 @@
 * convert the sorting code in listobject.cc into a generic class, 
   replacing PyObject* with the type of the class T.
 
+* replaced usages of malloc, free, memcpy and memmove by standard C++
+  new [], delete [] and std::copy. Note that replacing memmove by std::copy
+  is possible if the destination starts before the source.
+  
+
 The Python license is
 
   PSF LICENSE AGREEMENT FOR PYTHON 2.3
@@ -84,7 +90,7 @@
 #endif
 
 #include <cassert>
-#include <cstdlib>
+#include <algorithm>
 #include <cstring>
 
 #include "lo-mappers.h"
@@ -432,8 +438,7 @@
 void
 octave_sort<T>::merge_freemem (void)
 {
-  if (ms.a)
-    free (ms.a);
+  delete [] ms.a;
   ms.alloced = 0;
   ms.a = 0;
 }
@@ -490,7 +495,7 @@
    * we don't care what's in the block.
    */
   merge_freemem ();
-  ms.a = static_cast <T *> (malloc (need * sizeof (T)));
+  ms.a = new T[need];
   if (ms.a)
     {
       ms.alloced = need;
@@ -520,7 +525,7 @@
 
   if (MERGE_GETMEM (na) < 0)
     return -1;
-  std::memcpy (ms.a, pa, na * sizeof (T));
+  std::copy (pa, pa + na, ms.a);
   dest = pa;
   pa = ms.a;
 
@@ -582,7 +587,7 @@
 	    {
 	      if (k < 0)
 		goto Fail;
-	      std::memcpy (dest, pa, k * sizeof (T));
+              std::copy (pa, pa + k, dest);
 	      dest += k;
 	      pa += k;
 	      na -= k;
@@ -606,7 +611,7 @@
 	    {
 	      if (k < 0)
 		goto Fail;
-	      std::memmove (dest, pb, k * sizeof (T));
+              std::copy (pb, pb + k, dest);
 	      dest += k;
 	      pb += k;
 	      nb -= k;
@@ -629,12 +634,12 @@
 
  Fail:
   if (na)
-    std::memcpy (dest, pa, na * sizeof (T));
+    std::copy (pa, pa + na, dest);
   return result;
 
  CopyB:
   /* The last element of pa belongs at the end of the merge. */
-  std::memmove (dest, pb, nb * sizeof (T));
+  std::copy (pb, pb + nb, dest);
   dest[nb] = *pa;
 
   return 0;
@@ -660,7 +665,7 @@
   if (MERGE_GETMEM (nb) < 0)
     return -1;
   dest = pb + nb - 1;
-  std::memcpy (ms.a, pb, nb * sizeof (T));
+  std::copy (pb, pb + nb, ms.a);
   basea = pa;
   baseb = ms.a;
   pb = ms.a + nb - 1;
@@ -726,7 +731,7 @@
 	    {
 	      dest -= k;
 	      pa -= k;
-	      std::memmove (dest+1, pa+1, k * sizeof (T));
+              std::copy (pa+1, pa+1 + k, dest+1);
 	      na -= k;
 	      if (na == 0)
 		goto Succeed;
@@ -745,7 +750,7 @@
 	    {
 	      dest -= k;
 	      pb -= k;
-	      std::memcpy (dest+1, pb+1, k * sizeof (T));
+              std::copy (pb+1, pb+1 + k, dest+1);
 	      nb -= k;
 	      if (nb == 1)
 		goto CopyA;
@@ -770,14 +775,14 @@
 
 Fail:
   if (nb)
-    std::memcpy (dest-(nb-1), baseb, nb * sizeof (T));
+    std::copy (baseb, baseb + nb, dest-(nb-1));
   return result;
 
 CopyA:
   /* The first element of pb belongs at the front of the merge. */
   dest -= na;
   pa -= na;
-  std::memmove (dest+1, pa+1, na * sizeof (T));
+  std::copy (pa+1, pa+1 + na, dest+1);
   *dest = *pb;
 
   return 0;