PeopleCode中使用Map

参考 HRTR_UTILITIES

  1. /** 
  2. *@author Vinay Vegunta 
  3. *@date 03/09/2004 
  4. *This class provides a simple Hashtable implementation for storing and looking up objects based on String keys 
  5. */ 
  6.  
  7. class Hashtable 
  8.    method Hashtable(); 
  9.    method Put(&key As string, &value As object); 
  10.    method Get(&key As string) Returns object; 
  11.    method GetKeys() Returns array of string; 
  12.    method IsKey(&key As string) Returns boolean; 
  13.    method GetValues() Returns array of object; 
  14. private 
  15.    method GetKeyPosition(&key As string) Returns number; 
  16.    instance array of string &keyArray; 
  17.    instance array of object &valueArray; 
  18.     
  19. end-class; 
  20.  
  21. /* 
  22. *Constructor 
  23. */ 
  24. method Hashtable 
  25.    /*Instantiate the arrays */ 
  26.    &keyArray = CreateArrayRept("", 0); 
  27.    &valueArray = CreateArrayRept( Null, 0); 
  28. end-method; 
  29.  
  30. /** 
  31. *Put a value in the Hashtable. If the key already exists, it's value will be replaced with a new one . 
  32. */ 
  33. method Put 
  34.    /+ &key as String, +/ 
  35.    /+ &value as Object +/ 
  36.    Local number &nbr_pos = %This.GetKeyPosition(&key); 
  37.    /**Check if a new entry is to be made or an old value has to be replaced **/ 
  38.    If (&nbr_pos <> - 1) Then 
  39.       &keyArray [&nbr_pos] = &key; 
  40.       &valueArray [&nbr_pos] = &value; 
  41.    Else 
  42.       &keyArray.Push(&key); 
  43.       &valueArray.Push(&value); 
  44.    End-If; 
  45. end-method; 
  46.  
  47. /** 
  48. *Get the value for a given key. Returns null if no value is found . 
  49. */ 
  50. method Get 
  51.    /+ &key as String +/ 
  52.    /+ Returns Object +/ 
  53.    Local number &nbr_pos = %This.GetKeyPosition(&key); 
  54.    If (&nbr_pos <> - 1) Then 
  55.       Return &valueArray [&nbr_pos]; 
  56.    End-If; 
  57.    Return Null; 
  58. end-method; 
  59.  
  60. /** 
  61. *Returns the  array of keys  
  62. */ 
  63. method GetKeys 
  64.    /+ Returns Array of String +/ 
  65.    /**Return a clone of the array so that the original array cannot be tampered with **/ 
  66.    Return &keyArray.Clone(); 
  67. end-method; 
  68.  
  69. /** 
  70. *Returns the array of values   
  71. */ 
  72. method GetValues 
  73.    /+ Returns Array of Object +/; 
  74.    /**Return a clone of the array so that the original array cannot be tampered with **/ 
  75.    Return &valueArray.Clone(); 
  76. end-method; 
  77.  
  78. /** 
  79. *Returns a bolean indicating if the provided String is a key or not  
  80. */ 
  81. method IsKey 
  82.    /+ &key as String +/ 
  83.    /+ Returns Boolean +/ 
  84.    Local number &nbr_pos = %This.GetKeyPosition(&key); 
  85.    If (&nbr_pos <> - 1) Then 
  86.       Return True; 
  87.    End-If; 
  88.    Return False; 
  89. end-method; 
  90.  
  91. /** 
  92. *Returns the position in the array of the requested key. Will return -1 if the key is not present . 
  93. */ 
  94. method GetKeyPosition 
  95.    /+ &key as String +/ 
  96.    /+ Returns Number +/ 
  97.     
  98.    Local number &nbr_Len = &keyArray.Len; 
  99.    Local number &i; 
  100.    For &i = 1 To &nbr_Len 
  101.       If (Exact(&key, &keyArray [&i])) Then 
  102.          Return &i; 
  103.       End-If; 
  104.    End-For; 
  105.    Return - 1; 
  106. end-method;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值