Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How should I iterate over all elements in a list? #2

Closed
debug-ito opened this issue Feb 3, 2014 · 4 comments
Closed

How should I iterate over all elements in a list? #2

debug-ito opened this issue Feb 3, 2014 · 4 comments

Comments

@debug-ito
Copy link

I have trouble iterating over ALL elements in the list.

Currently, I can think of two ways:

  • Use size() method to know how many times I should iterate. Even if size() runs in constant time, the code would look ugly.
  • Catch an exception from next() or previous() method of Iterator. But there is no documentation about the exception, so I don't want to rely on that.

I suggest adding is_first() and is_last() methods to Iterator. That way, we can iterate the whole list safely.

@Leont
Copy link
Owner

Leont commented Feb 3, 2014

You're supposed to use begin and end for that, but that part is rather underdeveloped. I remembered I had this part working, but after not having touched this code for 3 years it seems I was mistaken in thinking that. Must fix that though.

is_first and is_last might work for that, will have to think about it.

@debug-ito
Copy link
Author

Thanks for the reply.

I think current iterator would also be problematic if the list is empty. Maybe this is another issue.

Anyway, I'm changing my mind to use arrays for my problem instead of List::DoubleLinked. So if you don't have time, just ignore this issue :)

@Leont
Copy link
Owner

Leont commented Aug 10, 2016

In 0.004, this should work conceptually the same as in C++. that is:

for (my $iter = $list->begin; $iter != $list->end; $iter = $iter->next) {
  do_something($iter->value)
}

@debug-ito
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants