<?
class Hashtable
{
 var $line = array();

 function Hashtable()
 {
 }

    //+-----------------------------------------------------------------------------------------------------------
   //Desc:将带有指定键和值的元素添加到 Hashtable 中。
 function add($key,$value)
 {
  if(!$this->containsKey($key))
  {
   $this->line[$key] = $value;
  }
  else
  {
   $this->halt("添加的键值对已经存在!");
  }
 }

    //+----------------------------------------------------------------------------------------------------------
   //Desc:获取 Hashtable 中特定键的值。
 function getValue($key)
 {
  if($this->containsKey($key)) return $this->line[$key];

  return null;
 }

    //+-----------------------------------------------------------------------------------------------------------
   //Desc:从 Hashtable 中移除带有指定键的元素。
 function remove($key)
 {
  if($this->containsKey($key))
  {
   unset($this->line[$key]);
  }
 }

    //+----------------------------------------------------------------------------------------------------------
   //Desc:获取包含 Hashtable 中的键的集合。 
 function getKeys()
 {
  $keyArr = array();
  foreach($this->line as $key=>$val)
  {
   $keyArr[] = $key;
  }
  return $keyArr;
 }

    //+---------------------------------------------------------------------------------------------------------
   //Desc:获取包含 Hashtable 中的值的集合。 
 function getValues()
 {
  $valArr = array();
  foreach($this->line as $val)
  {
   $valArr[] = $val;
  }
  return $valArr;
 }

    //+---------------------------------------------------------------------------------------------------------
   //Desc:确定 Hashtable 是否包含特定键。 
 function containsKey($key)
 {
  if(isset($this->line[$key])) return true;
  
  return false;
 }

    //+---------------------------------------------------------------------------------------------------------
   //Desc:确定 Hashtable 是否包含特定值。
 function containsValue($value)
 {
  $found = false;
  foreach($this->line as $val)
  {
   if($val == $value)
   {
    $found = true;
    break;
   }
  }
  return $found;
 }

    //+----------------------------------------------------------------------------------------------------------
   //Desc:创建 Hashtable 的浅表副本。
 function clone()
 {
  return $this->line;
 }

    //+----------------------------------------------------------------------------------------------------------
   //Desc:获取包含在 Hashtable 中的键值对的数目。 
 function count()
 {
  return count($this->line);
 }
   
 //+----------------------------------------------------------------------------------------------------------
   //Desc: 从 Hashtable 中移除所有元素。 
 function clear()
 {
  $this->line = array();
 }

    //+----------------------------------------------------------------------------------------------------------
   //Desc:异常信息
 function halt($msg)
 {
  die($msg);
 }
}
?>

posted on 2007-01-06 12:14  成都发哥  阅读(423)  评论(0)    收藏  举报