Help with proritized sorting

Hello,
I would like to use sorted stl container with particular sorting policy:
Element of the container is composed of several prioritized sub elements and the sort policy respect a hierachical ordering policy of the subelemnts:

1
2
3
4
5
6
7
8
9
example :
 pseudotype A{
int i;
} 
pseudotype B{
int j;
} 

an example of sorted AB container would be { (i=0,j=0), (i=0,j=1), (i=1,j=0),(i=1,j=1)} ..its first sorted on A prop then on B prop


I hope im clear...
For this i create this pattern:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
	template <class T,class U> class HierarchicalPolicy : public T , public U{
	public:

		HierarchicalPolicy<T,U>(){
			U();
			T();
		}
		virtual bool operator <(const HierarchicalPolicy<T,U> & t)const{
			if  (U::operator ==(t))
				return T::operator<(t);
			else return (U::operator <(t));
		}
		virtual bool operator ==(const HierarchicalPolicy<T,U> & t)const{
			return  (U::operator==(t)
				&& T::operator==(t));
		}
	};
	class NULLCLASS {

	public:
		NULLCLASS(){}
		bool operator <(const NULLCLASS & t)const{
			std::cout<<"Never Happens"<<std::endl; return true;//Normally never Happens
		}
		bool operator ==(const NULLCLASS & t)const{
			return true;
		}
	};


However it seams that performances of this pattern is very low due to recursive polymorphism of the < operator....

Could there be a solution in order to destroy the sexy but unefficient recusive plymorphism?
Last edited on
1
2
3
4
		virtual bool operator <(const NULLCLASS & t)const{
			std::cout<<"Never Happens"<<std::endl; 
			return true;//Normally never Happens
		}
That can't be sorted. A must be before B, and B must be before A.

Also, lines 5-6 do nothing
sorry, i don't understand your answer ...I have correct a little mistake with NULLCLASS 's operators that shouldnt be virtual but that work great...
But that doesnt solve my original question about performance issue of such a pattern...
Ideally i want to break the inheritance chain in order to avoid potential heavy polymorphic call (when inheritance chain become very long till break the call stack)...
Any idea?
Last edited on
I'm saying that your less comparator is wrong.

The best way to avoid inheritance is to not use it.
1
2
3
4
5
6
7
8
9
10
11
12
13
template <class First, class Second>
class pair{
public:
   First first;
   Second second;

   bool operator<(const pair<First, Second> &b) const{
      return 
         first<b.first
         or not b.first<first
            and second < b.second;
   }
};
Last edited on
Yeah thanks for the answer but for recursive definitiion of the policy it doesnt work well as it put the different composite on differents levels, it's not easy to manipulate for the user......
I will though about setter functors in order to wrap the depth of such a pattern
Topic archived. No new replies allowed.