Type Traits 例項,提高效率 (轉)

themoney發表於2007-09-15
Type Traits 例項,提高效率 (轉)[@more@]

#include "iostream"
using namespace std;

class SimpleCompareTrait { };
class ComplexCompareTrait { };

template class ComplexCompare
{
public:
 typedef T ValueType;
 typedef ComplexCompareTrait CompareTrait;
};

template class SimpleCompare
{
public:
 typedef T ValueType;
 typedef SimpleCompareTrait CompareTrait;
};

template
Iterator __LowerBound(Iterator first, Iterator last, T val,
  Compare comp, ComplexCompareTrait)
{
 size_t len = 0;
 size_t half;
 Iterator middle;
 
 len = last - first;
 while (len > 0)
 {
 half = len >> 1;
 middle = first;
 middle += half;
 if (comp(*middle, val) < 0)
 {
 first = middle;
 ++first;
 len = len - half - 1;
 } else
 len = half;
 }
 return first;
}

template
Iterator __LowerBound(Iterator first, Iterator last, T val,
  Compare comp, SimpleCompareTrait)
{
 size_t len = 0;
 size_t half;
 Iterator middle;
 
 len = last - first;
 while (len > 0)
 {
 half = len >> 1;
 middle = first;
 middle += half;
 if (comp(*middle, val))
 {
 first = middle;
 ++first;
 len = len - half - 1;
 } else
 len = half;
 }
 return first;
}

template
Iterator LowerBound(Iterator first, Iterator last, T val, Compare comp)
{
 return __LowerBound(first, last, val, comp, Compare::CompareTrait());
}

template
Iterator __BinSearch(Iterator first, Iterator last, T val,
  Compare comp, ComplexCompareTrait)
{
 size_t len = 0;
 size_t half;
 Iterator middle;
 
 len = last - first;
 while (len > 0)
 {
 half = len >> 1;
 middle = first;
 middle += half;
 int iComp = comp(*middle, val);
 if (iComp < 0)
 {
 first = middle;
 ++first;
 len = len - half - 1;
 }
 else if (iComp > 0)
 {
 len = half;
 } else
 return middle;
 }
 return last;
}

template
Iterator __BinSearch(Iterator first, Iterator last, T val,
  Compare comp, SimpleCompareTrait)
{
 size_t len = 0;
 size_t half;
 Iterator middle;
 
 len = last - first;
 while (len > 0)
 {
 half = len >> 1;
 middle = first;
 middle += half;
 if (comp(*middle, val))
 {
 first = middle;
 ++first;
 len = len - half - 1;
 }
 else if (comp(val, *middle))
 {
 len = half;
 } else
 return middle;
 }
 return last;
}

template
Iterator BinSearch(Iterator first, Iterator last, T val, Compare comp)
{
 return __BinSearch(first, last, val, comp, Compare::CompareTrait());
}

template class TheSimpleCompare : public SimpleCompare
{
public:
 bool operator()(const T& lv, const T& rv) const
 { return lv < rv; }
};
template class TheComplexCompare : public ComplexCompare
{
public:
 int operator()(const T& lv, const T& rv) const
 { return lv - rv; }
};

typedef TheSimpleCompare SimpleIntCompare;
typedef TheComplexCompare ComplexIntCompare;
/*
class SimpleIntCompare : public SimpleCompare
{
public:
bool operator()(int lv, int rv) const
{ return lv < rv; } 
};
class ComplexIntCompare : public ComplexCompare
{
public:
int operator()(int lv, int rv) const
{ return lv - rv; } 
};
*/

#define dimof(x) (sizeof(x)/sizeof(x[0]))

int main(int argc, char* argv[])
{
 int  val = 8;
 int  tab[] = { 1, 2, 3, 4, 5, 6, 7, 9, 10, 10000};

 int  *slow = LowerBound(tab, tab + dimof(tab) - 1, val, SimpleIntCompare());
 int  *clow = LowerBound(tab, tab + dimof(tab) - 1, val, ComplexIntCompare());

 int  *s = BinSearch(tab, tab + dimof(tab) - 1, val, SimpleIntCompare());
 int  *c = BinSearch(tab, tab + dimof(tab) - 1, val, ComplexIntCompare());

 cout << "*slow = " << *slow << ", *clow = " << *clow << endl;
 cout << "*s = " << *s << ", *c = " << *c << endl
  << endl;

 return 0;
}

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10794571/viewspace-969600/,如需轉載,請註明出處,否則將追究法律責任。

相關文章