473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

template sort

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

Sep 8 '05 #1
4 1739
Gr*****@nospam.com wrote:
....
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;
}
}
}
}

You can wrap your "ContainerOfPointers" with another class that does
provide iterators. Then you can use std::sort on the wrapper.

I suspect std::sort would be much much faster than the algorithm here.
Somthing like this:

template <class T>
class COPWrapperIterator;

template <class T>
class COPWrapper
{
ContainerOfPointers<T> & m_ref;

public:
COPWrapper( ContainerOfPointers<T>& ref )
: m_ref( ref )
{
}

COPWrapperIterator<T> begin();
COPWrapperIterator<T> end();

};

template <class T>
class COPWrapperIterator
{
COPWrapper<T> & m_copwrapper;
int m_index;
friend class COPWrapper<T>;
COPWrapperIterator( COPWrapper<T> & copwrapper, int index );
..... etc

};
Then your sort code would look somthing like:

template <class T>
void sort (ContainerOfPointers<T>& ref)
{
TTK_TEST_REF (ref); // does an assert

COPWrapper<T> wrapper( ref );

std::sort( wrapper.begin(), wrapper.end(), CompareLess() );

};
Sep 8 '05 #2
Thanks for that Gianni,

std::sort signatures are;

template <class RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last);

and

template <class RandomAccessIterator, class StrictWeakOrdering>
void sort(RandomAccessIterator first, RandomAccessIterator last,
StrictWeakOrdering comp);

so I am presuming my COPWrapperIterator template class inherits from
the appopriate iterator type , in this class RandomAccessIterator, and
from there imlement the methods required by same iterator type. yes?

thanks for your help

G

Sep 8 '05 #3
Gr*****@nospam.com schreef:
std::sort signatures are;

template <class RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last);

and

template <class RandomAccessIterator, class StrictWeakOrdering>
void sort(RandomAccessIterator first, RandomAccessIterator last,
StrictWeakOrdering comp);

so I am presuming my COPWrapperIterator template class inherits from
the appopriate iterator type , in this class RandomAccessIterator, and
from there imlement the methods required by same iterator type. yes?


Boost has boost::iterator which is a huge timesaving. However, it's
not needed. RandomAccessIterator in this case is just a descriptive
name.
It could have been called T, and is unrelated to a base class.
(www.boost.org)

E.g. an int* ia a proper RandomAccessIterator for an int[] array,
because all operations needed are provided.
HTH,
Michiel Salters

Sep 8 '05 #4
ah shi*te.... should have seen that!

thanks for your reply.

Graham

Sep 8 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Senthilvel | last post by:
Hi folks, I was trying to learn the standard library algorithms where i got stuck with the templates. I wrote one function to eliminate the duplicates(from The C++ Programming Language 3rd...
13
by: jsnX | last post by:
say i have a function object silly that takes a const ref to clowns class silly : public std::unary_function<const clown&, bool> { ... } and then i decide to feed it a bunch of pointers to...
5
by: dilip ranganathan | last post by:
Hi I have taken the liberty to cross-post this. It appeared on c.l.c++.m but the ICE is regarding VS.NET 7.1 C++ compiler. post follows: ==============================================...
0
by: Nicolas | last post by:
Hi, I've been working on this problem for a couple of days and now i have no more ideas. first, this problem only happens with jdk 1.5, everything was fine with jdk1.4. I'm using a...
1
by: cindypwl | last post by:
I was looking at some code in the MS NG, and I saw this code listed. template <class T>class List{}; template<class T> bool MySortFunc(List<T>& list){return true;} template <class T>struct...
0
by: Steve | last post by:
I have gridview that as a template field. I have all columns in the gridView sortable. The issue I'm facing is when I click on the Template field header to sort, I have to click it twice for it to...
10
by: Pavel Shved | last post by:
Is there a program of wide use that automatically extracts template parameters requirements from source code of template functions? I mean the following: assume we have a template function that...
3
by: utab | last post by:
Dear all, I have the below template (1) which I would like to implement to accept either a vector or an array. I thought on it some time and came with a solution as in (2) , however the call of...
5
by: randysimes | last post by:
I am making a previous program into a template to calculate all 11 options. (int, double, long double, char, etc) I believe I have the right idea, but the syntax is way off. Could you please provide...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.