<?php

interface FastEnumeration extends IteratorAggregate
{

};

interface FastEnumerator extends Iterator
{

};

define('EnumerationConcurrent', 0);
define('EnumerationReverse', 1);

class Enumerator implements FastEnumerator
{
        private $arr;
        private $reversed;
        
        function __construct(array &$arr, $reversed = 0) {
                $this->reversed  = $reversed;
                $this->arr = $arr;
                if ($this->reversed) {
                        end($this->arr);
                }
        }
        
        public function first() {
                if ($this->reversed) {
                        $cnt = count($this->arr);
                        $idx = $cnt > 1 ? $cnt - 1 : 0;
                } else {
                        $idx = 0;
                }
                return $this->arr[$idx];
        }
        
        public function last() {
                if ($this->reversed) {
                        $idx = 0;
                } else {
                        $cnt = count($this->arr);
                        $idx = $cnt > 1 ? $cnt - 1 : 0;
                }
                return $this->arr[$idx];
        }
        
        public function isfirst() {
                if ($this->reversed) {
                        $cnt = count($this->arr);
                        $idx = $cnt > 1 ? $cnt - 1 : 0;
                } else {
                        $idx = 0;
                }
                return key($this->arr) == $idx;
        }
        
        public function islast() {
                if ($this->reversed) {
                        $idx = 0;
                } else {
                        $cnt = count($this->arr);
                        $idx = $cnt > 1 ? $cnt - 1 : 0;
                }
                return key($this->arr) == $idx;
        }
        
        public function rewind() {
                if ($this->reversed) {
                        end($this->arr);
                } else {
                        reset($this->arr);
                }
        }
        
        public function current() {
                return current($this->arr);
        }
        
        public function key() {
                return key($this->arr);
        }
        
        public function end() {
                if ($this->reversed) {
                        reset($this->arr);
                } else {
                        end($this->arr);
                }
        }


        public function next() {
                if ($this->reversed) {
                        return prev($this->arr);
                } else {
                        return next($this->arr);
                }
        }
        
        public function prev() {
                if ($this->reversed) {
                        return next($this->arr);
                } else {
                        return prev($this->arr);
                }
        }

        public function valid() {
                $current = $this->current();
                return  !empty($current) && $current != null;
        }
};

class MArray implements FastEnumeration
{
        private $arr;
        
        public function getIterator() {
                return new Enumerator($this->arr);
        }

        public function reverseEnumerator() {
                return new Enumerator($this->arr, EnumerationReverse);
        }

        public function concurrentEnumerator() {
                return new Enumerator($this->arr, EnumerationConcurrent);
    }

    // ...
};

/* EOF */ ?>

On Sat, Jan 2, 2010 at 8:47 AM, Oskar Eisemuth <patch...@gmail.com> wrote:
> Hello
>
> Would it be possible to add functions to know the relative internal array
> position?
>
> I found "[PHP-DEV] RFC array functions" from 2006, but nothing really
> changed.
>
> The need to use next, prev in combination is ridiculous compared to a clean
> array_hasmore or array_pos_islast, as the internals already know this.
> To get an array_valid_position or array_pos_isvalid wouldn't be bad either.
>
> So would it possible to introduce:
>
> array_pos_isfirst(&  $array)
> array_pos_islast(&  $array)
> array_pos_isvalid(&  $array)
>
>
> Best regards
> Oskar Eisemuth
>
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to