Skip to content

Instantly share code, notes, and snippets.

@nexpr
Created February 3, 2016 12:27
Show Gist options
  • Save nexpr/0d170067818508c8fff4 to your computer and use it in GitHub Desktop.
Save nexpr/0d170067818508c8fff4 to your computer and use it in GitHub Desktop.
配列じゃない方のMap
<?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