c#哈希算法的實(shí)現(xiàn)方法及思路
有想過hash["A1"] = DateTime.Now;這句是怎么實(shí)現(xiàn)的嗎?我們來重溫下學(xué)校時代就學(xué)過的哈希算法吧。
我們要寫個class,實(shí)現(xiàn)如下主程序調(diào)用:
static void Main(string[] args)
{
MyHash hash = new MyHash();
hash["A1"] = DateTime.Now;
hash["A2"] = 1;
Console.WriteLine(Convert.ToString(hash["A1"]));
Console.WriteLine(Convert.ToString(hash["A2"]));
}
一看,也確實(shí)挺簡單的,就是一個所引器,如下:
class MyHash
{
public object this[string key]
{
get
{
}
set
{
}
}
}
程序中要保存的對象,最終是要保存在一個數(shù)組中的,而且需要通過一個轉(zhuǎn)換函數(shù)來進(jìn)行string key與數(shù)組Index的Map,如下:
private List<List<Tuple<string, object>>> lstArray = new List<List<Tuple<string, object>>>(defaultSize);
private int MapString2Int(string key)
{
int hashIndex=0;
char[] keyAry = key.ToCharArray();
foreach (var c in keyAry)
hashIndex += (int)c;
hashIndex = hashIndex % lstArray.Capacity;
return hashIndex;
}
這個函數(shù)是遍歷string key的每個char,累加,最終取模(同數(shù)組的長度),這樣得出的一個value肯定就在數(shù)組范圍內(nèi)。
如果2個key轉(zhuǎn)換出來的index相同呢?會導(dǎo)致沖突,一個最簡單的解決辦法是把數(shù)組中的每個元素變成List, 也就是說,如果string key轉(zhuǎn)換后出現(xiàn)了相同的Index,沒關(guān)系,只要把那2個元素都放在那個Index所標(biāo)識的數(shù)組位置中即可,本文中用的是List<Tuple<string, object>>。
下面是整個程序的代碼:
class Program
{
static void Main(string[] args)
{
MyHash hash = new MyHash();
hash["A1"] = DateTime.Now;
hash["A2"] = 1;
Console.WriteLine(Convert.ToString(hash["A1"]));
Console.WriteLine(Convert.ToString(hash["A2"]));
}
}
class MyHash
{
private const int defaultSize = 99999;
private List<List<Tuple<string, object>>> lstArray = new List<List<Tuple<string, object>>>(defaultSize);
public MyHash()
{
int i = lstArray.Capacity;
while(i>=0)
{
lstArray.Add(new List<Tuple<string,object>>());
i--;
}
}
public object this[string key]
{
get
{
EnsureNotNull(key);
List<Tuple<string, object>> lst;
Tuple<string, object> obj = FindByKey(key, out lst);
if (obj == null)
throw new Exception("Key不存在");
return obj.Item2;
}
set
{
EnsureNotNull(key);
List<Tuple<string, object>> lst;
Tuple<string, object> obj = FindByKey(key, out lst);
if (obj!=null)
lst.Remove(obj);
lst.Add(new Tuple<string, object>(key, value));
}
}
private Tuple<string, object> FindByKey(string key, out List<Tuple<string, object>> lst)
{
int hashIndex = MapString2Int(key);
lst = lstArray[hashIndex];
Tuple<string, object> obj = null;
for (var i = 0; i < lst.Count; i++)
{
if (lst[i].Item1 == key)
{
obj = lst[i];
break;
}
}
return obj;
}
private static void EnsureNotNull(string key)
{
if (key == null || key.Trim().Length == 0)
throw new Exception("Key不能為空");
}
private int MapString2Int(string key)
{
int hashIndex=0;
char[] keyAry = key.ToCharArray();
foreach (var c in keyAry)
hashIndex += (int)c;
hashIndex = hashIndex % lstArray.Capacity;
Console.WriteLine(string.Format("{0}相應(yīng)的Index為:{1}", key, hashIndex));
return hashIndex;
}
}
運(yùn)行效果圖:
- C# 生成高質(zhì)量縮略圖程序—終極算法
- C#的3DES加密解密算法實(shí)例代碼
- C#常見算法面試題小結(jié)
- asp.net(c#)兩種隨機(jī)數(shù)的算法,可用抽考題
- c#漢諾塔的遞歸算法與解析
- C#加密算法匯總(推薦)
- 基于C#代碼實(shí)現(xiàn)九宮格算法橫豎都等于4
- c# 實(shí)現(xiàn)MD5,SHA1,SHA256,SHA512等常用加密算法源代碼
- 基于私鑰加密公鑰解密的RSA算法C#實(shí)現(xiàn)方法
- C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘五 棧和隊(duì)列
- .net C# 實(shí)現(xiàn)任意List的笛卡爾乘積算法代碼
- C#實(shí)現(xiàn)的海盜分金算法實(shí)例
相關(guān)文章
C#使用游標(biāo)實(shí)現(xiàn)補(bǔ)間函數(shù)
這篇文章主要為大家詳細(xì)介紹了C#使用游標(biāo)實(shí)現(xiàn)補(bǔ)間函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02DevExpress之ChartControl創(chuàng)建Drill-Down樣式的Title實(shí)例
這篇文章主要介紹了DevExpress之ChartControl創(chuàng)建Drill-Down樣式的Title實(shí)現(xiàn)方法,以實(shí)例形式講述了創(chuàng)建Drill-Down樣式的Title原理與實(shí)現(xiàn)過程,需要的朋友可以參考下2014-10-10C#實(shí)現(xiàn)網(wǎng)絡(luò)小程序的步驟詳解
經(jīng)常要檢測某些IP地址范圍段的計(jì)算機(jī)是否在線。有很多的方法,比如進(jìn)入到網(wǎng)關(guān)的交換機(jī)上去查詢、使用現(xiàn)成的工具或者編寫一個簡單的DOS腳本等等,這些都比較容易實(shí)現(xiàn)。本文將用C#來實(shí)現(xiàn),感興趣的可以了解一下2022-12-12