473,667 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
ContainerOfPoin ters, 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 randomAccessIte rators for my
ContainerOfPoin ters template. Should I embark on doing so or am I
pi*ssing into the proverbial wind.
template <class T>
void sort (ContainerOfPoi nters<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 1755
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 randomAccessIte rators for my
ContainerOfPoin ters template. Should I embark on doing so or am I
pi*ssing into the proverbial wind.
template <class T>
void sort (ContainerOfPoi nters<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 "ContainerOfPoi nters" 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 COPWrapperItera tor;

template <class T>
class COPWrapper
{
ContainerOfPoin ters<T> & m_ref;

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

COPWrapperItera tor<T> begin();
COPWrapperItera tor<T> end();

};

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

};
Then your sort code would look somthing like:

template <class T>
void sort (ContainerOfPoi nters<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 RandomAccessIte rator>
void sort(RandomAcce ssIterator first, RandomAccessIte rator last);

and

template <class RandomAccessIte rator, class StrictWeakOrder ing>
void sort(RandomAcce ssIterator first, RandomAccessIte rator last,
StrictWeakOrder ing comp);

so I am presuming my COPWrapperItera tor template class inherits from
the appopriate iterator type , in this class RandomAccessIte rator, 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 RandomAccessIte rator>
void sort(RandomAcce ssIterator first, RandomAccessIte rator last);

and

template <class RandomAccessIte rator, class StrictWeakOrder ing>
void sort(RandomAcce ssIterator first, RandomAccessIte rator last,
StrictWeakOrder ing comp);

so I am presuming my COPWrapperItera tor template class inherits from
the appopriate iterator type , in this class RandomAccessIte rator, 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. RandomAccessIte rator 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 RandomAccessIte rator 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
4981
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 Edition) template<class Con> void RemoveDuplicates(Con& seq) { sort(seq.begin(),seq.end());
13
1637
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 clowns: for_each(clown_pointers.begin(), clown_pointers.end(), silly());
5
1670
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: ============================================== John Torjo wrote: >
0
1175
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 function to calculate the maximum value of some nodes passed as argument, which i found here :
1
1806
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 SORT{typedef bool (*FnPtr)(List<T>& list);}; int main(){ List<int> MyList;
0
2127
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 sort. The other non-template field columns sort with one mouse click. How can I get the template field column to sort without having to click the header twice?
10
2082
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 demands its template parameter(s) to meet certain requirements. For example simple sorting function template <typename Tvoid sort(T* t, int N) { for (int i=0;i<N-1; i++)
3
2546
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 my implementation is not natural in the way that I do not supply the vector directly. How can I improve on that? (1) template <class T>
5
1964
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 some insight as to how to at least set one function up, or something that I could use to finish setting up the rest. I can provide the previous program if if would help. Thanks in advance! #include <iostream> #include <cstdlib> #include...
0
8366
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8650
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2779
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.