post
poster: Jaguarstrike
description: PHP infinite arrays (Lazy Generator)
language: PHP
[download]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/*

 The Generator class takes one or two closures as contructor parameters
 Each closure passed to it should take a single param
 
 The first closure takes the key and generates the value
 
 The second closure takes the key and decides if the key should exist or not
 
 After it is instantiated, use it like a normal array

*/
class Generator implements Iterator, ArrayAccess
{
    private $position   = 0;
    private $generator  = NULL;
    private $existCheck = NULL;
    
    public function __construct($generator, $existCheck = NULL)
    {
        $this->generator  = $generator;
        $this->existCheck = $existCheck;
    }
    
    //ArrayAccess methods
    
    public function offsetExists($offset = NULL)
    {
        $offset = ($offset === NULL
        ?
            $this->position
        :
            $offset
        );
        
        $exist = $this->existCheck;
        
        return ($exist === NULL
        ?
            true
        :
            $exist($offset)
        );
    }
    
    public function offsetGet($offset)
    {
        $gen = $this->generator;
        
        if($this->offsetExists( $offset ))
        {
            return $gen($offset);
        }
    }
    
    public function offsetSet($offset, $value)
    {
        throw new Exception('Cannot set values on a generator');
    }
    
    public function offsetUnset($offset)
    {
        throw new Exception('Cannot unset values on a generator');
    }
    
    //Ends ArrayAccess methods
    
    //Iterator methods
    
    public function rewind()
    {
        $this->position = 0;
    }
    
    public function current()
    {
        $gen = $this->generator;
        
        return $gen
        ?
            $gen( $this->position )
        :
            NULL
        ;
    }
    
    public function key()
    {
        return $this->position;
    }
    
    function next()
    {
        ++$this->position;
    }
    
    public function valid()
    {
        return $this->offsetExists();
    }
    
    //End Iterator methods
}

/*
 Square Generator
 
 The first closure passed generates the value for a given key
 
*/

$sqr = new Generator(
    function($i)
    {
        return $i*$i;
    }
);

foreach(range(-5, 5) as $it)
{
    //var_dump( $sqr[$it] );
}

/*
 Fibonacci Generator
 
 The first closure passed generates the value for a given key
 
 The second closure passed makes sure the value is nonnegative
 (a negative would cause infinte recursion)
*/

$fib = new Generator(
    $fibGenFunc = function($i) use(&$fibGenFunc)
    {
        if($i == 0)
        {
            return 0;
        }
        elseif($i == 1)
        {
            return $i;
        }
        else
        {
            return $fibGenFunc($i - 1) + $fibGenFunc($i - 2);
        }
    },
    function($i)
    {
        return $i >= 0;
    }
);

foreach(range(-5, 5) as $it)
{
    //var_dump( $fib[$it] );
}