]> granicus.if.org Git - php/commitdiff
Add shortcut interface SeekableIterator to LimitIterator
authorMarcus Boerger <helly@php.net>
Tue, 11 Nov 2003 18:31:50 +0000 (18:31 +0000)
committerMarcus Boerger <helly@php.net>
Tue, 11 Nov 2003 18:31:50 +0000 (18:31 +0000)
ext/spl/examples/limititerator.inc
ext/spl/examples/seekableiterator.inc [new file with mode: 0755]

index c1a19fedd9de7054f4e1025c9aea757e424539df..cdc71e98ce61264149ed7ce211dcab28b80d97df 100755 (executable)
@@ -21,8 +21,12 @@ class LimitIterator implements Iterator
        {
                $this->it->rewind();
                $this->index = 0;
-               while($this->index < $this->offset && $this->it->hasMore()) {
-                       $this->next();
+               if (is_a($this->it, 'SeekableIterator')) {
+                       $this->index = $this->it->seek($this->offset);
+               } else {
+                       while($this->index < $this->offset && $this->it->hasMore()) {
+                               $this->next();
+                       }
                }
        }
        
diff --git a/ext/spl/examples/seekableiterator.inc b/ext/spl/examples/seekableiterator.inc
new file mode 100755 (executable)
index 0000000..157d273
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+
+class SeekableIterator implements Iterator
+{
+       function seek($index) {
+               $this->rewind();
+               $position = 0;
+               while($position < $index && $this->it->hasMore()) {
+                       $this->next();
+                       $position++;
+               }
+               return $position;
+       }
+}
+
+?>
\ No newline at end of file