bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: HAMT iterator


From: Bruno Haible
Subject: Re: HAMT iterator
Date: Sun, 11 Oct 2020 13:02:12 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-189-generic; KDE/5.18.0; x86_64; ; )

Hi Marc,

> I am going to implement the following iterator API as well:
> 
> /* An alternative interface to iterating through the entry of a hamt
>    that does not make use of a higher-order function like
>    hamt_do_while uses the opaque Hamt_iterator type.  */
> typedef struct hamt_iterator Hamt_iterator;
> 
> /* Return a newly created iterator for HAMT.  */
> extern Hamt_iterator *hamt_iterator_create (const Hamt *hamt);

The pointer return here is overkill. A prototype

  extern Hamt_iterator hamt_iterator_create (const Hamt *hamt);

is sufficient, because the way such an iterator is used is:

  Hamt_iterator iter = hamt_iterator_create (hamt);

  for (...)
    {
      ...
      Hamt_entry *elt;
      if (hamt_iterator_next (&iter, &elt))
        {
          ...
        }
      ...
    }

  hamt_iterator_free (&iter);

> /* Return an independent copy of ITER that is initially in the same
>    state.  */
> extern Hamt_iterator *hamt_iterator_copy (Hamt_iterator *iter);

Then a copy function is not needed, because the user's program can do

  Hamt_iterator iter_clone = iter;

> /* Return true if ITER is not at the end, place the current element in
>    *ELT_PTR and move the iterator forward.  Otherwise, return
>    *false.  */
> extern bool hamt_iterator_next (Hamt_iterator *iter,
>                                 Hamt_entry *const *elt_ptr);

The 'const' here forbids assigning to *ELT_PTR.

Bruno




reply via email to

[Prev in Thread] Current Thread [Next in Thread]