G
Grahamo
Hi
I'm working with deployed code and am looking for ways to optimise it
and clean up memory leaks etc. I'm working with a template
ContainerOfPointers, which is exactly that. It has methods like insert,
clear, splice, adopt, at etc. It was written before the advent of STL,
hence it's presence in the code.
I'm looking at a piece of code that works on such templates,
specifically a sort template function which is defined below. I am
under the impression that stl algorithms and such like are going to be
faster than home grown ones (the compiler is smarter than me...not
difficult). so ideally I'd like to see if its possible to replace these
sort of routines with STL ones.
Could anybody offer any advice on what appoach I should take. My goal
is to replace home grown algorithms with stl ones (even if it's to do a
comparison and not to deploy so I can at least see the results). It
would be nice to replace the body of the sort template function below
with that to stl::sort but I don't have randomAccessIterators for my
ContainerOfPointers template. Should I embark on doing so or am I
pi*ssing into the proverbial wind.
template <class T>
void sort (ContainerOfPointers<T>& ref)
{
TTK_TEST_REF (ref); // does an assert
bool flag (true);
while (flag)
{
flag = false;
int end(ref.entries());
for (int i(1); i < end; i++)
{
if (*ref.at (i-1) > *ref.at (i))
{
DoSwap(ref.at (i-1) , ref.at (i));
flag = true;
}
}
}
}
swaps straight forward enough.
template <class T>
void DoSwap (T& ref1, T& ref2)
{
T tmp (ref1);
ref1 = ref2;
ref2 = tmp;
};
thanks for any insights.
have a nice day.
GrahamO
I'm working with deployed code and am looking for ways to optimise it
and clean up memory leaks etc. I'm working with a template
ContainerOfPointers, which is exactly that. It has methods like insert,
clear, splice, adopt, at etc. It was written before the advent of STL,
hence it's presence in the code.
I'm looking at a piece of code that works on such templates,
specifically a sort template function which is defined below. I am
under the impression that stl algorithms and such like are going to be
faster than home grown ones (the compiler is smarter than me...not
difficult). so ideally I'd like to see if its possible to replace these
sort of routines with STL ones.
Could anybody offer any advice on what appoach I should take. My goal
is to replace home grown algorithms with stl ones (even if it's to do a
comparison and not to deploy so I can at least see the results). It
would be nice to replace the body of the sort template function below
with that to stl::sort but I don't have randomAccessIterators for my
ContainerOfPointers template. Should I embark on doing so or am I
pi*ssing into the proverbial wind.
template <class T>
void sort (ContainerOfPointers<T>& ref)
{
TTK_TEST_REF (ref); // does an assert
bool flag (true);
while (flag)
{
flag = false;
int end(ref.entries());
for (int i(1); i < end; i++)
{
if (*ref.at (i-1) > *ref.at (i))
{
DoSwap(ref.at (i-1) , ref.at (i));
flag = true;
}
}
}
}
swaps straight forward enough.
template <class T>
void DoSwap (T& ref1, T& ref2)
{
T tmp (ref1);
ref1 = ref2;
ref2 = tmp;
};
thanks for any insights.
have a nice day.
GrahamO