http://hg.python.org/cpython/file/6b6c79eba944/Objects/stringlib/fastsearch.h Changes include using our own Bloom implementation, Impala's native StringValue string type, and removing other search modes (e.g. FAST_COUNT).
| 38 | /// Changes include using our own Bloom implementation, Impala's native StringValue string |
| 39 | /// type, and removing other search modes (e.g. FAST_COUNT). |
| 40 | class StringSearch { |
| 41 | |
| 42 | public: |
| 43 | StringSearch() : pattern_(NULL), mask_(0) {} |
| 44 | |
| 45 | /// Initialize/Precompute a StringSearch object from the pattern |
| 46 | StringSearch(const StringValue* pattern) |
| 47 | : pattern_(pattern), mask_(0), skip_(0), rskip_(0) { |
| 48 | StringValue::SimpleString pattern_s = pattern->ToSimpleString(); |
| 49 | // Special cases |
| 50 | if (pattern_s.len <= 1) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | // Build compressed lookup table |
| 55 | int mlast = pattern_s.len - 1; |
| 56 | skip_ = mlast - 1; |
| 57 | rskip_ = mlast - 1; |
| 58 | |
| 59 | // In the Python implementation, building the bloom filter happens at the |
| 60 | // beginning of the Search operation. We build it during construction |
| 61 | // instead, so that the same StringSearch instance can be reused multiple |
| 62 | // times without rebuilding the bloom filter every time. |
| 63 | for (int i = 0; i < mlast; ++i) { |
| 64 | BloomAdd(pattern_s.ptr[i]); |
| 65 | if (pattern_s.ptr[i] == pattern_s.ptr[mlast]) |
| 66 | skip_ = mlast - i - 1; |
| 67 | } |
| 68 | BloomAdd(pattern_s.ptr[mlast]); |
| 69 | |
| 70 | // The order of iteration doesn't have any effect on the bloom filter, but |
| 71 | // it does on skip_. For this reason we need to calculate a separate rskip_ |
| 72 | // for reverse search. |
| 73 | for (int i = mlast; i > 0; i--) { |
| 74 | if (pattern_s.ptr[i] == pattern_s.ptr[0]) rskip_ = i - 1; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// Search for this pattern in str. |
| 79 | /// Returns the offset into str if the pattern exists |
| 80 | /// Returns -1 if the pattern is not found |
| 81 | int Search(const StringValue* str) const { |
| 82 | // Special cases |
| 83 | if (str == NULL || pattern_ == NULL || pattern_->Len() == 0) { |
| 84 | return -1; |
| 85 | } |
| 86 | StringValue::SimpleString pattern_s = pattern_->ToSimpleString(); |
| 87 | StringValue::SimpleString str_s = str->ToSimpleString(); |
| 88 | |
| 89 | int mlast = pattern_s.len - 1; |
| 90 | int w = str_s.len - pattern_s.len; |
| 91 | int n = str_s.len; |
| 92 | int m = pattern_s.len; |
| 93 | const char* s = str_s.ptr; |
| 94 | const char* p = pattern_s.ptr; |
| 95 | |
| 96 | // Special case if pattern->len == 1 |
| 97 | if (m == 1) { |
no outgoing calls
no test coverage detected