ITPub博客

首页 > 应用开发 > IT综合 > Type Traits 实例,提高效率 (转)

Type Traits 实例,提高效率 (转)

原创 IT综合 作者:themoney 时间:2007-09-15 23:34:58 0 删除 编辑
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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    119
  • 访问量
    337658