This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Remove algo logic duplication Round 3


Hello,

using the program below, it seems to me that to sort the array {1,2}, we do 9 copies of the comparison functor, and 13 iterator copies (used to be 4 and 8). As long as those are empty or pointers, that's free. But if copying has even a slight cost for the functor or the iterator, it is quickly going to be noticable.

Maybe we could use more references? The user-facing functions have to take iterators by value, but I think the inline helpers could use references.

(we also perform 2 comparisons where one should be enough, but that's a different issue)


#include <algorithm>
#include <iostream>

struct mycomp {
  mycomp(){std::cerr<<"mycomp()\n";}
  mycomp(mycomp const&){std::cerr<<"mycomp(mycomp const&)\n";}
  mycomp(mycomp &&){std::cerr<<"mycomp(mycomp &&)\n";}
  ~mycomp(){std::cerr<<"~mycomp()\n";}
  mycomp& operator=(mycomp const&){std::cerr<<"operator=(mycomp const&)\n";return*this;}
  mycomp& operator=(mycomp &&){std::cerr<<"operator=(mycomp &&)\n";return*this;}
  bool operator()(int a,int b)const{std::cerr<<"mycomp::operator()\n";return a<b;}
};

struct myiter {
  typedef long difference_type;
  typedef std::random_access_iterator_tag iterator_category;
  typedef int& reference;
  typedef int value_type;
  typedef int* pointer;
  myiter(){std::cerr<<"myiter()\n";}
  myiter(int*q):p(q){std::cerr<<"myiter(int*)\n";}
  myiter(myiter const&i):p(i.p){std::cerr<<"myiter(myiter const&)\n";}
  myiter(myiter &&i):p(i.p){std::cerr<<"myiter(myiter &&)\n";}
  ~myiter(){std::cerr<<"~myiter()\n";}
  myiter& operator=(myiter const&i){std::cerr<<"operator=(myiter const&)\n";p=i.p;return*this;}
  myiter& operator=(myiter &&i){std::cerr<<"operator=(myiter &&)\n";p=i.p;return*this;}
  int*p;
  int& operator*(){std::cerr<<"myiter::operator*\n";return *p;}
  myiter& operator++(){std::cerr<<"myiter::operator++\n";++p;return*this;}
  myiter operator++(int){std::cerr<<"myiter::operator++(int)\n";myiter r(p++);return r;}
  myiter& operator--(){std::cerr<<"myiter::operator--\n";--p;return*this;}
  myiter operator--(int){std::cerr<<"myiter::operator--(int)\n";myiter r(p--);return r;}
  myiter operator+(long n)const{std::cerr<<"myiter::operator+\n";return(p+n);}
  myiter operator-(long n)const{std::cerr<<"myiter::operator-\n";return(p-n);}
  long operator-(myiter const&i)const{std::cerr<<"myiter-myiter\n";return(p-i.p);}
  bool operator==(myiter const&i)const{std::cerr<<"myiter::operator==\n";return(p==i.p);}
  bool operator!=(myiter const&i)const{std::cerr<<"myiter::operator!=\n";return(p!=i.p);}
  bool operator<(myiter const&i)const{std::cerr<<"myiter::operator<\n";return(p<i.p);}
};


int main(){
  int a[]={1,2};
  std::sort(myiter(a),myiter(a+sizeof(a)/sizeof(a[0])),mycomp());
}


--
Marc Glisse


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]