Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8887787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:53:43+00:00 2026-06-14T21:53:43+00:00

I have a while (!Queue.empty()) loop that processes a queue of elements. There are

  • 0

I have a while (!Queue.empty()) loop that processes a queue of elements. There are a series of pattern matchers going from highest-priority to lowest-priority order. When a pattern is matched, the corresponding element is removed from the queue, and matching is restarted from the top (so that the highest-priority matchers get a chance to act first).

So right now it looks something like this (a simplified version):

while (!Queue.empty())
{
    auto & Element = *Queue.begin();

    if (MatchesPatternA(Element)) {    // Highest priority, since it's first
        // Act on it
        // Remove Element from queue
        continue;
    }
    if (MatchesPatternB(Element)) {
        // Act on it
        // Remove Element from queue
        continue;
    }
    if (MatchesPatternC(Element)) {    // Lowest priority, since it's last
        // Act on it
        // Remove Element from queue
        continue;
    }

    // If we got this far, that means no pattern was matched, so
    // Remove Element from queue
}

This works, but I want to refactor this loop in some way to remove the use of the keyword continue.

Why? Because if I want to outsource a pattern matching to an external function, it obviously breaks. E.g.

void ExternalMatching(...)
{
    if (MatchesPatternB(Element)) {
        // Act on it
        // Remove Element from queue
        continue;     // This won't work here
    }
}

while (!Queue.empty())
{
    auto & Element = *Queue.begin();

    if (MatchesPatternA(Element)) {
        // Act on it
        // Remove Element from queue
        continue;
    }
    ExternalMatching(...);
    if (MatchesPatternC(Element)) {
        // Act on it
        // Remove Element from queue
        continue;
    }

    // If we got this far, that means no pattern was matched, so
    // Remove Element from queue
}

I don’t want to have to write repetitive if statements like if (ExternalMatching(...)) { ... continue; }, I’d rather find a cleaner way to express this logic.

This simplified example might make it seem like a good idea to make pattern matching more general rather than having distinct MatchesPatternA, MatchesPatternB, MatchesPatternC, etc. functions. But in my situation the patterns are quite complicated, and I’m not quite ready to generalize them yet. So I want to keep that part as is, separate functions.

Any elegant ideas? Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:53:44+00:00Added an answer on June 14, 2026 at 9:53 pm

    If you have access to C++11 I would like to suggest another solution. Basicaly I created a container of handlers and actions that can be adjusted in runtime. It may be a pro or con for your design depending on your requirements. Here it is:

    #include <functional>
    
    typedef std::pair<std::function<bool(const ElementType &)>,
                      std::function<void(ElementType &)> > HandlerData;
    typedef std::vector<HandlerData> HandlerList;
    
    
    HandlerList get_handlers()
    {
      HandlerList handlers;
      handlers.emplace_back([](const ElementType &el){ return MatchesPatternA(el); },
                            [](ElementType &el){ /* Action */ });
      handlers.emplace_back([](const ElementType &el){ return MatchesPatternB(el); },
                            [](ElementType &el){ /* Action */ });
      handlers.emplace_back([](const ElementType &el){ return MatchesPatternC(el); },
                            [](ElementType &el){ /* Action */ });
      return handlers;
    }
    
    
    int main()
    {
      auto handlers = get_handlers();
      while(!Queue.empty()) {
        auto &Element = *Queue.begin();
    
        for(auto &h : handlers) {
          // check if handler matches the element
          if(h.first(Element)) {
            // act on element
            h.second(Element);
            break;
          }
        }
    
        // remove element
        Queue.pop_front();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The while loop I have while reading in from a file doesn't break. I'm
I have a while loop that goes while a BuffedReader still has data, what
I have a while loop that loops through 3 results and echo's these out
So with pygame you have a while loop that loops continuously, then your event
Working in Python. I have a function that reads from a queue and creates
I have a task that would benefit from the Thread Pool design pattern (many
I have given client's queue connection details to connect to it using Camel. While
hi i have a while loop: var i = 0; while(i < 20) {
I have a blocking queue of objects. I want to write a thread that
I have tried to create a concurrent queue class, in a producer-consumer pattern. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.