# C Level Iterators and Userland Classes

- Thread: https://externals.io/message/21872
- Original archive: https://news-web.php.net/php.internals/21872
- Started by l0t3k on 2006-02-15T17:23:44+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #21872 — l0t3k — 2006-02-15T17:23:44+00:00

URL: https://externals.io/message/21872#21872

A quick question about using iterators in C code:

If it sufficient to use the interface gotten from 
zend_class_entry->get_iterator to handle userland classes  which implement 
Iterator ?
The attachment is part of code i'm using to do Unicode based collation. Will 
this work for userland iterators as well ?

l0t3k

---

## #21873 — Marcus Boerger — 2006-02-15T21:50:41+00:00

URL: https://externals.io/message/21872#21873 · In reply to: #21872

Hello l0t3k,

  first of all your question does not make any sense to me whatsoever.

Anyway looking at the code you seem to be using a c-level iterator
manually instead of using it inside foreach() construct.

The first thing i can see is that you use the iterator index while the
source code (zend_interface.h) says:
    ulong index; /* private to fe_reset/fe_fetch opcodes */
Unil now noone tried to emulate this stuff so i am not sure whether this
is correct or not. But one thing i can say is that your usage revealed
an error in the engien code. Index is incremented to early. Which is fixed
by now.

The second thing is that you did not use the dtor() handler so you have
a memory leak.

Otherwise i cannot see anything wrong with your iterator usage. And the
more i think over it you are probably right in using that index.

To try to answer what i think you want to know: What ever object you use.
If it implements Traversable or derived its zend_class_entry has a
get_iterator function that gets you access to an iteator struct and the
handler table. You should never touch the data member of that iterator
struct and the handlers are documented. In your case there is no need to
use handler invalidate_current().

hope this helps
marcus

Wednesday, February 15, 2006, 6:23:44 PM, you wrote:

> A quick question about using iterators in C code:

> If it sufficient to use the interface gotten from 
> zend_class_entry->get_iterator to handle userland classes  which implement 
> Iterator ?
> The attachment is part of code i'm using to do Unicode based collation. Will
> this work for userland iterators as well ?

> l0t3k 

Best regards,
 Marcus

---

## #21874 — l0t3k — 2006-02-15T22:28:15+00:00

URL: https://externals.io/message/21872#21874 · In reply to: #21873

"Marcus Boerger" <helly@php.net> wrote in message 
news:1355657626.20060215225041@marcus-boerger.de...
> Hello l0t3k,
>
>  first of all your question does not make any sense to me whatsoever.
What im trying to allow for is sorting by iterator as well as standard 
arrays, for example :
// assume en_US as default locale

$coll = new Collator("en_US");

$iter = new TextIterator("The rain in Spain drop gently on the Plains", 
TextIterator::WORD);

var_dump( $coll->sort($iter));    // returns the sorted list of words

$expr = new Regex($patttern, $input, $flags);
var_dump($coll->sort($expr, SORT_DESC));    // Sorts matches in descending 
order

> Anyway looking at the code you seem to be using a c-level iterator
> manually instead of using it inside foreach() construct.
see above...

> The first thing i can see is that you use the iterator index while the
> source code (zend_interface.h) says:
>    ulong index; /* private to fe_reset/fe_fetch opcodes */
> Unil now noone tried to emulate this stuff so i am not sure whether this
> is correct or not.
i wrote this more than ayear ago and i remember copying it from engine code.

> The second thing is that you did not use the dtor() handler so you have
> a memory leak.
which dtor handler ?

> To try to answer what i think you want to know: What ever object you use.
> If it implements Traversable or derived its zend_class_entry has a
> get_iterator function that gets you access to an iteator struct and the
> handler table. You should never touch the data member of that iterator
> struct and the handlers are documented. In your case there is no need to
> use handler invalidate_current().

That helps. Thanks a lot....

clayton

> hope this helps
> marcus
>
> Wednesday, February 15, 2006, 6:23:44 PM, you wrote:
>
> > […]
>
> > […]
>
> > […]
>
> Best regards,
> Marcus

---

## #21875 — Marcus Boerger — 2006-02-15T22:36:05+00:00

URL: https://externals.io/message/21872#21875 · In reply to: #21874

Hello l0t3k, Anrei,

Wednesday, February 15, 2006, 11:28:15 PM, you wrote:

> "Marcus Boerger" <helly@php.net> wrote in message 
> news:1355657626.20060215225041@marcus-boerger.de...
> > […]
> What im trying to allow for is sorting by iterator as well as standard 
> arrays, for example :
> // assume en_US as default locale

> $coll = new Collator("en_US");

> $iter = new TextIterator("The rain in Spain drop gently on the Plains", 
> TextIterator::WORD);

> var_dump( $coll->sort($iter));    // returns the sorted list of words

> $expr = new Regex($patttern, $input, $flags);
> var_dump($coll->sort($expr, SORT_DESC));    // Sorts matches in descending 
> order

> > […]
> see above...

ic, sounds interesting. Actually Andrei doesn't it look like something for
ext/unicode?

> > […]
> i wrote this more than ayear ago and i remember copying it from engine code.

> > […]
> which dtor handler ?

Zend/zend_iterators.h:
typedef struct _zend_object_iterator_funcs {
        /* release all resources associated with this iterator instance */
        void (*dtor)(zend_object_iterator *iter TSRMLS_DC);

^^^that one :-)

> > […]

> That helps. Thanks a lot....

> clayton

> > […]






Best regards,
 Marcus

---

## #21876 — l0t3k — 2006-02-15T22:45:13+00:00

URL: https://externals.io/message/21872#21876 · In reply to: #21875

> ic, sounds interesting. Actually Andrei doesn't it look like something for
> ext/unicode?
that's what it's for, actually...

> > […]
>
> Zend/zend_iterators.h:
> typedef struct _zend_object_iterator_funcs {
>        /* release all resources associated with this iterator instance */
>        void (*dtor)(zend_object_iterator *iter TSRMLS_DC);
>
> ^^^that one :-)

Solid ! Thanks again..

clayton



>
>
>
>
>
> Best regards,
> Marcus

---

## #21890 — l0t3k — 2006-02-17T00:09:37+00:00

URL: https://externals.io/message/21872#21890 · In reply to: #21873

Marcus,
 one last question ...

> You should never touch the data member of that iterator

does this mean that i should make a copy of whatever i get from
the get_current_data() handler ? There seems to be a bit of inconsistency
in dealing with the current element in the iterators For example, the 
TextIterator
reuses the same zval value, whereas the classes in SPL call zval_ptr_dtor()
between iterations.

clayton

---

## #21897 — Marcus Boerger — 2006-02-17T08:18:51+00:00

URL: https://externals.io/message/21872#21897 · In reply to: #21890

Hello l0t3k,

  in SPL the iterator classes can be overloaded in TextIterator not.
Foreach does what is necccessary and especially if inside foreach()
someone uses the value it is being copied automatically. Since you do
it all yourself you also need to do the copying yourself. So there is
no inconsistency, just diiferent usage.

regards
marcus

Friday, February 17, 2006, 1:09:37 AM, you wrote:

> Marcus,
>  one last question ...

> > […]

> does this mean that i should make a copy of whatever i get from
> the get_current_data() handler ? There seems to be a bit of inconsistency
> in dealing with the current element in the iterators For example, the 
> TextIterator
> reuses the same zval value, whereas the classes in SPL call zval_ptr_dtor()
> between iterations.

> clayton




Best regards,
 Marcus
