|
| 1 | +#ifndef CSLIB_ALGORITHM_SEARCH_KMP_SEARCH_HPP |
| 2 | +#define CSLIB_ALGORITHM_SEARCH_KMP_SEARCH_HPP |
| 3 | + |
| 4 | +// MIT License |
| 5 | +// |
| 6 | +// Copyright (c) 2018 Alexandr Borzykh |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +// of this software and associated documentation files (the "Software"), to deal |
| 10 | +// in the Software without restriction, including without limitation the rights |
| 11 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +// copies of the Software, and to permit persons to whom the Software is |
| 13 | +// furnished to do so, subject to the following conditions: |
| 14 | +// |
| 15 | +// The above copyright notice and this permission notice shall be included in all |
| 16 | +// copies or substantial portions of the Software. |
| 17 | +// |
| 18 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | +// SOFTWARE. |
| 25 | + |
| 26 | +#include <cslib/data_structure/dynamic_array.hpp> |
| 27 | + |
| 28 | +#include <iterator> |
| 29 | + |
| 30 | +namespace cslib { |
| 31 | +namespace algorithm |
| 32 | +{ |
| 33 | + |
| 34 | + /* Name: Knuth-Morris-Pratt searching algorithm |
| 35 | + * Runtime complexity: O(n) |
| 36 | + * Space complexity: O(m), where m is pattern |
| 37 | + * |
| 38 | + * Interesting fact: it's widely used with strings, but implementation is generic and can work with basically all containers and types (contained type must have operator==) |
| 39 | + */ |
| 40 | + |
| 41 | + template < typename IteratorT > |
| 42 | + IteratorT kmp_search(IteratorT begin, IteratorT end, IteratorT pattern_begin, IteratorT pattern_end) |
| 43 | + { |
| 44 | + using iterator_traits = std::iterator_traits<IteratorT>; |
| 45 | + |
| 46 | + const typename iterator_traits::difference_type source_length = std::distance(begin, end), pattern_length = std::distance(pattern_begin, pattern_end); |
| 47 | + data_structure::dynamic_array<uint32_t> prefix_func(pattern_length, 0); |
| 48 | + |
| 49 | + uint32_t k; |
| 50 | + IteratorT pattern_iter; |
| 51 | + for (k = 0, pattern_iter = pattern_begin; pattern_iter != pattern_end; ++pattern_iter) |
| 52 | + { |
| 53 | + while ((k > 0) && (*pattern_iter != *(pattern_begin + k))) |
| 54 | + k = prefix_func[k - 1]; |
| 55 | + |
| 56 | + if (*pattern_iter == *(pattern_begin + k)) |
| 57 | + ++k; |
| 58 | + |
| 59 | + prefix_func[std::distance(pattern_begin, pattern_iter)] = k; |
| 60 | + } |
| 61 | + |
| 62 | + IteratorT source_iter; |
| 63 | + for (k = 0, source_iter = begin; source_iter != end; ++source_iter) |
| 64 | + { |
| 65 | + while ((k > 0) && (*(pattern_begin + k) != *source_iter)) |
| 66 | + k = prefix_func[k - 1]; |
| 67 | + |
| 68 | + if (*(pattern_begin + k) == *source_iter) |
| 69 | + ++k; |
| 70 | + |
| 71 | + if (k == pattern_length) |
| 72 | + return source_iter - pattern_length + 1; |
| 73 | + } |
| 74 | + |
| 75 | + return end; |
| 76 | + } |
| 77 | + |
| 78 | +}} |
| 79 | + |
| 80 | +#endif |
0 commit comments