【问题标题】:Implementation of `continue` keyword inside iterator pattern在迭代器模式中实现`continue`关键字
【发布时间】:2021-09-12 02:25:33
【问题描述】:

我发现continue 功能只能通过递归在迭代器模式中实现。 问题当然是循环数百万个应继续的无效元素应该非常慢或堆栈溢出。

如何在不使用迭代器模式内递归的情况下实现continue关键字?

带有递归的 PHP 代码:

class TableIterator implements \Iterator
{
    private $row;
    private $fileLines;

    public function __construct()
    {
        $this->fileLines = [
            "1, 2, 3, 4, 5, 6, 7, 8",
            "my condition, 9, 10, 11, 12, 13, 14, 15",
            "my condition, 16, 17, 18, 19, 20, 21, 22, 23",
            "24, 25, 26, 27, 28, 29, 30, 31",
            "32, 33, 34, 35, 36, 37, my condition 2, 38",
            "39, 40, 41, 42, 43, 44, 45, 46"
        ];
    }

    public function rewind(): void
    {
        reset($this->fileLines);
    }

    public function current(): array
    {
        return $this->row;
    }

    public function key(): int
    {
        return key($this->fileLines);
    }

    public function next(): void
    {
        next($this->fileLines);
    }

    public function valid(): bool
    {
        $this->row = explode(", ", current($this->fileLines));

        if($this->row[0] === 'my condition')
            $this->continue(); // here is the recursion problem
        
        if($this->row[6] === 'my condition 2')
            return false;

        return true;
    }
    
    private function continue()
    {
        $this->next();
        $this->valid();
    }
}

foreach(new TableIterator() as $key => $value)
    print_r($value);

如果有 PHP 的解决方案或 hack,我会很高兴。 谢谢。

【问题讨论】:

    标签: php iterator continue


    【解决方案1】:

    我想出了一些解决方法,但它仍然不是理想的解决方案!

    问题可能是是否有任何解决方案?

    if($value === null) continue; 继续迭代。 评论它以获取空值并且仍然能够获取密钥。问题可能出在空值是预期值时!

    class TableIterator implements \Iterator
    {
        private $row;
        private $fileLines;
        private $continue;
    
        public function __construct()
        {
            $this->fileLines = [
                "1, 2, 3, 4, 5, 6, 7, 8",
                "my condition, 9, 10, 11, 12, 13, 14, 15",
                "my condition, 16, 17, 18, 19, 20, 21, 22, 23",
                "24, 25, 26, 27, 28, 29, 30, 31",
                "32, 33, 34, 35, 36, 37, my condition 2, 38",
                "39, 40, 41, 42, 43, 44, 45, 46"
            ];
        }
    
        public function rewind(): void
        {
            reset($this->fileLines);
        }
    
        public function current(): ?array
        {
            if($this->continue)
            {
                $this->continue = false;
                return null;
            }
            
            return $this->row;
        }
    
        public function key(): int
        {
            return key($this->fileLines);
        }
    
        public function next(): void
        {
            next($this->fileLines);
        }
    
        public function valid(): bool
        {
            $this->row = explode(", ", current($this->fileLines));
    
            if($this->row[0] === 'my condition')
                $this->continue = true;
            
            if($this->row[6] === 'my condition 2')
                return false;
    
            return true;
        }
    }
    
    foreach(new TableIterator() as $key => $value)
    {
        if($value === null) continue;
        var_dump($value);
    }
    

    【讨论】:

      【解决方案2】:

      这就是解决方案!

      我不会在 2021 年费心去解释它。 如果你好奇,可以爬取整个算法。

      class TableIterator implements \Iterator
      {
          private $row;
          private $fileLines;
      
          public function __construct()
          {
              $this->fileLines = [
                  "my condition, 16, 17, 18, 19, 20, 21, 22, 23",
                  "my condition, 16, 17, 18, 19, 20, 21, 22, 23",
                  "my condition, 16, 17, 18, 19, 20, 21, 22, 23",
                  "1, 2, 3, 4, 5, 6, 7, 8",
                  "my condition, 9, 10, 11, 12, 13, 14, 15",
                  "my condition, 16, 17, 18, 19, 20, 21, 22, 23",
                  "24, 25, 26, 27, 28, 29, 30, 31",
                  "32, 33, 34, 35, 36, 37, my condition 2, 38",
                  "39, 40, 41, 42, 43, 44, 45, 46"
              ];
          }
      
          public function rewind(): void
          {
              reset($this->fileLines);
              
              while($this->continue())
              {
                  next($this->fileLines);
              }
          }
      
          public function current(): array
          {
              return $this->row;
          }
      
          public function key(): int
          {
              return key($this->fileLines);
          }
      
          public function next(): void
          {
              do {
                  next($this->fileLines);
              }
              while($this->continue());
          }
      
          public function valid(): bool
          {
              if($this->row[6] === 'my condition 2')
                  return false;
      
              return true;
          }
          
          private function continue()
          {
              $this->row = explode(", ", current($this->fileLines));
              
              return $this->row[0] === 'my condition' ? true : false;
          }
      }
      
      foreach(new TableIterator() as $key => $value)
      {
          var_dump($value);
      }
      

      【讨论】:

        猜你喜欢
        • 2021-01-19
        • 2020-09-09
        • 1970-01-01
        • 2020-08-13
        • 1970-01-01
        • 1970-01-01
        • 2014-08-18
        • 1970-01-01
        • 2020-11-09
        相关资源
        最近更新 更多