00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __lfeat_sieve_h
00022 #define __lfeat_sieve_h
00023
00024 #include "BoundedSet.h"
00025
00026 namespace lfeat
00027 {
00028
00029 template <typename _Key>
00030 class SieveExtractor
00031 {
00032 public:
00033 virtual void operator()(const _Key& k) = 0;
00034 };
00035
00036 template <typename _Key, typename _Compare = std::less<_Key> >
00037 class Sieve
00038 {
00039
00040 public:
00041 Sieve(int iWidth, int iHeight, int iLength);
00042 void insert(_Key& iElem, int iWidth, int iHeight);
00043
00044
00045 void extract(SieveExtractor<_Key>& iEx);
00046
00047 private:
00048 std::vector<lfeat::bounded_set< _Key, _Compare > > _buckets;
00049 int _width, _height;
00050
00051
00052 };
00053
00054 template <typename _Key, typename _Compare>
00055 Sieve<_Key, _Compare>::Sieve(int iWidth, int iHeight, int iLength) :
00056 _buckets(std::vector<bounded_set< _Key, _Compare > >(iWidth* iHeight)),
00057 _width(iWidth), _height(iHeight)
00058 {
00059 typename std::vector<bounded_set< _Key, _Compare > >::iterator aVB, aVE;
00060 aVB = _buckets.begin();
00061 aVE = _buckets.end();
00062 for (; aVB != aVE; ++aVB)
00063 {
00064 aVB->setMaxSize(iLength);
00065 }
00066 }
00067
00068 template <typename _Key, typename _Compare>
00069 void Sieve<_Key, _Compare>::insert(_Key& iElem, int iWidth, int iHeight)
00070 {
00071 _buckets[iWidth * _height + iHeight].insert(iElem);
00072 }
00073
00074 template <typename _Key, typename _Compare>
00075 void Sieve<_Key, _Compare>::extract(SieveExtractor<_Key>& iEx)
00076 {
00077 typename std::vector<bounded_set< _Key, _Compare > >::iterator aVB, aVE;
00078 aVB = _buckets.begin();
00079 aVE = _buckets.end();
00080 for (; aVB != aVE; ++aVB)
00081 {
00082 typename std::set<_Key, _Compare>::iterator aSB, aSE;
00083 std::set<_Key, _Compare>& aS = aVB->getSet();
00084 aSB = aS.begin();
00085 aSE = aS.end();
00086 for (; aSB != aSE; ++aSB)
00087 {
00088 iEx(*aSB);
00089 }
00090 }
00091 }
00092
00093 }
00094
00095 #endif // __lfeat_sieve_h