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

  • SEARCH
  • Home
  • 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 7731843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:34:56+00:00 2026-06-01T06:34:56+00:00

The following code shows what I currently have. It is an adapter for circular

  • 0

The following code shows what I currently have. It is an adapter for
circular data-structures. The main function shows how it is used. This
is all nice and fast but I really would like to have iterators over
the structure defined by circ. All approaches up till now involved
either some counting scheme (count the range if with the circulator,
build an iterator that counts increments and decrements) or boolean
values to check if the iterator has been moved to avoid begin and end
being equal.

Are there some common solutions to adapt a circular structure to
iterators? What other work-arounds are possible?

I would like to preserve the iteration speed as much as possible but
am willing to make compromises here. I would value a fully conforming
iterator over a minor speed penalty.

#include <cstddef> // nullptr
#include <iostream>
#include <boost/noncopyable.hpp>
#include <boost/operators.hpp>

// circular structure that we want to iterate over
struct circ : private boost::noncopyable {
  unsigned int i;
  circ* next;
  circ* prev;
};

// whacked up circulator, imagine some template cream here to make it
// generic, omitted to preserve sanity
struct circulator
  : public boost::incrementable<circulator>
  , public boost::decrementable<circulator>
  , public boost::equality_comparable<circulator, circulator>
  , public boost::dereferenceable<circulator, circ*>
{
  circulator()
    : c(nullptr) {}
  circulator(circ& c) : c(&c) {}

  bool operator==(const circulator& other) const {
    return this->c == other.c;
  }

  circulator& operator++() { c = c->next; return *this; }
  circulator& operator--() { c = c->prev; return *this; }

  explicit operator bool() const { return c; }

  circ& operator*() const { return *c; }

  circ* c;
};

int main()
{
  circ a, b, c, d;
  a.next = &b; a.prev = &a; a.i = 0;
  b.next = &c; b.prev = &a; b.i = 1;
  c.next = &d; c.prev = &b; c.i = 2;
  d.next = &a; d.prev = &c; d.i = 3;
  circulator begin{a}, end{a};
  if(begin)
    do {
      std::cout << begin->i << std::endl;
      begin = ++begin;
    } while(begin != end);

  return 0;
}

If need be I can add some of my previous approaches, but they are
rather verbose and would add unnecessary bloat to the question.

EDIT: It would be nice if the resulting iterator would be bidirectional. Although I can give up this requirement.

  • 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-01T06:34:57+00:00Added an answer on June 1, 2026 at 6:34 am

    If it were me, I’d have operator++ notice the terminal condition, and set c to some sentinal value:

    circulator(circ& c) : c(&c), start(&c) {}
    circulator& operator++() { c = c->next; if(c==start) c=nullptr; return *this; }
    

    Usage:

    circulator begin{a}, end;
    while(begin != end) {
      begin++;
    }
    

    Note that this usage defines the end iterator as holding a nullptr, which means that you can’t do this:

    circulator end;
    --end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that shows either a bug or a misunderstanding on
Consider the following code which shows compile time error : #include <stdio.h> int main(int
I currently have the following code to show a countdown to a specific date:
Currently I have the following code: Tag tag = getParent(); while(tag != null) {
I have the following code snippet that shows a list of numbers, and highlights
The following code shows a button that allows you to select a file (should
The following code shows 1 view, 1 partial view, and 1 controller . The
The following piece of code shows an Insert table dialog: Dialog d = WordApp.Dialogs[WdWordDialog.wdDialogTableInsertTable];
In the following code, the first log statement shows a decimal as expected, but
i have following code to show div on page <div id=all_users> <div id=user11 userid=11

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.