php中自定义hashmap类

<?php


/**
 * @(#)hashable.php
 * PHP version 5.2.3
 *
 *
 * Copyright 2007 SoftRoad. All Rights Reserved.
 *
 *====================================================================
 * 変更履歴
 *
 * 新規作成                            2007 年5月17日   蒋彪
 */

class HashTable {

 var $H_table;

 /*
  *   コンストラクタ
  */
 function __construct() {
  $this->H_table = array ();
 }

 /*
  Object put(key,value)
 */
 function put($key, $value) {
  if (!array_key_exists($key, $this->H_table)) {
   $this->H_table[$key] = $value;
   return null;
  } else {
   $tempValue = $this->H_table[$key];
   $this->H_table[$key] = $value;
   return $tempValue;
  }
 }

 /*
  void clear()
 */
 function clear() {
  $this->H_table = null;
  $this->H_table = array ();
 }

 /*
  boolean containsValue(value)
 */
 function containsValue($value) {

  while ($curValue = current($this->H_table)) {
   if ($curValue == $value) {
    return true;
   }
   next($this->H_table);
  }

  return false;

 }

 /*
  boolean containsKey(key)
 */
 function containsKey($key) {
  if (array_key_exists($key, $this->H_table)) {
   return true;
  } else {
   return false;
  }
 }

 /*
  object get(key)
 */
 function get($key) {
  if (array_key_exists($key, $this->H_table))
   return $this->H_table[$key];
  else
   return null;
 }

 /*
  boolean isEmpty()
 */
 function isEmpty() {
  return (count($this->H_table) == 0);
 }

 /*
  *int size()
  */
 function size() {
  return count($this->H_table);
 }

 /*
  *対象を削除します
  */
 function remove($key) {
  $temp_table = array ();
  if (array_key_exists($key, $this->H_table)) {
   $tempValue = $this->H_table[$key];
   while ($curValue = current($this->H_table)) {
    if (!(key($this->H_table) == $key))
     $temp_table[key($this->H_table)] = $curValue;

    next($this->H_table);
   }
   $this->H_table = null;
   $this->H_table = $temp_table;
   return $tempValue;
  } else
   return null;
 }

 function toString() {
  print_r($this->H_table);
 }
}
?>
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值