Created
February 3, 2016 12:27
-
-
Save nexpr/0d170067818508c8fff4 to your computer and use it in GitHub Desktop.
配列じゃない方のMap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Map{ | |
private $keys = []; | |
private $values = []; | |
function set($k, $v){ | |
$index = $this->getIndex($k); | |
if($index === false){ | |
$this->keys[] = $k; | |
$this->values[] = $v; | |
}else{ | |
$this->keys[$index] = $k; | |
$this->values[$index] = $v; | |
} | |
} | |
function get($k){ | |
$index = $this->getIndex($k); | |
return $index === false ? null : $this->values[$index]; | |
} | |
function remove($k){ | |
$index = $this->getIndex($k); | |
if($index === false){ | |
return false; | |
} | |
unset($this->keys[$index], $this->values[$index]); | |
$this->keys = array_values($this->keys); | |
$this->values = array_values($this->values); | |
return true; | |
} | |
function clear(){ | |
$this->keys = []; | |
$this->values = []; | |
} | |
private function getIndex($k){ | |
return array_search($k, $this->keys, true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment