Type Traits 例項,提高效率 (轉)
#include "iostream"
using namespace std;
class SimpleCompareTrait { };
class ComplexCompareTrait { };
template
{
public:
typedef T ValueType;
typedef ComplexCompareTrait CompareTrait;
};
template
{
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
{
public:
bool operator()(const T& lv, const T& rv) const
{ return lv < rv; }
};
template
{
public:
int operator()(const T& lv, const T& rv) const
{ return lv - rv; }
};
typedef TheSimpleCompare
typedef TheComplexCompare
/*
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 《STL原始碼剖析》-- type_traits.h原始碼AI
- <input type="file">美化例項程式碼
- <input type="file">美化效果程式碼例項
- js清空<input type="file">值程式碼例項JS
- 美化<input type="range">控制元件程式碼例項控制元件
- APIHOOK例項剖析 (轉)APIHook
- type check例題
- 演示中斷處理的例項(例項六)(轉)
- oracle單例項轉RACOracle單例
- (轉)MySQL優化例項MySql優化
- 網站綜合例項(轉)網站
- [轉]BDB例項程式碼
- 網站綜合例項 (轉)網站
- AsmL例項研究(二) (轉)ASM
- 如何將獨立例項轉換成叢集例項EU
- 理解 traitsAI
- js通過type屬性值篩選input元素程式碼例項JS
- (轉)Python例項手冊Python
- MIME TYPE (轉)
- 封裝type方法例項封裝
- CSS 例項之翻轉圖片CSS
- BIND 9快速安裝例項(轉)
- 基本複製應用例項(轉)
- XML程式設計例項(二) (轉)XML程式設計
- 研發專案流程例項(轉)
- PAM認證模組使用例項(轉)
- ssh埠轉發的深入例項
- [轉載] Python 機器學習經典例項Python機器學習
- js網址跳轉例項程式碼JS
- Linux的nat埠轉換例項Linux
- (轉)例項分析:MySQL優化經驗MySql優化
- Python的時間轉換 【例項】Python
- [轉]使用 db2icrt 建立例項DB2
- CBuilder中使用管道技術例項 (轉)UI
- (譯)win32asm例項-1 (轉)Win32ASM
- (譯)win32asm例項-3 (轉)Win32ASM
- 限制程式執行例項數 (轉)
- JAVA例項 陰陽曆演算法(轉)Java演算法