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

Add a way to iterate results one by one #32

Open
trikko opened this issue Sep 26, 2019 · 1 comment
Open

Add a way to iterate results one by one #32

trikko opened this issue Sep 26, 2019 · 1 comment

Comments

@trikko
Copy link
Contributor

trikko commented Sep 26, 2019

Currently functions like lxb_dom_elements_by_class_name iterates the whole dom using lxb_dom_node_simple_walk. In many cases this could be a suboptimal solution.

For example:

<div class="a"></div>
<div class="a"></div>
<div class="a">test</div>
<div class="a"></div>
... many more ...

If i need to find the first non-empty div, I could stop the search after the third result rather than read all the other.

This could be achived probably returning a range/iterator like this:

struct lxb_dom_range_t
{
  bool empty;
  lxb_dom_element_t* front;
  void (*lxb_dom_range_next)(lxb_dom_element_t* front); 
}

or something similar.

Of course this can be easily converted to a plain array looping thru it.

@trikko
Copy link
Contributor Author

trikko commented Mar 13, 2022

An iterator could be done with some little editing.

lxb_dom_node_simple_walk() starts always from the root!

It would be useful to add a param (you can replace with null everywhere in the current code) that tells from which node the walk will start with.

If this params is not null we just skip line 403-406 and move over to the next node.

In this way we can do something like:

lxb_dom_elements_by_class_name_iterator(root, node, classname, len)

so:

   node = 0;
   while(true)
   {
      node = lxb_dom_elements_by_class_name_iterator(root, node, "a", 1);
      
      if (node == 0) break;
      else {
      // I do my check on node
      // if (the_div_is_not_empty) break;
      }
      
   }

In this way:

  • Collection is not needed
  • No extra allocation for collections!
  • I browse only the nodes I need
  • I can implement many function (limit, skip, restarting a search and so on)
  • I can mixing iterator together (f.e. coupling two interator in the same while-loop)

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

1 participant