TrinityCore
FuzzyFind.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITY_FUZZYFIND_H
19#define TRINITY_FUZZYFIND_H
20
21#include <map>
22#include <string>
23#include <type_traits>
24
25namespace Trinity
26{
27 namespace Containers
28 {
29 template <typename Container, typename NeedleContainer, typename ContainsOperator = bool(std::string const&, std::string const&), typename T = void>
30 auto FuzzyFindIn(Container const& container, NeedleContainer const& needles, ContainsOperator const& contains = StringContainsStringI, int(*bonus)(decltype((*std::begin(std::declval<Container>())))) = nullptr)
31 {
32 using IteratorResult = decltype((*std::begin(container)));
33 using MappedType = std::conditional_t<std::is_reference_v<IteratorResult>, std::reference_wrapper<std::remove_reference_t<IteratorResult>>, IteratorResult>;
34 std::multimap<size_t, MappedType, std::greater<size_t>> results;
35
36 for (auto outerIt = std::begin(container), outerEnd = std::end(container); outerIt != outerEnd; ++outerIt)
37 {
38 size_t count = 0;
39 for (auto innerIt = std::begin(needles), innerEnd = std::end(needles); innerIt != innerEnd; ++innerIt)
40 if (contains(*outerIt, *innerIt))
41 ++count;
42
43 if (!count)
44 continue;
45
46 if (bonus)
47 count += bonus(*outerIt);
48
49 results.emplace(count, *outerIt);
50 }
51
52 return results;
53 }
54 }
55}
56
57#endif
bool StringContainsStringI(std::string_view haystack, std::string_view needle)
Definition: Util.cpp:896
auto FuzzyFindIn(Container const &container, NeedleContainer const &needles, ContainsOperator const &contains=StringContainsStringI, int(*bonus)(decltype((*std::begin(std::declval< Container >()))))=nullptr)
Definition: FuzzyFind.h:30