V
Virchanza
I'm cleaning up some old code of mine, I want to use functions
from <algorithm> instead of using my own hand-coded algorithms for
searching and sorting. (I had been programming in C++ for a few years
before I actually studied <algorithm>).
My program has a few null-terminated arrays in it. For instance,
here's a function I have for sorting a null-terminated array of IP
addresses:
void SortIPs(uint_least32_t *const p_ip_array)
{
if (!p_ip_array[0] || !p_ip_array[1])
{
return;
}
The_Start:
{
uint_least32_t *pa = p_ip_array,
*pb = p_ip_array + 1;
for (;
{
if ( IP_A_less_than_B(*pb,*pa) )
{
uint_least32_t const temp = *pa;
*pa = *pb;
*pb = temp;
goto The_Start;
}
++pa;
++pb;
if (!*pb)
return;
}
}
}
I want to use the std::sort function instead, but I see that you have
to supply a "one past the last valid element" iterator to it -- which
isn't cool when dealing with simple null-terminated arrays...
What do you think would be the most efficient way to do this using the
STL? Here's what I have so far:
template<class T>
void GetIteratorFor_OnePastLastElement(T p)
{
for ( ; static_cast<bool>(*p); ++p);
return begin;
}
#include <algorithm>
inline void SortIPs(uint_least32_t *const p_ip_data_array)
{
std::sort(p_ip_data_array,
GetIteratorFor_OnePastLastElement(p_ip_data_array),
IP_A_less_than_B);
}
I wonder if this might end up being less efficient than my original
hand-written code because of the performance hit in finding the null
terminator before the sort begins.
Of course I could always change the code to keep track of an "end
pointer" but I prefer to use null-terminated arrays.
from <algorithm> instead of using my own hand-coded algorithms for
searching and sorting. (I had been programming in C++ for a few years
before I actually studied <algorithm>).
My program has a few null-terminated arrays in it. For instance,
here's a function I have for sorting a null-terminated array of IP
addresses:
void SortIPs(uint_least32_t *const p_ip_array)
{
if (!p_ip_array[0] || !p_ip_array[1])
{
return;
}
The_Start:
{
uint_least32_t *pa = p_ip_array,
*pb = p_ip_array + 1;
for (;
{
if ( IP_A_less_than_B(*pb,*pa) )
{
uint_least32_t const temp = *pa;
*pa = *pb;
*pb = temp;
goto The_Start;
}
++pa;
++pb;
if (!*pb)
return;
}
}
}
I want to use the std::sort function instead, but I see that you have
to supply a "one past the last valid element" iterator to it -- which
isn't cool when dealing with simple null-terminated arrays...
What do you think would be the most efficient way to do this using the
STL? Here's what I have so far:
template<class T>
void GetIteratorFor_OnePastLastElement(T p)
{
for ( ; static_cast<bool>(*p); ++p);
return begin;
}
#include <algorithm>
inline void SortIPs(uint_least32_t *const p_ip_data_array)
{
std::sort(p_ip_data_array,
GetIteratorFor_OnePastLastElement(p_ip_data_array),
IP_A_less_than_B);
}
I wonder if this might end up being less efficient than my original
hand-written code because of the performance hit in finding the null
terminator before the sort begins.
Of course I could always change the code to keep track of an "end
pointer" but I prefer to use null-terminated arrays.