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

Removing while iterating #20

Closed
ghost opened this issue Jun 15, 2016 · 4 comments
Closed

Removing while iterating #20

ghost opened this issue Jun 15, 2016 · 4 comments

Comments

@ghost
Copy link

ghost commented Jun 15, 2016

New (and awsome) data structures but still php, this should work 😋

<?php
class Foo {
    public $bar;
    public function __construct($bar) {
        $this->bar = $bar;
    }
}

$set = new \Ds\Set;
for ($i = 0; $i<=9; $i++) {
    $set[] = new Foo($i);
}
foreach ($set as $key => $value) {
    echo $value->bar;
}
echo PHP_EOL;
foreach ($set as $key => $value) {
    echo $value->bar;
    $set->remove($value);
}
echo PHP_EOL;
var_dump($set);
0123456789
01234
object(Ds\Set)#1 (5) {
  [0]=>
  object(class@anonymous)#7 (1) {
    ["bar"]=>
    int(5)
  }
  [1]=>
  object(class@anonymous)#8 (1) {
    ["bar"]=>
    int(6)
  }
  [2]=>
  object(class@anonymous)#9 (1) {
    ["bar"]=>
    int(7)
  }
  [3]=>
  object(class@anonymous)#10 (1) {
    ["bar"]=>
    int(8)
  }
  [4]=>
  object(class@anonymous)#11 (1) {
    ["bar"]=>
    int(9)
  }
}
@rtheunissen
Copy link
Member

rtheunissen commented Jun 16, 2016

You are absolutely right - this does not have the same behaviour as the PHP array, which if I understand correctly updates its own internal traversal pointer during unset. I'm still not decided or convinced what to do regarding modification during iteration. Java throws a ConcurrentModificationException, which should be trivial to implement. The other option is to adjust the iterator's position during a remove (not as easy to implement, also adds a small amount of overhead).

I haven't looked at this code in a while (on vacation still) but there will be a solution coming your way. Thanks for bringing this up. 👍

<?php
class Foo {
    public $bar;
    public function __construct($bar) {
        $this->bar = $bar;
    }
}

$set = [];
for ($i = 0; $i<=9; $i++) {
    $set[] = new Foo($i);
}
foreach ($set as $key => $value) {
    echo $value->bar;
}
echo PHP_EOL;
foreach ($set as $value) {
    if(($key = array_search($value, $set)) !== false) {
        unset($set[$key]);
    }
}
echo PHP_EOL;
var_dump($set);
0123456789
0123456789
array(0) {
}

@rtheunissen
Copy link
Member

See #17

@ghost
Copy link
Author

ghost commented Jun 16, 2016

I'm trying to find something fast to write a LINQ php like library, php-ds got me out of programming hell.
Finally i will rewrite it directly in c, benchmark are far of what i expected, php fault.
The php-ds restrictive approach is quite understandable but in this case (#17) i will suggest a php approach, less flexible. Play with the iterator position is not the best but it's php, don't care 😛
Thanks for your hard work 👍

Edit: Quite interesting benchmark

@rtheunissen
Copy link
Member

Closing in favour of #17

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