It is because when putting the key value in array using string (first, second, ...). it is because you trying to remove (unset) the map by using the value from the items. Since you insert map using (first, second, ...), when the capacity is full, you array pop from the items, which is it save the value (1,2,..) so when unsetting the map, it is not work. cause the map[1] it is not found. cause it save the map[first, second]
very nice and informative. I really miss such topics with php implementation
Thank you very much! More topics such as this coming.
I'd like to pay attention for two not important but significant things.
First of all, this expression ($this->length >= $this->capacity) shouldn't be like that, I suppose,
$this->length === $this->capacity is more true, because length > capacity never happen
Secondly, I noticed some problem if we do these sequence
action and how LS looks like 👇
1 step put(2, 6) -> 2(6)
2 step put(1,5) -> 2(6) -> 1(5)
3 step put(1,2) -> 1(5) -> 1(2)
4 step get(2) -> null
but 2 has to be 6 - why?
because we hadn't to evicted 2 on 3 step - 1 already exists and has to be changed position
Am i right?
Btw the test return FAILURE
```php
1) Tests\LRUCache\LRUCacheArrayTest::it_should_keep_elements_in_the_right_order
Failed asserting that 2 is null.
```
It is because when putting the key value in array using string (first, second, ...). it is because you trying to remove (unset) the map by using the value from the items. Since you insert map using (first, second, ...), when the capacity is full, you array pop from the items, which is it save the value (1,2,..) so when unsetting the map, it is not work. cause the map[1] it is not found. cause it save the map[first, second]
hmm it's all green to me. However, I did not push the last changes. I did it now. Is it still red?
Ah, I miss something in the code. nevermind
in the put function, I put $value to unshift, array_unshift($this->items, `$value`);
but after clone and check your repository, it is $key to unshift array_unshift($this->items, `$key`);
Ya, I am the one who do the mistakes. thank you