lilypond-devel
[Top][All Lists]
Advanced

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

Reverse in std_vector.hh


From: Ruud van Silfhout
Subject: Reverse in std_vector.hh
Date: Thu, 07 Sep 2006 21:58:32 +0200
User-agent: Thunderbird 1.5.0.5 (Windows/20060719)

Hi,

I have made an extension for the reverse function in std_vector.hh. The code suggests to implement template specialisation for reverse vector<int>.
This is what I have done, plus specialisation for float and double vectors.
The code depends on the availability of memrev, which is not available on all platforms.So a check is required (during configure) to (con)figure out if memrev is supported,. The result of the check being that the symbol MEMREV_SUPPORTED must be defined. I don't know how to implement this.

In case memrev is not available there are two options:
1. skip the specialized template implementation for <int> <float> <double> and keep the generic template code. This is what is implemented now. 2. implement a replacement for memrev. a simple replacement is added below. I don't know where to put it. it uses malloc to allocate memory for each item. This can also be done using alloca, which allocates memory on the stack and, as a consequence, does not have to be freed. but for alloca the same holds as for memrev, it is not supported on all platforms so additional complexity has to be added t to the configure scripting to determine whether alloca is supported, so I left that out.

Bye,
Ruud van Silfhout

additional memrev implementation:

void *memrev(void *block, size_t elsize, size_t elnum)
{
 void *pSwap = malloc(elsize);  // could have nbeen alloca(elsize)
 char *pLeft = (char*)block;
 char *pRight = (char*)block + (elnum-1)*elsize;
 while (pLeft < pRight)
   {
     memcpy (pSwap,  pLeft,  elsize);
     memcpy (pLeft,  pRight, elsize);
     memcpy (pRight, pSwap,  elsize);
     pLeft += elsize;
     pRight -= elsize;
   }
 free(pSwap);  // not needed if alloca is used
 return block;
}


Index: flower/include/std-vector.hh
===================================================================
RCS file: /sources/lilypond/lilypond/flower/include/std-vector.hh,v
retrieving revision 1.26
diff -u -r1.26 std-vector.hh
--- flower/include/std-vector.hh        7 Sep 2006 11:16:10 -0000       1.26
+++ flower/include/std-vector.hh        7 Sep 2006 19:52:35 -0000
@@ -222,13 +222,32 @@
 }
 #endif
 
+#ifdef MEMREV_SUPPORTED
+void
+reverse (vector<int> &v)
+{
+  memrev (v.begin (), sizeof(int), v.size ());
+}
+
+void
+reverse (vector<float> &v)
+{
+  memrev (v.begin (), sizeof(float), v.size ());
+}
+
+void
+reverse (vector<double> &v)
+{
+  memrev (v.begin (), sizeof(double), v.size ());
+}
+
+#endif
+
 template<typename T>
 void
 reverse (vector<T> &v)
 {
-  // CHECKME: for a simple vector, like vector<int>, this should
-  // expand to memrev.
-  reverse (v.begin (), v.end ());
+  reverse (v.begin(), v.end ());
 }
 
 template<typename T>

reply via email to

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