1 文本格式
using System;
using System.Collections;
using System.Collections.Generic;
namespace Legalsoft.Truffer
{
public abstract class Hashtable<K>
{
private int nhash { get; set; }
private int nmax { get; set; }
private int nn { get; set; }
private int ng { get; set; }
private int[] htable { get; set; }
private int[] next { get; set; }
private int[] garbg { get; set; }
private ulong[] thehash { get; set; }
public virtual ulong fn(K k) { return 0L; }
public Hashtable(int nh, int nm)
{
nhash = nh;
nmax = nm;
nn = 0;
ng = 0;
htable = new int[nh];
next = new int[nm];
garbg = new int[nm];
thehash = new ulong[nm];
for (int j = 0; j < nh; j++)
{
htable[j] = -1;
}
}
public int iget(K key)
{
//ulong pp = hash.fn(key);
//ulong pp = new Hashfn1(nhash).fn(key);
ulong pp = fn(key);
int j = (int)(pp % (ulong)nhash);
for (int k = htable[j]; k != -1; k = next[k])
{
if (thehash[k] == pp)
{
return k;
}
}
return -1;
}
public int iset(K key)
{
int kprev = 0;
//ulong pp = new Hashfn1(nhash).fn(key);
ulong pp = fn(key);
int j = (int)(pp % (ulong)nhash);
int k;
if (htable[j] == -1)
{
k = ng > 0 ? garbg[--ng] : nn++;
htable[j] = k;
}
else
{
k = htable[j];
for (; k != -1; k = next[k])
{
if (thehash[k] == pp)
{
return k;
}
kprev = k;
}
k = ng > 0 ? garbg[--ng] : nn++;
next[kprev] = k;
}
if (k >= nmax)
{
throw new Exception("storing too many values");
}
thehash[k] = pp;
next[k] = -1;
return k;
}
public int ierase(K key)
{
//ulong pp = new Hashfn1(nhash).fn(key);
ulong pp = fn(key);
int j = (int)(pp % (ulong)nhash);
if (htable[j] == -1)
{
return -1;
}
int kprev = -1;
for (int k = htable[j]; k != -1; k = next[k])
{
if (thehash[k] == pp)
{
if (kprev == -1)
{
htable[j] = next[k];
}
else
{
next[kprev] = next[k];
}
garbg[ng++] = k;
return k;
}
kprev = k;
}
return -1;
}
public int ireserve()
{
int k = ng > 0 ? garbg[--ng] : nn++;
if (k >= nmax)
{
throw new Exception("reserving too many values");
}
next[k] = -2;
return k;
}
public int irelinquish(int k)
{
if (next[k] != -2)
{
return -1;
}
garbg[ng++] = k;
return k;
}
}
}
2 代码格式
using System;
using System.Collections;
using System.Collections.Generic;
namespace Legalsoft.Truffer
{
public abstract class Hashtable<K>
{
private int nhash { get; set; }
private int nmax { get; set; }
private int nn { get; set; }
private int ng { get; set; }
private int[] htable { get; set; }
private int[] next { get; set; }
private int[] garbg { get; set; }
private ulong[] thehash { get; set; }
public virtual ulong fn(K k) { return 0L; }
public Hashtable(int nh, int nm)
{
nhash = nh;
nmax = nm;
nn = 0;
ng = 0;
htable = new int[nh];
next = new int[nm];
garbg = new int[nm];
thehash = new ulong[nm];
for (int j = 0; j < nh; j++)
{
htable[j] = -1;
}
}
public int iget(K key)
{
//ulong pp = hash.fn(key);
//ulong pp = new Hashfn1(nhash).fn(key);
ulong pp = fn(key);
int j = (int)(pp % (ulong)nhash);
for (int k = htable[j]; k != -1; k = next[k])
{
if (thehash[k] == pp)
{
return k;
}
}
return -1;
}
public int iset(K key)
{
int kprev = 0;
//ulong pp = new Hashfn1(nhash).fn(key);
ulong pp = fn(key);
int j = (int)(pp % (ulong)nhash);
int k;
if (htable[j] == -1)
{
k = ng > 0 ? garbg[--ng] : nn++;
htable[j] = k;
}
else
{
k = htable[j];
for (; k != -1; k = next[k])
{
if (thehash[k] == pp)
{
return k;
}
kprev = k;
}
k = ng > 0 ? garbg[--ng] : nn++;
next[kprev] = k;
}
if (k >= nmax)
{
throw new Exception("storing too many values");
}
thehash[k] = pp;
next[k] = -1;
return k;
}
public int ierase(K key)
{
//ulong pp = new Hashfn1(nhash).fn(key);
ulong pp = fn(key);
int j = (int)(pp % (ulong)nhash);
if (htable[j] == -1)
{
return -1;
}
int kprev = -1;
for (int k = htable[j]; k != -1; k = next[k])
{
if (thehash[k] == pp)
{
if (kprev == -1)
{
htable[j] = next[k];
}
else
{
next[kprev] = next[k];
}
garbg[ng++] = k;
return k;
}
kprev = k;
}
return -1;
}
public int ireserve()
{
int k = ng > 0 ? garbg[--ng] : nn++;
if (k >= nmax)
{
throw new Exception("reserving too many values");
}
next[k] = -2;
return k;
}
public int irelinquish(int k)
{
if (next[k] != -2)
{
return -1;
}
garbg[ng++] = k;
return k;
}
}
}