001:
002:
003: #ifndef _ExtractLocalLookup_HH
004: #define _ExtractLocalLookup_HH 1
005:
006: #include <cstdio>
007: #include <functional>
008: #include <algorithm>
009: #include <utility>
010:
011: #include "RZMagic.hh"
012: #include "ExtractIntermediate.hh"
013:
014: namespace RZ {
015: struct LocalSums: Sums {
016: unsigned int block_size;
017: LocalSums(struct RZWrapper const &);
018: LocalSums(struct IntermediateWrapper const &);
019: };
020:
021: struct LookupTable;
022: }
023:
024:
025:
026:
027:
028:
029:
030:
031:
032:
033:
034:
035:
036:
037:
038:
039:
040:
041: struct RZ::LookupTable {
042: struct LookupEntry { unsigned int sig_a, sig_b, sig_c, bk_i; };
043:
044: typedef std::vector< LookupEntry > Lookup;
045: typedef std::pair< Lookup::iterator, Lookup::iterator > Range;
046:
047: private:
048: struct LookupLess {
049: bool operator()(unsigned int c, LookupEntry b) { return c < b.sig_c; }
050: bool operator()(LookupEntry a, unsigned int c) { return a.sig_c < c; }
051: };
052: struct EntryLess: std::binary_function< LookupEntry, LookupEntry, bool > {
053: bool operator()(LookupEntry a, LookupEntry b)
054: {
055: return a.sig_c < b.sig_c;
056: }
057: };
058: struct LowerBound: std::unary_function< LookupEntry, bool > {
059: unsigned int lower_bound;
060: bool operator()(LookupEntry b) { return lower_bound <= b.sig_c; }
061: LowerBound(unsigned int l): lower_bound(l) {}
062: };
063:
064:
065:
066: private:
067: Lookup lookup_table;
068:
069:
070:
071: unsigned int sig_bit_shift;
072: std::vector< Lookup::iterator > fast_lookup_hash_table;
073:
074: public:
075: inline Range fast_range(unsigned int sigc)
076: {
077: unsigned int hash = sigc >> sig_bit_shift;
078: return std::equal_range(
079: fast_lookup_hash_table[hash], fast_lookup_hash_table[hash+1],
080: sigc, LookupLess());
081: }
082: inline Range slow_range(unsigned int sigc)
083: {
084: return std::equal_range(
085: lookup_table.begin(), lookup_table.end(), sigc, LookupLess());
086: }
087:
088: private:
089: inline void setup_fast_lookup()
090: {
091: sig_bit_shift = 24;
092: for (unsigned int n = lookup_table.size(); n > 256; n >>= 1)
093: --sig_bit_shift;
094:
095:
096:
097: {
098: Lookup::iterator b = lookup_table.begin(), e = lookup_table.end();
099: {
100: unsigned int cut = 0, step = 1 << sig_bit_shift;
101: do {
102: b = std::find_if(b, e, LowerBound(cut));
103: fast_lookup_hash_table.push_back(b);
104: } while (cut += step, cut - step < cut);
105: }
106: fast_lookup_hash_table.push_back(e);
107:
108:
109:
110:
111: }
112: }
113:
114:
115:
116: public:
117: LookupTable() {}
118: LookupTable(RZSignatureTable::Signature const &sigtab)
119: {
120: extract_lookup_table(sigtab);
121: std::sort(lookup_table.begin(), lookup_table.end(), EntryLess());
122: setup_fast_lookup();
123: }
124: LookupTable(unsigned int const nblks, std::FILE * const RZWRAPPER)
125: {
126: extract_lookup_table(nblks, RZWRAPPER);
127: std::sort(lookup_table.begin(), lookup_table.end(), EntryLess());
128: setup_fast_lookup();
129: }
130:
131: private:
132: void extract_lookup_table(RZSignatureTable::Signature const &);
133: void extract_lookup_table(unsigned int, std::FILE *);
134: };
135:
136:
137:
138: #endif