简体   繁体   中英

Multithreaded idiomatic find first of substrings in a string using modern C++

It is easy to find a string in a set of strings using set::find or first of a set of strings in a set of strings using std::find_first_of. But I think that STL doesn't handle this case of find_first_of set of strings (substrings) in a string. For low latency reason I use parallel execution, would you please let me know if this implementation is idiomatic using modern C++ :

#include <string>
#include <list>
#include <atomic>
#include <execution>
#include <iostream>

class Intent{
    const std::list<std::string> m_Context;
    const std::string m_Name;
    std::atomic_bool m_Found;
public:
    Intent(const std::list<std::string> context, const std::string name)
        : m_Context(context)
        , m_Name(name)
        , m_Found(false)
        {}
    Intent(const Intent & intent) = delete;
    Intent & operator=(const Intent & intent) = delete;
    Intent(Intent && intent) : m_Context(std::move(intent.m_Context))
                    , m_Name(std::move(intent.m_Name))
                    , m_Found(static_cast< bool >(intent.m_Found))
                    {}

    bool find(const std::string & sentence)
    {
        for_each( std::execution::par
                , std::begin(m_Context)
                , std::end(m_Context)
                , [& m_Found = m_Found, & sentence](const std::string & context_element){
                    //
                    // May be after lauching thread per context_element one of them make intent Found
                    // so no need to run string::find in the remaining threads.
                    //
                    if(!m_Found){ 
                        if(sentence.find(context_element) != std::string::npos)
                        {
                            m_Found = true;
                        }
                    }
                }
            );
        return m_Found;
    }
    const bool getFound() const {return m_Found;}
    const std::string & getName() const {return m_Name;}
};

int main()
{
    Intent intent({"hello", "Hi", "Good morning"}, "GREETING");
    std::cout << intent.find("Hi my friend.");
}

I think the idiomatic way of doing it would be to use std::find_if . Then you don't need the atomic<bool> either.

// return iterator to found element or end()
auto find(const std::string & sentence)
{
    return std::find_if( std::execution::par
            , std::begin(m_Context)
            , std::end(m_Context)
            , [&sentence](const std::string & context_element) {
                return sentence.find(context_element) != std::string::npos;
            }
        );
}

If you really only want a bool :

bool find(const std::string & sentence)
{
    return std::find_if( std::execution::par
            , std::begin(m_Context)
            , std::end(m_Context)
            , [&sentence](const std::string & context_element) {
                return sentence.find(context_element) != std::string::npos;
            }
        ) != std::end(m_Context);
}

You may want to consider using a std::vector instead of a std::list too. vector s provide random access iterators while list s only provide bidirectional iterators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM