<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      面試準備 - HashTable 的C#實現 開放地址法

      Hashtable是很經常在面試中遇到的數據結構,因為他的O(1)操作時間和O(n)空間

      之所以自己寫一份是因為:

      • 加深對于hashtable的理解
      • 某些公司面試的時候需要coding.......

      開放地址法  Xn=(Xn-1 +b ) % size

      理論上b要和size是要精心選擇的,不過我這邊沒有做特別的處理,101的默認size是從c#源代碼中抄襲的。。。。

      代碼盡量簡單一點是為了理解方便

      hashtable快滿的時候擴展一倍空間,數據和標志位還有key 這三個數組都要擴展

      刪除的時候不能直接刪除元素,只能打一個標志(因為用了開放地方方法)

      目前只支持string和int類型的key(按位131進制)

      非線程安全- 因為這是范例代碼

      支持泛型

        
          public class Hashtable<T>
              
          {
              public Hashtable()
              {
                  this.dataArray = new T[this.m];
                  this.avaiableCapacity = this.m;
                  this.keyArray = new int[this.m];
                  for (int i = 0; i < this.keyArray.Length; i++)
                  {
                      this.keyArray[i] = -1;
                  }
                  this.flagArray = new bool[this.m];
              }
      
              private int m = 101;
      
              private int l = 1;
      
              private int avaiableCapacity;
      
              private double factor = 0.35;
      
              private T[] dataArray;
      
              private int[] keyArray;
      
              private bool[] flagArray;
      
              public void Add(string s, T item)
              {
                  if (string.IsNullOrEmpty(s))
                  {
                      throw new ArgumentNullException("s");
                  }
      
                  if ((double)this.avaiableCapacity / this.m < this.factor)
                  {
                      this.ExtendCapacity();
                  }
      
                  var code = HashtableHelper.GetStringHash(s);
                  this.AddItem(code, item, this.dataArray, code, this.keyArray, this.flagArray);
              }
      
              public T Get(string s)
              {
                  if (string.IsNullOrEmpty(s))
                  {
                      throw new ArgumentNullException("s");
                  }
      
                  var code = HashtableHelper.GetStringHash(s);
                  return this.GetItem(code, this.dataArray, code, this.keyArray, this.flagArray);
              }
      
              private void ExtendCapacity()
              {
                  this.m *= 2;
                  this.avaiableCapacity += this.m;
                  T[] newItems = new T[this.m];
                  int[] newKeys = new int[this.m];
                  bool[] newFlags = new bool[this.m];
      
                  for (int i = 0; i < newKeys.Length; i++)
                  {
                      newKeys[i] = -1;
                  }
      
                  for (int i = 0; i < this.dataArray.Length; i++)
                  {
                      if (this.keyArray[i] >= 0 && !this.flagArray[i])
                      {
                          //var code = HashtableHelper.GetStringHash(s);
                          this.AddItem(
                              this.keyArray[i],
                              this.dataArray[i],
                              newItems,
                              this.keyArray[i],
                              newKeys,
                              this.flagArray);
                      }
                  }
                  this.dataArray = newItems;
                  this.keyArray = newKeys;
                  this.flagArray = newFlags;
                  // throw new NotImplementedException();
              }
      
              private int AddItem(int code, T item, T[] data, int hashCode, int[] keys, bool[] flags)
              {
                  int address = code % this.m;
                  if (keys[address] < 0)
                  {
                      data[address] = item;
                      keys[address] = hashCode;
                      this.avaiableCapacity--;
                      return address;
                  }
                  else if (keys[address] == hashCode)
                  {
                      if (flags[address])
                      {
                          flags[address] = false;
                          data[address] = item;
                          return address;
                      }
                      throw new ArgumentException("duplicated key");
                  }
                  else
                  {
                      int nextAddress = address + this.l; //open addressing Xn=Xn-1 + b
                      return this.AddItem(nextAddress, item, data, hashCode, keys, flags);
                  }
              }
      
              private T GetItem(int code, T[] data, int hashCode, int[] keys, bool[] flags)
              {
                  int address = code % this.m;
                  if (keys[address] < 0)
                  {
                      return default(T);
                  }
                  else if (keys[address] == hashCode)
                  {
                      if (flags[address])
                      {
                          return default(T);
                      }
                      return data[address];
                  }
                  else
                  {
                      int nextAddress = address + this.l; //open addressing Xn=Xn-1 + b
                      return this.GetItem(nextAddress, data, hashCode, keys, flags);
                  }
              }
      
              public void Delete(string s)
              {
                  if (string.IsNullOrEmpty(s))
                  {
                      throw new ArgumentNullException("s");
                  }
      
                  var code = HashtableHelper.GetStringHash(s);
                  this.DeleteItem(code, this.dataArray, code, this.keyArray, this.flagArray);
              }
      
              private void DeleteItem(int code, T[] data, int hashCode, int[] keys, bool[] flags)
              {
                  int address = code % this.m;
                  if (keys[address] < 0)
                  {
                      return;
                      //not exist
                  }
                  else if (keys[address] == hashCode)
                  {
                      if (!this.flagArray[address])
                      {
                          flags[address] = true;
                          this.avaiableCapacity++;
                      }
                  }
                  else
                  {
                      int nextAddress = address + this.l; //open addressing Xn=Xn-1 + b
                      this.DeleteItem(nextAddress, data, hashCode, keys, flags);
                  }
              }
          }
      
      
          public class HashtableHelper
          {
              public static int GetStringHash(string s)
              {
                  if (string.IsNullOrEmpty(s))
                  {
                      throw new ArgumentNullException("s");
                  }
      
                  var bytes = Encoding.ASCII.GetBytes(s);
                  int checksum = GetBytesHash(bytes, 0, bytes.Length);
                  return checksum;
              }
      
              public static int GetBytesHash(byte[] array, int ibStart, int cbSize)
              {
                  if (array == null || array.Length == 0)
                  {
                      throw new ArgumentNullException("array");
                  }
      
                  int checksum = 0;
                  for (int i = ibStart; i < (ibStart + cbSize); i++)
                  {
                      checksum = (checksum * 131) + array[i];
                  }
                  return checksum;
              }
      
              public static int GetBytesHash(char[] array, int ibStart, int cbSize)
              {
                  if (array == null || array.Length == 0)
                  {
                      throw new ArgumentNullException("array");
                  }
      
                  int checksum = 0;
                  for (int i = ibStart; i < (ibStart + cbSize); i++)
                  {
                      checksum = (checksum * 131) + array[i];
                  }
                  return checksum;
              }
          }

       

      posted on 2014-01-31 08:51  聽說讀寫  閱讀(2260)  評論(2)    收藏  舉報

      導航

      主站蜘蛛池模板: 亚洲精品一区二区三区蜜臀| 日韩美女视频一区二区三区| 阳春市| 国产精品人成在线观看免费| 国产稚嫩高中生呻吟激情在线视频| 国产999久久高清免费观看| 国产片AV国语在线观看手机版| 成年在线观看免费人视频 | 天堂a无码a无线孕交| 亚洲AV无码AV在线影院| 色综合国产一区二区三区| 久久亚洲av午夜福利精品一区| 人妻精品动漫h无码| 又大又粗又爽18禁免费看| 深夜宅男福利免费在线观看| 花垣县| 国产成人啪精品午夜网站| 午夜爽爽爽男女免费观看影院| 国产伦码精品一区二区| 亚洲成av人片不卡无码手机版| 挺进粗大尤物人妻中文字幕| 亚洲不卡一区三区三区四| 国产精品无码一区二区桃花视频| 亚洲综合在线亚洲优优色| 安仁县| 国产精品中文字幕自拍| 精品久久人人妻人人做精品| 另类图片亚洲人妻中文无码| 亚洲国产综合一区二区精品| 99国产欧美另类久久久精品| 国产成人av一区二区三| 营口市| 久久精品激情亚洲一二区| 国产一区韩国主播| 国产三级a三级三级| 亚洲综合久久精品国产高清| 午夜福利理论片高清在线| 亚洲午夜精品国产电影在线观看| 国产精品三级中文字幕| 精品免费国产一区二区三区四区| 视频一区视频二区卡通动漫|