php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #32439 SPL: RecursiveIteratorIterator is not general enough
Submitted: 2005-03-24 02:09 UTC Modified: 2005-03-24 16:34 UTC
From: Peter dot Albertsson at naturesown dot se Assigned: helly (profile)
Status: Wont fix Package: Feature/Change Request
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2005-03-24 02:09 UTC] Peter dot Albertsson at naturesown dot se
Description:
------------
The RecursiveIteratorIterator is not flexible enough when it comes to iterating trees. I also find some modes it operates in unneccasary.

I would like to choose in which directions the iterator iterates through trees:

1. Depth first or
2. Breadth first

combined with one of:

1. Left to right
2. Right to left

As the iterator is implemented today, it lacks the method of iterating breadth first. Instead I'm giving these options:

 - RIT_LEAVES_ONLY: very easy to implement by oneself, by checking if the node is a leaf node.
 - RIT_SELF_FIRST: This one I would consider the default option. but it is not.
 - RIT_CHILD_FIRST: OK.. got to admit that I don't even understand this one...

Most importantly, and I repeat, it lacks the option to iterate breadth first, which is required by many standard algorithms.

Best Regards,

  Peter Albertsson


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-03-24 11:04 UTC] helly@php.net
Directions:
1. Depth first or - we have that
2. Breadth first - that would insult full caching o fthe results which contradicts the ide of Iterators. If you want this do it in userland. For example you can use the function iterator_to_array() that is available in 5.1.

combined with one of:
1. Left to right
2. Right to left

Both require that the thing works on a tree structure - well we don't have a tree structure or object. And even if we had we would provide some tree walker methods and of course won't use the iterator stuff.

If you now you think you need tree's and everythink feel free to check out extension adt and get it fixed.

sorry nothing we can do atm.


 [2005-03-24 15:54 UTC] Peter dot Albertsson at naturesown dot se
I don't see how breadth first would break something that depth first doesnt't. The algorithm for breadth first is of  no higher complexity in time or memory requirements, and it certainly doesn't contradicts the idea of iterators. You say breadth first insults full caching of the results, I'm not sure what you mean and why would it do so? 

I didn't mean that PHP have a tree structure (or need one), but one wouldn't use a RecursiveIteratorIterator on something that doesn't look like a tree, let it be a directory tree or whatever tree structured data that one have.

Well, I already have these things implemented in PHP (if there is an interest, I can publish it somewhere).

The left to right or right to left functionality is best implemented at the RecursiveIterator level so that is what I have done, while depth first or breadth first is implemented at the RecursiveIteratorIterator level.

As a bonus, a depth limit option is available too, that simply prevents the iterator to go deeper than a chosen level.

My skill level in C will not allow me to do any fixes, but I'm working on that part. ;)

So, if anyone is interested in the PHP classes I wrote for iterating tree like structures in any direction, let me know.

Best Regards,

   Peter Albertsson
 [2005-03-24 16:34 UTC] helly@php.net
Sure i'd like to see what you've done.

Coming back to breadth first. The one way of doing this is to first iterate all elements and then again check for childs and iterate them. This 'again' involes one of two solutions. First you could store all elements that have children in an array and second you could rewind the iterator and try again. While the first solution caches all elements with children the second requires a working rewind() which doesn't hold for all iterators. Thus this is nothing we'd want to support in c i guess.

The other thing you mentioned the max depth is already implemented in 5.1-dev.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 10:01:32 2024 UTC