delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/16/21:43:15

From: authentic AT tip DOT nl (Rick)
Newsgroups: comp.lang.c++,comp.os.msdos.djgpp
Subject: Re: list.sort(const void*(*fnc)()) again;=)
Date: Wed, 15 Apr 1998 22:31:04 GMT
Organization: NL-NIC
Lines: 237
Message-ID: <6h60uh$5ko$1@cadmium.aware.nl>
References: <6gsv3d$dli$1 AT cadmium DOT aware DOT nl> <3533f431 DOT 6208206 AT news DOT ist DOT utl DOT pt>
Reply-To: authentic AT tip DOT nl
NNTP-Posting-Host: nijmegen-124.trb.pop.tip.nl
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

deepblack AT geocities DOT com (Luís Coelho) wrote:

Luis thank you for your reactions. Thanks to you, and also to
Oleg..

As another non-standard reaction I will give the solution.. .
Hope you can appreciate it..and hope that visual c would
compile it too(tested on djgpp..)

Rick

#include<std/string.h>

#include <iostream.h>
#include <stl.h>


class datab
{


        public:
        friend ostream& operator<<(ostream&, const datab&);

        datab(char* _name='\0',int _id=0,char* _tel='\0')
               :name(string(_name)),id(_id),telef(string(_tel)){};

        friend int operator<(datab&,datab&);


        const string get_name()const{return name;}
        const string get_telef()const{return telef;}
        int    get_id()const {return id;}

        private:
        const string name;
        const string telef;
        int   id;

};
ostream& operator<<(ostream& os, const datab& _datab)
{
         os<<_datab.name<<'\t'
            <<_datab.id<<'\t'
            <<_datab.telef<<endl;
}

//This get called on a standard list sort

int operator<(datab& el1,datab& el2)
{
        return (el1.id< el2.id)< 0 ?1:0;
}


template <class T>
class sorted_list:public list<T>
{
        public:
        //overladen van ingebouwde sort: dat is alles
        void string_sort(const string (T::*get_function)()const);
        //Om dat te doen moet je dus eigenlijk de call
        //de merge overladen(die gebruikt operator < en !=)
        void string_merge(sorted_list<T>&,const string
(T::*get_function)()const);

};

//Merge&Sort Code below is merely a cut and paste thing from
//the gcc sources(with minor adjustments). Thanks again.
//A great source of info for me..

template <class T>
void sorted_list<T>::string_sort
(const string (T::*get_function)()const)
{
    if (size() < 2) return;
        sorted_list<T> carry;
    sorted_list<T> counter[64];
    int fill = 0;
    while (!empty()) {
	carry.splice(carry.begin(), *this, begin());
	int i = 0;
	while(i < fill && !counter[i].empty()) {
	    counter[i].string_merge(carry,get_function);
	    carry.swap(counter[i++]);
	}
	carry.swap(counter[i]);         
	if (i == fill) ++fill;
    } 

    for (int i = 1; i < fill; ++i)
       counter[i].string_merge(counter[i-1],get_function);
    swap(counter[fill-1]);
}

template <class T>
void sorted_list<T>::string_merge
(sorted_list<T>& x,const string (T::*get_function)()const)
{
    iterator first1 = begin();
    iterator last1 = end();
    iterator first2 = x.begin();
    iterator last2 = x.end();
    while (first1 != last1 && first2 != last2)
        if (((*first2).*get_function)() <
            ((*first1).*get_function)())
        {
            iterator next = first2;
	    transfer(first1, first2, ++next);
	    first2 = next;
	} else
	    ++first1;
    if (first2 != last2)
       transfer(last1, first2, last2);
    length += x.length;
    x.length= 0;
}


int main()
{
        sorted_list<datab> lijst1;
        ostream_iterator<datab>outs1(cout);
        sorted_list<datab>::iterator i;
        const string (datab::*string_fnc)()const;

        list<datab>lijst2;
        ostream_iterator<datab>outs2(cout);


        datab temp("Annet",1,"743");
        lijst1.push_front(temp);
        lijst1.push_front(datab("Patty",2,"456"));
        lijst1.push_front(datab("Han",3,"123"));
        lijst1.push_front(datab("Harry",4,"741"));

        //This calls the stl inhereted sort
        string_fnc=&datab::get_name;

        i=lijst1.begin();
        cout<<(*i).get_name()<<endl;
        string temp2=(string)(temp.*string_fnc)();
        string temp3=(string)((*i).*string_fnc)();
        cout<<temp2<<temp3<<endl;

        cout<<"\nVOLGENS NAAM\n"<<endl;
        lijst1.string_sort(string_fnc);
        copy(lijst1.begin(),lijst1.end(),outs1);
        string_fnc=&datab::get_telef;

        cout<<"\nVOLGENS TELEFID\n"<<endl;
        lijst1.string_sort(string_fnc);
        copy(lijst1.begin(),lijst1.end(),outs1);

        cout<<"\nVOLGENS (standard)ID\n"<<endl;
        lijst1.sort();
        copy(lijst1.begin(),lijst1.end(),outs1);




        return 0;

}



>On Sun, 12 Apr 1998 12:04:31 GMT, authentic AT tip DOT nl (Rick) uttered
>the following words:

>>Dear c++ programmers,
Question 1 answered by a few very nice people.
I hope all compilers complie;=)


>bool operator < (myclass e11, myclass e12);?
>^^^^
Probably


>const void* (myclass::*get_fnc) () const = &myclass::get_myname;

>I think you always need the & on member functions. Could be
>wrong, though.

Well it compiled without &, and given the code I think the compiler
has only one choice here: give it the adress of the function...
But maybe adding & is good for readability;=))) hahahaha

Remains:

3. How do I reference that pointer inside my template
    list ?(the example below)

thats the more problematic one, and also the only
problem that is between me and my generiek listsort.

when i have a child of stl list(like said before: simple)
and:
myclass temp(.....);
mylist<myclass> lijst1;
mylist<myclass>::iterator i;
const void* (myclass::*get_fnc) () const = &myclass::get_myname;

//fill list1
i=lijst1.begin;

I CAN directly reference a member fnc
cout<<(*i).getmyname()<<endl;

I CAN indirectly reference a member fnc
for a class object:
char* uit=(char *)tmp.*get_fnc();

I CAN indirectly reference with:
char* meeruit=(char *)((i*).*get_fnc());

(method showed kindly by Oleg Zabluda)

But both last statements give me a sigabort.
but it was in the stringclass thus probably
has nothing to do with the problem at hand.

Still I like to tell it to the wonderful djgpp people
in case they want to know ;=)

I hope that this compilation of answers
problems and own inventions is appreciated,

Rick



>Hope that helped,


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019